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_size, #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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 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

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)


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

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:



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

def border_class
  @border_class
end

#column_widthsArray[Integer]

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

Returns:

  • (Array[Integer])


136
137
138
# File 'lib/tty/table/renderer/basic.rb', line 136

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

#filterObject

A callable object used for formatting field content



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

def filter
  @filter
end

#indentInteger

The table indentation value

Returns:

  • (Integer)


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

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)


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

def multiline
  @multiline
end

#paddingTTY::Table::Padder

The table padding settings

Returns:

  • (TTY::Table::Padder)


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

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)


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

def resize
  @resize
end

#widthInteger

The table total width

Returns:

  • (Integer)


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

def width
  @width
end

Instance Method Details

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

Store border characters, style and separator for the table rendering

Parameters:

Yields:



148
149
150
151
152
153
# 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

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



223
224
225
226
227
228
229
230
231
# 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

#renderString

Renders table as string with border

Examples:

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

Returns:

  • (String)

    the string representation of table



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# 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_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



209
210
211
212
213
214
215
216
# 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