Class: NattyUI::Features::Table

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

Overview

Helper class to define a table layout.

See Also:

Defined Under Namespace

Classes: Cell

Instance Method Summary collapse

Instance Method Details

#[](row, col) ⇒ Cell?

Get the Cell at a table position.

Parameters:

  • row (Integer)

    row index

  • col (Integer)

    column index

Returns:

  • (Cell)

    at row/column

  • (nil)

    if no cell defined at row/column



113
# File 'lib/natty-ui/wrapper/table.rb', line 113

def [](row, col) = @rows.dig(row, col)

#[]=(row, col, value) ⇒ Cell, ...

Change Cell or (text) value at a table position.

Examples:

change Cell at row 2, column 3

table[2, 3] = table.cell('Hello World', align: right, style: 'bold')

change text at row 2, column 3

table[2, 3] = 'Hello Ruby!'

delete Cell at row 2, column 3

table[2, 3] = nil

Parameters:

  • row (Integer)

    row index

  • col (Integer)

    column index

  • value (Cell, #to_s, nil)

    Cell or text to use at specified position

Returns:

  • (Cell, #to_s, nil)

    the value



128
129
130
131
132
133
134
135
136
# File 'lib/natty-ui/wrapper/table.rb', line 128

def []=(row, col, value)
  row = (@rows[row] ||= [])
  if value.nil? || value.is_a?(Cell)
    row[col] = value
  else
    cell = row[col]
    cell ? cell.value = value : row[col] = Cell.new(value, nil, nil)
  end
end

#add_column(*rows, align: nil, style: nil) ⇒ Table

Add a new column to the table.

Examples:

add a column of three rows with bold styled text

table.add_column('One', 'Two', 'Three', style: :bold)

Parameters:

  • rows (#map)

    Enumerable-like object containing texts for each row

  • align (:left, :right, :center) (defaults to: nil)

    text alignment

  • style (String) (defaults to: nil)

    text style; see Ansi.try_convert

Returns:



177
178
179
180
181
182
183
# File 'lib/natty-ui/wrapper/table.rb', line 177

def add_column(*rows, align: nil, style: nil)
  rows = rows[0] if rows.size == 1 && rows[0].respond_to?(:map)
  row_idx = -1
  rows.each do |cell|
    (@rows[row_idx += 1] ||= []) << as_cell(cell, align, style)
  end
end

#add_row(*columns, align: nil, style: nil) ⇒ Table Also known as: add

Add a new row to the table.

Examples:

add a row with three right-aligned columns

table.add_row('One', 'Two', 'Three', align: :right)

Parameters:

  • columns (#map)

    Enumerable-like object containing column texts

  • align (:left, :right, :center) (defaults to: nil)

    text alignment

  • style (String) (defaults to: nil)

    text style; see Ansi.try_convert

Returns:



158
159
160
161
162
163
164
165
# File 'lib/natty-ui/wrapper/table.rb', line 158

def add_row(*columns, align: nil, style: nil)
  if columns.size == 1 && columns[0].respond_to?(:map)
    columns = columns[0]
  end
  columns = columns.map { as_cell(_1, align, style) if _1 }.to_a
  @rows << (columns.empty? ? nil : columns)
  self
end

#align_column(column, alignment) ⇒ Table

Change text alignment of one or more column.

Examples:

align first column right

table.align_column(0, :right)

center first three columns

table.align_column(0..2, :center)

center the columns with index 3, 4 and 7

table.align_column([3, 4, 7], :center)

Parameters:

  • column (Integer, Enumerable<Integer>)

    index of column(s) to change

  • alignment (:left, :right, :center)

Returns:



263
264
265
266
267
268
269
270
271
# File 'lib/natty-ui/wrapper/table.rb', line 263

def align_column(column, alignment)
  if column.is_a?(Integer)
    column = [column]
  elsif !column.is_a?(Enumerable)
    raise(TypeError, "invalid column value - #{column}")
  end
  @rows.each { |row| column.each { row[_1]&.align = alignment } }
  self
end

#align_row(row, alignment) ⇒ Table

Change text alignment of one or more rows.

Examples:

align first row right

table.align_row(0, :right)

center first three rows

table.align_row(0..2, :center)

center the rows with index 3, 4 and 7

table.align_row([3, 4, 7], :center)

Parameters:

  • row (Integer, Enumerable<Integer>)

    index of row(s) to change

  • alignment (:left, :right, :center)

Returns:



241
242
243
244
245
246
247
248
249
# File 'lib/natty-ui/wrapper/table.rb', line 241

def align_row(row, alignment)
  if row.is_a?(Integer)
    row = [row]
  elsif !row.is_a?(Enumerable)
    raise(TypeError, "invalid row value - #{row}")
  end
  row.each { |r| @rows[r]&.each { _1&.align = alignment } }
  self
end

#cell(value, align: :left, style: nil) ⇒ Cell

Create a new cell.

Examples:

create a Cell with right aligned bold text "Hello World"

table[2, 3] = table.cell('Hello World', align: right, style: 'bold')

Parameters:

  • value (#to_s)

    text value

  • align (:left, :right, :center) (defaults to: :left)

    text alignment

  • style (String) (defaults to: nil)

    text style; see Ansi.try_convert

Returns:

  • (Cell)

    a new cell



147
# File 'lib/natty-ui/wrapper/table.rb', line 147

def cell(value, align: :left, style: nil) = Cell.new(value, align, style)

#col_countInteger

Returns count of columns.

Returns:

  • (Integer)

    count of columns



105
# File 'lib/natty-ui/wrapper/table.rb', line 105

def col_count = @rows.max_by { _1&.size || 0 }&.size || 0

#row_countInteger

Returns count of rows.

Returns:

  • (Integer)

    count of rows



102
# File 'lib/natty-ui/wrapper/table.rb', line 102

def row_count = @rows.size

#style_column(column, style) ⇒ Table

Change style of one or more columns.

Examples:

define bold red text style for the first column

table.style_column(0, 'bold red')

define yellow text style for the first three columns

table.style_column(0..2, 'yellow')

define green text style for columns with index 3, 4 and 7

table.style_column([3, 4, 7], 'green')

Parameters:

  • column (Integer, Enumerable<Integer>)

    index of column(s) to change

  • style (String, nil)

    text style; see Ansi.try_convert

Returns:



219
220
221
222
223
224
225
226
227
# File 'lib/natty-ui/wrapper/table.rb', line 219

def style_column(column, style)
  if column.is_a?(Integer)
    column = [column]
  elsif !column.is_a?(Enumerable)
    raise(TypeError, "invalid column value - #{column}")
  end
  @rows.each { |row| column.each { row[_1]&.style = style } }
  self
end

#style_row(row, style) ⇒ Table

Change style of one or more rows.

Examples:

define bold red text style for the first row

table.style_row(0, 'bold red')

define yellow text style for the first three rows

table.style_row(0..2, 'yellow')

define green text style for rows 3, 4 and 7

table.style_row([3, 4, 7], 'green')

Parameters:

  • row (Integer, Enumerable<Integer>)

    index of row(s) to change

  • style (String, nil)

    text style; see Ansi.try_convert

Returns:



197
198
199
200
201
202
203
204
205
# File 'lib/natty-ui/wrapper/table.rb', line 197

def style_row(row, style)
  if row.is_a?(Integer)
    row = [row]
  elsif !row.is_a?(Enumerable)
    raise(TypeError, "invalid row value - #{row}")
  end
  row.each { |r| @rows[r]&.each { _1&.style = style } }
  self
end

#to_aArray<Array<Cell>>

Convert the table to the compactest (two-dimensional) array representation.

Returns:

  • (Array<Array<Cell>>)


277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/natty-ui/wrapper/table.rb', line 277

def to_a
  ret = []
  ridx = -1
  @rows.each do |row|
    ridx += 1
    next unless row
    count = 0
    row =
      row.map do |cell|
        next unless cell
        next if cell.value.empty?
        count += 1
        cell.dup
      end
    ret[ridx] = row if count.positive?
  end
  ret
end