TUI
This commit is contained in:
7
.claude/settings.local.json
Normal file
7
.claude/settings.local.json
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"permissions": {
|
||||||
|
"allow": [
|
||||||
|
"Bash(ruby *)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,8 @@ require_relative 'bbs/flow'
|
|||||||
require_relative 'bbs/flow_runner'
|
require_relative 'bbs/flow_runner'
|
||||||
require_relative 'bbs/renderer'
|
require_relative 'bbs/renderer'
|
||||||
require_relative 'bbs/store'
|
require_relative 'bbs/store'
|
||||||
|
require_relative 'bbs/tui'
|
||||||
|
require_relative 'bbs/tui_runner'
|
||||||
require_relative 'bbs/session'
|
require_relative 'bbs/session'
|
||||||
require_relative 'bbs/server'
|
require_relative 'bbs/server'
|
||||||
|
|
||||||
|
|||||||
@@ -118,6 +118,10 @@ module BBS
|
|||||||
def output(&block)
|
def output(&block)
|
||||||
@steps << { type: :output, block: block }
|
@steps << { type: :output, block: block }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def tui(definition)
|
||||||
|
@steps << { type: :tui, definition: definition }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class MenuBuilder
|
class MenuBuilder
|
||||||
|
|||||||
@@ -70,6 +70,7 @@ module BBS
|
|||||||
when :pick then run_pick(step)
|
when :pick then run_pick(step)
|
||||||
when :body then run_body(step)
|
when :body then run_body(step)
|
||||||
when :output then run_output(step)
|
when :output then run_output(step)
|
||||||
|
when :tui then run_tui(step)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -341,6 +342,13 @@ module BBS
|
|||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def run_tui(step)
|
||||||
|
TUIRunner.new(@session, @session_id, step[:definition]).run
|
||||||
|
nil
|
||||||
|
rescue IOError, Errno::EPIPE, Errno::ECONNRESET
|
||||||
|
:halt
|
||||||
|
end
|
||||||
|
|
||||||
# ── helpers ────────────────────────────────────────────────────────────────
|
# ── helpers ────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
def strip_md(text)
|
def strip_md(text)
|
||||||
|
|||||||
@@ -19,6 +19,27 @@ module BBS
|
|||||||
send_raw [IAC, DO, SGA_OPT].pack('C*')
|
send_raw [IAC, DO, SGA_OPT].pack('C*')
|
||||||
end
|
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
|
def readline
|
||||||
line = +""
|
line = +""
|
||||||
last_cr = false
|
last_cr = false
|
||||||
@@ -70,6 +91,22 @@ module BBS
|
|||||||
|
|
||||||
private
|
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
|
def absorb_iac
|
||||||
cmd = @client.read(1)
|
cmd = @client.read(1)
|
||||||
return if cmd.nil?
|
return if cmd.nil?
|
||||||
|
|||||||
29
lib/bbs/tui.rb
Normal file
29
lib/bbs/tui.rb
Normal file
@@ -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
|
||||||
103
lib/bbs/tui_runner.rb
Normal file
103
lib/bbs/tui_runner.rb
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user