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.10'
Instance Attribute Summary collapse
-
#column_widths ⇒ Object
Returns the value of attribute column_widths.
-
#headings ⇒ Object
Returns the value of attribute headings.
-
#new_line_symbol ⇒ Object
Returns the value of attribute new_line_symbol.
-
#rows ⇒ Object
Returns the value of attribute rows.
Class Method Summary collapse
Instance Method Summary collapse
- #add_hash(hash, options) ⇒ Object
- #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.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/terminal/table.rb', line 77 def initialize(object = nil, = {}) @rows = [] @headings = [] @column_widths = [] if [:use_new_line_symbol] @new_line_symbol = '⏎' else @new_line_symbol = ' ' end if object if object.is_a?(Hash) add_hash(object, ) elsif 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.
74 75 76 |
# File 'lib/terminal/table.rb', line 74 def column_widths @column_widths end |
#headings ⇒ Object
Returns the value of attribute headings.
73 74 75 |
# File 'lib/terminal/table.rb', line 73 def headings @headings end |
#new_line_symbol ⇒ Object
Returns the value of attribute new_line_symbol.
75 76 77 |
# File 'lib/terminal/table.rb', line 75 def new_line_symbol @new_line_symbol end |
#rows ⇒ Object
Returns the value of attribute rows.
72 73 74 |
# File 'lib/terminal/table.rb', line 72 def rows @rows end |
Class Method Details
.special_tokens ⇒ Object
168 169 170 |
# File 'lib/terminal/table.rb', line 168 def special_tokens String::CHARS_OF_WIDTH_OF_1 + String::CHARS_OF_WIDTH_OF_0 end |
Instance Method Details
#add_hash(hash, options) ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/terminal/table.rb', line 110 def add_hash(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 |
#add_object(object, options) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/terminal/table.rb', line 102 def add_object(object, ) if object.respond_to?(:to_hash) add_hash(object.to_hash, ) elsif object.respond_to?(:each) @rows << object end end |
#recalculate_column_widths! ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/terminal/table.rb', line 125 def recalculate_column_widths! @rows = rows.map { |row| row.map { |item| item.to_s.gsub("\r\n", @new_line_symbol).gsub("\n", @new_line_symbol).gsub("\r", @new_line_symbol) } } 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
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/terminal/table.rb', line 141 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 |