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.



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/looksee.rb', line 320

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