Class: Terminal::Table
- Inherits:
-
Object
- Object
- Terminal::Table
- Defined in:
- lib/terminal/table.rb,
lib/terminal/table/version.rb
Constant Summary collapse
- VERSION =
'0.0.6'
Instance Attribute Summary collapse
-
#column_widths ⇒ Object
Returns the value of attribute column_widths.
-
#headings ⇒ Object
Returns the value of attribute headings.
-
#rows ⇒ Object
Returns the value of attribute rows.
Instance Method Summary collapse
- #add_object(object, options) ⇒ Object
-
#initialize(object = nil, options = {}) {|_self| ... } ⇒ Table
constructor
A new instance of Table.
- #recalculate_column_widths! ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(object = nil, options = {}) {|_self| ... } ⇒ Table
Returns a new instance of Table.
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, = {}) @rows = [] @headings = [] @column_widths = [] if object if object.respond_to?(:each) object.each { |o| add_object(o, ) } else add_object(object, ) end end yield self if block_given? recalculate_column_widths! end |
Instance Attribute Details
#column_widths ⇒ Object
Returns the value of attribute column_widths.
47 48 49 |
# File 'lib/terminal/table.rb', line 47 def column_widths @column_widths end |
#headings ⇒ Object
Returns the value of attribute headings.
46 47 48 |
# File 'lib/terminal/table.rb', line 46 def headings @headings end |
#rows ⇒ Object
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, ) if object.respond_to?(:to_hash) hash = object.to_hash if [:only] hash.keep_if { |k, v| [:only].map(&:to_sym).include?(k) } elsif [:except] hash.delete_if { |k, v| [: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_s ⇒ Object
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 = '' = '+' + @column_widths.map { |w| '-' * (w + 2) }.join('+') + '+' + "\n" if @headings.count > 0 result += content = @headings.each_with_index.map { |grid, i| grid.to_s.tljust(@column_widths[i]) } result += '| ' + content.join(' | ') + " |\n" end result += @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 + end |