Terminal Table

Simple, feature rich ASCII table generator.

Installation:

Install [Gemcutter](http://gemcutter.org) then execute:
$ sudo gem install terminal-table

Features:

  • Fast

  • Simple

  • Optional headings

  • Alignment of columns, headings, or cells

  • Supports column span

  • Easy modification of table strings (+, -, |)

Examples:

require 'rubygems'
require 'terminal-table/import'

puts table(['a', 'b'], [1, 2], [3, 4], :separator, [4, 6])

t = table ['a', 'b']
t << [1, 2]
t << [3, 4]
t.add_separator
t << [4, 6]
puts t

user_table = table do |t|
  t.headings = 'First Name', 'Last Name', 'Email'
  t << ['TJ',  'Holowaychuk', '[email protected]']
  t << ['Bob', 'Someone',     '[email protected]']
  t << ['Joe', 'Whatever',    '[email protected]']
end
puts user_table

user_table = table do
  self.headings = 'First Name', 'Last Name', 'Email'
  add_row ['TJ',  'Holowaychuk', { :value => '[email protected]', :alignment => :right }]
  add_row ['Bob', 'Someone',     '[email protected]']
  add_row ['Joe', 'Whatever',    '[email protected]']
  align_column 1, :center
end
puts user_table

puts
user_table = table do
  self.headings = ['First Name', 'Last Name', {:value => 'Phones', :colspan => 2, :alignment => :center}]
  add_row ['Bob', 'Someone',     '123', '456']
  add_row ['TJ',  'Holowaychuk', {:value => "No phones", :colspan => 2, :alignment => :center}]
  add_row ['Joe', 'Whatever',    '4324', '343242']
end
puts user_table

rows = []
rows << ['Lines',      100]
rows << ['Comments',   20]
rows << ['Ruby',       70]
rows << ['JavaScript', 30]
puts table([nil, 'Lines'], *rows)

rows = []
rows << ['Lines',      100]
rows << ['Comments',   20]
rows << ['Ruby',       70]
rows << ['JavaScript', 30]
puts table(nil, *rows)

Results:

+---+---+
| a | b |
+---+---+
| 1 | 2 |
| 3 | 4 |
+---+---+
| 4 | 6 |
+---+---+

+---+---+
| a | b |
+---+---+
| 1 | 2 |
| 3 | 4 |
+---+---+
| 4 | 6 |
+---+---+

+------------+-------------+---------------------+
| First Name | Last Name   | Email               |
+------------+-------------+---------------------+
| TJ         | Holowaychuk | [email protected]  |
| Bob        | Someone     | [email protected] |
| Joe        | Whatever    | [email protected] |
+------------+-------------+---------------------+

+------------+-----------------------------+---------------------+
| First Name | Last Name                   | Email               |
+------------+-----------------------------+---------------------+
| TJ         |         Holowaychuk         |  [email protected] |
| Bob        |           Someone           | [email protected] |
| Joe        |          Whatever           | [email protected] |
+------------+-----------------------------+---------------------+

+------------+-------------+-----------+--------+
| First Name | Last Name   |       Phones       |
+------------+-------------+-----------+--------+
| Bob        | Someone     | 123       | 456    |
| TJ         | Holowaychuk |     No phones      |
| Joe        | Whatever    | 4324      | 343242 |
+------------+-------------+-----------+--------+

+------------+-------+
|            | Lines |
+------------+-------+
| Lines      | 100   |
| Comments   | 20    |
| Ruby       | 70    |
| JavaScript | 30    |
+------------+-------+

+------------+-----+
| Lines      | 100 |
| Comments   | 20  |
| Ruby       | 70  |
| JavaScript | 30  |
+------------+-----+

License:

(The MIT License)

Copyright © 2008-2009 TJ Holowaychuk <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, an d/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.