From be21826eccc8eb2930b7a83a97b8ed9672219a9c Mon Sep 17 00:00:00 2001 From: Zsolt Tasnadi Date: Tue, 12 May 2026 21:25:21 +0200 Subject: [PATCH] list_view --- lib/bbs/tui_runner.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/bbs/tui_runner.rb b/lib/bbs/tui_runner.rb index fb0f10d..0c7f623 100644 --- a/lib/bbs/tui_runner.rb +++ b/lib/bbs/tui_runner.rb @@ -134,6 +134,27 @@ module BBS end end + def list_view(items, x:, y:, height:, scroll:, selected: nil, + label: :to_s, hint: nil, hint_y: nil, + empty: 'No items found.', + style: nil, highlight: nil) + visible = items[scroll, height] || [] + if visible.empty? + text empty, x: x, y: y, style: style + return + end + + call_label = label.is_a?(Symbol) ? ->(item) { item.public_send(label) } : label + list visible.map { |item| call_label.(item).to_s }, + x: x, y: y, + selected: selected && (selected - scroll), + style: style, highlight: highlight + + return unless hint && selected && (item = items[selected]) + hint_text = (hint.is_a?(Symbol) ? item.public_send(hint) : hint.(item)).to_s.strip + text hint_text, x: x, y: (hint_y || y + height + 1), style: style unless hint_text.empty? + end + def fill(x:, y:, width:, height:, background:) background_code = BACKGROUND_COLORS.fetch(background, background.to_s) height.times do |row_offset|