class HammerCLI::Output::Adapter::Table

Constants

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 11
def tags
  [:screen, :flat]
end

Protected Instance Methods

field_filter() click to toggle source
# File lib/hammer_cli/output/adapter/table.rb, line 64
def field_filter
  filtered = [Fields::ContainerField]
  filtered << Fields::Id unless @context[:show_ids]
  HammerCLI::Output::FieldFilter.new(filtered)
end

Private Instance Methods

label_for(field) click to toggle source
# File lib/hammer_cli/output/adapter/table.rb, line 72
def label_for(field)
  width = width_for(field)
  if width
    "%-#{width}s" % field.label.to_s
  else
    field.label.to_s
  end
end
max_width_for(field) click to toggle source
# File lib/hammer_cli/output/adapter/table.rb, line 81
def max_width_for(field)
  width = width_for(field)
  width ||= field.parameters[:max_width]
  width = MIN_COLUMN_WIDTH if width && width < MIN_COLUMN_WIDTH
  width
end
sort_columns(output, sort_order) click to toggle source
# File lib/hammer_cli/output/adapter/table.rb, line 95
def sort_columns(output, sort_order)
  return output if sort_order.length == 1 # don't sort one column
  delimiter = ' | '
  lines = output.split("\n")
  out = []

  headers = lines.first.split(delimiter).map(&:strip)

  # create mapping table for column indexes
  sort_map = []
  sort_order.each { |c| sort_map << headers.index(c) }

  lines.each do |line|
    columns = line.split(delimiter)
    if columns.length == 1 # dashes
      columns = columns.first.split('-|-')
      if columns.length == 1
        out << columns.first
      else # new style dashes
        new_row = []
        sort_map.each { |i| new_row << columns[i] }
        out << new_row.join('-|-')
      end
    else
      # reorder row
      new_row = []
      sort_map.each { |i| new_row << columns[i] }
      out << new_row.join(delimiter)
    end
  end

  out.join("\n")
end
width_for(field) click to toggle source
# File lib/hammer_cli/output/adapter/table.rb, line 88
def width_for(field)
  width = field.parameters[:width]
  width = MIN_COLUMN_WIDTH if width && width < MIN_COLUMN_WIDTH
  width
end