Module: Litestream::Commands::Output

Defined in:
lib/litestream/commands.rb

Class Method Summary collapse

Class Method Details

.format(data) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/litestream/commands.rb', line 25

def format(data)
  return "" if data.nil? || data.empty?

  headers = data.first.keys.map(&:to_s)
  widths = headers.map.with_index { |h, i|
    [h.length, data.map { |r| r[data.first.keys[i]].to_s.length }.max].max
  }

  format_str = widths.map { |w| "%-#{w}s" }.join("  ")
  ([headers] + data.map(&:values)).map { |row|
    sprintf(format_str, *row.map(&:to_s))
  }.join("\n")
end