lib/cl/help/table.rb in cl-0.1.2 vs lib/cl/help/table.rb in cl-0.1.3
- old
+ new
@@ -1,8 +1,10 @@
class Cl
class Help
class Table
+ include Wrap
+
attr_reader :data, :padding
def initialize(data)
@data = data
end
@@ -20,10 +22,17 @@
def rows
data.map { |row| cells(row).join(' ').rstrip }
end
def cells(row)
- row.map.with_index { |cell, ix| cell.to_s.ljust(widths[ix]) }
+ row.map.with_index do |cell, ix|
+ indent(wrap(cell.to_s), widths[ix - 1]).ljust(widths[ix])
+ end
+ end
+
+ 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
def width
widths = cols[0..-2].map { |col| col.max_by(&:size).size }.inject(&:+).to_i
widths + cols.size - 1