Class: Flux::Util::Table
- Inherits:
-
Object
- Object
- Flux::Util::Table
- Defined in:
- lib/flux/util/table.rb
Overview
A table pretty printer that allows us to take a peek at the data after it’s been stripped of formatting information. It exists to make testing easier.
If any cell in the first row is prefixed with ‘>’, then that column’s contents will be right-aligned. This is useful when showing numbers.
Constant Summary collapse
- ALIGN_RIGHT =
'>'
Instance Attribute Summary collapse
-
#field_sep ⇒ Object
readonly
Returns the value of attribute field_sep.
-
#max_width ⇒ Object
readonly
Returns the value of attribute max_width.
-
#raw_data ⇒ Object
readonly
Returns the value of attribute raw_data.
-
#shell ⇒ Object
readonly
Returns the value of attribute shell.
Instance Method Summary collapse
-
#align_right?(index) ⇒ Boolean
Whether a given column’s contents will be aligned to the right.
-
#body ⇒ Object
All rows but the first.
-
#data ⇒ Object
The table data, stripped of format information.
-
#headers ⇒ Object
The table’s first row.
-
#initialize(shell, raw_data, field_sep, max_width) ⇒ Table
constructor
A new instance of Table.
-
#meta ⇒ Object
The table’s format information.
- #to_s ⇒ Object
Constructor Details
#initialize(shell, raw_data, field_sep, max_width) ⇒ Table
Returns a new instance of Table.
26 27 28 29 30 31 |
# File 'lib/flux/util/table.rb', line 26 def initialize(shell, raw_data, field_sep, max_width) @shell = shell @raw_data = raw_data @field_sep = field_sep @max_width = max_width || terminal_width end |
Instance Attribute Details
#field_sep ⇒ Object (readonly)
Returns the value of attribute field_sep.
23 24 25 |
# File 'lib/flux/util/table.rb', line 23 def field_sep @field_sep end |
#max_width ⇒ Object (readonly)
Returns the value of attribute max_width.
23 24 25 |
# File 'lib/flux/util/table.rb', line 23 def max_width @max_width end |
#raw_data ⇒ Object (readonly)
Returns the value of attribute raw_data.
23 24 25 |
# File 'lib/flux/util/table.rb', line 23 def raw_data @raw_data end |
#shell ⇒ Object (readonly)
Returns the value of attribute shell.
23 24 25 |
# File 'lib/flux/util/table.rb', line 23 def shell @shell end |
Instance Method Details
#align_right?(index) ⇒ Boolean
Returns whether a given column’s contents will be aligned to the right.
34 35 36 |
# File 'lib/flux/util/table.rb', line 34 def align_right?(index) [index].include?(ALIGN_RIGHT) end |
#body ⇒ Object
Returns all rows but the first.
39 40 41 |
# File 'lib/flux/util/table.rb', line 39 def body @body ||= raw_data[1..-1] end |
#data ⇒ Object
Returns the table data, stripped of format information.
44 45 46 |
# File 'lib/flux/util/table.rb', line 44 def data @data ||= body.unshift(headers) end |
#headers ⇒ Object
Returns the table’s first row.
49 50 51 52 53 |
# File 'lib/flux/util/table.rb', line 49 def headers @headers = raw_data.first.each_with_index.map { |e, i| align_right?(i) ? e[1..-1] : e } end |
#meta ⇒ Object
Returns the table’s format information.
56 57 58 59 60 |
# File 'lib/flux/util/table.rb', line 56 def @meta ||= @raw_data.first.inject([]) { |a, e| a << (e =~ /^>/ ? ALIGN_RIGHT : '') } end |
#to_s ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/flux/util/table.rb', line 62 def to_s @s ||= begin widths = data.transpose.inject([]) { |a, col| a << col.max { |a, b| a.to_s.size <=> b.to_s.size }.to_s.size } # we don't need the last column's width if it is to be left-aligned widths[-1] = nil unless align_right?(-1) format = .each_with_index.inject('') { |a, (e, i)| w = widths[i] ? widths[i].to_s : '' align = align_right?(i) ? '' : '-' a << "#{field_sep}%" << align << w << 's' }.strip max_width data.inject('') { |a, r| l = a << truncate(format % r, max_width) << "\n" } end end |