Class: TTY::Table::ColumnSet

Inherits:
Object
  • Object
show all
Extended by:
Delegatable
Includes:
Equatable
Defined in:
lib/tty/table/column_set.rb

Overview

A class that represents table columns properties.

Instance Attribute Summary collapse

Attributes included from Equatable

#comparison_attrs

Instance Method Summary collapse

Methods included from Delegatable

delegatable_method

Methods included from Equatable

#attr_reader, included, #inherited

Constructor Details

#initialize(table) ⇒ ColumnSet

Returns a new instance of ColumnSet.



15
16
17
# File 'lib/tty/table/column_set.rb', line 15

def initialize(table)
  @table = table
end

Instance Attribute Details

#tableObject (readonly)

Returns the value of attribute table.



11
12
13
# File 'lib/tty/table/column_set.rb', line 11

def table
  @table
end

Instance Method Details

#extract_widths!Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calcualte maximum column widths

Returns:

  • (Array)

    column widths



33
34
35
36
37
38
39
40
41
42
# File 'lib/tty/table/column_set.rb', line 33

def extract_widths!
  return column_widths unless column_widths.empty?

  rows     = table.to_a
  data     = table.header ? rows + [table.header] : rows
  colcount = data.max { |row_a, row_b| row_a.size <=> row_b.size }.size

  table.column_widths = find_maximas colcount, data
  self
end

#total_widthInteger

Calculate total table width

Returns:

  • (Integer)


24
25
26
# File 'lib/tty/table/column_set.rb', line 24

def total_width
  column_widths.reduce(:+)
end