Class: Term::Table
Instance Attribute Summary collapse
-
#border ⇒ Object
TODO:.
-
#columns ⇒ Object
TODO:.
-
#height ⇒ Object
TODO:.
-
#indent ⇒ Object
TODO:.
-
#padding ⇒ Object
TODO:.
-
#strip_color ⇒ Object
TODO:.
-
#width ⇒ Object
TODO:.
Class Method Summary collapse
Instance Method Summary collapse
- #column_order ⇒ Object
-
#display ⇒ Object
(opts={}).
- #in_columns ⇒ Object (also: #by_columns)
- #in_rows ⇒ Object (also: #by_rows)
-
#initialize(data, options = {}) ⇒ Table
constructor
A new instance of Table.
- #num_columns ⇒ Object
- #num_rows ⇒ Object
- #render(rows, options = {}) ⇒ Object
- #row_order ⇒ Object
- #sliced_into(n) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(data, options = {}) ⇒ Table
Returns a new instance of Table.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/epitools/term.rb', line 75 def initialize(data, ={}) @data = data.map(&:to_s) @strip_color = [:ansi] || [:colorized] || [:colored] || [:strip_color] || [:strip_ansi] if strip_color @max_size = @data.map { |e| e.strip_color.size }.max else @max_size = @data.map(&:size).max end @indent = [:indent] || 0 @border = [:border] @columns = [:columns] @padding = [:padding] || 1 if (.keys & [:horiz, :horizontal, :horizontally]).any? @direction = :horizontal else @direction = :vertical end # Update the terminal size @width, @height = Term.size end |
Instance Attribute Details
#border ⇒ Object
TODO:
-
make Table’s configuration eaiser to remember by putting the formatting parameters in initialize eg: Table.new(elements, :sort=>:vertical).to_s
69 70 71 |
# File 'lib/epitools/term.rb', line 69 def border @border end |
#columns ⇒ Object
TODO:
-
make Table’s configuration eaiser to remember by putting the formatting parameters in initialize eg: Table.new(elements, :sort=>:vertical).to_s
69 70 71 |
# File 'lib/epitools/term.rb', line 69 def columns @columns end |
#height ⇒ Object
TODO:
-
make Table’s configuration eaiser to remember by putting the formatting parameters in initialize eg: Table.new(elements, :sort=>:vertical).to_s
69 70 71 |
# File 'lib/epitools/term.rb', line 69 def height @height end |
#indent ⇒ Object
TODO:
-
make Table’s configuration eaiser to remember by putting the formatting parameters in initialize eg: Table.new(elements, :sort=>:vertical).to_s
69 70 71 |
# File 'lib/epitools/term.rb', line 69 def indent @indent end |
#padding ⇒ Object
TODO:
-
make Table’s configuration eaiser to remember by putting the formatting parameters in initialize eg: Table.new(elements, :sort=>:vertical).to_s
69 70 71 |
# File 'lib/epitools/term.rb', line 69 def padding @padding end |
#strip_color ⇒ Object
TODO:
-
make Table’s configuration eaiser to remember by putting the formatting parameters in initialize eg: Table.new(elements, :sort=>:vertical).to_s
69 70 71 |
# File 'lib/epitools/term.rb', line 69 def strip_color @strip_color end |
#width ⇒ Object
TODO:
-
make Table’s configuration eaiser to remember by putting the formatting parameters in initialize eg: Table.new(elements, :sort=>:vertical).to_s
69 70 71 |
# File 'lib/epitools/term.rb', line 69 def width @width end |
Class Method Details
.[](data) ⇒ Object
71 72 73 |
# File 'lib/epitools/term.rb', line 71 def self.[](data) self.new(data) end |
Instance Method Details
#column_order ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/epitools/term.rb', line 111 def column_order cols = [] @data.each_slice(num_rows) { |col| cols << col } if (diff = cols.first.size - cols.last.size) > 0 cols.last.concat [''] * diff end cols.transpose end |
#display ⇒ Object
(opts={})
150 151 152 153 154 155 156 157 |
# File 'lib/epitools/term.rb', line 150 def display #(opts={}) case @direction when :horizontal puts in_rows when :vertical puts in_columns end end |
#in_columns ⇒ Object Also known as: by_columns
138 139 140 141 |
# File 'lib/epitools/term.rb', line 138 def in_columns return '' if @data.empty? render sliced_into(num_rows).transpose end |
#in_rows ⇒ Object Also known as: by_rows
144 145 146 147 |
# File 'lib/epitools/term.rb', line 144 def in_rows return '' if @data.empty? render sliced_into(num_columns) end |
#num_columns ⇒ Object
100 101 102 103 104 105 |
# File 'lib/epitools/term.rb', line 100 def num_columns return @columns if @columns w = @width w -= indent (w-2) / (@max_size + @padding) end |
#num_rows ⇒ Object
107 108 109 |
# File 'lib/epitools/term.rb', line 107 def num_rows (@data.size / num_columns.to_f).ceil end |
#render(rows, options = {}) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/epitools/term.rb', line 163 def render(rows, ={}) num_cols = rows.first.size result = [] if @border separator = "+#{(["-" * @max_size] * num_cols).join('+')}+" result << separator end for row in rows justified = row.map do |e| if (diff = @max_size - e.strip_color.size) > 0 e = e + (" " * diff) end e end if @border line = "|#{justified.join('|')}|" else line = justified.join(' '*@padding) end result << (" "*indent) + line end result << separator if @border result.join("\n") end |
#row_order ⇒ Object
120 121 122 123 124 125 126 127 |
# File 'lib/epitools/term.rb', line 120 def row_order rows = [] @data.each_slice(num_columns) { |row| rows << row } if (diff = rows.first.size - rows.last.size) > 0 rows.last.concat [''] * diff end rows end |
#sliced_into(n) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/epitools/term.rb', line 129 def sliced_into(n) elems = [] @data.each_slice(n) { |e| elems << e } if (diff = elems.first.size - elems.last.size) > 0 elems.last.concat [''] * diff end elems end |
#to_s ⇒ Object
159 160 161 |
# File 'lib/epitools/term.rb', line 159 def to_s by_rows end |