class TTY::Table::Border

Abstract base class that is responsible for building the table border.

Constants

EACH_ROW

Represent a separtor on each row

EMPTY_CHAR
SPACE_CHAR

Attributes

characters[RW]

Store characters for border

@api private

border_options[R]

The table custom border options including styling

@api private

widths[R]

The row field widths

@api private

Public Class Methods

def_border(characters=(not_set=true), &block) click to toggle source

Define border characters

@param [Hash] characters

the border characters

@return [Hash]

@api public

# File lib/tty/table/border.rb, line 59
def self.def_border(characters=(not_set=true), &block)
  return self.characters = characters unless not_set

  dsl = TTY::Table::BorderDSL.new(&block)
  self.characters = dsl.characters
end
new(column_widths, options = nil) click to toggle source

Instantiate a new object

@param [Array] column_widths

the table column widths

@param [BorderOptions] options

@return [Object]

@api private

# File lib/tty/table/border.rb, line 41
def initialize(column_widths, options = nil)
  if self.class == Border
    fail NotImplementedError, "#{self} is an abstract class"
  else
    @widths = column_widths
    @border_options = TTY::Table::BorderOptions.from options
    @color  = Pastel.new
  end
end

Public Instance Methods

[](type) click to toggle source

Retrive individual character by type

@param [String] type

the character type

@return [String]

@api private

# File lib/tty/table/border.rb, line 74
def [](type)
  characters = self.class.characters
  chars = if border_options.nil?
            characters
          else
            characters.merge(border_options.characters)
          end
  chars[type] || EMPTY_CHAR
end
bottom_line() click to toggle source

A line spannig all columns marking bottom of a table.

@return [String]

@api private

# File lib/tty/table/border.rb, line 107
def bottom_line
  (result = render(:bottom)).empty? ? nil : result
end
color?() click to toggle source

Check if border color is set

@return [Boolean]

@api public

# File lib/tty/table/border.rb, line 89
def color?
  border_options && border_options.style
end
row_line(row) click to toggle source

A line spanning all columns delemeting fields in a row.

@param [TTY::Table::Row] row

the table row

@return [String]

@api public

# File lib/tty/table/border.rb, line 128
def row_line(row)
  line = RowLine.new(self['left'], self['center'], self['right'])
  line.colorize(self, border_options.style) if color?

  result = row_heights(row, line)
  result.empty? ? EMPTY_CHAR : result
end
separator() click to toggle source

A line spanning all columns delemeting rows in a table.

@return [String]

@api private

# File lib/tty/table/border.rb, line 116
def separator
  (result = render(:mid)).empty? ? nil : result
end
set_color(color, *strings) click to toggle source

Set color on characters

@param [Symbol] color

@param [Array] array of strings

@return [Array]

@api public

# File lib/tty/table/border.rb, line 145
def set_color(color, *strings)
  strings.map do |string|
    if string.gsub(/\s+/, '').empty?
      string
    else
      @color.decorate(string, color)
    end
  end
end
top_line() click to toggle source

A line spanning all columns marking top of a table.

@return [String]

@api private

# File lib/tty/table/border.rb, line 98
def top_line
  (result = render(:top)).empty? ? nil : result
end

Protected Instance Methods

render(type) click to toggle source

Generate particular border type

@param [String] type

border type one of [:top, :bottom, :mid]

@api private

# File lib/tty/table/border.rb, line 212
def render(type)
  type = type.to_s
  border_char = self[type]
  line = render_line(border_char,
                     self["#{type}_left"]  || border_char,
                     self["#{type}_right"] || border_char,
                     self["#{type}_mid"])

  return line unless color?
  set_color(border_options.style, line).join
end
render_line(line, left, right, intersection) click to toggle source

Generate a border string

@param [String] line

the line character

@return [String]

@api private

# File lib/tty/table/border.rb, line 232
def render_line(line, left, right, intersection)
  left + widths.map { |width| line * width }.join(intersection) + right
end
row_height_line(row, line_index, line) click to toggle source

Generate border for a given multiline row

@param [TTY::Table::Row] row

the table row

@param [Integer] line

the index for current line inside multiline

@param [TTY::Table::Border::RowLine] line

@return [String]

@api private

# File lib/tty/table/border.rb, line 198
def row_height_line(row, line_index, line)
  line.left + row.fields.each_with_index.map do |field, index|
    direction     = field.alignment || :left
    field_content = field.lines[line_index] || SPACE_CHAR * field.length
    Strings.align(field_content, widths[index], direction: direction)
  end.join(line.center) + line.right
end
row_heights(row, line) click to toggle source

Separate multiline string into individual rows with border.

@param [TTY::Table::Row] row

the table row

@param [TTY::Table::Border::RowLine] line

@api private

# File lib/tty/table/border.rb, line 175
def row_heights(row, line)
  if row.size > 0
    row.height.times.map do |line_index|
      row_height_line(row, line_index, line)
    end.join("\n")
  else
    line.left + line.right
  end
end