Class: TTY::Table::Border

Inherits:
Object
  • Object
show all
Includes:
Equatable
Defined in:
lib/tty/table/border.rb,
lib/tty/table/border/null.rb,
lib/tty/table/border/ascii.rb,
lib/tty/table/border/unicode.rb,
lib/tty/table/border/row_line.rb

Overview

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

Direct Known Subclasses

ASCII, Null, Unicode

Defined Under Namespace

Classes: ASCII, Null, RowLine, Unicode

Constant Summary collapse

EMPTY_CHAR =
''.freeze
SPACE_CHAR =
' '.freeze
EACH_ROW =

Represent a separtor on each row

:each_row
SEPARATOR =

specify a separator as a row

:separator

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column_widths, options = nil) ⇒ Object

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.

Instantiate a new object

Parameters:

  • column_widths (Array)

    the table column widths

  • options (BorderOptions) (defaults to: nil)

45
46
47
48
49
50
51
52
53
# File 'lib/tty/table/border.rb', line 45

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

Class Attribute Details

.charactersObject

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.

Store characters for border


32
33
34
# File 'lib/tty/table/border.rb', line 32

def characters
  @characters
end

Class Method Details

.def_border(characters = (not_set=true), &block) ⇒ Hash

Define border characters

Parameters:

  • characters (Hash) (defaults to: (not_set=true))

    the border characters

Returns:

  • (Hash)

63
64
65
66
67
68
# File 'lib/tty/table/border.rb', line 63

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

Instance Method Details

#[](type) ⇒ String

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.

Retrive individual character by type

Parameters:

  • type (String)

    the character type

Returns:

  • (String)

78
79
80
81
82
83
84
85
86
# File 'lib/tty/table/border.rb', line 78

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_lineString

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.

A line spannig all columns marking bottom of a table.

Returns:

  • (String)

111
112
113
# File 'lib/tty/table/border.rb', line 111

def bottom_line
  (result = render(:bottom)).empty? ? nil : result
end

#color?Boolean

Check if border color is set

Returns:

  • (Boolean)

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

def color?
  border_options && border_options.style
end

#row_line(row) ⇒ String

A line spanning all columns delemeting fields in a row.

Parameters:

Returns:

  • (String)

132
133
134
135
136
137
138
# File 'lib/tty/table/border.rb', line 132

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

#separatorString

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.

A line spanning all columns delemeting rows in a table.

Returns:

  • (String)

120
121
122
# File 'lib/tty/table/border.rb', line 120

def separator
  (result = render(:mid)).empty? ? nil : result
end

#set_color(color, *strings) ⇒ Array[String]

Set color on characters

Parameters:

  • color (Symbol)
  • strings (Array[String])

    array of strings

Returns:

  • (Array[String])

149
150
151
152
153
154
155
156
157
# File 'lib/tty/table/border.rb', line 149

def set_color(color, *strings)
  strings.map do |string|
    if string.gsub(/\s+/, '').empty?
      string
    else
      @color.decorate(string, color)
    end
  end
end

#top_lineString

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.

A line spanning all columns marking top of a table.

Returns:

  • (String)

102
103
104
# File 'lib/tty/table/border.rb', line 102

def top_line
  (result = render(:top)).empty? ? nil : result
end