terminal-table 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.rdoc CHANGED
@@ -1,4 +1,9 @@
1
1
 
2
+ === 1.4.0 / 2009-12-18
3
+
4
+ * Can now add :seperator arbitrarily in a table [thanks splattael]
5
+ * Fix common typo: seperator -> separator [thanks splattael]
6
+
2
7
  === 1.3.0 / 2009-10-16
3
8
 
4
9
  * Major refactoring (functionality remains the same)
data/README.rdoc CHANGED
@@ -22,11 +22,13 @@ Simple, feature rich ASCII table generator.
22
22
  require 'rubygems'
23
23
  require 'terminal-table/import'
24
24
 
25
- puts table(['a', 'b'], [1, 2], [3, 4])
25
+ puts table(['a', 'b'], [1, 2], [3, 4], :separator, [4, 6])
26
26
 
27
27
  t = table ['a', 'b']
28
28
  t << [1, 2]
29
29
  t << [3, 4]
30
+ t.add_separator
31
+ t << [4, 6]
30
32
  puts t
31
33
 
32
34
  user_table = table do |t|
@@ -39,7 +41,7 @@ Simple, feature rich ASCII table generator.
39
41
 
40
42
  user_table = table do
41
43
  self.headings = 'First Name', 'Last Name', 'Email'
42
- add_row ['TJ', 'Holowaychuk', { :value => 'tj@vision-media.ca', :align => :right }]
44
+ add_row ['TJ', 'Holowaychuk', { :value => 'tj@vision-media.ca', :alignment => :right }]
43
45
  add_row ['Bob', 'Someone', 'bob@vision-media.ca']
44
46
  add_row ['Joe', 'Whatever', 'joe@vision-media.ca']
45
47
  align_column 1, :center
@@ -77,6 +79,9 @@ Simple, feature rich ASCII table generator.
77
79
  | 1 | 2 |
78
80
  | 3 | 4 |
79
81
  +---+---+
82
+ | 4 | 6 |
83
+ +---+---+
84
+
80
85
 
81
86
  +---+---+
82
87
  | a | b |
@@ -84,6 +89,8 @@ Simple, feature rich ASCII table generator.
84
89
  | 1 | 2 |
85
90
  | 3 | 4 |
86
91
  +---+---+
92
+ | 4 | 6 |
93
+ +---+---+
87
94
 
88
95
  +------------+-------------+---------------------+
89
96
  | First Name | Last Name | Email |
data/Todo.rdoc CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  == Minor:
7
7
 
8
- * Programmatically add seperator rows
8
+ * Programmatically add separator rows
9
9
  * Arbitrary dimensions
10
10
  * Add multi-column sorting
11
11
  * Change; pre-create Cell and Heading objects to clean up Table a bit
data/examples/examples.rb CHANGED
@@ -9,6 +9,8 @@ puts
9
9
  t = table ['a', 'b']
10
10
  t << [1, 2]
11
11
  t << [3, 4]
12
+ t << :separator
13
+ t << [4, 6]
12
14
  puts t
13
15
 
14
16
  puts
@@ -23,9 +25,11 @@ puts user_table
23
25
  puts
24
26
  user_table = table do
25
27
  self.headings = 'First Name', 'Last Name', 'Email'
26
- add_row ['TJ', 'Holowaychuk', ['tj@vision-media.ca', :right]]
28
+ add_row ['TJ', 'Holowaychuk', 'tj@vision-media.ca']
27
29
  add_row ['Bob', 'Someone', 'bob@vision-media.ca']
28
30
  add_row ['Joe', 'Whatever', 'joe@vision-media.ca']
31
+ add_separator
32
+ add_row ['Total', { :value => '3', :colspan => 2, :alignment => :right }]
29
33
  align_column 1, :center
30
34
  end
31
35
  puts user_table
@@ -6,7 +6,7 @@ module Terminal
6
6
  # Exceptions
7
7
  #++
8
8
 
9
- class Error < StandardError; end
9
+ Error = Class.new StandardError
10
10
 
11
11
  ##
12
12
  # Table characters, x axis, y axis, and intersection.
@@ -36,44 +36,70 @@ module Terminal
36
36
  # Render the table.
37
37
 
38
38
  def render
39
- buffer = seperator << "\n"
39
+ buffer = [separator, "\n"]
40
40
  if has_headings?
41
- buffer << Y + headings.map_with_index do |heading, i|
42
- width = 0
43
- if Hash === heading and not heading[:colspan].nil?
44
- i.upto(i + heading[:colspan] - 1) do |col|
45
- width += length_of_column(col)
46
- end
47
- width += (heading[:colspan] - 1) * (Y.length + 2)
48
- else
49
- width = length_of_column i
50
- end
51
- Heading.new(width, heading).render
52
- end.join(Y) + Y
53
- buffer << "\n#{seperator}\n"
41
+ buffer << render_headings
42
+ buffer << "\n" << separator << "\n"
54
43
  end
55
- buffer << rows.map do |row|
56
- Y + row.map_with_index do |cell, i|
57
- width = 0
58
- if Hash === cell and not cell[:colspan].nil?
59
- i.upto(i + cell[:colspan] - 1) do |col|
60
- width += length_of_column(col)
61
- end
62
- width += (cell[:colspan] - 1) * (Y.length + 2)
63
- else
64
- width = length_of_column i
65
- end
66
- Cell.new(width, cell).render
67
- end.join(Y) + Y
44
+ buffer << @rows.map do |row|
45
+ render_row(row)
68
46
  end.join("\n")
69
- buffer << "\n#{seperator}\n"
47
+ buffer << "\n" << separator << "\n"
48
+ buffer.join
70
49
  end
71
50
  alias :to_s :render
72
51
 
73
52
  ##
74
- # Create a seperator based on colum lengths.
53
+ # Render headings.
54
+
55
+ def render_headings
56
+ Y + headings.map_with_index do |heading, i|
57
+ width = 0
58
+ if heading.is_a?(Hash) and !heading[:colspan].nil?
59
+ i.upto(i + heading[:colspan] - 1) do |col|
60
+ width += length_of_column(col)
61
+ end
62
+ width += (heading[:colspan] - 1) * (Y.length + 2)
63
+ else
64
+ width = length_of_column(i)
65
+ end
66
+ Heading.new( width, heading).render
67
+ end.join(Y) + Y
68
+ end
69
+
70
+ ##
71
+ # Render the given _row_.
72
+
73
+ def render_row row
74
+ if row == :separator
75
+ separator
76
+ else
77
+ Y + row.map_with_index do |cell, i|
78
+ render_cell(cell, i)
79
+ end.join(Y) + Y
80
+ end
81
+ end
75
82
 
76
- def seperator
83
+ ##
84
+ # Render the given _cell_ at index _i_.
85
+
86
+ def render_cell cell, i
87
+ width = 0
88
+ if cell.is_a?(Hash) and !cell[:colspan].nil?
89
+ i.upto(i + cell[:colspan] - 1) do |col|
90
+ width += length_of_column(col)
91
+ end
92
+ width += (cell[:colspan] - 1) * (Y.length + 2)
93
+ else
94
+ width = length_of_column(i)
95
+ end
96
+ Cell.new(width, cell).render
97
+ end
98
+
99
+ ##
100
+ # Create a separator based on colum lengths.
101
+
102
+ def separator
77
103
  I + columns.collect_with_index do |col, i|
78
104
  X * (length_of_column(i) + 2)
79
105
  end.join(I) + I
@@ -83,12 +109,19 @@ module Terminal
83
109
  # Add a row.
84
110
 
85
111
  def add_row row
86
- rows << row
112
+ @rows << row
87
113
  end
88
114
  alias :<< :add_row
89
115
 
90
116
  ##
91
- # Check if headings are present.
117
+ # Add a separator.
118
+
119
+ def add_separator
120
+ @rows << :separator
121
+ end
122
+
123
+ ##
124
+ # Weither or not any headings are present, since they are optional.
92
125
 
93
126
  def has_headings?
94
127
  not headings.empty?
@@ -157,7 +190,21 @@ module Terminal
157
190
  def headings_with_rows
158
191
  [headings] + rows
159
192
  end
160
-
193
+
194
+ ##
195
+ # Return rows without separator rows.
196
+
197
+ def rows
198
+ @rows.reject { |row| row == :separator }
199
+ end
200
+
201
+ ##
202
+ # Return rows including separator rows.
203
+
204
+ def all_rows
205
+ @rows
206
+ end
207
+
161
208
  ##
162
209
  # Check if _other_ is equal to self. _other_ is considered equal
163
210
  # if it contains the same headings and rows.
@@ -1,6 +1,6 @@
1
1
 
2
2
  module Terminal
3
3
  class Table
4
- VERSION = '1.3.0'
4
+ VERSION = '1.4.0'
5
5
  end
6
6
  end
data/spec/table_spec.rb CHANGED
@@ -56,10 +56,18 @@ module Terminal
56
56
  @table.length_of_column(1).should == 18
57
57
  end
58
58
 
59
- it "should render seperators" do
59
+ it "should render separators" do
60
60
  @table.headings = ['Char', 'Num']
61
61
  @table << ['a', 1]
62
- @table.seperator.should == '+------+-----+'
62
+ @table.separator.should == '+------+-----+'
63
+ end
64
+
65
+ it "should add separator" do
66
+ @table << ['a', 1]
67
+ @table.add_separator
68
+ @table << ['b', 2]
69
+ @table.rows.size.should == 2
70
+ @table.all_rows.size.should == 3
63
71
  end
64
72
 
65
73
  it "should bitch and complain when you have no rows" do
@@ -95,6 +103,24 @@ module Terminal
95
103
  EOF
96
104
  end
97
105
 
106
+ it "should render separators" do
107
+ @table.headings = ['Char', 'Num']
108
+ @table << ['a', 1]
109
+ @table << ['b', 2]
110
+ @table.add_separator
111
+ @table << ['c', 3]
112
+ @table.render.should == <<-EOF.deindent
113
+ +------+-----+
114
+ | Char | Num |
115
+ +------+-----+
116
+ | a | 1 |
117
+ | b | 2 |
118
+ +------+-----+
119
+ | c | 3 |
120
+ +------+-----+
121
+ EOF
122
+ end
123
+
98
124
  it "should render properly using block syntax" do
99
125
  table = Terminal::Table.new do |t|
100
126
  t << ['a', 1]
@@ -214,7 +240,7 @@ module Terminal
214
240
  table_one = Table.new
215
241
  table_two = Table.new
216
242
 
217
- table_one.rows << "a"
243
+ table_one.add_row "a"
218
244
 
219
245
  table_one.should_not == table_two
220
246
  table_two.should_not == table_one
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{terminal-table}
5
- s.version = "1.3.0"
5
+ s.version = "1.4.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["TJ Holowaychuk"]
9
- s.date = %q{2009-10-16}
9
+ s.date = %q{2009-12-18}
10
10
  s.description = %q{Simple, feature rich ascii table generation library}
11
11
  s.email = %q{tj@vision-media.ca}
12
12
  s.extra_rdoc_files = ["README.rdoc", "lib/terminal-table.rb", "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", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terminal-table
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TJ Holowaychuk
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-16 00:00:00 -07:00
12
+ date: 2009-12-18 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15