From 73c711336d5ca4b2133ba90dff178d1e6b02f016 Mon Sep 17 00:00:00 2001 From: Zsolt Tasnadi Date: Mon, 11 May 2026 20:24:40 +0200 Subject: [PATCH] TUI --- .claude/settings.local.json | 7 +++ lib/bbs.rb | 2 + lib/bbs/flow.rb | 4 ++ lib/bbs/flow_runner.rb | 8 +++ lib/bbs/telnet.rb | 37 +++++++++++++ lib/bbs/tui.rb | 29 ++++++++++ lib/bbs/tui_runner.rb | 103 ++++++++++++++++++++++++++++++++++++ 7 files changed, 190 insertions(+) create mode 100644 .claude/settings.local.json create mode 100644 lib/bbs/tui.rb create mode 100644 lib/bbs/tui_runner.rb diff --git a/.claude/settings.local.json b/.claude/settings.local.json new file mode 100644 index 0000000..1c2478a --- /dev/null +++ b/.claude/settings.local.json @@ -0,0 +1,7 @@ +{ + "permissions": { + "allow": [ + "Bash(ruby *)" + ] + } +} diff --git a/lib/bbs.rb b/lib/bbs.rb index 6318148..ad601b7 100644 --- a/lib/bbs.rb +++ b/lib/bbs.rb @@ -8,6 +8,8 @@ require_relative 'bbs/flow' require_relative 'bbs/flow_runner' require_relative 'bbs/renderer' require_relative 'bbs/store' +require_relative 'bbs/tui' +require_relative 'bbs/tui_runner' require_relative 'bbs/session' require_relative 'bbs/server' diff --git a/lib/bbs/flow.rb b/lib/bbs/flow.rb index 8427bd0..440e64f 100644 --- a/lib/bbs/flow.rb +++ b/lib/bbs/flow.rb @@ -118,6 +118,10 @@ module BBS def output(&block) @steps << { type: :output, block: block } end + + def tui(definition) + @steps << { type: :tui, definition: definition } + end end class MenuBuilder diff --git a/lib/bbs/flow_runner.rb b/lib/bbs/flow_runner.rb index e9b66ff..ce6d437 100644 --- a/lib/bbs/flow_runner.rb +++ b/lib/bbs/flow_runner.rb @@ -70,6 +70,7 @@ module BBS when :pick then run_pick(step) when :body then run_body(step) when :output then run_output(step) + when :tui then run_tui(step) end end @@ -341,6 +342,13 @@ module BBS nil end + def run_tui(step) + TUIRunner.new(@session, @session_id, step[:definition]).run + nil + rescue IOError, Errno::EPIPE, Errno::ECONNRESET + :halt + end + # ── helpers ──────────────────────────────────────────────────────────────── def strip_md(text) diff --git a/lib/bbs/telnet.rb b/lib/bbs/telnet.rb index 43eb8a3..bb6edf3 100644 --- a/lib/bbs/telnet.rb +++ b/lib/bbs/telnet.rb @@ -19,6 +19,27 @@ module BBS send_raw [IAC, DO, SGA_OPT].pack('C*') end + def readkey + loop do + raw = @client.read(1) + return nil if raw.nil? + byte = raw.ord + + if byte == IAC + absorb_iac + next + end + + case byte + when 13, 10 then return :enter + when 27 then return read_escape_sequence + when 8, 127 then return :backspace + when 3 then return :interrupt + when 32..126 then return raw + end + end + end + def readline line = +"" last_cr = false @@ -70,6 +91,22 @@ module BBS private + def read_escape_sequence + b1 = @client.read(1) + return :escape if b1.nil? || b1 != '[' + + b2 = @client.read(1) + return :escape if b2.nil? + + case b2 + when 'A' then :up + when 'B' then :down + when 'C' then :right + when 'D' then :left + else :escape + end + end + def absorb_iac cmd = @client.read(1) return if cmd.nil? diff --git a/lib/bbs/tui.rb b/lib/bbs/tui.rb new file mode 100644 index 0000000..4a260f1 --- /dev/null +++ b/lib/bbs/tui.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module BBS + class TUI + attr_reader :init_block, :render_block, :key_bindings + + def self.define(&block) + tui = new + tui.instance_eval(&block) + tui.freeze + end + + def initialize + @key_bindings = {} + end + + def init(&block) + @init_block = block + end + + def render(&block) + @render_block = block + end + + def key(k, action = nil, &block) + @key_bindings[k] = action || block + end + end +end diff --git a/lib/bbs/tui_runner.rb b/lib/bbs/tui_runner.rb new file mode 100644 index 0000000..1a1c08c --- /dev/null +++ b/lib/bbs/tui_runner.rb @@ -0,0 +1,103 @@ +# frozen_string_literal: true + +module BBS + class TUIRunner + def initialize(session, session_id, tui) + @session = session + @session_id = session_id + @tui = tui + end + + def run + ctx = Context.new(@session, @session_id) + ctx.instance_eval(&@tui.init_block) if @tui.init_block + + loop do + ctx.do_render(@tui.render_block) if @tui.render_block + + key = @session.readkey + break if key.nil? + + handler = @tui.key_bindings[key] + next unless handler + + result = handler.is_a?(Symbol) ? handler : ctx.instance_eval(&handler) + break if result == :halt + end + rescue IOError, Errno::EPIPE, Errno::ECONNRESET + nil + ensure + @session.write("\e[?25h\e[2J\e[H") + end + + class Context + STYLES = FlowRunner::STYLES + + def initialize(session, session_id) + @session = session + @session_id = session_id + @buf = +"" + end + + def do_render(block) + @buf = +"\e[?25l" + instance_eval(&block) + @buf << "\e[?25h" + @session.write(@buf) + end + + # ── drawing primitives ──────────────────────────────────────────────── + + def clear + @buf << "\e[2J\e[H" + end + + def at(col, row) + @buf << "\e[#{row};#{col}H" + end + + def text(content, x: nil, y: nil, style: nil) + at(x, y) if x && y + color = STYLES[style] + @buf << (color ? "#{color}#{content}\e[0m" : content.to_s) + end + + def bar(y:, content:, width: 80, style: :muted) + text content.to_s.ljust(width), x: 1, y: y, style: style + end + + def list(items, x:, y:, selected: nil, style: :muted, highlight: :success) + norm = STYLES.fetch(style, STYLES[:muted]) + hi = STYLES.fetch(highlight, STYLES[:success]) + items.each_with_index do |item, i| + at x, y + i + @buf << (i == selected ? "#{hi}▶ #{item}\e[0m" : "#{norm} #{item}\e[0m") + end + end + + def box(x:, y:, w:, h:, title: nil, style: :muted) + color = STYLES.fetch(style, STYLES[:muted]) + inner = w - 2 + + at x, y + if title + prefix = "══ #{title} " + fill = [inner - prefix.length, 0].max + @buf << "#{color}╔#{prefix}#{'═' * fill}╗\e[0m" + else + @buf << "#{color}╔#{'═' * inner}╗\e[0m" + end + + (1...h - 1).each do |dy| + at x, y + dy + @buf << "#{color}║\e[0m#{' ' * inner}#{color}║\e[0m" + end + + at x, y + h - 1 + @buf << "#{color}╚#{'═' * inner}╝\e[0m" + + yield x + 1, y + 1, inner, h - 2 if block_given? + end + end + end +end