Class: RatatuiRuby::Widgets::Table
- Inherits:
-
Object
- Object
- RatatuiRuby::Widgets::Table
- Includes:
- CoerceableWidget
- Defined in:
- lib/ratatui_ruby/widgets/table.rb
Overview
Displays structured data in rows and columns.
Data is often multidimensional. You need to show relationships between fields (Name, Age, ID). Aligning columns manually in a monospaced environment is painful and error-prone.
This widget creates a grid. It enforces column widths using constraints. It renders headers, rows, and footers aligned perfectly.
Use it to display database records, logs, or file lists.
Example
Run the interactive demo from the terminal:
ruby examples//app.rb
Constant Summary collapse
- HIGHLIGHT_ALWAYS =
Highlight spacing: always show the spacing column.
:always- HIGHLIGHT_WHEN_SELECTED =
Highlight spacing: show spacing only when a row is selected (default).
:when_selected- HIGHLIGHT_NEVER =
Highlight spacing: never show the spacing column.
:never- FLEX_LEGACY =
Flex: use legacy column sizing.
:legacy- FLEX_START =
Flex: align columns to start.
:start- FLEX_CENTER =
Flex: center columns.
:center- FLEX_END =
Flex: align columns to end.
:end- FLEX_SPACE_BETWEEN =
Flex: space between columns.
:space_between- FLEX_SPACE_AROUND =
Flex: space around columns.
:space_around- FLEX_SPACE_EVENLY =
Flex: space evenly between columns.
:space_evenly
Instance Method Summary collapse
-
#cell_selected? ⇒ Boolean
Returns true if both a row and column are selected (a cell is selected).
-
#column_selected? ⇒ Boolean
Returns true if a column is selected.
-
#empty? ⇒ Boolean
Returns true if the table has no rows.
-
#initialize(header: nil, rows: [], widths: [], row_highlight_style: nil, highlight_symbol: "> ", highlight_spacing: :when_selected, column_highlight_style: nil, cell_highlight_style: nil, selected_row: nil, selected_column: nil, offset: nil, block: nil, footer: nil, flex: :legacy, style: nil, column_spacing: 1) ⇒ Table
constructor
Creates a new Table.
-
#row_selected? ⇒ Boolean
Returns true if a row is selected.
Methods included from CoerceableWidget
Constructor Details
#initialize(header: nil, rows: [], widths: [], row_highlight_style: nil, highlight_symbol: "> ", highlight_spacing: :when_selected, column_highlight_style: nil, cell_highlight_style: nil, selected_row: nil, selected_column: nil, offset: nil, block: nil, footer: nil, flex: :legacy, style: nil, column_spacing: 1) ⇒ Table
Creates a new Table.
- header
-
Array of strings, Text::Spans, Text::Lines, or paragraphs.
- rows
-
2D Array where each cell is String, Text::Span, Text::Line, Paragraph, or Cell.
- widths
-
Array of Constraints or Integers (integers coerce to Constraint.length).
- row_highlight_style
-
Style object.
- highlight_symbol
-
String.
- highlight_spacing
-
Symbol (optional, default:
:when_selected). - column_highlight_style
-
Style object.
- cell_highlight_style
-
Style object.
- selected_row
-
Integer (nullable).
- selected_column
-
Integer (nullable).
- offset
-
Numeric (nullable, coerced to Integer). Forces scroll position when set.
- block
-
Block (optional).
- footer
-
Array of strings/paragraphs (optional).
- flex
-
Symbol (optional, default:
:legacy). - style
-
Style object or Hash (optional).
- column_spacing
-
Integer (optional, default: 1).
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/ratatui_ruby/widgets/table.rb', line 156 def initialize(header: nil, rows: [], widths: [], row_highlight_style: nil, highlight_symbol: "> ", highlight_spacing: :when_selected, column_highlight_style: nil, cell_highlight_style: nil, selected_row: nil, selected_column: nil, offset: nil, block: nil, footer: nil, flex: :legacy, style: nil, column_spacing: 1) coerced_widths = widths.map do |w| w.is_a?(Integer) ? Layout::Constraint.length(w) : w end super( header:, rows:, widths: coerced_widths, row_highlight_style:, highlight_symbol:, highlight_spacing:, column_highlight_style:, cell_highlight_style:, selected_row: selected_row.nil? ? nil : Integer(selected_row), selected_column: selected_column.nil? ? nil : Integer(selected_column), offset: offset.nil? ? nil : Integer(offset), block:, footer:, flex:, style:, column_spacing: Integer(column_spacing) ) end |
Instance Method Details
#cell_selected? ⇒ Boolean
Returns true if both a row and column are selected (a cell is selected).
198 199 200 |
# File 'lib/ratatui_ruby/widgets/table.rb', line 198 def cell_selected? row_selected? && column_selected? end |
#column_selected? ⇒ Boolean
Returns true if a column is selected.
191 192 193 |
# File 'lib/ratatui_ruby/widgets/table.rb', line 191 def column_selected? !selected_column.nil? end |
#empty? ⇒ Boolean
Returns true if the table has no rows.
205 206 207 |
# File 'lib/ratatui_ruby/widgets/table.rb', line 205 def empty? rows.empty? end |
#row_selected? ⇒ Boolean
Returns true if a row is selected.
184 185 186 |
# File 'lib/ratatui_ruby/widgets/table.rb', line 184 def row_selected? !selected_row.nil? end |
