class TTY::Table::Renderer::Basic
Renders table without any border styles.
@api private
Attributes
The table column alignments
@return [Array]
@api private
The table enforced column widths
@return [Array]
@api public
A callable object used for formatting field content
@api public
The table indentation value
@return [Integer]
@api public
The table column span behaviour. When true the column's line breaks cause the column to span multiple rows. By default set to false.
@return [Boolean]
@api public
The table padding settings
@return [TTY::Table::Padder]
@api public
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.
@return [Integer]
@api public
The table total width
@return [Integer]
@api public
Public Class Methods
Initialize a Renderer
@param [Hash] options @option options [String] :alignments
used to format table individual column alignment
@option options [Hash] :border
the border options
@option options [String] :column_widths
used to format table individula column width
@option options [Integer] :indent
indent the first column by indent value
@option options [Integer,Array] :padding
add padding to table fields
@return [TTY::Table::Renderer::Basic]
@api private
# File lib/tty/table/renderer/basic.rb, line 112 def initialize(table, options = {}) @table = assert_table_type(table) @multiline = options.fetch(:multiline) { false } @border = TTY::Table::BorderOptions.from(options.delete(:border)) @column_widths = options.fetch(:column_widths, nil) alignment = Array(options[:alignment]) * table.columns_size @alignments = TTY::Table::AlignmentSet.new(options[:alignments] || alignment) @filter = options.fetch(:filter) { proc { |val, _| val } } @width = options.fetch(:width) { TTY::Screen.width } @border_class = options.fetch(:border_class) { Border::Null } @indent = options.fetch(:indent) { 0 } @resize = options.fetch(:resize) { false } @padding = Strings::Padder.parse(options[:padding]) end
Public Instance Methods
Store border characters, style and separator for the table rendering
@param [Hash, Table::BorderOptions] options
@yield [Table::BorderOptions]
block representing border options
@api public
# File lib/tty/table/renderer/basic.rb, line 145 def border(options=(not_set=true), &block) @border = TTY::Table::BorderOptions.new unless @border if block_given? border_dsl = TTY::Table::BorderDSL.new(&block) @border = border_dsl.options elsif !not_set @border = TTY::Table::BorderOptions.from(options) end @border end
Parses supplied column widths, if not present calculates natural widths.
@return [Array]
@api public
# File lib/tty/table/renderer/basic.rb, line 133 def column_widths @column_widths = Columns.widths_from(table, @column_widths) end
Initialize operations
@return [Array[String, Operation]]
@api private
# File lib/tty/table/renderer/basic.rb, line 224 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
Change the value of indentation
@param [Integer]
the indentation value
@api public
# File lib/tty/table/renderer/basic.rb, line 163 def indent=(value) @indent = value end
Sets the output padding,
@param [Integer] value
the amount of padding, not allowed to be zero
@api public
# File lib/tty/table/renderer/basic.rb, line 173 def padding=(value) @padding = Strings::Padder.parse(value) end
Renders table as string with border
@example
renderer = TTY::Table::Renderer::Basic.new(table) renderer.render
@return [String]
the string representation of table
@api public
# File lib/tty/table/renderer/basic.rb, line 187 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 applicable operations for this table
@api private
# File lib/tty/table/renderer/basic.rb, line 210 def select_operations ops = [] ops << :escape unless multiline ops << :alignment ops << (multiline ? :wrapping : :truncation) ops << :padding ops << :filter end
Protected Instance Methods
Render table data
@api private
# File lib/tty/table/renderer/basic.rb, line 246 def render_data first_row = table.first data_border = border_class.new(column_widths, border) header = render_header(first_row, data_border) rows_with_border = render_rows(data_border) bottom_line = data_border.bottom_line Indentation.indent(bottom_line, @indent) if bottom_line [header, rows_with_border, bottom_line].compact end
Format the header if present
@param [TTY::Table::Row, TTY::Table::Header] row
the first row in the table
@param [TTY::Table::Border] data_boder
the border for this table
@return [String]
@api private
# File lib/tty/table/renderer/basic.rb, line 269 def render_header(row, data_border) top_line = data_border.top_line if row.is_a?(TTY::Table::Header) header = [top_line, data_border.row_line(row), data_border.separator] Indentation.indent(header.compact, @indent) else top_line end end
Format a single row with border
@param [Array] row
a row to decorate
@param [TTY::Table::Border] data_boder
the border for this table
@param [Boolean] is_last_row
@api private
# File lib/tty/table/renderer/basic.rb, line 306 def render_row(row, data_border, is_last_row) separator = data_border.separator row_line = data_border.row_line(row) if (border.separator == TTY::Table::Border::EACH_ROW) && is_last_row line = [row_line, separator] else line = row_line end Indentation.indent(line, @indent) end
Format the rows
@param [TTY::Table::Border] data_boder
the border for this table
@return [Arrays]
@api private
# File lib/tty/table/renderer/basic.rb, line 287 def render_rows(data_border) rows = table.rows size = rows.size rows.each_with_index.map do |row, index| render_row(row, data_border, size != (index += 1)) end end