Files
rubbs/lib/bbs/tui.rb
Zsolt Tasnadi cac10176a2 Add page-based TUI DSL
Each screen is now a self-contained `page` block with its own `enter`,
`render`, and `key` handlers. Shared logic lives in a `helpers` block
mixed into the render context. A `chrome` block renders persistent UI
(header, sidebar, footer) before every frame.

New context primitives: `go(page)` for transitions, `reload` to re-run
the current page's `enter` block, `printable` key binding for text input.

`FlowRunner#run_tui` now passes the flow context to TUIRunner so username
and other session data are available without reaching into internals.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-11 21:30:06 +02:00

47 lines
1.0 KiB
Ruby

# frozen_string_literal: true
module BBS
class TUI
attr_reader :init_block, :chrome_block, :pages, :start_page, :helper_module
def self.define(&block)
tui = new
tui.instance_eval(&block)
tui
end
def initialize
@pages = {}
@start_page = :idle
end
def init(&block) = @init_block = block
def chrome(&block) = @chrome_block = block
def start(name) = @start_page = name
def helpers(&block)
@helper_module = Module.new(&block)
end
def page(name, &block)
p = Page.new(name)
p.instance_eval(&block)
@pages[name] = p
end
class Page
attr_reader :name, :enter_block, :render_block, :key_bindings
def initialize(name)
@name = name
@key_bindings = {}
end
def enter(&block) = @enter_block = block
def render(&block) = @render_block = block
def key(k, &block) = (@key_bindings[k] = block)
def printable(&block) = (@key_bindings[:printable] = block)
end
end
end