class Tabulo::Border

@!visibility private

Constants

STYLES
Style

Public Class Methods

from(initializer, styler = nil) click to toggle source

@!visibility private

# File lib/tabulo/border.rb, line 65
def self.from(initializer, styler = nil)
  new(**options(initializer).merge(styler: styler))
end
new( corner_top_left: "", corner_top_right: "", corner_bottom_right: "", corner_bottom_left: "", edge_top: "", edge_right: "", edge_bottom: "", edge_left: "", tee_top: "", tee_right: "", tee_bottom: "", tee_left: "", divider_vertical: "", divider_horizontal: "", intersection: "", styler: nil) click to toggle source

@param [nil, to_proc] styler (nil) A lambda or other callable object taking

a single parameter, representing a section of the table's borders (which for this purpose
include any horizontal and vertical lines inside the table), and returning a string.
If passed <tt>nil</tt>, then no additional styling will be applied to borders. If passed a
callable, then that callable will be called for each border section, with the
resulting string rendered in place of that border. The extra width of the string returned by the
{styler} is not taken into consideration by the internal table rendering calculations
Thus it can be used to apply ANSI escape codes to border characters, to colour the borders
for example, without breaking the table formatting.

@return [Border] a new {Border}

# File lib/tabulo/border.rb, line 119
def initialize(
  corner_top_left: "",
  corner_top_right: "",
  corner_bottom_right: "",
  corner_bottom_left: "",
  edge_top: "",
  edge_right: "",
  edge_bottom: "",
  edge_left: "",
  tee_top: "",
  tee_right: "",
  tee_bottom: "",
  tee_left: "",
  divider_vertical: "",
  divider_horizontal: "",
  intersection: "",
  styler: nil)

  @corner_top_left = corner_top_left
  @corner_top_right = corner_top_right
  @corner_bottom_right = corner_bottom_right
  @corner_bottom_left = corner_bottom_left

  @edge_top = edge_top
  @edge_right = edge_right
  @edge_bottom = edge_bottom
  @edge_left = edge_left

  @tee_top = tee_top
  @tee_right = tee_right
  @tee_bottom = tee_bottom
  @tee_left = tee_left

  @divider_vertical = divider_vertical
  @divider_horizontal = divider_horizontal

  @intersection = intersection

  @styler = styler
end

Private Class Methods

options(kind) click to toggle source
# File lib/tabulo/border.rb, line 103
def self.options(kind)
  opts = STYLES[kind]
  return opts.to_h if opts
  raise InvalidBorderError
end

Public Instance Methods

horizontal_rule(column_widths, position = :bottom) click to toggle source

@!visibility private

# File lib/tabulo/border.rb, line 70
def horizontal_rule(column_widths, position = :bottom)
  left, center, right, segment =
    case position
    when :title_top
      [@corner_top_left, @edge_top, @corner_top_right, @edge_top]
    when :title_bottom
      [@tee_left, @tee_top, @tee_right, @edge_top]
    when :top
      [@corner_top_left, @tee_top, @corner_top_right, @edge_top]
    when :middle
      [@tee_left, @intersection, @tee_right, @divider_horizontal]
    when :bottom
      [@corner_bottom_left, @tee_bottom, @corner_bottom_right, @edge_bottom]
    end
  segments = column_widths.map { |width| segment * width }

  # Prevent weird bottom edge of title if segments empty but right/left not empty, as in
  # Markdown border.
  left = right = "" if segments.all?(&:empty?)

  style("#{left}#{segments.join(center)}#{right}")
end
join_cell_contents(cells) click to toggle source

@!visibility private

# File lib/tabulo/border.rb, line 94
def join_cell_contents(cells)
  styled_divider_vertical = style(@divider_vertical)
  styled_edge_left = style(@edge_left)
  styled_edge_right = style(@edge_right)
  styled_edge_left + cells.join(styled_divider_vertical) + styled_edge_right
end

Private Instance Methods

style(s) click to toggle source
# File lib/tabulo/border.rb, line 160
def style(s)
  (@styler && !s.empty?) ? @styler.call(s) : s
end