Class: TTY::Table::Border

Inherits:
Object
  • Object
show all
Includes:
Equatable, Unicode
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

Overview

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

Direct Known Subclasses

ASCII, Null, Unicode

Defined Under Namespace

Classes: ASCII, Null, Unicode

Constant Summary collapse

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

Represent a separtor on each row

:each_row

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Equatable

#comparison_attrs

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Equatable

#attr_reader, included, #inherited

Methods included from Unicode

#as_unicode, #clean_utf8, #utf8?

Constructor Details

#initialize(row, 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:



49
50
51
52
53
54
55
56
57
# File 'lib/tty/table/border.rb', line 49

def initialize(row, options=nil)
  if self.class == Border
    raise NotImplementedError, "#{self} is an abstract class"
  else
    @row = row
    @widths = row.map { |cell| cell.chars.to_a.size }
    @border = TTY::Table::BorderOptions.from options
  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



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

def characters
  @characters
end

Instance Attribute Details

#borderObject (readonly)

The table custom border characters



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

def border
  @border
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)


67
68
69
70
71
72
# File 'lib/tty/table/border.rb', line 67

def self.def_border(characters=(not_set=true), &block)
  return self.characters = characters if !not_set

  dsl = TTY::Table::BorderDSL.new(&block)
  self.characters = dsl.characters
end

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

Set color on characters

Parameters:

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

    of strings

Returns:

  • (Array[String])


106
107
108
# File 'lib/tty/table/border.rb', line 106

def self.set_color(color, *strings)
  strings.map { |string| TTY.terminal.color.set(string, color) }
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 individula character by type

Parameters:

  • type (String)

    the character type

Returns:

  • (String)


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

def [](type)
  characters = self.class.characters
  chars = border.nil? ? characters : characters.merge(border.characters)
  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)


151
152
153
# File 'lib/tty/table/border.rb', line 151

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 && border.style
end

#row_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 delemeting cells in a row.

Returns:

  • (String)


133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/tty/table/border.rb', line 133

def row_line
  right_char  = self['right']
  left_char   = self['left']
  center_char = self['center']

  if color?
    right_char, center_char, left_char = Border.set_color(border.style, right_char, center_char, left_char)
  end

  result = left_char + row.join(center_char) + right_char
  result.empty? ? nil : 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)


124
125
126
# File 'lib/tty/table/border.rb', line 124

def separator
  (result = render(:mid)).empty? ? nil : result
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)


115
116
117
# File 'lib/tty/table/border.rb', line 115

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