class Chamomile::Table
Tabular data display with selection, scrolling, and focus gating.
Constants
- Column
- DEFAULT_KEY_MAP
Attributes
Public Class Methods
Source
# File lib/chamomile/components/table.rb, line 24 def initialize(columns: [], rows: [], height: 10, key_map: DEFAULT_KEY_MAP, &block) @columns = normalize_columns(columns) @rows = rows @height = height @key_map = key_map @cursor = 0 @offset = 0 @focused = false return unless block builder = ColumnBuilder.new block.call(builder) @columns = builder.columns unless builder.columns.empty? end
Accepts columns as keyword arg or via block DSL.
# Keyword form (original) Table.new(columns: [Column.new(title: "Name", width: 20)], rows: rows) # Block DSL form (new) Table.new(rows: rows) do |t| t.column "Name", width: 20 t.column "Size", width: 10 end # Hash form (new) Table.new(columns: [{ title: "Name", width: 20 }], rows: rows)
Public Instance Methods
Source
# File lib/chamomile/components/table.rb, line 72 def cursor=(n) @cursor = n.clamp(0, [row_count - 1, 0].max) clamp_offset end
Source
# File lib/chamomile/components/table.rb, line 77 def focus @focused = true self end
Source
# File lib/chamomile/components/table.rb, line 67 def goto_bottom @cursor = [row_count - 1, 0].max clamp_offset end
Source
# File lib/chamomile/components/table.rb, line 62 def goto_top @cursor = 0 clamp_offset end
Source
# File lib/chamomile/components/table.rb, line 92 def handle(msg) return unless @focused case msg when Chamomile::KeyEvent handle_key(msg) end nil end
Handle an incoming event.
Also aliased as: update
Source
# File lib/chamomile/components/table.rb, line 57 def move_down(n = 1) @cursor = (@cursor + n).clamp(0, [row_count - 1, 0].max) clamp_offset end
Source
# File lib/chamomile/components/table.rb, line 52 def move_up(n = 1) @cursor = (@cursor - n).clamp(0, [row_count - 1, 0].max) clamp_offset end
Source
# File lib/chamomile/components/table.rb, line 40 def rows=(rows) @rows = rows @cursor = @cursor.clamp(0, [row_count - 1, 0].max) clamp_offset end
Source
# File lib/chamomile/components/table.rb, line 46 def selected_row return nil if @rows.empty? @rows[@cursor] end
Source
# File lib/chamomile/components/table.rb, line 106 def view header = render_header separator = render_separator body = render_body [header, separator, body].compact.join("\n") end