Module: Looksee::Columnizer

Defined in:
lib/looksee/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.



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

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