Class: Terminal::Table
- Inherits:
-
Object
- Object
- Terminal::Table
- Defined in:
- lib/terminal-table/row.rb,
lib/terminal-table/cell.rb,
lib/terminal-table/util.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, Util Classes: AsciiBorder, Border, Cell, MarkdownBorder, Row, Separator, Style, UnicodeBorder, UnicodeRoundBorder, UnicodeThickEdgeBorder
Constant Summary collapse
- VERSION =
'3.0.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(border_type: :div) ⇒ 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.
-
#elaborate_rows ⇒ Object
Elaborate rows to form an Array of Rows and Separators with adjacency properties added.
-
#initialize(options = {}, &block) ⇒ Table
constructor
Generates a ASCII/Unicode 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/Unicode table with the given options.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/terminal-table/table.rb', line 12 def initialize = {}, &block @elaborated = false @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.
204 205 206 207 208 |
# File 'lib/terminal-table/table.rb', line 204 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(border_type: :div) ⇒ Object
Add a separator.
49 50 51 |
# File 'lib/terminal-table/table.rb', line 49 def add_separator(border_type: :div) @rows << Separator.new(self, border_type: border_type) end |
#align_column(n, alignment) ⇒ Object
Align column n to the given alignment of :center, :left, or :right.
29 30 31 32 33 34 |
# File 'lib/terminal-table/table.rb', line 29 def align_column n, alignment # nil forces the column method to return the cell itself column(n, nil).each do |cell| 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_width 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 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 |
#elaborate_rows ⇒ Object
Elaborate rows to form an Array of Rows and Separators with adjacency properties added.
This is separated from the String rendering so that certain features may be tweaked before the String is built.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/terminal-table/table.rb', line 126 def elaborate_rows buffer = style.border_top ? [Separator.new(self, border_type: :top, implicit: true)] : [] unless @title.nil? buffer << Row.new(self, []) buffer << Separator.new(self, implicit: true) end @headings.each do |row| unless row.cells.empty? buffer << row buffer << Separator.new(self, border_type: :double, implicit: true) end end if style.all_separators @rows.each_with_index do |row, idx| # last separator is bottom, others are :div border_type = (idx == @rows.size - 1) ? :bot : :div buffer << row buffer << Separator.new(self, border_type: border_type, implicit: true) end else buffer += @rows buffer << Separator.new(self, border_type: :bot, implicit: true) if style.border_bottom end # After all implicit Separators are inserted we need to save off the # adjacent rows so that we can decide what type of intersections to use # based on column spans in the adjacent row(s). buffer.each_with_index do |r, idx| if r.is_a?(Separator) prev_row = idx > 0 ? buffer[idx - 1] : nil next_row = buffer.fetch(idx + 1, nil) r.save_adjacent_rows(prev_row, next_row) end end @elaborated = true @rows = buffer 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.
169 170 171 172 |
# File 'lib/terminal-table/table.rb', line 169 def render elaborate_rows unless @elaborated @rows.map { |r| style.margin_left + r.render.rstrip }.join("\n") end |
#rows ⇒ Object
Return rows without separator rows.
178 179 180 |
# File 'lib/terminal-table/table.rb', line 178 def rows @rows.reject { |row| row.is_a? Separator } end |
#rows=(array) ⇒ Object
182 183 184 185 |
# File 'lib/terminal-table/table.rb', line 182 def rows= array @rows = [] array.each { |arr| self << arr } end |
#style ⇒ Object
191 192 193 |
# File 'lib/terminal-table/table.rb', line 191 def style @style ||= Style.new end |
#style=(options) ⇒ Object
187 188 189 |
# File 'lib/terminal-table/table.rb', line 187 def style=() style.apply end |