smtlaissezfaire-terminal-table 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/History.rdoc ADDED
@@ -0,0 +1,14 @@
1
+
2
+ === 1.0.5 / 2009-03-14
3
+
4
+ * Allowing nil to be passed to table for headings
5
+ * Revised doc to show that rows can be splatted now
6
+ * Misc refactoring
7
+
8
+ === 1.0.3 / 2009-01-15
9
+
10
+ * Moved yield or eval to Terminal::Table initialize where it belongs
11
+
12
+ === 1.0.0 / 2009-01-13
13
+
14
+ * Initial release
data/Manifest ADDED
@@ -0,0 +1,25 @@
1
+ examples/examples.rb
2
+ History.rdoc
3
+ lib/terminal-table/cell.rb
4
+ lib/terminal-table/core_ext.rb
5
+ lib/terminal-table/heading.rb
6
+ lib/terminal-table/import.rb
7
+ lib/terminal-table/table.rb
8
+ lib/terminal-table/table_helper.rb
9
+ lib/terminal-table/version.rb
10
+ lib/terminal-table.rb
11
+ Manifest
12
+ Rakefile
13
+ README.rdoc
14
+ spec/cell_spec.rb
15
+ spec/core_ext_spec.rb
16
+ spec/import_spec.rb
17
+ spec/spec.opts
18
+ spec/spec_helper.rb
19
+ spec/table_helper_spec.rb
20
+ spec/table_spec.rb
21
+ tasks/docs.rake
22
+ tasks/gemspec.rake
23
+ tasks/spec.rake
24
+ terminal-table.gemspec
25
+ Todo.rdoc
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
+ * Optional headings
11
+ * Alignment of columns, headings, or cells
12
+ * Easy modification of table strings (+, -, |)
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-2009 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) 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,15 @@
1
+
2
+ == Major:
3
+
4
+ * Nothing
5
+
6
+ == Minor:
7
+
8
+ * Programmatically add seperator rows
9
+ * Arbitrary dimensions
10
+ * Add multi-column sorting
11
+ * Change; pre-create Cell and Heading objects to clean up Table a bit
12
+
13
+ == Brainstorming:
14
+
15
+ * Nothing
@@ -0,0 +1,57 @@
1
+
2
+ $:.unshift File.dirname(__FILE__) + '/../lib'
3
+ require 'terminal-table/import'
4
+
5
+ puts
6
+ puts table(['a', 'b'], [1, 2], [3, 4])
7
+
8
+ puts
9
+ t = table ['a', 'b']
10
+ t << [1, 2]
11
+ t << [3, 4]
12
+ puts t
13
+
14
+ puts
15
+ user_table = table do |t|
16
+ t.headings = 'First Name', 'Last Name', 'Email'
17
+ t << %w( TJ Holowaychuk tj@vision-media.ca )
18
+ t << %w( Bob Someone bob@vision-media.ca )
19
+ t << %w( Joe Whatever bob@vision-media.ca )
20
+ end
21
+ puts user_table
22
+
23
+ puts
24
+ user_table = table do
25
+ self.headings = 'First Name', 'Last Name', 'Email'
26
+ add_row ['TJ', 'Holowaychuk', ['tj@vision-media.ca', :right]]
27
+ add_row ['Bob', 'Someone', 'bob@vision-media.ca']
28
+ add_row ['Joe', 'Whatever', 'joe@vision-media.ca']
29
+ align_column 1, :center
30
+ end
31
+ puts user_table
32
+
33
+ rows = []
34
+ rows << ['Lines', 100]
35
+ rows << ['Comments', 20]
36
+ rows << ['Ruby', 70]
37
+ rows << ['JavaScript', 30]
38
+ puts table([nil, 'Lines'], *rows)
39
+
40
+ rows = []
41
+ rows << ['Lines', 100]
42
+ rows << ['Comments', 20]
43
+ rows << ['Ruby', 70]
44
+ rows << ['JavaScript', 30]
45
+ puts table(nil, *rows)
46
+
47
+
48
+ Terminal::Table::X = '='
49
+ Terminal::Table::Y = '!'
50
+ Terminal::Table::I = '*'
51
+
52
+ rows = []
53
+ rows << [nil, nil, nil]
54
+ rows << [nil, ':)', nil]
55
+ rows << [nil, nil, nil]
56
+ puts table(nil, *rows)
57
+
@@ -0,0 +1,31 @@
1
+ #--
2
+ # Copyright (c) 2008-2009 TJ Holowaychuk <tj@vision-media.ca>
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'
31
+ require 'terminal-table/table_helper'
@@ -0,0 +1,23 @@
1
+
2
+ module Terminal
3
+ class Table
4
+ class Cell
5
+
6
+ attr_accessor :value, :alignment
7
+
8
+ def initialize width, value = nil, alignment = :left
9
+ @width, @alignment, @value = width, alignment, value
10
+ end
11
+
12
+ def render
13
+ " #{value.to_s} ".align alignment, @width + 2
14
+ end
15
+ alias :to_s :render
16
+
17
+ def length
18
+ value.to_s.length + 2
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,40 @@
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
+ alias_method :left, :ljust
11
+ alias_method :right, :rjust
12
+
13
+ end
14
+
15
+ module Enumerable
16
+ def map_with_index &block
17
+ result = []
18
+ each_with_index { |v, i| result << yield(v, i) }
19
+ result
20
+ end
21
+ alias :collect_with_index :map_with_index
22
+ end
23
+
24
+ class Object
25
+
26
+ ##
27
+ # Yields or instance_eval's a +block+ depending on the arity of a block
28
+ # in order to support both types of block syntax for DSL's.
29
+
30
+ def yield_or_eval &block
31
+ if block_given?
32
+ if block.arity > 0
33
+ yield self
34
+ else
35
+ self.instance_eval &block
36
+ end
37
+ end
38
+ end
39
+
40
+ end
@@ -0,0 +1,10 @@
1
+
2
+ module Terminal
3
+ class Table
4
+ class Heading < Cell
5
+ def initialize width, value = nil, alignment = :center
6
+ super
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ require 'terminal-table'
2
+
3
+ class Object
4
+ include Terminal::Table::TableHelper
5
+ end
@@ -0,0 +1,159 @@
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
+ # Exceptions
34
+ #++
35
+
36
+ class Error < StandardError; end
37
+
38
+ ##
39
+ # Table characters, x axis, y axis, and intersection.
40
+
41
+ X, Y, I = '-', '|', '+'
42
+
43
+ attr_accessor :headings, :rows
44
+
45
+ ##
46
+ # Generates a ASCII table.
47
+
48
+ def initialize options = {}, &block
49
+ @headings = options.fetch :headings, []
50
+ @rows = options.fetch :rows, []
51
+ yield_or_eval &block if block_given?
52
+ end
53
+
54
+ ##
55
+ # Render the table. Often you will simply call _puts_ with an
56
+ # instance in order to output it to the terminal.
57
+
58
+ def render
59
+ buffer = seperator << "\n"
60
+ if has_headings?
61
+ buffer << Y + headings.map_with_index do |heading, i|
62
+ Heading.new(length_of_column(i), *heading).render
63
+ end.join(Y) + Y
64
+ buffer << "\n#{seperator}\n"
65
+ end
66
+ buffer << rows.map do |row|
67
+ Y + row.map_with_index do |cell, i|
68
+ Cell.new(length_of_column(i), *cell).render
69
+ end.join(Y) + Y
70
+ end.join("\n")
71
+ buffer << "\n#{seperator}\n"
72
+ end
73
+ alias :to_s :render
74
+
75
+ ##
76
+ # Create a seperator based on colum lengths.
77
+
78
+ def seperator
79
+ I + columns.collect_with_index do |col, i|
80
+ X * (length_of_column(i) + 2)
81
+ end.join(I) + I
82
+ end
83
+
84
+ ##
85
+ # Add a row.
86
+
87
+ def add_row row
88
+ rows << row
89
+ end
90
+ alias :<< :add_row
91
+
92
+ ##
93
+ # Weither or not any headings are present, since they are optional.
94
+
95
+ def has_headings?
96
+ !headings.empty?
97
+ end
98
+
99
+ ##
100
+ # Return +n+ column.
101
+
102
+ def column n
103
+ rows.map { |row| row[n] }.compact
104
+ end
105
+
106
+ ##
107
+ # Return +n+ column including headings.
108
+
109
+ def column_with_headings n
110
+ headings_with_rows.map { |row| row[n] }.compact
111
+ end
112
+
113
+ ##
114
+ # Return columns.
115
+
116
+ def columns
117
+ (0..number_of_columns-1).map { |n| column n }
118
+ end
119
+
120
+ ##
121
+ # Return the largest cell found within column +n+.
122
+
123
+ def largest_cell_in_column n
124
+ column_with_headings(n).sort_by { |cell| Cell.new(0, *cell).length }.last
125
+ end
126
+
127
+ ##
128
+ # Return length of column +n+.
129
+
130
+ def length_of_column n
131
+ largest_cell_in_column(n).to_s.length
132
+ end
133
+
134
+ ##
135
+ # Return total number of columns available.
136
+
137
+ def number_of_columns
138
+ return rows.first.length unless rows.empty?
139
+ raise Error, 'your table needs some rows.'
140
+ end
141
+
142
+ ##
143
+ # Align column +n+ to +alignment+ of :center, :left, or :right.
144
+
145
+ def align_column n, alignment
146
+ column(n).each_with_index do |col, i|
147
+ @rows[i][n] = [col, alignment] unless col.is_a? Array
148
+ end
149
+ end
150
+
151
+ ##
152
+ # Return headings combined with rows.
153
+
154
+ def headings_with_rows
155
+ [headings] + rows
156
+ end
157
+
158
+ end
159
+ end
@@ -0,0 +1,53 @@
1
+ module Terminal
2
+ class Table
3
+ module TableHelper
4
+ ##
5
+ # Generates a Terminal::Table object.
6
+ #
7
+ # === Examples:
8
+ #
9
+ # puts table(['a', 'b'], [1, 2], [3, 4])
10
+ #
11
+ # # OR
12
+ #
13
+ # t = table ['a', 'b']
14
+ # t << [1, 2]
15
+ # t << [3, 4]
16
+ # puts t
17
+ #
18
+ # # OR
19
+ #
20
+ # user_table = table do |t|
21
+ # t.headings = 'First Name', 'Last Name', 'Email'
22
+ # t << ['TJ', 'Holowaychuk', 'tj@vision-media.ca']
23
+ # t << ['Bob', 'Someone', 'bob@vision-media.ca']
24
+ # t << ['Joe', 'Whatever', 'joe@vision-media.ca']
25
+ # end
26
+ # puts user_table
27
+ #
28
+ # # OR
29
+ #
30
+ # user_table = table do
31
+ # self.headings = 'First Name', 'Last Name', 'Email'
32
+ # add_row ['TJ', 'Holowaychuk', 'tj@vision-media.ca']
33
+ # add_row ['Bob', 'Someone', 'bob@vision-media.ca']
34
+ # add_row ['Joe', 'Whatever', 'joe@vision-media.ca']
35
+ # end
36
+ # puts user_table
37
+ #
38
+ # # OR
39
+ #
40
+ # rows = []
41
+ # rows << ['Lines', 100]
42
+ # rows << ['Comments', 20]
43
+ # rows << ['Ruby', 70]
44
+ # rows << ['JavaScript', 30]
45
+ # puts table(nil, *rows)
46
+ #
47
+
48
+ def table headings = [], *rows, &block
49
+ Terminal::Table.new :headings => headings.to_a, :rows => rows, &block
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,6 @@
1
+
2
+ module Terminal
3
+ class Table
4
+ VERSION = '1.0.5'
5
+ end
6
+ end
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 default alignment to the left" do
7
+ cell = Cell.new 0, 'foo'
8
+ cell.value.should == 'foo'
9
+ cell.alignment.should == :left
10
+ end
11
+
12
+ it "should override alignment" do
13
+ cell = Cell.new 0, 'foo', :center
14
+ cell.value.should == 'foo'
15
+ cell.alignment.should == :center
16
+ end
17
+
18
+ end
@@ -0,0 +1,18 @@
1
+
2
+ describe String do
3
+
4
+ describe "#align" do
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
+ end
17
+
18
+ end
@@ -0,0 +1,11 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
2
+ require "terminal-table/import"
3
+
4
+ describe Kernel do
5
+ describe "#table" do
6
+ it "should allow creation of a terminal table" do
7
+ table(['foo', 'bar'], ['a', 'b'], [1, 2]).
8
+ should be_instance_of(Terminal::Table)
9
+ end
10
+ end
11
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,8 @@
1
+
2
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/terminal-table")
3
+
4
+ class String
5
+ def deindent
6
+ gsub /^ */, ''
7
+ end
8
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
2
+
3
+ module Terminal
4
+ describe Table::TableHelper do
5
+ before do
6
+ @obj = Class.new do
7
+ include Table::TableHelper
8
+ end.new
9
+ end
10
+
11
+ describe "#table" do
12
+ it "should allow creation of a terminal table" do
13
+ table = @obj.table(['foo', 'bar'], ['a', 'b'], [1, 2])
14
+ table.should be_instance_of(Terminal::Table)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,187 @@
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 bitch and complain when you have no rows" do
64
+ lambda { @table.render }.should raise_error(Terminal::Table::Error)
65
+ end
66
+
67
+ it "should render properly" do
68
+ @table.headings = ['Char', 'Num']
69
+ @table << ['a', 1]
70
+ @table << ['b', 2]
71
+ @table << ['c', 3]
72
+ @table.render.should == <<-EOF.deindent
73
+ +------+-----+
74
+ | Char | Num |
75
+ +------+-----+
76
+ | a | 1 |
77
+ | b | 2 |
78
+ | c | 3 |
79
+ +------+-----+
80
+ EOF
81
+ end
82
+
83
+ it "should render properly without headings" do
84
+ @table << ['a', 1]
85
+ @table << ['b', 2]
86
+ @table << ['c', 3]
87
+ @table.render.should == <<-EOF.deindent
88
+ +---+---+
89
+ | a | 1 |
90
+ | b | 2 |
91
+ | c | 3 |
92
+ +---+---+
93
+ EOF
94
+ end
95
+
96
+ it "should render properly using block syntax" do
97
+ table = Terminal::Table.new do |t|
98
+ t << ['a', 1]
99
+ t << ['b', 2]
100
+ t << ['c', 3]
101
+ end
102
+ table.render.should == <<-EOF.deindent
103
+ +---+---+
104
+ | a | 1 |
105
+ | b | 2 |
106
+ | c | 3 |
107
+ +---+---+
108
+ EOF
109
+ end
110
+
111
+ it "should render properly using instance_eval block syntax" do
112
+ table = Terminal::Table.new do
113
+ add_row ['a', 1]
114
+ add_row ['b', 2]
115
+ add_row ['c', 3]
116
+ end
117
+ table.render.should == <<-EOF.deindent
118
+ +---+---+
119
+ | a | 1 |
120
+ | b | 2 |
121
+ | c | 3 |
122
+ +---+---+
123
+ EOF
124
+ end
125
+
126
+ it "should allows a hash of options for creation" do
127
+ headings = ['Char', 'Num']
128
+ rows = [['a', 1], ['b', 2], ['c', 3]]
129
+ Terminal::Table.new(:rows => rows, :headings => headings).render.should == <<-EOF.deindent
130
+ +------+-----+
131
+ | Char | Num |
132
+ +------+-----+
133
+ | a | 1 |
134
+ | b | 2 |
135
+ | c | 3 |
136
+ +------+-----+
137
+ EOF
138
+ end
139
+
140
+ it "should flex for large cells" do
141
+ @table.headings = ['Just some characters', 'Num']
142
+ @table.rows = [['a', 1], ['b', 2], ['c', 3]]
143
+ @table.render.should == <<-EOF.deindent
144
+ +----------------------+-----+
145
+ | Just some characters | Num |
146
+ +----------------------+-----+
147
+ | a | 1 |
148
+ | b | 2 |
149
+ | c | 3 |
150
+ +----------------------+-----+
151
+ EOF
152
+ end
153
+
154
+ it "should allow alignment of headings and cells" do
155
+ @table.headings = ['Characters', ['Nums', :right ]]
156
+ @table << [['a', :center ], 1]
157
+ @table << ['b', 222222222222222]
158
+ @table << ['c', 3]
159
+ @table.render.should == <<-EOF.deindent
160
+ +------------+-----------------+
161
+ | Characters | Nums |
162
+ +------------+-----------------+
163
+ | a | 1 |
164
+ | b | 222222222222222 |
165
+ | c | 3 |
166
+ +------------+-----------------+
167
+ EOF
168
+
169
+ end
170
+
171
+ it "should align columns, but allow specifics to remain" do
172
+ @table.headings = ['Just some characters', 'Num']
173
+ @table.rows = [[['a', :left], 1], ['b', 2], ['c', 3]]
174
+ @table.align_column 0, :center
175
+ @table.align_column 1, :right
176
+ @table.render.should == <<-EOF.deindent
177
+ +----------------------+-----+
178
+ | Just some characters | Num |
179
+ +----------------------+-----+
180
+ | a | 1 |
181
+ | b | 2 |
182
+ | c | 3 |
183
+ +----------------------+-----+
184
+ EOF
185
+ end
186
+
187
+ 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
@@ -0,0 +1,3 @@
1
+
2
+ desc 'Build gemspec file'
3
+ task :gemspec => [:build_gemspec]
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,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{terminal-table}
5
+ s.version = "1.0.5"
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-07-12}
10
+ s.description = %q{Simple,}
11
+ s.email = %q{tj@vision-media.ca}
12
+ s.extra_rdoc_files = ["lib/terminal-table/cell.rb", "lib/terminal-table/core_ext.rb", "lib/terminal-table/heading.rb", "lib/terminal-table/import.rb", "lib/terminal-table/table.rb", "lib/terminal-table/table_helper.rb", "lib/terminal-table/version.rb", "lib/terminal-table.rb", "README.rdoc", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
13
+ s.files = ["examples/examples.rb", "History.rdoc", "lib/terminal-table/cell.rb", "lib/terminal-table/core_ext.rb", "lib/terminal-table/heading.rb", "lib/terminal-table/import.rb", "lib/terminal-table/table.rb", "lib/terminal-table/table_helper.rb", "lib/terminal-table/version.rb", "lib/terminal-table.rb", "Manifest", "Rakefile", "README.rdoc", "spec/cell_spec.rb", "spec/core_ext_spec.rb", "spec/import_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "spec/table_helper_spec.rb", "spec/table_spec.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake", "terminal-table.gemspec", "Todo.rdoc"]
14
+ s.homepage = %q{http://github.com/visionmedia/terminal-table}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Terminal-table", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{terminal-table}
18
+ s.rubygems_version = %q{1.3.4}
19
+ s.summary = %q{Simple,}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smtlaissezfaire-terminal-table
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.5
5
+ platform: ruby
6
+ authors:
7
+ - TJ Holowaychuk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-12 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Simple,
17
+ email: tj@vision-media.ca
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - lib/terminal-table/cell.rb
24
+ - lib/terminal-table/core_ext.rb
25
+ - lib/terminal-table/heading.rb
26
+ - lib/terminal-table/import.rb
27
+ - lib/terminal-table/table.rb
28
+ - lib/terminal-table/table_helper.rb
29
+ - lib/terminal-table/version.rb
30
+ - lib/terminal-table.rb
31
+ - README.rdoc
32
+ - tasks/docs.rake
33
+ - tasks/gemspec.rake
34
+ - tasks/spec.rake
35
+ files:
36
+ - examples/examples.rb
37
+ - History.rdoc
38
+ - lib/terminal-table/cell.rb
39
+ - lib/terminal-table/core_ext.rb
40
+ - lib/terminal-table/heading.rb
41
+ - lib/terminal-table/import.rb
42
+ - lib/terminal-table/table.rb
43
+ - lib/terminal-table/table_helper.rb
44
+ - lib/terminal-table/version.rb
45
+ - lib/terminal-table.rb
46
+ - Manifest
47
+ - Rakefile
48
+ - README.rdoc
49
+ - spec/cell_spec.rb
50
+ - spec/core_ext_spec.rb
51
+ - spec/import_spec.rb
52
+ - spec/spec.opts
53
+ - spec/spec_helper.rb
54
+ - spec/table_helper_spec.rb
55
+ - spec/table_spec.rb
56
+ - tasks/docs.rake
57
+ - tasks/gemspec.rake
58
+ - tasks/spec.rake
59
+ - terminal-table.gemspec
60
+ - Todo.rdoc
61
+ has_rdoc: false
62
+ homepage: http://github.com/visionmedia/terminal-table
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --line-numbers
66
+ - --inline-source
67
+ - --title
68
+ - Terminal-table
69
+ - --main
70
+ - README.rdoc
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "1.2"
84
+ version:
85
+ requirements: []
86
+
87
+ rubyforge_project: terminal-table
88
+ rubygems_version: 1.2.0
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Simple,
92
+ test_files: []
93
+