class Term::Table

Attributes

border[RW]

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

columns[RW]

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

height[RW]

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

indent[RW]

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

padding[RW]

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

strip_color[RW]

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

width[RW]

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

[](data, **opts) click to toggle source
# File lib/epitools/term.rb, line 165
def self.[](data, **opts)
  new(data, **opts)
end
hprint(thing) click to toggle source
# File lib/epitools/term.rb, line 157
def self.hprint(thing)
  puts new(thing).in_rows
end
new(data, **options) click to toggle source
# 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
print(thing, **opts) click to toggle source
vprint(thing) click to toggle source
# File lib/epitools/term.rb, line 161
def self.vprint(thing)
  puts new(thing).in_columns
end

Public Instance Methods

by_cols()
Alias for: in_columns
by_columns()
Alias for: in_columns
by_rows()
Alias for: in_rows
column_order() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/epitools/term.rb, line 233
def in_columns
  return '' if @data.empty?
  render sliced_into(num_rows).transpose
end
Also aliased as: by_columns, by_cols
in_rows() click to toggle source
# File lib/epitools/term.rb, line 240
def in_rows
  return '' if @data.empty?
  render sliced_into(num_columns)
end
Also aliased as: by_rows
num_columns() click to toggle source
# 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() click to toggle source
# File lib/epitools/term.rb, line 202
def num_rows
  (@data.size / num_columns.to_f).ceil
end
render(rows, **options) click to toggle source
# 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
row_order() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/epitools/term.rb, line 255
def to_s
  by_rows
end