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

Inherits:
Object
  • Object
show all
Includes:
Validatable
Defined in:
lib/tty/table/renderer/basic.rb

Overview

Renders table without any border styles.

Direct Known Subclasses

ASCII, Color, Unicode

Constant Summary

Constants included from Validatable

Validatable::MIN_CELL_WIDTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validatable

#assert_matching_widths, #assert_row_sizes, #assert_string_values, #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

Parameters:

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

Options Hash (options):

  • :column_aligns (String)

    used to format table individual column alignment

  • :column_widths (String)

    used to format table individula column width

  • :indent (Integer)

    indent the first column by indent value

  • :padding (Integer, Array)

    add padding to table fields



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/tty/table/renderer/basic.rb', line 105

def initialize(table, options = {})
  @table         = table || (raise ArgumentRequired, "Expected TTY::Table instance, got #{table.inspect}")
  @multiline     = options.fetch(:multiline) { false }
  @operations    = TTY::Table::Operations.new(table)
  if not multiline
    @operations.add(:escape, Operation::Escape.new)
  end
  @border        = TTY::Table::BorderOptions.from(options.delete(:border))
  @column_widths = options.fetch(:column_widths, nil)
  @column_aligns = Array(options.delete(:column_aligns)).map(&:to_sym)
  @filter        = options.fetch(:filter) { proc { |val, row, col| val } }
  @width         = options.fetch(:width) { TTY.terminal.width }
  @border_class  = options.fetch(:border_class) { Border::Null }
  @indent        = options.fetch(:indent) { 0 }
  @resize        = options.fetch(:resize) { false }
  @padding       = TTY::Table::Padder.parse(options.fetch(:padding) { nil })
end

Instance Attribute Details

#border_classTTY::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

Returns:



26
27
28
# File 'lib/tty/table/renderer/basic.rb', line 26

def border_class
  @border_class
end

#column_alignsArray

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

Returns:

  • (Array)


40
41
42
# File 'lib/tty/table/renderer/basic.rb', line 40

def column_aligns
  @column_aligns
end

#column_widthsArray[Integer]

Parses supplied column widths, if not present calculates natural widths

Returns:

  • (Array[Integer])


33
34
35
# File 'lib/tty/table/renderer/basic.rb', line 33

def column_widths
  @column_widths
end

#filterObject

A callable object used for formatting field content



50
51
52
# File 'lib/tty/table/renderer/basic.rb', line 50

def filter
  @filter
end

#indentInteger

The table indentation value

Returns:

  • (Integer)


65
66
67
# File 'lib/tty/table/renderer/basic.rb', line 65

def indent
  @indent
end

#multilineBoolean

The table column span behaviour. When true the column’s line breaks cause the column to span multiple rows. By default set to false.

Returns:

  • (Boolean)


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

def multiline
  @multiline
end

#operationsObject (readonly)

The table operations applied to rows



45
46
47
# File 'lib/tty/table/renderer/basic.rb', line 45

def operations
  @operations
end

#paddingTTY::Table::Padder

The table padding settings

Returns:



88
89
90
# File 'lib/tty/table/renderer/basic.rb', line 88

def padding
  @padding
end

#resizeInteger

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.

Returns:

  • (Integer)


81
82
83
# File 'lib/tty/table/renderer/basic.rb', line 81

def resize
  @resize
end

#tableTTY::Table (readonly)

Table to be rendered

Returns:



18
19
20
# File 'lib/tty/table/renderer/basic.rb', line 18

def table
  @table
end

#widthInteger

The table totabl width

Returns:

  • (Integer)


72
73
74
# File 'lib/tty/table/renderer/basic.rb', line 72

def width
  @width
end

Instance Method Details

#add_operationsObject

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



153
154
155
156
157
158
159
160
# File 'lib/tty/table/renderer/basic.rb', line 153

def add_operations
  operations.add(:alignment,  Operation::AlignmentSet.new(column_aligns,
                                                          column_widths))
  operations.add(:filter,     Operation::Filter.new(filter))
  operations.add(:truncation, Operation::Truncation.new(column_widths))
  operations.add(:wrapping,   Operation::Wrapped.new(column_widths, padding))
  operations.add(:padding,    Operation::Padding.new(padding, multiline))
end

#border(options = (not_set=true)) { ... } ⇒ Object

Store border characters, style and separator for the table rendering

Parameters:

Yields:

  • block representing border options



139
140
141
142
143
144
145
146
147
148
# File 'lib/tty/table/renderer/basic.rb', line 139

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

#columns_constraintsObject

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.

Return column contraints



181
182
183
# File 'lib/tty/table/renderer/basic.rb', line 181

def columns_constraints
  TTY::Table::Columns.new(self)
end

#indentationTTY::Table::Indentation

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.

Initializes indentation



167
168
169
# File 'lib/tty/table/renderer/basic.rb', line 167

def indentation
  @indentation ||= TTY::Table::Indentation.new(self)
end

#insert_indent(line) ⇒ Object

Delegate indentation insertion



174
175
176
# File 'lib/tty/table/renderer/basic.rb', line 174

def insert_indent(line)
  indentation.insert_indent(line)
end

#renderString

Renders table

Returns:

  • (String)

    string representation of table



200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/tty/table/renderer/basic.rb', line 200

def render
  return if table.empty?

  operations.run_operations(:escape) unless multiline
  columns_constraints.enforce
  add_operations
  ops = [:alignment]
  ops << :padding unless padding.empty?
  multiline ? ops << :wrapping : ops << :truncation
  ops << :filter
  operations.run_operations(*ops)

  render_data.compact.join("\n")
end