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
- .[](data, **opts) ⇒ Object
- .hprint(thing) ⇒ Object
- .print(thing, **opts) ⇒ Object
- .vprint(thing) ⇒ Object
Instance Method Summary collapse
- #column_order ⇒ Object
-
#display ⇒ Object
(**opts).
- #in_columns ⇒ Object (also: #by_columns, #by_cols)
- #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.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/epitools/term.rb', line 169 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
-
strip ansi
-
wrap contents
-
rounded corners
- far future
-
dynamic sortable filterable toggleable table
150 151 152 |
# File 'lib/epitools/term.rb', line 150 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
-
strip ansi
-
wrap contents
-
rounded corners
- far future
-
dynamic sortable filterable toggleable table
150 151 152 |
# File 'lib/epitools/term.rb', line 150 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
-
strip ansi
-
wrap contents
-
rounded corners
- far future
-
dynamic sortable filterable toggleable table
150 151 152 |
# File 'lib/epitools/term.rb', line 150 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
-
strip ansi
-
wrap contents
-
rounded corners
- far future
-
dynamic sortable filterable toggleable table
150 151 152 |
# File 'lib/epitools/term.rb', line 150 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
-
strip ansi
-
wrap contents
-
rounded corners
- far future
-
dynamic sortable filterable toggleable table
150 151 152 |
# File 'lib/epitools/term.rb', line 150 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
-
strip ansi
-
wrap contents
-
rounded corners
- far future
-
dynamic sortable filterable toggleable table
150 151 152 |
# File 'lib/epitools/term.rb', line 150 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
-
strip ansi
-
wrap contents
-
rounded corners
- far future
-
dynamic sortable filterable toggleable table
150 151 152 |
# File 'lib/epitools/term.rb', line 150 def width @width end |
Class Method Details
.[](data, **opts) ⇒ Object
165 166 167 |
# File 'lib/epitools/term.rb', line 165 def self.[](data, **opts) new(data, **opts) end |
.hprint(thing) ⇒ Object
157 158 159 |
# File 'lib/epitools/term.rb', line 157 def self.hprint(thing) puts new(thing).in_rows end |
.print(thing, **opts) ⇒ Object
152 153 154 155 |
# File 'lib/epitools/term.rb', line 152 def self.print(thing, **opts) raise "Can't tablize a #{thing.class}" unless thing.class < Enumerable puts new(thing, **opts).display end |
.vprint(thing) ⇒ Object
161 162 163 |
# File 'lib/epitools/term.rb', line 161 def self.vprint(thing) puts new(thing).in_columns end |
Instance Method Details
#column_order ⇒ Object
206 207 208 209 210 211 212 213 |
# File 'lib/epitools/term.rb', line 206 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)
246 247 248 249 250 251 252 253 |
# File 'lib/epitools/term.rb', line 246 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, by_cols
233 234 235 236 |
# File 'lib/epitools/term.rb', line 233 def in_columns return '' if @data.empty? render sliced_into(num_rows).transpose end |
#in_rows ⇒ Object Also known as: by_rows
240 241 242 243 |
# File 'lib/epitools/term.rb', line 240 def in_rows return '' if @data.empty? render sliced_into(num_columns) end |
#num_columns ⇒ Object
194 195 196 197 198 199 200 |
# File 'lib/epitools/term.rb', line 194 def num_columns return @columns if @columns w = @width w -= indent cols = (w-2) / (@max_size + @padding) cols > 0 ? cols : 1 end |
#num_rows ⇒ Object
202 203 204 |
# File 'lib/epitools/term.rb', line 202 def num_rows (@data.size / num_columns.to_f).ceil end |
#render(rows, **options) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/epitools/term.rb', line 259 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
215 216 217 218 219 220 221 222 |
# File 'lib/epitools/term.rb', line 215 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
224 225 226 227 228 229 230 231 |
# File 'lib/epitools/term.rb', line 224 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
255 256 257 |
# File 'lib/epitools/term.rb', line 255 def to_s by_rows end |