Method: Qcmd::Plaintext#table

Defined in:
lib/qcmd/plaintext.rb

#table(headers, rows) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/qcmd/plaintext.rb', line 137

def table headers, rows
  print
  columns = headers.map(&:size)

  # coerce row values to strings
  rows.each do |row|
    columns.each_with_index do |col, n|
      row[n] = row[n].to_s
    end
  end

  rows.each do |row|
    columns.each_with_index do |col, n|
      columns[n] = [col, row[n].size].max + 1
    end
  end

  row_format = columns.map {|n| "%#{n}s\t"}.join('')
  print row_format % headers
  print
  rows.each do |row|
    print row_format % row
  end
  print
end