Class: Terminal::Table
- Inherits:
-
Object
- Object
- Terminal::Table
- Defined in:
- lib/terminal-table/row.rb,
lib/terminal-table/cell.rb,
lib/terminal-table/style.rb,
lib/terminal-table/table.rb,
lib/terminal-table/version.rb,
lib/terminal-table/separator.rb,
lib/terminal-table/table_helper.rb
Defined Under Namespace
Modules: TableHelper Classes: Cell, Row, Separator, Style
Constant Summary collapse
- VERSION =
'1.7.2'
Instance Attribute Summary collapse
-
#headings ⇒ Object
Returns the value of attribute headings.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Check if other is equal to self.
-
#add_row(array) ⇒ Object
(also: #<<)
Add a row.
-
#add_separator ⇒ Object
Add a separator.
-
#align_column(n, alignment) ⇒ Object
Align column n to the given alignment of :center, :left, or :right.
- #cell_padding ⇒ Object
- #cell_spacing ⇒ Object
-
#column(n, method = :value, array = rows) ⇒ Object
Return column n.
-
#column_width(n) ⇒ Object
(also: #length_of_column)
Return length of column n.
-
#column_with_headings(n, method = :value) ⇒ Object
Return n column including headings.
-
#columns ⇒ Object
Return columns.
-
#initialize(options = {}, &block) ⇒ Table
constructor
Generates a ASCII table with the given options.
-
#number_of_columns ⇒ Object
Return total number of columns available.
-
#render ⇒ Object
(also: #to_s)
Render the table.
-
#rows ⇒ Object
Return rows without separator rows.
- #rows=(array) ⇒ Object
- #style ⇒ Object
- #style=(options) ⇒ Object
Constructor Details
#initialize(options = {}, &block) ⇒ Table
Generates a ASCII table with the given options.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/terminal-table/table.rb', line 12 def initialize = {}, &block @headings = [] @rows = [] @column_widths = [] self.style = .fetch :style, {} self.headings = .fetch :headings, [] self.rows = .fetch :rows, [] self.title = .fetch :title, nil yield_or_eval(&block) if block style.on_change(:width) { require_column_widths_recalc } end |
Instance Attribute Details
#headings ⇒ Object
Returns the value of attribute headings.
7 8 9 |
# File 'lib/terminal-table/table.rb', line 7 def headings @headings end |
#title ⇒ Object
Returns the value of attribute title.
6 7 8 |
# File 'lib/terminal-table/table.rb', line 6 def title @title end |
Instance Method Details
#==(other) ⇒ Object
Check if other is equal to self. other is considered equal if it contains the same headings and rows.
175 176 177 178 179 |
# File 'lib/terminal-table/table.rb', line 175 def == other if other.respond_to? :render and other.respond_to? :rows self.headings == other.headings and self.rows == other.rows end end |
#add_row(array) ⇒ Object Also known as: <<
Add a row.
39 40 41 42 43 |
# File 'lib/terminal-table/table.rb', line 39 def add_row array row = array == :separator ? Separator.new(self) : Row.new(self, array) @rows << row require_column_widths_recalc unless row.is_a?(Separator) end |
#add_separator ⇒ Object
Add a separator.
49 50 51 |
# File 'lib/terminal-table/table.rb', line 49 def add_separator self << :separator end |
#align_column(n, alignment) ⇒ Object
Align column n to the given alignment of :center, :left, or :right.
28 29 30 31 32 33 34 |
# File 'lib/terminal-table/table.rb', line 28 def align_column n, alignment r = rows column(n).each_with_index do |col, i| cell = r[i][n] cell.alignment = alignment unless cell.alignment? end end |
#cell_padding ⇒ Object
57 58 59 |
# File 'lib/terminal-table/table.rb', line 57 def cell_padding style.padding_left + style.padding_right end |
#cell_spacing ⇒ Object
53 54 55 |
# File 'lib/terminal-table/table.rb', line 53 def cell_spacing cell_padding + style.border_y.length end |
#column(n, method = :value, array = rows) ⇒ Object
Return column n.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/terminal-table/table.rb', line 64 def column n, method = :value, array = rows array.map { |row| # for each cells in a row, find the column with index # just greater than the required one, and go back one. index = col = 0 row.cells.each do |cell| break if index > n index += cell.colspan col += 1 end cell = row[col - 1] cell && method ? cell.__send__(method) : cell }.compact end |
#column_width(n) ⇒ Object Also known as: length_of_column
Return length of column n.
96 97 98 |
# File 'lib/terminal-table/table.rb', line 96 def column_width n width = column_widths[n] || 0 end |
#column_with_headings(n, method = :value) ⇒ Object
Return n column including headings.
82 83 84 |
# File 'lib/terminal-table/table.rb', line 82 def column_with_headings n, method = :value column n, method, headings_with_rows end |
#columns ⇒ Object
Return columns.
89 90 91 |
# File 'lib/terminal-table/table.rb', line 89 def columns (0...number_of_columns).map { |n| column n } end |
#number_of_columns ⇒ Object
Return total number of columns available.
104 105 106 |
# File 'lib/terminal-table/table.rb', line 104 def number_of_columns headings_with_rows.map { |r| r.number_of_columns }.max || 0 end |
#render ⇒ Object Also known as: to_s
Render the table.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/terminal-table/table.rb', line 123 def render separator = Separator.new(self) buffer = [separator] unless @title.nil? buffer << Row.new(self, []) buffer << separator end @headings.each do |row| unless row.cells.empty? buffer << row buffer << separator end end if style.all_separators buffer += @rows.product([separator]).flatten else buffer += @rows buffer << separator end buffer.map { |r| style.margin_left + r.render.rstrip }.join("\n") end |
#rows ⇒ Object
Return rows without separator rows.
149 150 151 |
# File 'lib/terminal-table/table.rb', line 149 def rows @rows.reject { |row| row.is_a? Separator } end |
#rows=(array) ⇒ Object
153 154 155 156 |
# File 'lib/terminal-table/table.rb', line 153 def rows= array @rows = [] array.each { |arr| self << arr } end |
#style ⇒ Object
162 163 164 |
# File 'lib/terminal-table/table.rb', line 162 def style @style ||= Style.new end |
#style=(options) ⇒ Object
158 159 160 |
# File 'lib/terminal-table/table.rb', line 158 def style=() style.apply end |