Class: Fingerer::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/fingerer/table.rb

Constant Summary collapse

@@_width =
74

Class Method Summary collapse

Class Method Details

.header(title = "") ⇒ Object



23
24
25
26
# File 'lib/fingerer/table.rb', line 23

def self.header(title = "")
  title = title[0..(@@_width - 5)]
  "+-[ #{title} ]#{"-" * (@@_width - (title.length + 3))}+"
end

.lineObject



28
29
30
# File 'lib/fingerer/table.rb', line 28

def self.line
  "+#{"-" * (@@_width + 2)}+"
end

.row(*cols) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fingerer/table.rb', line 6

def self.row(*cols)
  count = cols.count;

  return "|#{" " * (@@_width + 2)}|" if count == 0

  width = ((@@_width - (count - 1)) / count).to_i
  remainder = (@@_width - ((width * count) + (count - 1))).to_i
  widths = Array.new(count, width)

  # add remainder to last element, if needed
  widths[-1] += remainder if remainder

  widths.map!{|item| "%-#{item}.#{item}s"}

  sprintf("| #{widths.join(" ")} |", *cols)
end