Module: Looksee::Columnizer

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



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/looksee.rb', line 316

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