class Cl::Help::Table

Attributes

data[R]
padding[R]

Public Class Methods

new(data) click to toggle source
# File lib/cl/help/table.rb, line 8
def initialize(data)
  @data = data
end

Public Instance Methods

any?() click to toggle source
# File lib/cl/help/table.rb, line 12
def any?
  data.any?
end
cells(row) click to toggle source
# File lib/cl/help/table.rb, line 26
def cells(row)
  row.map.with_index do |cell, ix|
    indent(wrap(cell.to_s), widths[ix - 1]).ljust(widths[ix])
  end
end
cols() click to toggle source
# File lib/cl/help/table.rb, line 53
def cols
  @cols ||= data.transpose
end
format(padding = 8) click to toggle source
# File lib/cl/help/table.rb, line 16
def format(padding = 8)
  @padding = padding
  rows.join("\n")
end
Also aliased as: to_s
indent(str, width) click to toggle source
# File lib/cl/help/table.rb, line 32
def indent(str, width)
  return str if str.empty? || !width
  [str.lines[0], *str.lines[1..-1].map { |str| ' ' * (width + 1) + str }].join.rstrip
end
pad(width, ix) click to toggle source
# File lib/cl/help/table.rb, line 49
def pad(width, ix)
  ix < cols.size - 2 ? width : width + padding.to_i
end
rows() click to toggle source
# File lib/cl/help/table.rb, line 22
def rows
  data.map { |row| cells(row).join(' ').rstrip }
end
to_s(padding = 8)
Alias for: format
width() click to toggle source
# File lib/cl/help/table.rb, line 37
def width
  widths = cols[0..-2].map { |col| col.max_by(&:size).size }.inject(&:+).to_i
  widths + cols.size - 1
end
widths() click to toggle source
# File lib/cl/help/table.rb, line 42
def widths
  cols.map.with_index do |col, ix|
    max = col.compact.max_by(&:size)
    pad(max ? max.size : 0, ix)
  end
end