Module: Gerrit::Cli::Util

Defined in:
lib/gerrit/cli/util.rb

Class Method Summary collapse

Class Method Details

.render_table(rows, opts = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gerrit/cli/util.rb', line 8

def render_table(rows, opts={})
  return "" if rows.empty?

  max_col_lengths = []
  rows.first.length.times { max_col_lengths << 0 }

  # Compute maximum length of each column
  rows.each do |row|
    if row.length != max_col_lengths.length
      raise ArgumentError, "Column mismatch"
    end

    row.each_with_index do |c, ii|
      len = c.to_s.length
      if len> max_col_lengths[ii]
        max_col_lengths[ii] = len
      end
    end
  end

  delim = opts[:delimiter] || ' '
  row_format = max_col_lengths.map {|len| "%-#{len}s" }.join(delim)

  rows.map {|row| row_format % row }.join("\n")
end