Class: TTY::Table::Renderer::Basic Private
- Inherits:
-
Object
- Object
- TTY::Table::Renderer::Basic
- Includes:
- Validatable
- Defined in:
- lib/tty/table/renderer/basic.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Renders table without any border styles.
Instance Attribute Summary collapse
-
#alignments ⇒ Array
private
The table column alignments.
-
#border_class ⇒ TTY::Table::Border
private
Table border to be rendered.
-
#column_widths ⇒ Array[Integer]
Parses supplied column widths, if not present calculates natural widths.
-
#filter ⇒ Object
A callable object used for formatting field content.
-
#indent ⇒ Integer
The table indentation value.
-
#multiline ⇒ Boolean
The table column span behaviour.
-
#padding ⇒ TTY::Table::Padder
The table padding settings.
-
#resize ⇒ Integer
The table resizing behaviour.
-
#width ⇒ Integer
The table total width.
Instance Method Summary collapse
-
#border(border_opts = (not_set = true)) {|Table::BorderOptions| ... } ⇒ Object
(also: #border=)
Store border characters, style and separator for the table rendering.
-
#create_operations(widths) ⇒ Array[String, Operation]
private
Initialize operations.
-
#initialize(table, options = {}) ⇒ TTY::Table::Renderer::Basic
constructor
private
Initialize a Renderer.
-
#render ⇒ String
Renders table as string with border.
-
#select_operations ⇒ Object
private
Select applicable operations for this table.
Methods included from Validatable
#assert_row_size, #assert_row_sizes, #assert_table_type, #validate_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
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/tty/table/renderer/basic.rb', line 112 def initialize(table, = {}) @table = assert_table_type(table) @multiline = .fetch(:multiline) { false } @border = BorderDSL.new(.delete(:border)). unless @table.separators.empty? @border.separator ||= @table.separators end @column_widths = .fetch(:column_widths, nil) alignment = Array([:alignment]) * table.columns_size @alignments = AlignmentSet.new([:alignments] || alignment) @filter = .fetch(:filter) { proc { |val, _| val } } @width = .fetch(:width) { TTY::Screen.width } @border_class = .fetch(:border_class) { Border::Null } @indent = .fetch(:indent) { 0 } @resize = .fetch(:resize) { false } @padding = Strings::Padder.parse([:padding]) end |
Instance Attribute Details
#alignments ⇒ 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
50 51 52 |
# File 'lib/tty/table/renderer/basic.rb', line 50 def alignments @alignments end |
#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
36 37 38 |
# File 'lib/tty/table/renderer/basic.rb', line 36 def border_class @border_class end |
#column_widths ⇒ Array[Integer]
Parses supplied column widths, if not present calculates natural widths.
136 137 138 |
# File 'lib/tty/table/renderer/basic.rb', line 136 def column_widths @column_widths = Columns.widths_from(table, @column_widths) end |
#filter ⇒ Object
A callable object used for formatting field content
55 56 57 |
# File 'lib/tty/table/renderer/basic.rb', line 55 def filter @filter end |
#indent ⇒ Integer
The table indentation value
70 71 72 |
# File 'lib/tty/table/renderer/basic.rb', line 70 def indent @indent 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.
63 64 65 |
# File 'lib/tty/table/renderer/basic.rb', line 63 def multiline @multiline end |
#padding ⇒ TTY::Table::Padder
The table padding settings
93 94 95 |
# File 'lib/tty/table/renderer/basic.rb', line 93 def padding @padding end |
#resize ⇒ Integer
The table resizing behaviour. If true the algorithm will automatically expand or shrink table to fit the terminal width or specified width. By default its false.
86 87 88 |
# File 'lib/tty/table/renderer/basic.rb', line 86 def resize @resize end |
#width ⇒ Integer
The table total width
77 78 79 |
# File 'lib/tty/table/renderer/basic.rb', line 77 def width @width end |
Instance Method Details
#border(border_opts = (not_set = true)) {|Table::BorderOptions| ... } ⇒ Object Also known as: border=
Store border characters, style and separator for the table rendering
148 149 150 151 152 153 |
# File 'lib/tty/table/renderer/basic.rb', line 148 def border(border_opts = (not_set = true), &block) return @border if not_set && !block_given? border_opts = {} if not_set @border = BorderDSL.new(border_opts, &block). end |
#create_operations(widths) ⇒ Array[String, Operation]
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 operations
223 224 225 226 227 228 229 230 231 |
# File 'lib/tty/table/renderer/basic.rb', line 223 def create_operations(widths) [ [:alignment, Operation::Alignment.new(alignments, widths)], [:filter, Operation::Filter.new(filter)], [:truncation, Operation::Truncation.new(widths)], [:wrapping, Operation::Wrapped.new(widths)], [:padding, Operation::Padding.new(padding)] ] end |
#render ⇒ String
Renders table as string with border
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/tty/table/renderer/basic.rb', line 186 def render return if table.empty? operations = TTY::Table::Operations.new operations.add(:escape, Operation::Escape.new) operations.apply_to(table, :escape) unless multiline column_constraint = ColumnConstraint.new(table, self) @column_widths = column_constraint.enforce widths_without_padding = @column_widths.map do |_width| _width - padding.left - padding.right end create_operations(widths_without_padding).each do |op| operations.add(*op) end operations.apply_to(table, *select_operations) render_data.compact.join("\n") end |
#select_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.
Select applicable operations for this table
209 210 211 212 213 214 215 216 |
# File 'lib/tty/table/renderer/basic.rb', line 209 def select_operations ops = [] ops << :escape unless multiline ops << :alignment ops << (multiline ? :wrapping : :truncation) ops << :padding ops << :filter end |