Module: SchwarmCli::Formatters::Table

Defined in:
lib/schwarm_cli/formatters/table.rb

Class Method Summary collapse

Class Method Details

.format_list(records, columns:, meta: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/schwarm_cli/formatters/table.rb', line 6

def self.format_list(records, columns:, meta: nil)
  if records.empty?
    puts "No results found."
    return
  end

  widths = column_widths(records, columns)
  puts format_row(columns.map(&:first), columns, widths)
  records.each do |record|
    puts format_row(columns.map { |_, key| record[key].to_s }, columns, widths)
  end
  footer = pagination_footer(records, meta)
  puts "\n#{footer}" if footer
end

.format_record(record, fields:) ⇒ Object



31
32
33
34
35
36
# File 'lib/schwarm_cli/formatters/table.rb', line 31

def self.format_record(record, fields:)
  label_width = fields.keys.map(&:length).max
  fields.each do |label, key|
    puts "#{label.rjust(label_width)}: #{record[key]}"
  end
end