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.1'
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.
206 207 208 209 210 |
# File 'lib/terminal-table/table.rb', line 206 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.
41 42 43 44 45 |
# File 'lib/terminal-table/table.rb', line 41 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.
51 52 53 |
# File 'lib/terminal-table/table.rb', line 51 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 35 36 |
# File 'lib/terminal-table/table.rb', line 29 def align_column n, alignment r = rows column(n).each_with_index do |col, i| cell = r[i][n] next unless cell cell.alignment = alignment unless cell.alignment? end end |
#cell_padding ⇒ Object
59 60 61 |
# File 'lib/terminal-table/table.rb', line 59 def cell_padding style.padding_left + style.padding_right end |
#cell_spacing ⇒ Object
55 56 57 |
# File 'lib/terminal-table/table.rb', line 55 def cell_spacing cell_padding + style.border_y_width end |
#column(n, method = :value, array = rows) ⇒ Object
Return column n.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/terminal-table/table.rb', line 66 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.
98 99 100 |
# File 'lib/terminal-table/table.rb', line 98 def column_width n column_widths[n] || 0 end |
#column_with_headings(n, method = :value) ⇒ Object
Return n column including headings.
84 85 86 |
# File 'lib/terminal-table/table.rb', line 84 def column_with_headings n, method = :value column n, method, headings_with_rows end |
#columns ⇒ Object
Return columns.
91 92 93 |
# File 'lib/terminal-table/table.rb', line 91 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.
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 165 166 |
# File 'lib/terminal-table/table.rb', line 128 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.
106 107 108 |
# File 'lib/terminal-table/table.rb', line 106 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.
171 172 173 174 |
# File 'lib/terminal-table/table.rb', line 171 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.
180 181 182 |
# File 'lib/terminal-table/table.rb', line 180 def rows @rows.reject { |row| row.is_a? Separator } end |
#rows=(array) ⇒ Object
184 185 186 187 |
# File 'lib/terminal-table/table.rb', line 184 def rows= array @rows = [] array.each { |arr| self << arr } end |
#style ⇒ Object
193 194 195 |
# File 'lib/terminal-table/table.rb', line 193 def style @style ||= Style.new end |
#style=(options) ⇒ Object
189 190 191 |
# File 'lib/terminal-table/table.rb', line 189 def style=() style.apply end |