terminal-table 1.4.5 → 1.5.0

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/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
- t = table ['a', 'b']
9
- t.style = {:padding_left => 2, :width => 80}
10
- t << [1, 2]
11
- t << [3, 4]
12
- t << :separator
13
- t << [4, 6]
14
- puts t
15
-
16
- puts
17
- user_table = table do |v|
18
- v.title = "Contact Information"
19
- v.headings = 'First Name', 'Last Name', 'Email'
20
- v << %w( TJ Holowaychuk tj@vision-media.ca )
21
- v << %w( Bob Someone bob@vision-media.ca )
22
- v << %w( Joe Whatever bob@vision-media.ca )
23
- end
24
- puts user_table
25
-
26
- puts
27
- user_table = table do |v|
28
- v.style.width = 80
29
- v.headings = 'First Name', 'Last Name', 'Email'
30
- v << %w( TJ Holowaychuk tj@vision-media.ca )
31
- v << %w( Bob Someone bob@vision-media.ca )
32
- v << %w( Joe Whatever bob@vision-media.ca )
33
- end
34
- puts user_table
35
-
36
- puts
37
- user_table = table do
38
- self.headings = 'First Name', 'Last Name', 'Email'
39
- add_row ['TJ', 'Holowaychuk', 'tj@vision-media.ca']
40
- add_row ['Bob', 'Someone', 'bob@vision-media.ca']
41
- add_row ['Joe', 'Whatever', 'joe@vision-media.ca']
42
- add_separator
43
- add_row ['Total', { :value => '3', :colspan => 2, :alignment => :right }]
44
- align_column 1, :center
45
- end
46
- puts user_table
47
-
48
- puts
49
- user_table = table do
50
- self.headings = ['First Name', 'Last Name', {:value => 'Phones', :colspan => 2, :alignment => :center}]
51
- add_row ['Bob', 'Someone', '123', '456']
52
- add_row :separator
53
- add_row ['TJ', 'Holowaychuk', {:value => "No phones\navaiable", :colspan => 2, :alignment => :center}]
54
- add_row :separator
55
- add_row ['Joe', 'Whatever', '4324', '343242']
56
- end
57
- puts user_table
58
-
59
- rows = []
60
- rows << ['Lines', 100]
61
- rows << ['Comments', 20]
62
- rows << ['Ruby', 70]
63
- rows << ['JavaScript', 30]
64
- puts table([nil, 'Lines'], *rows)
65
-
66
- rows = []
67
- rows << ['Lines', 100]
68
- rows << ['Comments', 20]
69
- rows << ['Ruby', 70]
70
- rows << ['JavaScript', 30]
71
- puts table(nil, *rows)
72
-
73
- rows = []
74
- rows << ['Lines', 100]
75
- rows << ['Comments', 20]
76
- rows << ['Ruby', 70]
77
- rows << ['JavaScript', 30]
78
- table = table([{ :value => 'Stats', :colspan => 2, :alignment => :center }], *rows)
79
- table.align_column 1, :right
80
- puts table
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
@@ -22,6 +22,6 @@
22
22
  #++
23
23
 
24
24
  $:.unshift File.dirname(__FILE__)
25
- %w(version core_ext table cell row separator style table_helper).each do |file|
25
+ %w(version table cell row separator style table_helper).each do |file|
26
26
  require "terminal-table/#{file}"
27
- end
27
+ end
@@ -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}".align(alignment, render_width + @table.cell_padding)
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(/[\x03|\x1a]/, '')
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
@@ -96,9 +96,13 @@ module Terminal
96
96
  ##
97
97
  # Set the headings
98
98
 
99
- def headings= array
100
- @headings = Row.new(self, array)
101
- recalc_column_widths @headings
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
- unless @headings.cells.empty?
115
- buffer << @headings
116
- buffer << separator
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 #{wanted} characters."
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
- [@headings] + rows
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
@@ -1,6 +1,6 @@
1
1
 
2
2
  module Terminal
3
3
  class Table
4
- VERSION = '1.4.5'
4
+ VERSION = '1.5.0'
5
5
  end
6
6
  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 File.dirname(__FILE__) + '/spec_helper'
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
@@ -1,5 +1,5 @@
1
1
 
2
- require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'spec_helper'
3
3
  require "terminal-table/import"
4
4
 
5
5
  describe Object do
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