Class: Flux::Util::Table

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_sepObject (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_widthObject (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_dataObject (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

#shellObject (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.

Returns:

  • (Boolean)

    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)
  meta[index].include?(ALIGN_RIGHT)
end

#bodyObject

Returns all rows but the first.

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

#dataObject

Returns the table data, stripped of format information.

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

#headersObject

Returns the table’s first row.

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

#metaObject

Returns the table’s format information.

Returns:

  • the table’s format information



56
57
58
59
60
# File 'lib/flux/util/table.rb', line 56

def meta
  @meta ||= @raw_data.first.inject([]) { |a, e|
    a << (e =~ /^>/ ? ALIGN_RIGHT : '')
  }
end

#to_sObject



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  = meta.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