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

Instance Method Summary collapse

Methods inherited from Abstract

#initialize, #print_error, #print_message

Constructor Details

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

Instance Method Details



19
20
21
22
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
58
59
60
# File 'lib/hammer_cli/output/adapter/table.rb', line 19

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

  rows = collection.collect do |d|
    row = {}
    fields.each do |f|
      row[label_for(f)] = WrapperFormatter.new(
        @formatters.formatter_for_type(f.class), f.parameters).format(data_for_field(f, d) || "")
    end
    row
  end

  if rows.empty?
    keys = fields.map { |f| [label_for(f), ''] }
    rows = [Hash[keys]]
    @header_only = true
  end

  options = fields.collect do |f|
    { label_for(f) => {
        :width => max_width_for(f)
      }
    }
  end

  sort_order = fields.map { |f| f.label.upcase }

  printer = TablePrint::Printer.new(rows, options)
  TablePrint::Config.max_width = MAX_COLUMN_WIDTH
  TablePrint::Config.multibyte = true

  output = sort_columns(printer.table_print, sort_order)
  dashes = /\n([-|]+)\n/.match(output)

  if @header_only
    output = output.lines.first
  end

  puts dashes[1] if dashes
  puts output
  puts dashes[1] if dashes
end


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

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

#tagsObject



11
12
13
# File 'lib/hammer_cli/output/adapter/table.rb', line 11

def tags
  [:screen, :flat]
end