Skip to content
This repository

Ruby ASCII Table Generator, simple and feature rich.

tree: 5aa4b48e72

Fetching latest commit…

Cannot retrieve the latest commit at this time

examples
lib
spec
tasks - Doc
.gitignore
.rvmrc Add .rvmrc and Gemfile
Gemfile Add color support to Cell
History.rdoc Release 1.4.3
Manifest Release 1.3.0
README.rdoc
Rakefile Docs
Todo.rdoc Clean up code
terminal-table.gemspec Release 1.4.3
README.rdoc

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 arbitrary width

  • Supports column span

  • Supports multiline cell content

  • Supports colorized cell content

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

Examples:

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

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

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

user_table = table do |t|
  t.headings = 'First Name', 'Last Name', 'Email'
  t << ['TJ',  'Holowaychuk', 'tj@vision-media.ca']
  t << ['Bob', 'Someone',     'bob@vision-media.ca']
  t << ['Joe', 'Whatever',    'joe@vision-media.ca']
end
puts user_table

user_table = table do
  self.headings = 'First Name', 'Last Name', 'Email'
  add_row ['TJ',  'Holowaychuk', { :value => 'tj@vision-media.ca', :alignment => :right }]
  add_row ['Bob', 'Someone',     'bob@vision-media.ca']
  add_row ['Joe', 'Whatever',    'joe@vision-media.ca']
  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 | tj@vision-media.ca  |
| Bob        | Someone     | bob@vision-media.ca |
| Joe        | Whatever    | joe@vision-media.ca |
+------------+-------------+---------------------+

+------------+-----------------------------+---------------------+
| First Name | Last Name                   | Email               |
+------------+-----------------------------+---------------------+
| TJ         |         Holowaychuk         |  tj@vision-media.ca |
| Bob        |           Someone           | bob@vision-media.ca |
| Joe        |          Whatever           | joe@vision-media.ca |
+------------+-----------------------------+---------------------+

+------------+-------------+-----------+--------+
| 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 <tj@vision-media.ca>

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.

Something went wrong with that request. Please try again.