terminal-table 1.4.5 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/History.rdoc +48 -54
- data/Manifest +0 -4
- data/README.rdoc +240 -240
- data/Rakefile +14 -14
- data/Todo.rdoc +13 -13
- data/examples/examples.rb +83 -80
- data/lib/terminal-table.rb +2 -2
- data/lib/terminal-table/cell.rb +7 -9
- data/lib/terminal-table/import.rb +4 -4
- data/lib/terminal-table/table.rb +14 -8
- data/lib/terminal-table/table_helper.rb +9 -9
- data/lib/terminal-table/version.rb +1 -1
- data/spec/cell_spec.rb +12 -6
- data/spec/import_spec.rb +1 -1
- data/spec/spec_helper.rb +8 -8
- data/spec/table_spec.rb +54 -7
- data/tasks/docs.rake +12 -12
- data/tasks/gemspec.rake +2 -2
- data/tasks/spec.rake +24 -24
- data/terminal-table.gemspec +6 -5
- metadata +53 -63
- data/lib/terminal-table/core_ext.rb +0 -8
- data/lib/terminal-table/row.rb +0 -48
- data/lib/terminal-table/separator.rb +0 -14
- data/lib/terminal-table/style.rb +0 -61
- data/spec/core_ext_spec.rb +0 -18
- data/spec/spec.opts +0 -1
data/Rakefile
CHANGED
@@ -1,15 +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, feature rich ascii table generation library"
|
11
|
-
p.url = "http://github.com/visionmedia/terminal-table"
|
12
|
-
p.runtime_dependencies = []
|
13
|
-
end
|
14
|
-
|
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, feature rich ascii table generation library"
|
11
|
+
p.url = "http://github.com/visionmedia/terminal-table"
|
12
|
+
p.runtime_dependencies = []
|
13
|
+
end
|
14
|
+
|
15
15
|
Dir['tasks/**/*.rake'].sort.each { |lib| load lib }
|
data/Todo.rdoc
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
|
2
|
-
== Major:
|
3
|
-
|
4
|
-
* Nothing
|
5
|
-
|
6
|
-
== Minor:
|
7
|
-
|
8
|
-
* Programmatically add separator rows
|
9
|
-
* Add multi-column sorting
|
10
|
-
* Change; pre-create Cell and Heading objects to clean up Table a bit
|
11
|
-
|
12
|
-
== Brainstorming:
|
13
|
-
|
1
|
+
|
2
|
+
== Major:
|
3
|
+
|
4
|
+
* Nothing
|
5
|
+
|
6
|
+
== Minor:
|
7
|
+
|
8
|
+
* Programmatically add separator rows
|
9
|
+
* Add multi-column sorting
|
10
|
+
* Change; pre-create Cell and Heading objects to clean up Table a bit
|
11
|
+
|
12
|
+
== Brainstorming:
|
13
|
+
|
14
14
|
* Nothing
|
data/examples/examples.rb
CHANGED
@@ -1,80 +1,83 @@
|
|
1
|
-
$:.unshift File.dirname(__FILE__) + '/../lib'
|
2
|
-
require 'terminal-table/import'
|
3
|
-
|
4
|
-
puts
|
5
|
-
puts table(['a', 'b'], [1, 2], [3, 4])
|
6
|
-
|
7
|
-
puts
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
t
|
12
|
-
t
|
13
|
-
t << [
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
v
|
22
|
-
v
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
user_table
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
v
|
32
|
-
v
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
user_table
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
add_row ['
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
user_table
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
add_row
|
55
|
-
add_row
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
rows
|
63
|
-
rows << ['
|
64
|
-
|
65
|
-
|
66
|
-
rows
|
67
|
-
|
68
|
-
|
69
|
-
rows
|
70
|
-
rows << ['
|
71
|
-
|
72
|
-
|
73
|
-
rows
|
74
|
-
|
75
|
-
|
76
|
-
rows
|
77
|
-
rows << ['
|
78
|
-
|
79
|
-
|
80
|
-
|
1
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
2
|
+
require 'terminal-table/import'
|
3
|
+
|
4
|
+
puts
|
5
|
+
puts table(['a', 'b'], [1, 2], [3, 4])
|
6
|
+
|
7
|
+
puts
|
8
|
+
puts table(['name', 'content'], ['ftp.example.com', '1.1.1.1'], ['www.example.com', '|lalalala|lalala|'])
|
9
|
+
|
10
|
+
puts
|
11
|
+
t = table ['a', 'b']
|
12
|
+
t.style = {:padding_left => 2, :width => 80}
|
13
|
+
t << [1, 2]
|
14
|
+
t << [3, 4]
|
15
|
+
t << :separator
|
16
|
+
t << [4, 6]
|
17
|
+
puts t
|
18
|
+
|
19
|
+
puts
|
20
|
+
user_table = table do |v|
|
21
|
+
v.title = "Contact Information"
|
22
|
+
v.headings = 'First Name', 'Last Name', 'Email'
|
23
|
+
v << %w( TJ Holowaychuk tj@vision-media.ca )
|
24
|
+
v << %w( Bob Someone bob@vision-media.ca )
|
25
|
+
v << %w( Joe Whatever bob@vision-media.ca )
|
26
|
+
end
|
27
|
+
puts user_table
|
28
|
+
|
29
|
+
puts
|
30
|
+
user_table = table do |v|
|
31
|
+
v.style.width = 80
|
32
|
+
v.headings = 'First Name', 'Last Name', 'Email'
|
33
|
+
v << %w( TJ Holowaychuk tj@vision-media.ca )
|
34
|
+
v << %w( Bob Someone bob@vision-media.ca )
|
35
|
+
v << %w( Joe Whatever bob@vision-media.ca )
|
36
|
+
end
|
37
|
+
puts user_table
|
38
|
+
|
39
|
+
puts
|
40
|
+
user_table = table do
|
41
|
+
self.headings = 'First Name', 'Last Name', 'Email'
|
42
|
+
add_row ['TJ', 'Holowaychuk', 'tj@vision-media.ca']
|
43
|
+
add_row ['Bob', 'Someone', 'bob@vision-media.ca']
|
44
|
+
add_row ['Joe', 'Whatever', 'joe@vision-media.ca']
|
45
|
+
add_separator
|
46
|
+
add_row ['Total', { :value => '3', :colspan => 2, :alignment => :right }]
|
47
|
+
align_column 1, :center
|
48
|
+
end
|
49
|
+
puts user_table
|
50
|
+
|
51
|
+
puts
|
52
|
+
user_table = table do
|
53
|
+
self.headings = ['First Name', 'Last Name', {:value => 'Phones', :colspan => 2, :alignment => :center}]
|
54
|
+
add_row ['Bob', 'Someone', '123', '456']
|
55
|
+
add_row :separator
|
56
|
+
add_row ['TJ', 'Holowaychuk', {:value => "No phones\navaiable", :colspan => 2, :alignment => :center}]
|
57
|
+
add_row :separator
|
58
|
+
add_row ['Joe', 'Whatever', '4324', '343242']
|
59
|
+
end
|
60
|
+
puts user_table
|
61
|
+
|
62
|
+
rows = []
|
63
|
+
rows << ['Lines', 100]
|
64
|
+
rows << ['Comments', 20]
|
65
|
+
rows << ['Ruby', 70]
|
66
|
+
rows << ['JavaScript', 30]
|
67
|
+
puts table([nil, 'Lines'], *rows)
|
68
|
+
|
69
|
+
rows = []
|
70
|
+
rows << ['Lines', 100]
|
71
|
+
rows << ['Comments', 20]
|
72
|
+
rows << ['Ruby', 70]
|
73
|
+
rows << ['JavaScript', 30]
|
74
|
+
puts table(nil, *rows)
|
75
|
+
|
76
|
+
rows = []
|
77
|
+
rows << ['Lines', 100]
|
78
|
+
rows << ['Comments', 20]
|
79
|
+
rows << ['Ruby', 70]
|
80
|
+
rows << ['JavaScript', 30]
|
81
|
+
table = table([{ :value => 'Stats', :colspan => 2, :alignment => :center }], *rows)
|
82
|
+
table.align_column 1, :right
|
83
|
+
puts table
|
data/lib/terminal-table.rb
CHANGED
data/lib/terminal-table/cell.rb
CHANGED
@@ -2,12 +2,6 @@
|
|
2
2
|
module Terminal
|
3
3
|
class Table
|
4
4
|
class Cell
|
5
|
-
|
6
|
-
##
|
7
|
-
# Cell width.
|
8
|
-
|
9
|
-
attr_reader :width
|
10
|
-
|
11
5
|
##
|
12
6
|
# Cell value.
|
13
7
|
|
@@ -36,7 +30,7 @@ module Terminal
|
|
36
30
|
end
|
37
31
|
|
38
32
|
def alignment
|
39
|
-
@alignment || :left
|
33
|
+
@alignment || @table.style.alignment || :left
|
40
34
|
end
|
41
35
|
|
42
36
|
def alignment=(val)
|
@@ -48,6 +42,10 @@ module Terminal
|
|
48
42
|
end
|
49
43
|
end
|
50
44
|
|
45
|
+
def align(val, position, length)
|
46
|
+
positions = { :left => :ljust, :right => :rjust, :center => :center }
|
47
|
+
val.public_send(positions[position], length)
|
48
|
+
end
|
51
49
|
def lines
|
52
50
|
@value.to_s.split(/\n/)
|
53
51
|
end
|
@@ -59,7 +57,7 @@ module Terminal
|
|
59
57
|
left = " " * @table.style.padding_left
|
60
58
|
right = " " * @table.style.padding_right
|
61
59
|
render_width = lines[line].to_s.size - escape(lines[line]).size + width
|
62
|
-
"#{left}#{lines[line]}#{right}"
|
60
|
+
align("#{left}#{lines[line]}#{right}", alignment, render_width + @table.cell_padding)
|
63
61
|
end
|
64
62
|
alias :to_s :render
|
65
63
|
|
@@ -87,7 +85,7 @@ module Terminal
|
|
87
85
|
def escape(line)
|
88
86
|
line.to_s.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
|
89
87
|
gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
|
90
|
-
gsub(/
|
88
|
+
gsub(/(\x03|\x1a)/, '')
|
91
89
|
end
|
92
90
|
end
|
93
91
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
2
|
-
require 'terminal-table'
|
3
|
-
|
4
|
-
include Terminal::Table::TableHelper
|
1
|
+
|
2
|
+
require 'terminal-table'
|
3
|
+
|
4
|
+
include Terminal::Table::TableHelper
|
data/lib/terminal-table/table.rb
CHANGED
@@ -96,9 +96,13 @@ module Terminal
|
|
96
96
|
##
|
97
97
|
# Set the headings
|
98
98
|
|
99
|
-
def headings=
|
100
|
-
|
101
|
-
|
99
|
+
def headings= arrays
|
100
|
+
arrays = [arrays] unless arrays.first.is_a?(Array)
|
101
|
+
@headings = arrays.map do |array|
|
102
|
+
row = Row.new(self, array)
|
103
|
+
recalc_column_widths row
|
104
|
+
row
|
105
|
+
end
|
102
106
|
end
|
103
107
|
|
104
108
|
##
|
@@ -111,9 +115,11 @@ module Terminal
|
|
111
115
|
buffer << Row.new(self, [title_cell_options])
|
112
116
|
buffer << separator
|
113
117
|
end
|
114
|
-
|
115
|
-
|
116
|
-
|
118
|
+
@headings.each do |row|
|
119
|
+
unless row.cells.empty?
|
120
|
+
buffer << row
|
121
|
+
buffer << separator
|
122
|
+
end
|
117
123
|
end
|
118
124
|
buffer += @rows
|
119
125
|
buffer << separator
|
@@ -166,7 +172,7 @@ module Terminal
|
|
166
172
|
return [] if style.width.nil?
|
167
173
|
spacing = style.width - columns_width
|
168
174
|
if spacing < 0
|
169
|
-
raise "Table width exceeds wanted width of #{
|
175
|
+
raise "Table width exceeds wanted width of #{style.width} characters."
|
170
176
|
else
|
171
177
|
per_col = spacing / number_of_columns
|
172
178
|
arr = (1...number_of_columns).to_a.map { |i| per_col }
|
@@ -201,7 +207,7 @@ module Terminal
|
|
201
207
|
# Return headings combined with rows.
|
202
208
|
|
203
209
|
def headings_with_rows
|
204
|
-
|
210
|
+
@headings + rows
|
205
211
|
end
|
206
212
|
|
207
213
|
def yield_or_eval &block
|
@@ -1,9 +1,9 @@
|
|
1
|
-
module Terminal
|
2
|
-
class Table
|
3
|
-
module TableHelper
|
4
|
-
def table headings = [], *rows, &block
|
5
|
-
Terminal::Table.new :headings => headings.to_a, :rows => rows, &block
|
6
|
-
end
|
7
|
-
end
|
8
|
-
end
|
9
|
-
end
|
1
|
+
module Terminal
|
2
|
+
class Table
|
3
|
+
module TableHelper
|
4
|
+
def table headings = [], *rows, &block
|
5
|
+
Terminal::Table.new :headings => headings.to_a, :rows => rows, &block
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
data/spec/cell_spec.rb
CHANGED
@@ -3,11 +3,11 @@ require 'term/ansicolor'
|
|
3
3
|
|
4
4
|
class String; include Term::ANSIColor; end
|
5
5
|
|
6
|
-
require
|
6
|
+
require 'spec_helper'
|
7
7
|
|
8
8
|
describe Terminal::Table do
|
9
9
|
Cell = Terminal::Table::Cell
|
10
|
-
|
10
|
+
|
11
11
|
it "should default alignment to the left" do
|
12
12
|
cell = Cell.new :value => 'foo', :table => Terminal::Table.new, :index => 0
|
13
13
|
cell.value.should == 'foo'
|
@@ -19,7 +19,7 @@ describe Terminal::Table do
|
|
19
19
|
cell.value.should == 'foo'
|
20
20
|
cell.alignment.should == :center
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
it "should allow :left, :right and :center for alignment" do
|
24
24
|
@cell = Cell.new :value => 'foo', :table => Terminal::Table.new, :index => 0
|
25
25
|
@cell.alignment = :left
|
@@ -27,7 +27,7 @@ describe Terminal::Table do
|
|
27
27
|
@cell.alignment = :center
|
28
28
|
lambda { @cell.alignment = "foo" }.should raise_error
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
it "should allow multiline content" do
|
32
32
|
cell = Cell.new :value => "barrissimo\n"+"foo".yellow, :table => Terminal::Table.new, :index => 0
|
33
33
|
cell.value.should == "barrissimo\n"+"foo".yellow
|
@@ -35,14 +35,14 @@ describe Terminal::Table do
|
|
35
35
|
cell.value_for_column_width_recalc.should == 'barrissimo'
|
36
36
|
cell.render(0).should == " barrissimo "
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
it "should allow colorized content" do
|
40
40
|
cell = Cell.new :value => "foo".red, :table => Terminal::Table.new, :index => 0
|
41
41
|
cell.value.should == "\e[31mfoo\e[0m"
|
42
42
|
cell.value_for_column_width_recalc.should == 'foo'
|
43
43
|
cell.render.should == " \e[31mfoo\e[0m "
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
it "should render padding properly" do
|
47
47
|
@table = Terminal::Table.new(:rows => [['foo', '2'], ['3', '4']], :style => {:padding_right => 3})
|
48
48
|
cell = @table.rows.first.cells.first
|
@@ -51,4 +51,10 @@ describe Terminal::Table do
|
|
51
51
|
cell.render.should == " foo "
|
52
52
|
end
|
53
53
|
|
54
|
+
it "should not ignore pipe characters" do
|
55
|
+
cell = Cell.new :value => "f|o|o", :table => Terminal::Table.new, :index => 0
|
56
|
+
cell.value.should == "f|o|o"
|
57
|
+
cell.value_for_column_width_recalc.should == 'f|o|o'
|
58
|
+
cell.render.should == " f|o|o "
|
59
|
+
end
|
54
60
|
end
|
data/spec/import_spec.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
|
2
|
-
require File.dirname(__FILE__) + '/../lib/terminal-table'
|
3
|
-
|
4
|
-
class String
|
5
|
-
def deindent
|
6
|
-
strip.gsub(/^ */, '')
|
7
|
-
end
|
8
|
-
end
|
1
|
+
|
2
|
+
require File.dirname(__FILE__) + '/../lib/terminal-table'
|
3
|
+
|
4
|
+
class String
|
5
|
+
def deindent
|
6
|
+
strip.gsub(/^ */, '')
|
7
|
+
end
|
8
|
+
end
|