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



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

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



131
132
133
134
135
136
137
138
139
# File 'lib/natty-ui/wrapper/table.rb', line 131

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:



181
182
183
184
185
186
187
# File 'lib/natty-ui/wrapper/table.rb', line 181

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:



161
162
163
164
165
166
167
168
169
# File 'lib/natty-ui/wrapper/table.rb', line 161

def add_row(*columns, align: nil, style: nil)
  if columns.size == 1 && columns[0].respond_to?(:map)
    columns = columns[0]
  end
  columns =
    columns.map { |cell| as_cell(cell, align, style) if cell }.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:



267
268
269
270
271
272
273
274
275
# File 'lib/natty-ui/wrapper/table.rb', line 267

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:



245
246
247
248
249
250
251
252
253
# File 'lib/natty-ui/wrapper/table.rb', line 245

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



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

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

#col_countInteger

Returns count of columns.

Returns:

  • (Integer)

    count of columns



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

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

#row_countInteger

Returns count of rows.

Returns:

  • (Integer)

    count of rows



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

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:



223
224
225
226
227
228
229
230
231
# File 'lib/natty-ui/wrapper/table.rb', line 223

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:



201
202
203
204
205
206
207
208
209
# File 'lib/natty-ui/wrapper/table.rb', line 201

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>>)


281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/natty-ui/wrapper/table.rb', line 281

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