Class: TuiTui::Widget::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/tui_tui/widget/table.rb

Overview

Drawing companion for ScrollList with a fixed header row. The first row of the rect is the header (built from columns:, an array of [label, width] pairs); the remaining rows are the scrolled body, drawn by Widget::List. Cell content comes from the caller's block, keeping the table domain-agnostic: yield(index, selected) returns one value per column — a String (styled with the row's base style) or a Span (its own style wins, e.g. to accent one cell). Labels may be Spans too.

Each cell is truncated/padded to its column width (CJK-safe via DisplayText), columns are joined with a 1-space gap, and anything past the rect's right edge is clipped.

Constant Summary collapse

COLUMN_GAP =
1

Instance Method Summary collapse

Constructor Details

#initialize(scroll) ⇒ Table

Returns a new instance of Table.



25
26
27
28
# File 'lib/tui_tui/widget/table.rb', line 25

def initialize(scroll)
  @scroll = scroll
  @list = List.new(scroll)
end

Instance Method Details

#draw(canvas, rect, columns:, header_style: nil, highlight: nil, scrollbar: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/tui_tui/widget/table.rb', line 30

def draw(canvas, rect, columns:, header_style: nil, highlight: nil, scrollbar: nil)
  header, body = rect.split_rows(1, :rest)
  draw_header(canvas, header, columns, header_style)
  return canvas if body.rows <= 0

  @list.draw(canvas, body, highlight: highlight, scrollbar: scrollbar) do |index, selected|
    format_row(yield(index, selected), columns, selected ? highlight : nil)
  end
end

#index_at(rect, event, scrollbar: nil) ⇒ Object

Map a MouseEvent to the row index under it, or nil. The header row and anything outside the body map to nil. Pass the same rect and scrollbar: used for draw.



43
44
45
46
47
48
# File 'lib/tui_tui/widget/table.rb', line 43

def index_at(rect, event, scrollbar: nil)
  _header, body = rect.split_rows(1, :rest)
  return nil if body.rows <= 0

  @list.index_at(body, event, scrollbar: scrollbar)
end