class Term::Table
Attributes
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
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
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
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
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
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
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
Public Class Methods
# File lib/epitools/term.rb, line 165 def self.[](data, **opts) new(data, **opts) end
# File lib/epitools/term.rb, line 157 def self.hprint(thing) puts new(thing).in_rows end
# File lib/epitools/term.rb, line 169 def initialize(data, **options) @data = data.map(&:to_s) @strip_color = options[:ansi] || options[:colorized] || options[:colored] || options[:strip_color] || options[:strip_ansi] if strip_color @max_size = @data.map { |e| e.strip_color.size }.max else @max_size = @data.map(&:size).max end @indent = options[:indent] || 0 @border = options[:border] @columns = options[:columns] @padding = options[:padding] || 1 if (options.keys & [:horiz, :horizontal, :horizontally]).any? @direction = :horizontal else @direction = :vertical end # Update the terminal size @width, @height = Term.size end
# 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
# File lib/epitools/term.rb, line 161 def self.vprint(thing) puts new(thing).in_columns end
Public Instance Methods
# 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
# File lib/epitools/term.rb, line 246 def display #(**opts) case @direction when :horizontal puts in_rows when :vertical puts in_columns end end
# File lib/epitools/term.rb, line 233 def in_columns return '' if @data.empty? render sliced_into(num_rows).transpose end
# File lib/epitools/term.rb, line 240 def in_rows return '' if @data.empty? render sliced_into(num_columns) end
# 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
# File lib/epitools/term.rb, line 202 def num_rows (@data.size / num_columns.to_f).ceil end
# File lib/epitools/term.rb, line 259 def render(rows, **options) 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
# 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
# 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
# File lib/epitools/term.rb, line 255 def to_s by_rows end