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

Inherits:
Object
  • Object
show all
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.

Direct Known Subclasses

ASCII, Unicode

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validatable

#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

Parameters:

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

Options Hash (options):

  • :alignments (String)

    used to format table individual column alignment

  • :border (Hash[Symbol])

    the border options

  • :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


113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tty/table/renderer/basic.rb', line 113

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

Instance Attribute Details

#alignmentsArray

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)

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

def alignments
  @alignments
end

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


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

def border_class
  @border_class
end

#column_widthsArray[Integer]

Parses supplied column widths, if not present calculates natural widths.

Returns:

  • (Array[Integer])

134
135
136
# File 'lib/tty/table/renderer/basic.rb', line 134

def column_widths
  @column_widths = Columns.widths_from(table, @column_widths)
end

#filterObject

A callable object used for formatting field content


56
57
58
# File 'lib/tty/table/renderer/basic.rb', line 56

def filter
  @filter
end

#indentInteger

The table indentation value

Returns:

  • (Integer)

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

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)

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

def multiline
  @multiline
end

#paddingTTY::Table::Padder

The table padding settings

Returns:

  • (TTY::Table::Padder)

94
95
96
# File 'lib/tty/table/renderer/basic.rb', line 94

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)

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

def resize
  @resize
end

#widthInteger

The table total width

Returns:

  • (Integer)

78
79
80
# File 'lib/tty/table/renderer/basic.rb', line 78

def width
  @width
end

Instance Method Details

#border(options = (not_set=true)) {|Table::BorderOptions| ... } ⇒ Object Also known as: border=

Store border characters, style and separator for the table rendering

Parameters:

Yields:


146
147
148
149
150
151
152
153
154
155
156
# File 'lib/tty/table/renderer/basic.rb', line 146

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.separator ||= @table.separators unless @table.separators.empty?
  @border
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

Returns:


226
227
228
229
230
231
232
233
234
# File 'lib/tty/table/renderer/basic.rb', line 226

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

#renderString

Renders table as string with border

Examples:

renderer = TTY::Table::Renderer::Basic.new(table)
renderer.render

Returns:

  • (String)

    the string representation of table


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/tty/table/renderer/basic.rb', line 189

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_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.

Select applicable operations for this table


212
213
214
215
216
217
218
219
# File 'lib/tty/table/renderer/basic.rb', line 212

def select_operations
  ops = []
  ops << :escape unless multiline
  ops << :alignment
  ops << (multiline ? :wrapping : :truncation)
  ops << :padding
  ops << :filter
end