Module: Reflexive::Columnizer

Defined in:
lib/reflexive/columnizer.rb

Class Method Summary collapse

Class Method Details

.columnize(strings, width) ⇒ Object

Arrange the given strings in columns, restricted to the given width. Smart enough to ignore content in terminal control sequences.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/reflexive/columnizer.rb', line 11

def columnize(strings, width)
  return '' if strings.empty?
  num_columns = 1
  layout = [strings]
  loop do
    break if layout.first.length <= 1
    next_layout = layout_in_columns(strings, num_columns + 1)
    break if layout_width(next_layout) > width
    layout = next_layout
    num_columns += 1
  end

  pad_strings(layout)
  rectangularize_layout(layout)
  layout.transpose.map do |row|
    '  ' + row.compact.join('  ')
  end.join("\n") << "\n"
end