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
- SEPARATOR
specify a separator as a row
- SPACE_CHAR
Attributes
Store characters for border
@api private
The row field widths
@api private
Public Class Methods
Define border characters
@param [Hash] characters
the border characters
@return [Hash]
@api public
# File lib/tty/table/border.rb, line 39 def self.def_border(characters = (not_set = true), &block) return self.characters = characters unless not_set dsl = BorderDSL.new(&block) self.characters = dsl.characters end
Instantiate a new object
@param [Array] column_widths
the table column widths
@param [BorderOptions] border_opts
@return [Object]
@api private
# File lib/tty/table/border.rb, line 56 def initialize(column_widths, border_opts = nil) if self.class == Border raise NotImplementedError, "#{self} is an abstract class" end @widths = column_widths @dsl = BorderDSL.new(border_opts) @characters = self.class.characters.merge(@dsl.characters) @color = Pastel.new end
Public Instance Methods
Retrive individual character by type
@param [String] type
the character type
@return [String]
@api private
# File lib/tty/table/border.rb, line 75 def [](type) @characters[type] || EMPTY_CHAR end
A line spannig all columns marking bottom of a table.
@return [String]
@api private
# File lib/tty/table/border.rb, line 102 def bottom_line (result = render(:bottom)).empty? ? nil : result end
Check if border color is set
@return [Boolean]
@api public
# File lib/tty/table/border.rb, line 84 def color? !!@dsl.style end
A line spanning all columns delemeting rows in a table.
@return [String]
@api private
# File lib/tty/table/border.rb, line 111 def middle_line (result = render(:mid)).empty? ? nil : result end
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 123 def row_line(row) line = RowLine.new(self["left"], self["center"], self["right"]) line.colorize(self, @dsl.style) if color? result = row_heights(row, line) result.empty? ? EMPTY_CHAR : result end
Set color for a string
@param [Symbol] color
@param [String] string
the string to color
@return [String]
@api public
# File lib/tty/table/border.rb, line 141 def set_color(color, string) return string if string.gsub(/\s+/, EMPTY_CHAR).empty? @color.decorate(string, color) end
A line spanning all columns marking top of a table.
@return [String]
@api private
# File lib/tty/table/border.rb, line 93 def top_line (result = render(:top)).empty? ? nil : result end
Protected Instance Methods
The table custom border options including styling
@api private
# File lib/tty/table/border.rb, line 157 def border_options @dsl.options end
Generate particular border type
@param [String] type
border type one of :top, :bottom and :mid
@return [String]
@api private
# File lib/tty/table/border.rb, line 208 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(@dsl.style, line) end
Generate a border string
@param [String] line
the line character
@return [String]
@api private
# File lib/tty/table/border.rb, line 229 def render_line(line, left, right, intersection) left + widths.map { |width| line * width }.join(intersection) + right end
Generate border for a given multiline row
@param [TTY::Table::Row] row
the table row
@param [Integer] line_index
the index for current line inside multiline
@param [TTY::Table::Border::RowLine] line
@return [String]
@api private
# File lib/tty/table/border.rb, line 192 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
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 169 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