visionmedia-terminal-table 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.rdoc +4 -0
- data/Manifest +19 -0
- data/README.rdoc +128 -0
- data/Rakefile +15 -0
- data/Todo.rdoc +13 -0
- data/lib/terminal-table/cell.rb +33 -0
- data/lib/terminal-table/core_ext.rb +41 -0
- data/lib/terminal-table/heading.rb +8 -0
- data/lib/terminal-table/import.rb +55 -0
- data/lib/terminal-table/table.rb +146 -0
- data/lib/terminal-table/version.rb +9 -0
- data/lib/terminal-table.rb +30 -0
- data/spec/cell_spec.rb +18 -0
- data/spec/core_ext_spec.rb +18 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/table_spec.rb +153 -0
- data/tasks/docs.rake +13 -0
- data/tasks/gemspec.rake +3 -0
- data/tasks/spec.rake +25 -0
- data/terminal-table.gemspec +34 -0
- metadata +95 -0
data/History.rdoc
ADDED
data/Manifest
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
History.rdoc
|
2
|
+
Rakefile
|
3
|
+
README.rdoc
|
4
|
+
lib/terminal-table.rb
|
5
|
+
lib/terminal-table/core_ext.rb
|
6
|
+
lib/terminal-table/cell.rb
|
7
|
+
lib/terminal-table/heading.rb
|
8
|
+
lib/terminal-table/table.rb
|
9
|
+
lib/terminal-table/version.rb
|
10
|
+
lib/terminal-table/import.rb
|
11
|
+
spec/spec_helper.rb
|
12
|
+
spec/core_ext_spec.rb
|
13
|
+
spec/table_spec.rb
|
14
|
+
spec/cell_spec.rb
|
15
|
+
tasks/docs.rake
|
16
|
+
tasks/gemspec.rake
|
17
|
+
tasks/spec.rake
|
18
|
+
Todo.rdoc
|
19
|
+
Manifest
|
data/README.rdoc
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
|
2
|
+
= Terminal Table
|
3
|
+
|
4
|
+
Simple, feature rich ASCII table generator.
|
5
|
+
|
6
|
+
== Features:
|
7
|
+
|
8
|
+
* Fast
|
9
|
+
* Simple
|
10
|
+
* With or without headings
|
11
|
+
* Alignment of headings, cells, or columns
|
12
|
+
* Easy modification of table chars (+, -, |)
|
13
|
+
|
14
|
+
== Examples:
|
15
|
+
|
16
|
+
require 'rubygems'
|
17
|
+
require 'terminal-table/import'
|
18
|
+
|
19
|
+
puts table(['a', 'b'], [[1, 2], [3, 4]])
|
20
|
+
|
21
|
+
t = table ['a', 'b']
|
22
|
+
t << [1, 2]
|
23
|
+
t << [3, 4]
|
24
|
+
puts t
|
25
|
+
|
26
|
+
user_table = table do |t|
|
27
|
+
t.headings = 'First Name', 'Last Name', 'Email'
|
28
|
+
t << ['TJ', 'Holowaychuk', 'tj@vision-media.ca']
|
29
|
+
t << ['Bob', 'Someone', 'bob@vision-media.ca']
|
30
|
+
t << ['Joe', 'Whatever', 'joe@vision-media.ca']
|
31
|
+
end
|
32
|
+
puts user_table
|
33
|
+
|
34
|
+
user_table = table do
|
35
|
+
self.headings = 'First Name', 'Last Name', 'Email'
|
36
|
+
add_row ['TJ', 'Holowaychuk', { :value => 'tj@vision-media.ca', :align => :right }]
|
37
|
+
add_row ['Bob', 'Someone', 'bob@vision-media.ca']
|
38
|
+
add_row ['Joe', 'Whatever', 'joe@vision-media.ca']
|
39
|
+
align_column 1, :center
|
40
|
+
end
|
41
|
+
puts user_table
|
42
|
+
|
43
|
+
rows = []
|
44
|
+
rows << ['Lines', 100]
|
45
|
+
rows << ['Comments', 20]
|
46
|
+
rows << ['Ruby', 70]
|
47
|
+
rows << ['JavaScript', 30]
|
48
|
+
puts table([nil, 'Lines'], rows)
|
49
|
+
|
50
|
+
rows = []
|
51
|
+
rows << ['Lines', 100]
|
52
|
+
rows << ['Comments', 20]
|
53
|
+
rows << ['Ruby', 70]
|
54
|
+
rows << ['JavaScript', 30]
|
55
|
+
puts table(nil, rows)
|
56
|
+
|
57
|
+
== Results:
|
58
|
+
|
59
|
+
+---+---+
|
60
|
+
| a | b |
|
61
|
+
+---+---+
|
62
|
+
| 1 | 2 |
|
63
|
+
| 3 | 4 |
|
64
|
+
+---+---+
|
65
|
+
|
66
|
+
+---+---+
|
67
|
+
| a | b |
|
68
|
+
+---+---+
|
69
|
+
| 1 | 2 |
|
70
|
+
| 3 | 4 |
|
71
|
+
+---+---+
|
72
|
+
|
73
|
+
+------------+-------------+---------------------+
|
74
|
+
| First Name | Last Name | Email |
|
75
|
+
+------------+-------------+---------------------+
|
76
|
+
| TJ | Holowaychuk | tj@vision-media.ca |
|
77
|
+
| Bob | Someone | bob@vision-media.ca |
|
78
|
+
| Joe | Whatever | joe@vision-media.ca |
|
79
|
+
+------------+-------------+---------------------+
|
80
|
+
|
81
|
+
+------------+-----------------------------+---------------------+
|
82
|
+
| First Name | Last Name | Email |
|
83
|
+
+------------+-----------------------------+---------------------+
|
84
|
+
| TJ | Holowaychuk | tj@vision-media.ca |
|
85
|
+
| Bob | Someone | bob@vision-media.ca |
|
86
|
+
| Joe | Whatever | joe@vision-media.ca |
|
87
|
+
+------------+-----------------------------+---------------------+
|
88
|
+
|
89
|
+
+------------+-------+
|
90
|
+
| | Lines |
|
91
|
+
+------------+-------+
|
92
|
+
| Lines | 100 |
|
93
|
+
| Comments | 20 |
|
94
|
+
| Ruby | 70 |
|
95
|
+
| JavaScript | 30 |
|
96
|
+
+------------+-------+
|
97
|
+
|
98
|
+
+------------+-----+
|
99
|
+
| Lines | 100 |
|
100
|
+
| Comments | 20 |
|
101
|
+
| Ruby | 70 |
|
102
|
+
| JavaScript | 30 |
|
103
|
+
+------------+-----+
|
104
|
+
|
105
|
+
== License:
|
106
|
+
|
107
|
+
(The MIT License)
|
108
|
+
|
109
|
+
Copyright (c) 2008 TJ Holowaychuk <tj@vision-media.ca>
|
110
|
+
|
111
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
112
|
+
a copy of this software and associated documentation files (the
|
113
|
+
'Software'), to deal in the Software without restriction, including
|
114
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
115
|
+
distribute, sublicense, an d/or sell copies of the Software, and to
|
116
|
+
permit persons to whom the Software is furnished to do so, subject to
|
117
|
+
the following conditions:
|
118
|
+
|
119
|
+
The above copyright notice and this permission notice shall be
|
120
|
+
included in all copies or substantial portions of the Software.
|
121
|
+
|
122
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
123
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
124
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
125
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
126
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
127
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
128
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake'
|
4
|
+
require 'echoe'
|
5
|
+
require './lib/terminal-table.rb'
|
6
|
+
|
7
|
+
Echoe.new("terminal-table", Terminal::Table::VERSION::STRING) do |p|
|
8
|
+
p.author = "TJ Holowaychuk"
|
9
|
+
p.email = "tj@vision-media.ca"
|
10
|
+
p.summary = "Simple,"
|
11
|
+
p.url = "http://github.com/visionmedia/terminal-table"
|
12
|
+
p.runtime_dependencies = []
|
13
|
+
end
|
14
|
+
|
15
|
+
Dir['tasks/**/*.rake'].sort.each { |lib| load lib }
|
data/Todo.rdoc
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
module Terminal
|
3
|
+
class Table
|
4
|
+
class Cell
|
5
|
+
|
6
|
+
DEFAULT_ALIGNMENT = :left
|
7
|
+
|
8
|
+
attr_accessor :value, :alignment
|
9
|
+
|
10
|
+
def initialize render_length, initial = nil
|
11
|
+
@render_length = render_length
|
12
|
+
case initial
|
13
|
+
when Hash
|
14
|
+
@value = initial[:value]
|
15
|
+
@alignment = initial[:align] unless initial[:align].nil?
|
16
|
+
when
|
17
|
+
@value = initial
|
18
|
+
@alignment = DEFAULT_ALIGNMENT
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def render
|
23
|
+
" #{value.to_s} ".align alignment, @render_length + 2
|
24
|
+
end
|
25
|
+
alias :to_s :render
|
26
|
+
|
27
|
+
def length
|
28
|
+
value.to_s.length + 2
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
|
2
|
+
class String
|
3
|
+
|
4
|
+
##
|
5
|
+
# Align to +position+, which may be :left, :right, or :center.
|
6
|
+
|
7
|
+
def align position, length
|
8
|
+
send position, length
|
9
|
+
end
|
10
|
+
|
11
|
+
alias_method :left, :ljust
|
12
|
+
alias_method :right, :rjust
|
13
|
+
end
|
14
|
+
|
15
|
+
module Enumerable
|
16
|
+
def map_with_index &block
|
17
|
+
result = []
|
18
|
+
each_with_index do |v, i|
|
19
|
+
result << yield(v, i)
|
20
|
+
end
|
21
|
+
result
|
22
|
+
end
|
23
|
+
alias :collect_with_index :map_with_index
|
24
|
+
end
|
25
|
+
|
26
|
+
class Object
|
27
|
+
|
28
|
+
##
|
29
|
+
# Yields or instance_eval's a +block+ depending on the arity of a block
|
30
|
+
# in order to support both types of block syntax for DSL's.
|
31
|
+
|
32
|
+
def yield_or_eval &block
|
33
|
+
if block_given?
|
34
|
+
if block.arity > 0
|
35
|
+
yield self
|
36
|
+
else
|
37
|
+
self.instance_eval &block
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
|
2
|
+
require 'terminal-table'
|
3
|
+
|
4
|
+
module Kernel
|
5
|
+
|
6
|
+
##
|
7
|
+
# Generates a Terminal::Table object.
|
8
|
+
#
|
9
|
+
# === Examples:
|
10
|
+
#
|
11
|
+
# puts table(['a', 'b'], [[1, 2], [3, 4]])
|
12
|
+
#
|
13
|
+
# # OR
|
14
|
+
#
|
15
|
+
# t = table ['a', 'b']
|
16
|
+
# t << [1, 2]
|
17
|
+
# t << [3, 4]
|
18
|
+
# puts t
|
19
|
+
#
|
20
|
+
# # OR
|
21
|
+
#
|
22
|
+
# user_table = table do |t|
|
23
|
+
# t.headings = 'First Name', 'Last Name', 'Email'
|
24
|
+
# t << ['TJ', 'Holowaychuk', 'tj@vision-media.ca']
|
25
|
+
# t << ['Bob', 'Someone', 'bob@vision-media.ca']
|
26
|
+
# t << ['Joe', 'Whatever', 'joe@vision-media.ca']
|
27
|
+
# end
|
28
|
+
# puts user_table
|
29
|
+
#
|
30
|
+
# # OR
|
31
|
+
#
|
32
|
+
# user_table = table do
|
33
|
+
# self.headings = 'First Name', 'Last Name', 'Email'
|
34
|
+
# add_row ['TJ', 'Holowaychuk', 'tj@vision-media.ca']
|
35
|
+
# add_row ['Bob', 'Someone', 'bob@vision-media.ca']
|
36
|
+
# add_row ['Joe', 'Whatever', 'joe@vision-media.ca']
|
37
|
+
# end
|
38
|
+
# puts user_table
|
39
|
+
#
|
40
|
+
# # OR
|
41
|
+
#
|
42
|
+
# rows = []
|
43
|
+
# rows << ['Lines', 100]
|
44
|
+
# rows << ['Comments', 20]
|
45
|
+
# rows << ['Ruby', 70]
|
46
|
+
# rows << ['JavaScript', 30]
|
47
|
+
# puts table(nil, rows)
|
48
|
+
#
|
49
|
+
|
50
|
+
def table headings = [], rows = [], &block
|
51
|
+
table = Terminal::Table.new :headings => headings, :rows => rows
|
52
|
+
table.yield_or_eval &block
|
53
|
+
table
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
|
2
|
+
module Terminal
|
3
|
+
|
4
|
+
##
|
5
|
+
# Generates an ASCII table for terminal usage.
|
6
|
+
#
|
7
|
+
# This class can be used in many ways, however ultimately you should
|
8
|
+
# require 'terminal-table/import' (which requires terminal-table itself) to use
|
9
|
+
# the Kernel helper method which is easier, and cleaner to use. View Kernel#table
|
10
|
+
# for examples.
|
11
|
+
#
|
12
|
+
# === Examples:
|
13
|
+
#
|
14
|
+
# table = Terminal::Table.new
|
15
|
+
# table.headings = ['Characters', { :value => 'Nums', :align => :right }]
|
16
|
+
# table << [{ :value => 'a', :align => :center }, 1]
|
17
|
+
# table << ['b', 222222222222222]
|
18
|
+
# table << ['c', 3]
|
19
|
+
# puts table.render # or simply puts table
|
20
|
+
#
|
21
|
+
# +------------+-----------------+
|
22
|
+
# | Characters | Nums |
|
23
|
+
# +------------+-----------------+
|
24
|
+
# | a | 1 |
|
25
|
+
# | b | 222222222222222 |
|
26
|
+
# | c | 3 |
|
27
|
+
# +------------+-----------------+
|
28
|
+
#
|
29
|
+
|
30
|
+
class Table
|
31
|
+
|
32
|
+
##
|
33
|
+
# Table characters, x axis, y axis, and intersection.
|
34
|
+
|
35
|
+
X, Y, I = '-', '|', '+'
|
36
|
+
|
37
|
+
attr_accessor :headings, :rows
|
38
|
+
|
39
|
+
##
|
40
|
+
# Generates a ASCII table.
|
41
|
+
|
42
|
+
def initialize options = {}
|
43
|
+
@headings = options.delete(:headings) || []
|
44
|
+
@rows = options.delete(:rows) || []
|
45
|
+
end
|
46
|
+
|
47
|
+
##
|
48
|
+
# Render the table. Often you will simply call _puts_ with an
|
49
|
+
# instance in order to output it to the terminal.
|
50
|
+
|
51
|
+
def render
|
52
|
+
sep = seperator
|
53
|
+
s = sep + "\n"
|
54
|
+
s << Y + headings.collect_with_index { |h, i| Heading.new(length_of_column(i), h).render }.join(Y) + Y if has_headings?
|
55
|
+
s << "\n" + sep + "\n" if has_headings?
|
56
|
+
s << rows.collect { |row| Y + row.collect_with_index { |c, i| Cell.new(length_of_column(i), c).render }.join(Y) + Y }.join("\n")
|
57
|
+
s << "\n" + sep + "\n"
|
58
|
+
end
|
59
|
+
alias :to_s :render
|
60
|
+
|
61
|
+
##
|
62
|
+
# Create a seperator based on colum lengths.
|
63
|
+
|
64
|
+
def seperator
|
65
|
+
I + columns.collect_with_index { |col, i| X * (length_of_column(i) + 2) }.join(I) + I
|
66
|
+
end
|
67
|
+
|
68
|
+
##
|
69
|
+
# Add a row.
|
70
|
+
|
71
|
+
def add_row row
|
72
|
+
rows << row
|
73
|
+
end
|
74
|
+
alias :<< :add_row
|
75
|
+
|
76
|
+
##
|
77
|
+
# Weither or not any headings are present, since they are optional.
|
78
|
+
|
79
|
+
def has_headings?
|
80
|
+
!headings.empty?
|
81
|
+
end
|
82
|
+
|
83
|
+
##
|
84
|
+
# Return +n+ column.
|
85
|
+
|
86
|
+
def column n
|
87
|
+
rows.collect { |row| row[n] }.compact
|
88
|
+
end
|
89
|
+
|
90
|
+
##
|
91
|
+
# Return +n+ column including headings.
|
92
|
+
|
93
|
+
def column_with_headings n
|
94
|
+
headings_with_rows.collect { |row| row[n] }.compact
|
95
|
+
end
|
96
|
+
|
97
|
+
##
|
98
|
+
# Return columns.
|
99
|
+
|
100
|
+
def columns
|
101
|
+
(0..number_of_columns-1).collect { |n| column n }
|
102
|
+
end
|
103
|
+
|
104
|
+
##
|
105
|
+
# Return the largest cell found within column +n+.
|
106
|
+
|
107
|
+
def largest_cell_in_column n
|
108
|
+
column_with_headings(n).sort_by { |c| Cell.new(0, c).length }.last
|
109
|
+
end
|
110
|
+
|
111
|
+
##
|
112
|
+
# Return length of column +n+.
|
113
|
+
|
114
|
+
def length_of_column n
|
115
|
+
largest_cell_in_column(n).to_s.length
|
116
|
+
end
|
117
|
+
|
118
|
+
##
|
119
|
+
# Return total number of columns available.
|
120
|
+
|
121
|
+
def number_of_columns
|
122
|
+
rows[0].length
|
123
|
+
end
|
124
|
+
|
125
|
+
##
|
126
|
+
# Align column +n+ to +alignment+ of :center, :left, or :right.
|
127
|
+
|
128
|
+
def align_column n, alignment
|
129
|
+
column(n).each_with_index do |c, i|
|
130
|
+
unless c.is_a? Hash
|
131
|
+
@rows[i][n] = { :value => c, :align => alignment }
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
private
|
137
|
+
|
138
|
+
##
|
139
|
+
# Return headings combined with rows.
|
140
|
+
|
141
|
+
def headings_with_rows
|
142
|
+
[headings] + rows
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2008 TJ Holowaychuk
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#++
|
23
|
+
|
24
|
+
$:.unshift File.dirname(__FILE__)
|
25
|
+
|
26
|
+
require 'terminal-table/version'
|
27
|
+
require 'terminal-table/core_ext'
|
28
|
+
require 'terminal-table/table'
|
29
|
+
require 'terminal-table/cell'
|
30
|
+
require 'terminal-table/heading'
|
data/spec/cell_spec.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
|
2
|
+
describe Terminal::Table do
|
3
|
+
|
4
|
+
Cell = Terminal::Table::Cell
|
5
|
+
|
6
|
+
it "should initialize with a string" do
|
7
|
+
cell = Cell.new 0, 'Foo'
|
8
|
+
cell.value.should == 'Foo'
|
9
|
+
cell.alignment.should == :left
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should initialize with a hash" do
|
13
|
+
cell = Cell.new 0, :value => 'Bar', :align => :right
|
14
|
+
cell.value.should == 'Bar'
|
15
|
+
cell.alignment.should == :right
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
|
2
|
+
describe String do
|
3
|
+
describe "#align" do
|
4
|
+
|
5
|
+
it "should center" do
|
6
|
+
'foo'.align(:center, 10).should == ' foo '
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should align left" do
|
10
|
+
'foo'.align(:left, 10).should == 'foo '
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should align right" do
|
14
|
+
'foo'.align(:right, 10).should == ' foo'
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/spec/table_spec.rb
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
|
2
|
+
describe Terminal::Table do
|
3
|
+
|
4
|
+
before :each do
|
5
|
+
@table = Terminal::Table.new
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should select columns" do
|
9
|
+
@table << ['foo', 'bar']
|
10
|
+
@table << ['big foo', 'big foo bar']
|
11
|
+
@table.column(1).should == ['bar', 'big foo bar']
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should count columns" do
|
15
|
+
@table << [1, 2, 3]
|
16
|
+
@table.number_of_columns.should == 3
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should iterate columns" do
|
20
|
+
@table << [1, 2, 3]
|
21
|
+
@table << [4, 5, 6]
|
22
|
+
@table.columns.should == [[1, 4], [2, 5], [3, 6]]
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should select columns" do
|
26
|
+
@table.headings = ['one', 'two']
|
27
|
+
@table << ['foo', 'bar']
|
28
|
+
@table << ['big foo', 'big foo bar']
|
29
|
+
@table.column(1).should == ['bar', 'big foo bar']
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should select columns when using hashes" do
|
33
|
+
@table.headings = ['one', 'two']
|
34
|
+
@table.rows = [[{ :value => 'a', :align => :left }, 1], ['b', 2], ['c', 3]]
|
35
|
+
@table.column(0).should == [{ :value => 'a', :align => :left }, 'b', 'c']
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should select largest cell in a column" do
|
39
|
+
@table << ['foo', 'bar']
|
40
|
+
@table << ['big foo', 'big foo bar']
|
41
|
+
@table.largest_cell_in_column(1).should == 'big foo bar'
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should find column length" do
|
45
|
+
@table << ['foo', 'bar', 'a']
|
46
|
+
@table << ['big foo', 'big foo bar']
|
47
|
+
@table.length_of_column(1).should == 11
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should find column length with headings" do
|
51
|
+
@table.headings = ['one', 'super long heading']
|
52
|
+
@table << ['foo', 'bar', 'a']
|
53
|
+
@table << ['big foo', 'big foo bar']
|
54
|
+
@table.length_of_column(1).should == 18
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should render seperators" do
|
58
|
+
@table.headings = ['Char', 'Num']
|
59
|
+
@table << ['a', 1]
|
60
|
+
@table.seperator.should == '+------+-----+'
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should render properly" do
|
64
|
+
@table.headings = ['Char', 'Num']
|
65
|
+
@table << ['a', 1]
|
66
|
+
@table << ['b', 2]
|
67
|
+
@table << ['c', 3]
|
68
|
+
@table.render.should == <<-EOF.deindent
|
69
|
+
+------+-----+
|
70
|
+
| Char | Num |
|
71
|
+
+------+-----+
|
72
|
+
| a | 1 |
|
73
|
+
| b | 2 |
|
74
|
+
| c | 3 |
|
75
|
+
+------+-----+
|
76
|
+
EOF
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should render properly without headings" do
|
80
|
+
@table << ['a', 1]
|
81
|
+
@table << ['b', 2]
|
82
|
+
@table << ['c', 3]
|
83
|
+
@table.render.should == <<-EOF.deindent
|
84
|
+
+---+---+
|
85
|
+
| a | 1 |
|
86
|
+
| b | 2 |
|
87
|
+
| c | 3 |
|
88
|
+
+---+---+
|
89
|
+
EOF
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should allows a hash of options for creation" do
|
93
|
+
headings = ['Char', 'Num']
|
94
|
+
rows = [['a', 1], ['b', 2], ['c', 3]]
|
95
|
+
Terminal::Table.new(:rows => rows, :headings => headings).render.should == <<-EOF.deindent
|
96
|
+
+------+-----+
|
97
|
+
| Char | Num |
|
98
|
+
+------+-----+
|
99
|
+
| a | 1 |
|
100
|
+
| b | 2 |
|
101
|
+
| c | 3 |
|
102
|
+
+------+-----+
|
103
|
+
EOF
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should flex for large cells" do
|
107
|
+
@table.headings = ['Just some characters', 'Num']
|
108
|
+
@table.rows = [['a', 1], ['b', 2], ['c', 3]]
|
109
|
+
@table.render.should == <<-EOF.deindent
|
110
|
+
+----------------------+-----+
|
111
|
+
| Just some characters | Num |
|
112
|
+
+----------------------+-----+
|
113
|
+
| a | 1 |
|
114
|
+
| b | 2 |
|
115
|
+
| c | 3 |
|
116
|
+
+----------------------+-----+
|
117
|
+
EOF
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should allow alignment of headings and cells" do
|
121
|
+
@table.headings = ['Characters', { :value => 'Nums', :align => :right }]
|
122
|
+
@table << [{ :value => 'a', :align => :center }, 1]
|
123
|
+
@table << ['b', 222222222222222]
|
124
|
+
@table << ['c', 3]
|
125
|
+
@table.render.should == <<-EOF.deindent
|
126
|
+
+------------+-----------------+
|
127
|
+
| Characters | Nums |
|
128
|
+
+------------+-----------------+
|
129
|
+
| a | 1 |
|
130
|
+
| b | 222222222222222 |
|
131
|
+
| c | 3 |
|
132
|
+
+------------+-----------------+
|
133
|
+
EOF
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should align columns, but allow specifics to remain" do
|
138
|
+
@table.headings = ['Just some characters', 'Num']
|
139
|
+
@table.rows = [[{ :value => 'a', :align => :left }, 1], ['b', 2], ['c', 3]]
|
140
|
+
@table.align_column 0, :center
|
141
|
+
@table.align_column 1, :right
|
142
|
+
@table.render.should == <<-EOF.deindent
|
143
|
+
+----------------------+-----+
|
144
|
+
| Just some characters | Num |
|
145
|
+
+----------------------+-----+
|
146
|
+
| a | 1 |
|
147
|
+
| b | 2 |
|
148
|
+
| c | 3 |
|
149
|
+
+----------------------+-----+
|
150
|
+
EOF
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
data/tasks/docs.rake
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
|
2
|
+
namespace :docs do
|
3
|
+
|
4
|
+
desc 'Remove rdoc products'
|
5
|
+
task :remove => [:clobber_docs]
|
6
|
+
|
7
|
+
desc 'Build docs, and open in browser for viewing (specify BROWSER)'
|
8
|
+
task :open => [:docs] do
|
9
|
+
browser = ENV["BROWSER"] || "safari"
|
10
|
+
sh "open -a #{browser} doc/index.html"
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
data/tasks/gemspec.rake
ADDED
data/tasks/spec.rake
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
|
2
|
+
require 'spec/rake/spectask'
|
3
|
+
|
4
|
+
desc "Run all specifications"
|
5
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
6
|
+
t.libs << "lib"
|
7
|
+
t.spec_opts = ["--color", "--require", "spec/spec_helper.rb"]
|
8
|
+
end
|
9
|
+
|
10
|
+
namespace :spec do
|
11
|
+
|
12
|
+
desc "Run all specifications verbosely"
|
13
|
+
Spec::Rake::SpecTask.new(:verbose) do |t|
|
14
|
+
t.libs << "lib"
|
15
|
+
t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
|
16
|
+
end
|
17
|
+
|
18
|
+
desc "Run specific specification verbosely (specify SPEC)"
|
19
|
+
Spec::Rake::SpecTask.new(:select) do |t|
|
20
|
+
t.libs << "lib"
|
21
|
+
t.spec_files = [ENV["SPEC"]]
|
22
|
+
t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{terminal-table}
|
5
|
+
s.version = "1.0.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["TJ Holowaychuk"]
|
9
|
+
s.date = %q{2009-01-15}
|
10
|
+
s.description = %q{Simple,}
|
11
|
+
s.email = %q{tj@vision-media.ca}
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/terminal-table.rb", "lib/terminal-table/core_ext.rb", "lib/terminal-table/cell.rb", "lib/terminal-table/heading.rb", "lib/terminal-table/table.rb", "lib/terminal-table/version.rb", "lib/terminal-table/import.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
|
13
|
+
s.files = ["History.rdoc", "Rakefile", "README.rdoc", "lib/terminal-table.rb", "lib/terminal-table/core_ext.rb", "lib/terminal-table/cell.rb", "lib/terminal-table/heading.rb", "lib/terminal-table/table.rb", "lib/terminal-table/version.rb", "lib/terminal-table/import.rb", "spec/spec_helper.rb", "spec/core_ext_spec.rb", "spec/table_spec.rb", "spec/cell_spec.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake", "Todo.rdoc", "Manifest", "terminal-table.gemspec"]
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.homepage = %q{http://github.com/visionmedia/terminal-table}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Terminal-table", "--main", "README.rdoc"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{terminal-table}
|
19
|
+
s.rubygems_version = %q{1.3.1}
|
20
|
+
s.summary = %q{Simple,}
|
21
|
+
|
22
|
+
if s.respond_to? :specification_version then
|
23
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
24
|
+
s.specification_version = 2
|
25
|
+
|
26
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
27
|
+
s.add_development_dependency(%q<echoe>, [">= 0"])
|
28
|
+
else
|
29
|
+
s.add_dependency(%q<echoe>, [">= 0"])
|
30
|
+
end
|
31
|
+
else
|
32
|
+
s.add_dependency(%q<echoe>, [">= 0"])
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: visionmedia-terminal-table
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- TJ Holowaychuk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-15 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: echoe
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
version:
|
24
|
+
description: Simple,
|
25
|
+
email: tj@vision-media.ca
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- README.rdoc
|
32
|
+
- lib/terminal-table.rb
|
33
|
+
- lib/terminal-table/core_ext.rb
|
34
|
+
- lib/terminal-table/cell.rb
|
35
|
+
- lib/terminal-table/heading.rb
|
36
|
+
- lib/terminal-table/table.rb
|
37
|
+
- lib/terminal-table/version.rb
|
38
|
+
- lib/terminal-table/import.rb
|
39
|
+
- tasks/docs.rake
|
40
|
+
- tasks/gemspec.rake
|
41
|
+
- tasks/spec.rake
|
42
|
+
files:
|
43
|
+
- History.rdoc
|
44
|
+
- Rakefile
|
45
|
+
- README.rdoc
|
46
|
+
- lib/terminal-table.rb
|
47
|
+
- lib/terminal-table/core_ext.rb
|
48
|
+
- lib/terminal-table/cell.rb
|
49
|
+
- lib/terminal-table/heading.rb
|
50
|
+
- lib/terminal-table/table.rb
|
51
|
+
- lib/terminal-table/version.rb
|
52
|
+
- lib/terminal-table/import.rb
|
53
|
+
- spec/spec_helper.rb
|
54
|
+
- spec/core_ext_spec.rb
|
55
|
+
- spec/table_spec.rb
|
56
|
+
- spec/cell_spec.rb
|
57
|
+
- tasks/docs.rake
|
58
|
+
- tasks/gemspec.rake
|
59
|
+
- tasks/spec.rake
|
60
|
+
- Todo.rdoc
|
61
|
+
- Manifest
|
62
|
+
- terminal-table.gemspec
|
63
|
+
has_rdoc: true
|
64
|
+
homepage: http://github.com/visionmedia/terminal-table
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options:
|
67
|
+
- --line-numbers
|
68
|
+
- --inline-source
|
69
|
+
- --title
|
70
|
+
- Terminal-table
|
71
|
+
- --main
|
72
|
+
- README.rdoc
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: "0"
|
80
|
+
version:
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: "1.2"
|
86
|
+
version:
|
87
|
+
requirements: []
|
88
|
+
|
89
|
+
rubyforge_project: terminal-table
|
90
|
+
rubygems_version: 1.2.0
|
91
|
+
signing_key:
|
92
|
+
specification_version: 2
|
93
|
+
summary: Simple,
|
94
|
+
test_files: []
|
95
|
+
|