Class: TTY::Table::Renderer::Basic

Inherits:
Object
  • Object
show all
Extended by:
Delegatable
Defined in:
lib/tty/table/renderer/basic.rb

Overview

Renders table without any border styles.

Direct Known Subclasses

ASCII, Color, Unicode

Constant Summary collapse

TABLE_DELEGATED_METHODS =
[:column_widths, :column_aligns]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Delegatable

delegatable_method

Constructor Details

#initialize(options = {}) ⇒ Table::Renderer::Basic

Initialize and setup a Renderer

Parameters:

  • options (Hash) (defaults to: {})

    :indent - Indent the first column by indent value :padding - Pad out the row cell by padding value



36
37
38
# File 'lib/tty/table/renderer/basic.rb', line 36

def initialize(options={})
  setup(options)
end

Instance Attribute Details

#border_classTTY::Table::Border (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.

Table border to be rendered

Returns:



23
24
25
# File 'lib/tty/table/renderer/basic.rb', line 23

def border_class
  @border_class
end

#tableTTY::Table (readonly)

Table to be rendered

Returns:



16
17
18
# File 'lib/tty/table/renderer/basic.rb', line 16

def table
  @table
end

Class Method Details

.render(table, options = {}) ⇒ Object



63
64
65
# File 'lib/tty/table/renderer/basic.rb', line 63

def self.render(table, options={})
  new(options).render(table)
end

Instance Method Details

#padding=(value) ⇒ Object

Sets the output padding,

Parameters:

  • value (Integer)

    the amount of padding, not allowed to be zero



58
59
60
# File 'lib/tty/table/renderer/basic.rb', line 58

def padding=(value)
  @padding = [0, value].max
end

#render(table, border_class = Border::Null) ⇒ String

Renders table

Parameters:

Returns:

  • (String)

    string representation of table



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tty/table/renderer/basic.rb', line 75

def render(table, border_class=Border::Null)
  @table = table
  @border_class = table.border_class || border_class
  return if table.empty?

  body = []
  unless table.length.zero?
    ColumnSet.new(table).extract_widths!
    # 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
    body += render_header
    body += render_rows
  end
  body.compact.join("\n")
end