Class: SimpleConsoleTable

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_console_table.rb

Overview

SimpleConsoleTable - a library capable of printing nicely formatted tables to stdout

Copyright © 2009 Sergey Sazonov

Example

Basic example

require 'rubygems'
require 'simple_console_table'

table = SimpleConsoleTable.new

table << ['one', 'two', 'three']
table << ['four', 'five', 'six', 'seven']

table.as_string

# +--------+--------+---------+---------+
# |   one  |   two  |  three  |         |
# +--------+--------+---------+---------+
# |  four  |  five  |    six  |  seven  |
# +--------+--------+---------+---------+

Use print to write the table directly to the console. Use as_string to obtain the result as a string.

Using objects other than strings

SimpleConsoleTable invokes to_s method on every object thus converting everything to strings.

class Dummy
  def initialize(dummy_id); @dummy_id = dummy_id; end
  def to_s; "Dummy #{@dummy_id}"; end
end

require 'rubygems'
require 'simple_console_table'

table = SimpleConsoleTable.new

table << ['one', 2, 'three']
table << [true, 'five', -6.89, Dummy.new(345)]

table.as_string

# +--------+--------+---------+-------------+
# |   one  |     2  |  three  |             |
# +--------+--------+---------+-------------+
# |  true  |  five  |  -6.89  |  Dummy 345  |
# +--------+--------+---------+-------------+

Passing nested arrays

SimpleConsoleTable invokes flatten method on every array passed to <<

require 'rubygems'
require 'simple_console_table'

table = SimpleConsoleTable.new

table << [1, 2, [3, 4], [5]]
table << [[[[[[6, 7, 8]]], [[[[9, 10]]]]]]]

table.as_string

# +-----+-----+-----+-----+------+
# |  1  |  2  |  3  |  4  |   5  |
# +-----+-----+-----+-----+------+
# |  6  |  7  |  8  |  9  |  10  |
# +-----+-----+-----+-----+------+

Constant Summary collapse

DEFAULT_FORMATTER =
lambda do |text_to_format, column_width|
  sprintf("  %#{column_width}.#{column_width}s  ", text_to_format)
end

Instance Method Summary collapse

Constructor Details

#initializeSimpleConsoleTable

Returns a new instance of SimpleConsoleTable.



76
77
78
79
# File 'lib/simple_console_table.rb', line 76

def initialize
  @rows = []
  @formatter = DEFAULT_FORMATTER
end

Instance Method Details

#<<(row) ⇒ Object

Adds row.flatten.collect { |cell_content| cell_content.to_s } to the table



82
83
84
# File 'lib/simple_console_table.rb', line 82

def <<(row)
  @rows << row.flatten.collect { |cell_content| cell_content.to_s }
end

#as_stringObject

Returns an empty string if the table is empty (that is, no rows have been added). Otherwise, returns the formatted table as a string.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/simple_console_table.rb', line 88

def as_string
  return '' if @rows.empty?

  column_widths = calculate_column_widths
  formatted_rows = format_table(column_widths)
  formatted_column_widths = formatted_rows.first.collect { |cell_content| cell_content.length }
  row_separator = construct_row_separator(formatted_column_widths)

  output = formatted_rows.collect do |row|
    ['', row, ''].flatten.join('|')
  end

  separated_output = ([row_separator] * (output.length + 1)).zip(output).flatten

  separated_output.join("\n")
end

A shortcut for puts(as_string)



106
107
108
# File 'lib/simple_console_table.rb', line 106

def print
  puts(as_string)
end