Class: NattyUI::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/natty-ui/helper/table.rb

Overview

Data structure for building terminal tables.

A Table is populated through its #rows and #columns collections and then passed to Features#table for rendering. Cells accept text and formatting attributes; rows and columns can carry default attributes that are merged with individual cell attributes during rendering.

Examples:

Build and render a simple table

ui.table border_frame: :double do |t|
  t.add_row '[b]Name', '[b]Score', align: :center
  t.add_row 'Alice', 42
  t.add_row 'Bob',   17
end

Access cells by row and column index

ui.table do |t|
  t[0, 0] = 'Header'
  t[1, 0] = 'Row 1'
end

Defined Under Namespace

Classes: Cell, Column, ColumnCollection, Row, RowCollection

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#columnsColumnCollection (readonly)

Column collection for this table.

Returns:



1256
1257
1258
# File 'lib/natty-ui/helper/table.rb', line 1256

def columns
  @columns
end

#empty?Boolean (readonly)

Returns true when the table has no cells.

Returns:

  • (Boolean)

    whether the table is empty



1262
# File 'lib/natty-ui/helper/table.rb', line 1262

def empty? = @rows.none?

#rowsRowCollection (readonly)

Row collection for this table.

Returns:



1251
1252
1253
# File 'lib/natty-ui/helper/table.rb', line 1251

def rows
  @rows
end

Instance Method Details

#[](row_index, column_index = nil) ⇒ Row, Cell

Returns (or creates) the row at row_index, or the cell at [row_index, column_index].

Examples:

Get or create a row

table[0]

Get or create a cell

table[1, 2] = 'value'

Parameters:

  • row_index (#to_int)

    zero-based row index

  • column_index (#to_int, nil) (defaults to: nil)

    zero-based column index, or nil for the row

Returns:



1294
1295
1296
1297
# File 'lib/natty-ui/helper/table.rb', line 1294

def [](row_index, column_index = nil)
  row = @rows[row_index]
  column_index ? row[column_index] : row
end

#add_column(*texts, **attributes) {|column| ... } ⇒ Object, Column

Appends a new column to the table.

Examples:

table.add_column 'Header A', 'Header B', align: :center

Parameters:

  • texts (#to_s, ...)

    one text value per row in the new column

  • attributes (Hash)

    attribute options applied to the column, (see Cell#initialize)

Yields:

Yield Parameters:

Returns:

  • (Object)

    return value of the block

  • (Column)

    the Column, if no block is specified



1321
1322
1323
# File 'lib/natty-ui/helper/table.rb', line 1321

def add_column(*texts, **attributes, &)
  @columns.add(*texts, **attributes, &)
end

#add_row(*texts, **attributes) {|row| ... } ⇒ Object, Row

Appends a new row to the table.

Examples:

table.add_row 'Alice', 42, align: :center

Parameters:

Yields:

  • (row)

    the new Row

Yield Parameters:

Returns:

  • (Object)

    return value of the block

  • (Row)

    the new row, if no block is specified



1308
1309
1310
# File 'lib/natty-ui/helper/table.rb', line 1308

def add_row(*texts, **attributes, &)
  @rows.add(*texts, **attributes, &)
end

#at(row_index, column_index = nil) ⇒ Row, ...

Returns the row at row_index, or the cell at [row_index, column_index].

Returns nil when the row (or cell) does not exist.

Examples:

Get a row

table.at(0)

Get a cell

table.at(0, 2)

Parameters:

  • row_index (#to_int)

    zero-based row index

  • column_index (#to_int, nil) (defaults to: nil)

    zero-based column index, or nil for the row

Returns:



1277
1278
1279
1280
# File 'lib/natty-ui/helper/table.rb', line 1277

def at(row_index, column_index = nil)
  row = @rows.at(row_index) or return
  column_index ? row.at(column_index) : row
end

#each_column {|column| ... } ⇒ nil, Enumerator

Iterates over all columns in the table.

Examples:

table.each_column { |col| col.attributes.align = :right }

Yields:

  • (column)

    each non-empty Column

Yield Parameters:

Returns:

  • (nil)

    if block is specified

  • (Enumerator)

    if no block is specified



1343
# File 'lib/natty-ui/helper/table.rb', line 1343

def each_column(&) = @columns.each(&)

#each_row {|row| ... } ⇒ nil, Enumerator

Iterates over all rows in the table.

Examples:

table.each_row { |r| r.attributes.align = :center }

Yields:

  • (row)

    each non-nil Row

Yield Parameters:

Returns:

  • (nil)

    if block is specified

  • (Enumerator)

    if no block is specified



1333
# File 'lib/natty-ui/helper/table.rb', line 1333

def each_row(&) = @rows.each(&)