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
print_record(fields, record) click to toggle source
tags() click to toggle source
# File lib/hammer_cli/output/adapter/table.rb, line 15
def tags
  [:screen, :flat]
end

Protected Instance Methods

calculate_column_width(field, collection) click to toggle source
# File lib/hammer_cli/output/adapter/table.rb, line 90
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 86
def calculate_widths(fields, collection)
  Hash[fields.map { |f| [f.label, calculate_column_width(f, collection)] }]
end
field_filter() click to toggle source
# File lib/hammer_cli/output/adapter/table.rb, line 104
def field_filter
  filtered = [Fields::ContainerField]
  filtered << Fields::Id unless @context[:show_ids]
  HammerCLI::Output::FieldFilter.new(filtered)
end
format_values(fields, collection) click to toggle source
# File lib/hammer_cli/output/adapter/table.rb, line 77
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 64
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 112
def max_width_for(field)
  if field.parameters[:max_width]
    [field.parameters[:max_width], MAX_COLUMN_WIDTH].min
  else
    MAX_COLUMN_WIDTH
  end
end