Class: TTY::Table
- Inherits:
-
Object
- Object
- TTY::Table
- Extended by:
- Forwardable
- Includes:
- Comparable, Enumerable, Conversion, Equatable, Renderer, Validatable
- Defined in:
- lib/tty/table.rb,
lib/tty/table/error.rb,
lib/tty/table/border.rb,
lib/tty/table/renderer.rb,
lib/tty/table/column_set.rb,
lib/tty/table/border/null.rb,
lib/tty/table/validatable.rb,
lib/tty/table/border/ascii.rb,
lib/tty/table/border/unicode.rb,
lib/tty/table/renderer/ascii.rb,
lib/tty/table/renderer/basic.rb,
lib/tty/table/renderer/color.rb,
lib/tty/table/renderer/unicode.rb,
lib/tty/table/operation/wrapped.rb,
lib/tty/table/operation/alignment.rb,
lib/tty/table/operation/truncation.rb,
lib/tty/table/operation/alignment_set.rb
Defined Under Namespace
Modules: Operation, Renderer, Validatable Classes: Border, ColumnSet, DimensionMismatchError
Constant Summary
Constants included from Validatable
Constants included from Renderer
Renderer::RENDERER_DELEGATED_METHODS, Renderer::RENDERER_MAPPER
Instance Attribute Summary collapse
-
#alignments ⇒ Operation::AlignmentSet
readonly
private
The table column alignments.
-
#column_widths ⇒ Array
The table enforced column widths.
-
#header ⇒ Enumerable
readonly
The table header.
Attributes included from Equatable
Class Method Summary collapse
-
.[](*rows) ⇒ Object
Create a new Table where each argument is a row.
-
.new(*args, &block) ⇒ Object
Instantiate a new Table.
-
.renderer ⇒ TTY::Table::Renderer
Determine renderer based on terminal capabilities.
- .renderer=(klass) ⇒ Object
Instance Method Summary collapse
-
#<<(row) ⇒ self
Add row to table.
-
#[](i, j) ⇒ Object
(also: #at, #element, #component)
Lookup element of the table given a row(i) and column(j).
-
#[]=(i, j, val) ⇒ Object
private
Set table value at row(i) and column(j).
-
#coerce(rows) ⇒ Array
Coerce an Enumerable into a Table This coercion mechanism is used by Table to handle Enumerable types and force them into array type.
-
#column(index) { ... } ⇒ self
Return a column number at the index of the table as an Array.
-
#column_size ⇒ Integer
Return the number of columns.
-
#direction ⇒ Object
The table orientation.
-
#each {|Array[Array]| ... } ⇒ self
Iterate over each tuple in the set.
-
#empty? ⇒ Boolean
Return true if this is an empty table, i.e.
-
#initialize(options = {}, &block) ⇒ TTY::Table
constructor
private
Initialize a Table.
-
#row(index) { ... } ⇒ self
Return a row number at the index of the table as an Array.
-
#row_size ⇒ Integer
Return the number of rows.
-
#size ⇒ Array
Return the number of rows and columns.
-
#to_s ⇒ String
Return string representation of table.
-
#width ⇒ Integer
Check table width.
Methods included from Validatable
#assert_matching_widths, #assert_row_sizes, #assert_string_values
Methods included from Equatable
#attr_reader, included, #inherited
Methods included from Renderer
#pick_renderer, #renderer, #renderer=
Methods included from Delegatable
Methods included from Conversion
Constructor Details
#initialize(options = {}, &block) ⇒ TTY::Table
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.
Initialize a Table
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/tty/table.rb', line 109 def initialize(={}, &block) @header = .fetch :header, nil @rows = coerce(.fetch :rows, []) @renderer = pick_renderer [:renderer] # TODO: assert that row_size is the same as column widths & aligns # TODO: this is where column extraction should happen! @column_widths = .fetch :column_widths, [] @alignments = Operation::AlignmentSet.new [:column_aligns] || [] assert_row_sizes @rows yield_or_eval &block if block_given? end |
Instance Attribute Details
#alignments ⇒ Operation::AlignmentSet (readonly)
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.
The table column alignments
41 42 43 |
# File 'lib/tty/table.rb', line 41 def alignments @alignments end |
#column_widths ⇒ Array
The table enforced column widths
34 35 36 |
# File 'lib/tty/table.rb', line 34 def column_widths @column_widths end |
#header ⇒ Enumerable (readonly)
The table header
19 20 21 |
# File 'lib/tty/table.rb', line 19 def header @header end |
Class Method Details
.[](*rows) ⇒ Object
Create a new Table where each argument is a row
59 60 61 |
# File 'lib/tty/table.rb', line 59 def self.[](*rows) self.new(:rows => rows) end |
.new(*args, &block) ⇒ Object
Instantiate a new Table
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/tty/table.rb', line 80 def self.new(*args, &block) = Utils.(args) if args.size.nonzero? rows = args.pop header = args.size.zero? ? nil : args.first super({:header => header, :rows => rows}.merge(), &block) else super(, &block) end end |
.renderer ⇒ TTY::Table::Renderer
Determine renderer based on terminal capabilities
11 12 13 14 15 16 17 |
# File 'lib/tty/table/renderer.rb', line 11 def self.renderer @renderer ||= if TTY.terminal.color? TTY::Table::Renderer::Color else TTY::Table::Renderer::Basic end end |
.renderer=(klass) ⇒ Object
20 21 22 |
# File 'lib/tty/table/renderer.rb', line 20 def self.renderer=(klass) @renderer = klass end |
Instance Method Details
#<<(row) ⇒ self
Add row to table
202 203 204 205 206 207 |
# File 'lib/tty/table.rb', line 202 def <<(row) rows_copy = rows.dup assert_row_sizes rows_copy << row rows << row self end |
#[](i, j) ⇒ Object Also known as: at, element, component
Lookup element of the table given a row(i) and column(j)
125 126 127 128 129 130 131 |
# File 'lib/tty/table.rb', line 125 def [](i, j) if i >= 0 && j >= 0 rows.fetch(i){return nil}[j] else raise IndexError.new("element at(#{i},#{j}) not found") end end |
#[]=(i, j, val) ⇒ Object
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.
Set table value at row(i) and column(j)
139 140 141 |
# File 'lib/tty/table.rb', line 139 def []=(i, j, val) @rows[i][j] = val end |
#coerce(rows) ⇒ Array
Coerce an Enumerable into a Table This coercion mechanism is used by Table to handle Enumerable types and force them into array type.
303 304 305 |
# File 'lib/tty/table.rb', line 303 def coerce(rows) convert_to_array(rows) end |
#column(index) { ... } ⇒ self
Return a column number at the index of the table as an Array. When a block is given, the elements of that Array are iterated over.
185 186 187 188 189 190 191 192 193 |
# File 'lib/tty/table.rb', line 185 def column(index) if block_given? return self if index >= column_size || index < 0 rows.map { |row| yield row[index].compact } else return nil if index >= column_size || index < 0 rows.map { |row| row[index] }.compact end end |
#column_size ⇒ Integer
Return the number of columns
236 237 238 239 |
# File 'lib/tty/table.rb', line 236 def column_size return rows[0].size if (rows.size > 0) return 0 end |
#direction ⇒ Object
The table orientation
49 50 51 |
# File 'lib/tty/table.rb', line 49 def direction # TODO implement table orientation end |
#each {|Array[Array]| ... } ⇒ self
Iterate over each tuple in the set
220 221 222 223 224 225 226 |
# File 'lib/tty/table.rb', line 220 def each return to_enum unless block_given? rows.each do |row| yield row end self end |
#empty? ⇒ Boolean
Return true if this is an empty table, i.e. if the number of rows or the number of columns is 0
280 281 282 |
# File 'lib/tty/table.rb', line 280 def empty? column_size == 0 || row_size == 0 end |
#row(index) { ... } ⇒ self
Return a row number at the index of the table as an Array. When a block is given, the elements of that Array are iterated over.
160 161 162 163 164 165 166 167 |
# File 'lib/tty/table.rb', line 160 def row(index, &block) if block_given? rows.fetch(index){return self}.each(&block) self else rows.fetch(index){return nil} end end |
#row_size ⇒ Integer
Return the number of rows
249 250 251 |
# File 'lib/tty/table.rb', line 249 def row_size rows.size end |
#size ⇒ Array
Return the number of rows and columns
261 262 263 |
# File 'lib/tty/table.rb', line 261 def size [row_size, column_size] end |
#to_s ⇒ String
Return string representation of table
289 290 291 |
# File 'lib/tty/table.rb', line 289 def to_s render(self) end |