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 = BorderDSL.new(options.delete(:border)).options unless @table.separators.empty? @border.separator ||= @table.separators end @column_widths = options.fetch(:column_widths, nil) alignment = Array(options[:alignment]) * table.columns_size @alignments = 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 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).options end
Parses supplied column widths, if not present calculates natural widths.
@return [Array]
@api public
# File lib/tty/table/renderer/basic.rb, line 136 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 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
Change the value of indentation
@param [Integer]
the indentation value
@api public
# File lib/tty/table/renderer/basic.rb, line 162 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 172 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 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 applicable operations for this table
@api private
# 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
Protected Instance Methods
Render table data
@api private
# File lib/tty/table/renderer/basic.rb, line 245 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 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_border
the border for this table
@return [String]
@api private
# File lib/tty/table/renderer/basic.rb, line 268 def render_header(row, data_border) top_line = data_border.top_line return top_line unless row.is_a?(TTY::Table::Header) header = [top_line, data_border.row_line(row)] if !border.separator || border.separator?(0) header << data_border.middle_line end Indentation.indent(header.compact, @indent) end
Format a single row with border
@param [Array] row
a row to decorate
@param [TTY::Table::Border] data_border
the border for this table
@param [Boolean] is_not_last_row
@api private
# File lib/tty/table/renderer/basic.rb, line 306 def render_row(row, index, data_border, is_not_last_row) row_line = data_border.row_line(row) line = [row_line] if border.separator?(index) && is_not_last_row line << data_border.middle_line end Indentation.indent(line, @indent) end
Format the rows
@param [TTY::Table::Border] data_border
the border for this table
@return [Arrays]
@api private
# File lib/tty/table/renderer/basic.rb, line 286 def render_rows(data_border) rows = table.rows size = rows.size offset = table.first.is_a?(TTY::Table::Header) ? 1 : 0 rows.each_with_index.map do |row, index| render_row(row, index+offset, data_border, size != (index + 1)) end end