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

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

Instance Method Summary collapse

Methods inherited from Abstract

#paginate_by_default?, #print_error, #print_message, #tags

Constructor Details

#initialize(context = {}, formatters = {}, filters = {}) ⇒ Table

Returns a new instance of Table.



5
6
7
8
# File 'lib/hammer_cli/output/adapter/table.rb', line 5

def initialize(context = {}, formatters = {}, filters = {})
  super
  @printed = 0
end

Instance Method Details

#featuresObject



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

def features
  return %i[rich_text serialized inline] if tags.empty?

  tags.map { |t| HammerCLI::Output::Utils.tag_to_feature(t) }
end


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
# File 'lib/hammer_cli/output/adapter/table.rb', line 20

def print_collection(all_fields, collection, options = {})
  current_chunk = options[:current_chunk] || :single
  fields = filter_fields(all_fields).filter_by_classes
                                    .filter_by_sets
                                    .filter_by_data(collection.first,
                                                    compact_only: true)
                                    .filtered_fields
  formatted_collection = format_values(fields, collection)

  columns = fields.each_with_object({}) do |field, result|
    result[field.label] = field.parameters
  end

  table_gen = HammerCLI::Output::Generators::Table.new(
    columns, formatted_collection, no_headers: @context[:no_headers]
  )

  meta = collection.respond_to?(:meta) ? collection.meta : nil

  output_stream.print(table_gen.header) if %i[first single].include?(current_chunk)

  output_stream.print(table_gen.body)

  @printed += collection.count

  # print closing line only after the last chunk
  output_stream.print(table_gen.footer) if %i[last single].include?(current_chunk)

  return unless meta && meta.pagination_set?

  leftovers = %i[last single].include?(current_chunk) && @printed < meta.subtotal
  if @context[:verbosity] >= meta.pagination_verbosity &&
     collection.count < meta.subtotal &&
     leftovers
    pages = (meta.subtotal.to_f / meta.per_page).ceil
    puts _("Page %{page} of %{total} (use --page and --per-page for navigation).") % {:page => meta.page, :total => pages}
  end
end


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

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