Class: Terminal::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/terminal/table.rb,
lib/terminal/table/version.rb

Constant Summary collapse

VERSION =
'0.0.6'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object = nil, options = {}) {|_self| ... } ⇒ Table

Returns a new instance of Table.

Yields:

  • (_self)

Yield Parameters:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/terminal/table.rb', line 49

def initialize(object = nil, options = {})
  @rows = []
  @headings = []
  @column_widths = []

  if object
    if object.respond_to?(:each)
      object.each { |o| add_object(o, options) }
    else
      add_object(object, options)
    end
  end

  yield self if block_given?
  recalculate_column_widths!
end

Instance Attribute Details

#column_widthsObject

Returns the value of attribute column_widths.



47
48
49
# File 'lib/terminal/table.rb', line 47

def column_widths
  @column_widths
end

#headingsObject

Returns the value of attribute headings.



46
47
48
# File 'lib/terminal/table.rb', line 46

def headings
  @headings
end

#rowsObject

Returns the value of attribute rows.



45
46
47
# File 'lib/terminal/table.rb', line 45

def rows
  @rows
end

Instance Method Details

#add_object(object, options) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/terminal/table.rb', line 66

def add_object(object, options)
  if object.respond_to?(:to_hash)
    hash = object.to_hash
    if options[:only]
      hash.keep_if { |k, v| options[:only].map(&:to_sym).include?(k) }
    elsif options[:except]
      hash.delete_if { |k, v| options[:except].map(&:to_sym).include?(k) }
    end

    @headings = hash.keys.map(&:to_s)
    @rows << hash.values
  end
end

#recalculate_column_widths!Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/terminal/table.rb', line 84

def recalculate_column_widths!
  @rows = rows.map { |row| row.map { |item| item.to_s.gsub("\r\n", " ").gsub("\n", " ").gsub("\r", " ") } }

  if @rows.count > 0
    (0...@rows.first.size).each do |col|
      @column_widths[col] = @rows.map { |row| row[col].to_s.twidth }.max
    end
  end

  if @headings.count > 0
    (0...@headings.size).each do |col|
      @column_widths[col] = [@column_widths[col] || 0, @headings[col].twidth].max
    end
  end
end

#to_sObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/terminal/table.rb', line 100

def to_s
  recalculate_column_widths!

  result = ''

  header_and_footer = '+' + @column_widths.map { |w| '-' * (w + 2) }.join('+') + '+' + "\n"

  if @headings.count > 0
    result += header_and_footer

    content = @headings.each_with_index.map { |grid, i| grid.to_s.tljust(@column_widths[i]) }

    result += '| ' + content.join(' | ') + " |\n"
  end

  result += header_and_footer

  @rows.each do |row|
    content = row.each_with_index.map { |grid, i| grid.to_s.tljust(@column_widths[i]) }

    result += '| ' + content.join(' | ') + " |\n"
  end

  result + header_and_footer
end