Class: Razor::CLI::TableFormat

Inherits:
Object
  • Object
show all
Includes:
CommandLineReporter
Defined in:
lib/razor/cli/table_format.rb

Instance Method Summary collapse

Instance Method Details

#average_width(headings) ⇒ Object

This calculates what an auto-sized fixed-width table’s column width would be.



54
55
56
57
58
59
60
61
62
# File 'lib/razor/cli/table_format.rb', line 54

def average_width(headings)
  # The 3 here = 2 for width gap + 1 for the column separator.
  # The 1 is for the last separator.
  console_width = `stty size | cut -d ' ' -f 2 2>/dev/null`
  if console_width.nil? || console_width.to_i <= 0
    console_width = 80
  end
  @average_width ||= ((console_width.to_i - (headings.count * 3) - 1) / headings.count)
end

#column_width!(headings, header, doc) ⇒ Object

This method has the side effect of modifying the remaining extra_width. It pulls everything together to come up with a single value for the column.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/razor/cli/table_format.rb', line 28

def column_width!(headings, header, doc)
  content_width = content_width(header, doc)
  average_width = average_width(headings)
  # Is the column too wide and can we do anything about it?
  if content_width > average_width && extra_width(headings, doc) > 0
    # Determine how much room we'd need to make to accommodate the content.
    remaining = content_width - average_width
    # Add back in what we can.
    width = average_width + [@extra_width, remaining].min
    # The new width can't be negative.
    @extra_width = [@extra_width - remaining, 0].max
    width
  else
    [content_width, average_width].min
  end
end

#content_width(header, doc) ⇒ Object



64
65
66
67
68
69
# File 'lib/razor/cli/table_format.rb', line 64

def content_width(header, doc)
  # Find longest item, including the header
  (doc.map do |page|
    (page[header] or '').to_s.length
  end << header.to_s.length).max
end

#extra_width(headings, doc) ⇒ Object

This calculates how much leeway would exist in all columns if we were to use an auto-sized fixed width for the whole table.



47
48
49
50
51
# File 'lib/razor/cli/table_format.rb', line 47

def extra_width(headings, doc)
  @extra_width ||= headings.map do |header|
                     [average_width(headings) - content_width(header, doc), 0].max
                   end.inject(:+)
end

#get_headers(doc) ⇒ Object

Traverse all headers to compile a unique list.



72
73
74
75
76
77
78
79
80
# File 'lib/razor/cli/table_format.rb', line 72

def get_headers(doc)
  [].tap do |headers|
    doc.map do |page|
      page.map do |item|
        headers << item[0] unless headers.include?(item[0])
      end
    end
  end
end

#run(doc, column_overrides) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/razor/cli/table_format.rb', line 5

def run(doc, column_overrides)
  suppress_output
  table(:border => true, :encoding => :ascii) do
    headings = (column_overrides or get_headers(doc))
    row do
      headings.each do |header|
        column(header, :width => column_width!(headings, header, doc))
      end
    end
    doc.each do |page|
      row do
        headings.each do |heading|
          column(page[heading])
        end
      end
    end
  end
  # Capturing stores the string, rather than printing to STDOUT (default).
  capture_output.strip
end