class TermiChunk::Report

Constants

BL
TL
X
Y

Attributes

padding[R]
rows[R]
title[R]

Public Class Methods

new(title:, padding: nil) { |self| ... } click to toggle source

Create a new report. @param title [String] the title to frame the report with @param padding [Integer] the amount of padding to put around the text within the report @yield [self] if a block is provided, the new report will be yielded to it @return [Report]

# File lib/termichunk/report.rb, line 16
def initialize(title:, padding: nil)
  @title = title
  @padding = padding || 0
  @rows = []
  yield self if block_given?
end

Public Instance Methods

<<(item) click to toggle source

Add a new entry to the report @param item [String, Report] the entry to add (a single-line string, a multi-line string, a sub-report)

# File lib/termichunk/report.rb, line 25
def <<(item)
  lines = item.to_s.lines
  if item.is_a?(self.class) && padding.nonzero?
    buffer = padding.times.map { "\n" }
    lines = [*buffer, *lines, *buffer]
  end
  lines.each { |l| rows << l }
end
to_s() click to toggle source

Retrieve a string-representation of the report. @return [String]

# File lib/termichunk/report.rb, line 36
def to_s
  y = padding.times.map { Y }
  [titlebar(TL), *y, body, *(y if body), titlebar(BL)].compact.join("\n")
end

Private Instance Methods

body() click to toggle source
# File lib/termichunk/report.rb, line 43
def body
  rows.map { |l| "#{Y} #{' ' * padding}#{l.chomp}" }.join("\n") unless rows.length.zero?
end
titlebar(corner) click to toggle source
# File lib/termichunk/report.rb, line 47
def titlebar(corner)
  heading = "#{corner}#{X}[ #{title} ]"
  heading += (X * (width + 2 + (padding * 2) - heading.length))
  heading
end
width() click to toggle source
# File lib/termichunk/report.rb, line 53
def width
  [(rows.map(&:length).max || 0) + 2, title.length + 6].max
end