Class: HammerCLI::Output::Adapter::Table

Inherits:
Abstract
  • Object
show all
Defined in:
lib/hammer_cli/output/adapter/table.rb

Constant Summary collapse

MAX_COLUMN_WIDTH =
80
MIN_COLUMN_WIDTH =
5
HLINE =
'-'
LINE_SEPARATOR =
'-|-'
COLUMN_SEPARATOR =
' | '

Instance Method Summary collapse

Methods inherited from Abstract

#initialize, #paginate_by_default?, #print_error, #print_message

Constructor Details

This class inherits a constructor from HammerCLI::Output::Adapter::Abstract

Instance Method Details



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hammer_cli/output/adapter/table.rb', line 23

def print_collection(all_fields, collection)
  fields = field_filter.filter(all_fields)

  formatted_collection = format_values(fields, collection)
  # calculate hash of column widths (label -> width)
  widths = calculate_widths(fields, formatted_collection)

  header_bits = []
  hline_bits = []
  fields.map do |f|
    header_bits << normalize_column(widths[f.label], f.label.upcase)
    hline_bits << HLINE * widths[f.label]
  end

  line = hline_bits.join(LINE_SEPARATOR)

  puts line
  puts header_bits.join(COLUMN_SEPARATOR)
  puts line

  formatted_collection.collect do |row|
    row_bits = fields.map do |f|
      normalize_column(widths[f.label], row[f.label] || "")
    end
    puts row_bits.join(COLUMN_SEPARATOR)
  end

  # print closing line only when the table isn't empty
  puts line unless formatted_collection.empty?

  if collection.meta.pagination_set? && collection.count < collection.meta.subtotal
    pages = (collection.meta.subtotal.to_f/collection.meta.per_page).ceil
    puts _("Page %{page} of %{total} (use --page and --per-page for navigation)") % {:page => collection.meta.page, :total => pages}
  end
end


19
20
21
# File 'lib/hammer_cli/output/adapter/table.rb', line 19

def print_record(fields, record)
  print_collection(fields, [record].flatten(1))
end

#tagsObject



15
16
17
# File 'lib/hammer_cli/output/adapter/table.rb', line 15

def tags
  [:screen, :flat]
end