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/orientation.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/orientation/vertical.rb,
lib/tty/table/orientation/horizontal.rb,
lib/tty/table/operation/alignment_set.rb
Defined Under Namespace
Modules: Operation, Renderer, Validatable Classes: Border, ColumnSet, DimensionMismatchError, Orientation, TupleMissing
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.
-
#border_class ⇒ Object
readonly
private
The table border class.
-
#column_widths ⇒ Array
The table enforced column widths.
-
#header ⇒ Enumerable
readonly
The table header.
-
#orientation ⇒ Object
The table orientation out of :horizontal and :vertical.
Attributes included from Equatable
Class Method Summary collapse
-
.[](*rows) ⇒ Object
Create a new Table where each argument is a row.
-
.extract_tuples(args) ⇒ Object
private
Extract header and row tuples from arguments.
-
.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 = false) ⇒ 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.
-
#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.
-
#rotate ⇒ self
private
Rotate the table between vertical and horizontal orientation.
-
#rotate_horizontal ⇒ Object
private
Rotate the table horizontally.
-
#rotate_vertical ⇒ Object
private
Rotate the table vertically.
-
#rotated? ⇒ Boolean
Marks this table as rotated.
-
#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, #validate_options!
Methods included from Equatable
#attr_reader, included, #inherited
Methods included from Renderer
#pick_renderer, #renderer, #renderer=, #renders_with
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
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/tty/table.rb', line 131 def initialize(={}, &block) @header = .fetch(:header) { nil } @rows = coerce .fetch(:rows) { [] } @renderer = pick_renderer [:renderer] @orientation = Orientation.coerce .fetch(:orientation) { :horizontal } # TODO: assert that row_size is the same as column widths & aligns @column_widths = Array(.delete(:column_widths)).map(&:to_i) @alignments = Operation::AlignmentSet.new Array(.delete(:column_aligns)).map(&:to_sym) assert_row_sizes @rows @orientation.transform(self) 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 |
#border_class ⇒ Object (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 border class
46 47 48 |
# File 'lib/tty/table.rb', line 46 def border_class @border_class 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 |
#orientation ⇒ Object
The table orientation out of :horizontal and :vertical
53 54 55 |
# File 'lib/tty/table.rb', line 53 def orientation @orientation end |
Class Method Details
.[](*rows) ⇒ Object
Create a new Table where each argument is a row
65 66 67 |
# File 'lib/tty/table.rb', line 65 def self.[](*rows) self.new(:rows => rows) end |
.extract_tuples(args) ⇒ 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.
Extract header and row tuples from arguments
103 104 105 106 107 108 109 110 111 |
# File 'lib/tty/table.rb', line 103 def self.extract_tuples(args) rows = args.pop header = args.size.zero? ? nil : args.first if rows.first.is_a?(Hash) header = rows.map(&:keys).flatten.uniq rows = rows.inject([]) { |arr, el| arr + el.values } end { :header => header, :rows => rows } end |
.new(*args, &block) ⇒ Object
Instantiate a new Table
89 90 91 92 93 94 95 96 |
# File 'lib/tty/table.rb', line 89 def self.new(*args, &block) = Utils.(args) if args.size.nonzero? super(extract_tuples(args).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
276 277 278 279 280 281 |
# File 'lib/tty/table.rb', line 276 def <<(row) rows_copy = rows.dup assert_row_sizes rows_copy << row rows << row self end |
#[](i, j = false) ⇒ Object Also known as: at, element, component
Lookup element of the table given a row(i) and column(j)
198 199 200 201 202 203 204 205 |
# File 'lib/tty/table.rb', line 198 def [](i, j=false) return row(i) unless j if i >= 0 && j >= 0 rows.fetch(i){return nil}[j] else raise TTY::Table::TupleMissing.new(i,j) 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)
213 214 215 |
# File 'lib/tty/table.rb', line 213 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.
377 378 379 |
# File 'lib/tty/table.rb', line 377 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.
259 260 261 262 263 264 265 266 267 |
# File 'lib/tty/table.rb', line 259 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
310 311 312 313 |
# File 'lib/tty/table.rb', line 310 def column_size return rows[0].size if (rows.size > 0) return 0 end |
#each {|Array[Array]| ... } ⇒ self
Iterate over each tuple in the set
294 295 296 297 298 299 300 |
# File 'lib/tty/table.rb', line 294 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
354 355 356 |
# File 'lib/tty/table.rb', line 354 def empty? column_size == 0 || row_size == 0 end |
#rotate ⇒ self
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.
Rotate the table between vertical and horizontal orientation
168 169 170 171 |
# File 'lib/tty/table.rb', line 168 def rotate orientation.transform(self) self end |
#rotate_horizontal ⇒ 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.
Rotate the table horizontally
185 186 187 188 189 190 191 192 193 |
# File 'lib/tty/table.rb', line 185 def rotate_horizontal transposed = rows.transpose if header && header.empty? @rows = transposed[1..-1] @header = transposed[0] elsif rotated? @rows = transposed end end |
#rotate_vertical ⇒ 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.
Rotate the table vertically
176 177 178 179 180 |
# File 'lib/tty/table.rb', line 176 def rotate_vertical @rows = ([header].compact + rows).transpose @header = [] if header @rotated = true end |
#rotated? ⇒ Boolean
Marks this table as rotated
159 160 161 |
# File 'lib/tty/table.rb', line 159 def rotated? @rotated 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.
234 235 236 237 238 239 240 241 |
# File 'lib/tty/table.rb', line 234 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
323 324 325 |
# File 'lib/tty/table.rb', line 323 def row_size rows.size end |
#size ⇒ Array
Return the number of rows and columns
335 336 337 |
# File 'lib/tty/table.rb', line 335 def size [row_size, column_size] end |
#to_s ⇒ String
Return string representation of table
363 364 365 |
# File 'lib/tty/table.rb', line 363 def to_s render(self) end |