lib/tailstrom/table.rb in tailstrom-0.0.4 vs lib/tailstrom/table.rb in tailstrom-0.0.5

- old
+ new

@@ -6,14 +6,14 @@ end def print_row(*cols) cols.each_with_index do |col, i| col_schema = @schema[i] - num_str = col ? num_with_delim(col) : '-' + str = format_string col print ' ' if i > 0 align = col_schema[:align].to_s == 'left' ? '-' : nil - printf "%#{align}#{col_schema[:width]}s", num_str + printf "%#{align}#{col_schema[:width]}s", str end self.puts end def print_header @@ -33,9 +33,20 @@ def puts(*args) @out.puts *args end private + def format_string(value) + case value + when Numeric + num_with_delim value + when nil + '-' + else + value + end + end + 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]}"