Class: TermiChunk::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/termichunk/report.rb

Constant Summary collapse

X =
'─'
Y =
'│'
TL =
'┌'
BL =
'└'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title:, padding: nil) {|self| ... } ⇒ Report

Create a new report.

Parameters:

  • title (String)

    the title to frame the report with

  • padding (Integer) (defaults to: nil)

    the amount of padding to put around the text within the report

Yields:

  • (self)

    if a block is provided, the new report will be yielded to it



16
17
18
19
20
21
# File 'lib/termichunk/report.rb', line 16

def initialize(title:, padding: nil)
  @title = title
  @padding = padding || 0
  @rows = []
  yield self if block_given?
end

Instance Attribute Details

#paddingObject (readonly)

Returns the value of attribute padding.



9
10
11
# File 'lib/termichunk/report.rb', line 9

def padding
  @padding
end

#rowsObject (readonly)

Returns the value of attribute rows.



9
10
11
# File 'lib/termichunk/report.rb', line 9

def rows
  @rows
end

#titleObject (readonly)

Returns the value of attribute title.



9
10
11
# File 'lib/termichunk/report.rb', line 9

def title
  @title
end

Instance Method Details

#<<(item) ⇒ Object

Add a new entry to the report

Parameters:

  • item (String, Report)

    the entry to add (a single-line string, a multi-line string, a sub-report)



25
26
27
28
29
30
31
32
# 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_sString

Retrieve a string-representation of the report.

Returns:

  • (String)


36
37
38
39
# 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