Class: TTY::Table::Renderer::Basic
- Inherits:
-
Object
- Object
- TTY::Table::Renderer::Basic
- Includes:
- Validatable
- Defined in:
- lib/tty/table/renderer/basic.rb
Overview
Renders table without any border styles.
Constant Summary
Constants included from Validatable
Instance Attribute Summary collapse
-
#border_class ⇒ TTY::Table::Border
private
Table border to be rendered.
-
#column_aligns ⇒ Array
private
The table column alignments.
-
#column_widths ⇒ Array
The table enforced column widths.
-
#filter ⇒ Object
A callable object used for formatting field content.
-
#multiline ⇒ Boolean
The table column span behaviour.
-
#operations ⇒ Object
readonly
The table operations applied to rows.
Instance Method Summary collapse
-
#add_operations ⇒ Object
private
Initialize and add operations.
-
#border(options = (not_set=true)) { ... } ⇒ Object
Store border characters, style and separator for the table rendering.
-
#initialize(table, options = {}) ⇒ TTY::Table::Renderer::Basic
constructor
private
Initialize a Renderer.
-
#padding=(value) ⇒ Object
Sets the output padding,.
-
#render ⇒ String
Renders table.
Methods included from Validatable
#assert_matching_widths, #assert_row_sizes, #assert_string_values, #validate_options!, #validate_rendering_options!
Constructor Details
#initialize(table, options = {}) ⇒ TTY::Table::Renderer::Basic
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 Renderer
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/tty/table/renderer/basic.rb', line 74 def initialize(table, ={}) () @table = table || (raise ArgumentRequired, "Expected TTY::Table instance, got #{table.inspect}") @multiline = .fetch(:multiline) { false } @operations = TTY::Table::Operations.new(table) unless multiline @operations.add_operation(:escape, Operation::Escape.new) @operations.run_operations(:escape) end @border = TTY::Table::BorderOptions.from(.delete(:border)) @column_widths = Array(.fetch(:column_widths) { ColumnSet.new(table).extract_widths }).map(&:to_i) @column_aligns = Array(.delete(:column_aligns)).map(&:to_sym) @filter = .fetch(:filter) { proc { |val, row, col| val } } @width = .fetch(:width) { TTY.terminal.width } @border_class = .fetch(:border_class) { Border::Null } end |
Instance Attribute Details
#border_class ⇒ TTY::Table::Border
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.
Table border to be rendered
26 27 28 |
# File 'lib/tty/table/renderer/basic.rb', line 26 def border_class @border_class end |
#column_aligns ⇒ Array
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
40 41 42 |
# File 'lib/tty/table/renderer/basic.rb', line 40 def column_aligns @column_aligns end |
#column_widths ⇒ Array
The table enforced column widths
33 34 35 |
# File 'lib/tty/table/renderer/basic.rb', line 33 def column_widths @column_widths end |
#filter ⇒ Object
A callable object used for formatting field content
50 51 52 |
# File 'lib/tty/table/renderer/basic.rb', line 50 def filter @filter end |
#multiline ⇒ Boolean
The table column span behaviour. When true the column’s line breaks cause the column to span multiple rows. By default set to false.
58 59 60 |
# File 'lib/tty/table/renderer/basic.rb', line 58 def multiline @multiline end |
#operations ⇒ Object (readonly)
The table operations applied to rows
45 46 47 |
# File 'lib/tty/table/renderer/basic.rb', line 45 def operations @operations end |
Instance Method Details
#add_operations ⇒ 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.
Initialize and add operations
115 116 117 118 119 120 |
# File 'lib/tty/table/renderer/basic.rb', line 115 def add_operations operations.add_operation(:alignment, Operation::AlignmentSet.new(column_aligns, column_widths)) operations.add_operation(:filter, Operation::Filter.new(filter)) operations.add_operation(:truncation, Operation::Truncation.new(column_widths)) operations.add_operation(:wrapping, Operation::Wrapped.new(column_widths)) end |
#border(options = (not_set=true)) { ... } ⇒ Object
Store border characters, style and separator for the table rendering
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/tty/table/renderer/basic.rb', line 101 def border(=(not_set=true), &block) @border = TTY::Table::BorderOptions.new unless @border if block_given? border_dsl = TTY::Table::BorderDSL.new(&block) @border = border_dsl. elsif !not_set @border = TTY::Table::BorderOptions.from() end @border end |
#padding=(value) ⇒ Object
Sets the output padding,
128 129 130 |
# File 'lib/tty/table/renderer/basic.rb', line 128 def padding=(value) @padding = [0, value].max end |
#render ⇒ String
Renders table
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/tty/table/renderer/basic.rb', line 137 def render return if table.empty? # TODO: throw an error if too many columns as compared to terminal width # and then change table.orientation from vertical to horizontal # TODO: Decide about table orientation add_operations ops = [:filter, :alignment] multiline ? ops << :wrapping : ops << :truncation operations.run_operations(*ops) render_data.compact.join("\n") end |