class HammerCLI::Output::Adapter::Table
Constants
- COLUMN_SEPARATOR
- HLINE
- LINE_SEPARATOR
- MAX_COLUMN_WIDTH
- MIN_COLUMN_WIDTH
Public Instance Methods
print_collection(all_fields, collection)
click to toggle source
# File lib/hammer_cli/output/adapter/table.rb, line 23 def print_collection(all_fields, collection) 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) # 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) unless @context[:no_headers] output_stream.puts line output_stream.puts header_bits.join(COLUMN_SEPARATOR) output_stream.puts line end formatted_collection.collect do |row| row_bits = fields.map do |f| normalize_column(widths[f.label], row[f.label] || "") end output_stream.puts row_bits.join(COLUMN_SEPARATOR) end # print closing line only when the table isn't empty # and there is no --no-headers option output_stream.puts line unless formatted_collection.empty? || @context[:no_headers] if collection.respond_to?(:meta) && collection.meta.pagination_set? && @context[:verbosity] >= collection.meta.pagination_verbosity && 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
print_record(fields, record)
click to toggle source
# File lib/hammer_cli/output/adapter/table.rb, line 19 def print_record(fields, record) print_collection(fields, [record].flatten(1)) end
Protected Instance Methods
calculate_column_width(field, collection)
click to toggle source
# File lib/hammer_cli/output/adapter/table.rb, line 98 def calculate_column_width(field, collection) if field.parameters[:width] return [field.parameters[:width], MIN_COLUMN_WIDTH].max end width = HammerCLI::Output::Utils.real_length(field.label.to_s) max_width = max_width_for(field) collection.each do |item| width = [HammerCLI::Output::Utils.real_length(item[field.label]), width].max return max_width if width >= max_width end width end
calculate_widths(fields, collection)
click to toggle source
# File lib/hammer_cli/output/adapter/table.rb, line 94 def calculate_widths(fields, collection) Hash[fields.map { |f| [f.label, calculate_column_width(f, collection)] }] end
classes_filter()
click to toggle source
Calls superclass method
HammerCLI::Output::Adapter::Abstract#classes_filter
# File lib/hammer_cli/output/adapter/table.rb, line 68 def classes_filter super << Fields::ContainerField end
format_values(fields, collection)
click to toggle source
# File lib/hammer_cli/output/adapter/table.rb, line 85 def format_values(fields, collection) collection.collect do |d| fields.inject({}) do |row, f| formatter = WrapperFormatter.new(@formatters.formatter_for_type(f.class), f.parameters) row.update(f.label => formatter.format(data_for_field(f, d) || "").to_s) end end end
normalize_column(width, value)
click to toggle source
# File lib/hammer_cli/output/adapter/table.rb, line 72 def normalize_column(width, value) value = value.to_s padding = width - HammerCLI::Output::Utils.real_length(value) if padding >= 0 value += (" " * padding) else value, real_length = HammerCLI::Output::Utils.real_truncate(value, width-3) value += '...' value += ' ' if real_length < (width - 3) end value end
Private Instance Methods
max_width_for(field)
click to toggle source
# File lib/hammer_cli/output/adapter/table.rb, line 114 def max_width_for(field) if field.parameters[:max_width] [field.parameters[:max_width], MAX_COLUMN_WIDTH].min else MAX_COLUMN_WIDTH end end