class Tailstrom::Table
Public Class Methods
new(schema)
click to toggle source
# File lib/tailstrom/table.rb, line 3 def initialize(schema) @schema = schema @out = $stdout end
Public Instance Methods
print_header()
click to toggle source
# File lib/tailstrom/table.rb, line 19 def print_header border = head = '' @schema.each_with_index do |col, i| if i > 0 border += '-' head += ' ' end align = col[:align].to_s == 'left' ? '-' : nil border += '-' * col[:width] head += "%#{align}#{col[:width]}s" % col[:name] end self.puts border, head, border end
print_row(*cols)
click to toggle source
# File lib/tailstrom/table.rb, line 8 def print_row(*cols) cols.each_with_index do |col, i| col_schema = @schema[i] str = format_string col print ' ' if i > 0 align = col_schema[:align].to_s == 'left' ? '-' : nil printf "%#{align}#{col_schema[:width]}s", str end self.puts end
puts(*args)
click to toggle source
# File lib/tailstrom/table.rb, line 33 def puts(*args) @out.puts *args end
Private Instance Methods
format_string(value)
click to toggle source
# File lib/tailstrom/table.rb, line 38 def format_string(value) case value when Numeric num_with_delim value when nil '-' else value end end
num_with_delim(num)
click to toggle source
# File lib/tailstrom/table.rb, line 49 def num_with_delim(num) head, tail = num.to_s.split('.') head.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,") if tail "#{head}.#{tail[0..2]}" else head end end