Class: RubyTUI::Widgets::Table
- Inherits:
-
Object
- Object
- RubyTUI::Widgets::Table
- Includes:
- StatefulWidget
- Defined in:
- lib/rubytui/widgets/table.rb
Overview
Table renders tabular data with headers, rows, and configurable column widths.
Instance Method Summary collapse
-
#initialize(header: nil, rows: [], widths: [], style: Style::DEFAULT, header_style: nil, highlight_style: nil, highlight_symbol: "> ", column_spacing: 1, block: nil) ⇒ Table
constructor
A new instance of Table.
-
#render(area, buf, state) ⇒ void
Render the header, separator and visible rows.
Constructor Details
#initialize(header: nil, rows: [], widths: [], style: Style::DEFAULT, header_style: nil, highlight_style: nil, highlight_symbol: "> ", column_spacing: 1, block: nil) ⇒ Table
Returns a new instance of Table.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rubytui/widgets/table.rb', line 70 def initialize( header: nil, rows: [], widths: [], style: Style::DEFAULT, header_style: nil, highlight_style: nil, highlight_symbol: "> ", column_spacing: 1, block: nil ) @header = header&.map { |h| normalize_cell(h) } @rows = rows.map { |row| row.map { |c| normalize_cell(c) } } @widths = widths @style = style @header_style = header_style || Style.new.fg(Color::CYAN).bold @highlight_style = highlight_style || Style.new.fg(Color::YELLOW).bold @highlight_symbol = highlight_symbol @highlight_symbol_width = Buffer.string_width(@highlight_symbol) @normal_symbol = " " * @highlight_symbol_width @column_spacing = column_spacing @block = block end |
Instance Method Details
#render(area, buf, state) ⇒ void
This method returns an undefined value.
Render the header, separator and visible rows.
Clamps state.selected to the row range and adjusts
state.offset in place so the selected row is visible. Cell text is
truncated to its column width; explicit widths are not reduced to fit the area.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/rubytui/widgets/table.rb', line 104 def render(area, buf, state) return if area.empty? render_area = area if @block @block.render(area, buf) render_area = @block.inner(area) end return if render_area.empty? # Resolve column widths col_widths = resolve_widths(render_area.width - @highlight_symbol_width) return if col_widths.empty? y = render_area.y # Render header if @header render_row(@header, col_widths, render_area.x, y, buf, @header_style, @normal_symbol) y += 1 # Header separator if y < render_area.bottom x = render_area.x + @highlight_symbol_width col_widths.each_with_index do |w, i| buf.set_string(x, y, "\u2500" * w, @header_style) x += w + (i < col_widths.length - 1 ? @column_spacing : 0) end y += 1 end end # Available height for rows visible_height = render_area.bottom - y return if visible_height <= 0 # Clamp state total = @rows.length state.selected = state.selected.clamp(0, [total - 1, 0].max) if state.selected < state.offset state.offset = state.selected elsif state.selected >= state.offset + visible_height state.offset = state.selected - visible_height + 1 end state.offset = state.offset.clamp(0, [total - visible_height, 0].max) # Render visible rows visible_rows = @rows[state.offset, visible_height] || [] visible_rows.each_with_index do |row, i| break if y >= render_area.bottom actual_index = state.offset + i is_selected = actual_index == state.selected row_style = is_selected ? @style.patch(@highlight_style) : @style symbol = is_selected ? @highlight_symbol : @normal_symbol render_row(row, col_widths, render_area.x, y, buf, row_style, symbol) # Fill remaining width for highlight if is_selected row_end = render_area.x + @highlight_symbol_width col_widths.each_with_index do |w, ci| row_end += w + (ci < col_widths.length - 1 ? @column_spacing : 0) end if row_end < render_area.right buf.set_string(row_end, y, " " * (render_area.right - row_end), row_style) end end y += 1 end end |