class HammerCLI::Output::Generators::Table

Constants

COLUMN_SEPARATOR
HLINE
LINE_SEPARATOR
MAX_COLUMN_WIDTH
MIN_COLUMN_WIDTH

Attributes

body[R]
header[R]
result[R]

Public Class Methods

new(columns, data, options = {}) click to toggle source
# File lib/hammer_cli/output/generators/table.rb, line 25
def initialize(columns, data, options = {})
  @columns = columns.map { |label, params| Column.new(label, params) }
  @data = data
  @options = options
  create_table
end

Private Instance Methods

calculate_column_width(column, data) click to toggle source
# File lib/hammer_cli/output/generators/table.rb, line 99
def calculate_column_width(column, data)
  if column.params[:width]
    return [column.params[:width], MIN_COLUMN_WIDTH].max
  end

  width = HammerCLI::Output::Utils.real_length(column.label)
  max_width = max_width_for(column)
  data.each do |item|
    width = [HammerCLI::Output::Utils.real_length(item[column.label]), width].max
    return max_width if width >= max_width
  end
  width
end
calculate_widths(columns, data) click to toggle source
# File lib/hammer_cli/output/generators/table.rb, line 95
def calculate_widths(columns, data)
  Hash[columns.map { |c| [c.label, calculate_column_width(c, data)] }]
end
create_body(widths) click to toggle source
# File lib/hammer_cli/output/generators/table.rb, line 44
def create_body(widths)
  result = StringIO.new
  @data.collect do |row|
    row_bits = @columns.map do |col|
      normalize_column(widths[col.label], row[col.label] || '')
    end
    result.puts(row_bits.join(COLUMN_SEPARATOR))
  end
  result.string
end
create_header(header_bits, line) click to toggle source
# File lib/hammer_cli/output/generators/table.rb, line 34
def create_header(header_bits, line)
  result = StringIO.new
  unless @options[:no_headers]
    result.puts(line)
    result.puts(header_bits.join(COLUMN_SEPARATOR))
    result.puts(line)
  end
  result.string
end
create_result() click to toggle source
# File lib/hammer_cli/output/generators/table.rb, line 61
def create_result
  result = StringIO.new
  result.print(@header, @body, @footer)
  result.string
end
create_table() click to toggle source
# File lib/hammer_cli/output/generators/table.rb, line 67
def create_table
  widths = calculate_widths(@columns, @data)
  header_bits = []
  hline_bits = []
  @columns.map do |col|
    header_bits << normalize_column(widths[col.label], col.label.upcase)
    hline_bits << HLINE * widths[col.label]
  end
  line = hline_bits.join(LINE_SEPARATOR)
  @header = create_header(header_bits, line)
  @body = create_body(widths)
  @footer = create_footer(line)
  @result = create_result
end
max_width_for(column) click to toggle source
# File lib/hammer_cli/output/generators/table.rb, line 113
def max_width_for(column)
  return MAX_COLUMN_WIDTH unless column.params[:max_width]

  column.params[:max_width]
end
normalize_column(width, value) click to toggle source
# File lib/hammer_cli/output/generators/table.rb, line 82
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