Sha256: 1945ebc6372016c9e8905f3c0bed84bb44a11191373439464dd36a1309d28f30

Contents?: true

Size: 1.64 KB

Versions: 7

Compression:

Stored size: 1.64 KB

Contents

module Tailstrom
  class Table
    def initialize(schema)
      @schema = schema
      @out = $stdout
    end

    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

    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

    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]}"
        else
          head
        end
      end
  end
end

if $0 == __FILE__
  nums = [
459938.0869565217,
588316.4761904762,
459265.652173913,
473729.63636363635,
625076.2857142857,
461625.76,
412747.04761904763,
367196.3,
0.125,
9.50,
10.1,
100,
100
  ]

  schema = [
    { :name => 'min', :width => 15 },
    { :name => 'max', :width => 15 },
  ]

  table = Tailstrom::Table.new schema
  table.print_header
  nums.each do |num|
    table.print_row num, num
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
tailstrom-0.0.11 lib/tailstrom/table.rb
tailstrom-0.0.10 lib/tailstrom/table.rb
tailstrom-0.0.9 lib/tailstrom/table.rb
tailstrom-0.0.8 lib/tailstrom/table.rb
tailstrom-0.0.7 lib/tailstrom/table.rb
tailstrom-0.0.6 lib/tailstrom/table.rb
tailstrom-0.0.5 lib/tailstrom/table.rb