terminal-table 1.4.3 → 1.4.4

Sign up to get free protection for your applications and to get access to all the features.
data/History.rdoc CHANGED
@@ -1,3 +1,8 @@
1
+ 1.4.4 / 2011-11-07
2
+ ==================
3
+
4
+ * Add support for multi-line rows, table titles, and colors
5
+
1
6
  1.4.3 / 2011-10-13
2
7
  ==================
3
8
 
data/README.rdoc CHANGED
@@ -1,138 +1,220 @@
1
-
2
1
  = Terminal Table
3
2
 
4
- Simple, feature rich ASCII table generator.
5
-
6
- == Installation:
7
-
8
- Install [Gemcutter](http://gemcutter.org) then execute:
9
- $ sudo gem install terminal-table
10
-
11
- == Features:
12
-
13
- * Fast
14
- * Simple
15
- * Optional headings
16
- * Alignment of columns, headings, or cells
17
- * Supports column span
18
- * Easy modification of table strings (+, -, |)
19
-
20
- == Examples:
21
-
22
- require 'rubygems'
23
- require 'terminal-table/import'
24
-
25
- puts table(['a', 'b'], [1, 2], [3, 4], :separator, [4, 6])
26
-
27
- t = table ['a', 'b']
28
- t << [1, 2]
29
- t << [3, 4]
30
- t.add_separator
31
- t << [4, 6]
32
- puts t
33
-
34
- user_table = table do |t|
35
- t.headings = 'First Name', 'Last Name', 'Email'
36
- t << ['TJ', 'Holowaychuk', 'tj@vision-media.ca']
37
- t << ['Bob', 'Someone', 'bob@vision-media.ca']
38
- t << ['Joe', 'Whatever', 'joe@vision-media.ca']
3
+ == Description
4
+
5
+ Terminal Table is a fast and simple, yet feature rich ASCII table generator written in Ruby.
6
+
7
+ == Installation
8
+
9
+ Install http://gemcutter.org and execute:
10
+
11
+ $ gem install terminal-table
12
+
13
+ == Usage
14
+
15
+ === Basics
16
+
17
+ To use Terminal Table:
18
+
19
+ require 'terminal-table'
20
+
21
+ To generate a table, provide an array of arrays (which are interpreted as rows):
22
+
23
+ rows = []
24
+ rows << ['One', 1]
25
+ rows << ['Two', 2]
26
+ rows << ['Three', 3]
27
+ table = Terminal::Table.new :rows => rows
28
+
29
+ # > puts table
30
+ #
31
+ # +-------+---+
32
+ # | One | 1 |
33
+ # | Two | 2 |
34
+ # | Three | 3 |
35
+ # +-------+---+
36
+
37
+
38
+ The constructor can also be given a block which is either yielded the Table object or instance evaluated:
39
+
40
+ table = Terminal::Table.new do |t|
41
+ t.rows = rows
39
42
  end
40
- puts user_table
41
-
42
- user_table = table do
43
- self.headings = 'First Name', 'Last Name', 'Email'
44
- add_row ['TJ', 'Holowaychuk', { :value => 'tj@vision-media.ca', :alignment => :right }]
45
- add_row ['Bob', 'Someone', 'bob@vision-media.ca']
46
- add_row ['Joe', 'Whatever', 'joe@vision-media.ca']
47
- align_column 1, :center
43
+
44
+ table = Terminal::Table.new do
45
+ self.rows = rows
48
46
  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 ['TJ', 'Holowaychuk', {:value => "No phones", :colspan => 2, :alignment => :center}]
56
- add_row ['Joe', 'Whatever', '4324', '343242']
47
+
48
+ Adding rows one by one:
49
+
50
+ table = Terminal::Table.new do |t|
51
+ t << ['One', 1]
52
+ t.add_row ['Two', 2]
57
53
  end
58
- puts user_table
59
-
60
- rows = []
61
- rows << ['Lines', 100]
62
- rows << ['Comments', 20]
63
- rows << ['Ruby', 70]
64
- rows << ['JavaScript', 30]
65
- puts table([nil, 'Lines'], *rows)
66
-
67
- rows = []
68
- rows << ['Lines', 100]
69
- rows << ['Comments', 20]
70
- rows << ['Ruby', 70]
71
- rows << ['JavaScript', 30]
72
- puts table(nil, *rows)
73
-
74
- == Results:
75
-
76
- +---+---+
77
- | a | b |
78
- +---+---+
79
- | 1 | 2 |
80
- | 3 | 4 |
81
- +---+---+
82
- | 4 | 6 |
83
- +---+---+
84
-
85
-
86
- +---+---+
87
- | a | b |
88
- +---+---+
89
- | 1 | 2 |
90
- | 3 | 4 |
91
- +---+---+
92
- | 4 | 6 |
93
- +---+---+
94
-
95
- +------------+-------------+---------------------+
96
- | First Name | Last Name | Email |
97
- +------------+-------------+---------------------+
98
- | TJ | Holowaychuk | tj@vision-media.ca |
99
- | Bob | Someone | bob@vision-media.ca |
100
- | Joe | Whatever | joe@vision-media.ca |
101
- +------------+-------------+---------------------+
102
-
103
- +------------+-----------------------------+---------------------+
104
- | First Name | Last Name | Email |
105
- +------------+-----------------------------+---------------------+
106
- | TJ | Holowaychuk | tj@vision-media.ca |
107
- | Bob | Someone | bob@vision-media.ca |
108
- | Joe | Whatever | joe@vision-media.ca |
109
- +------------+-----------------------------+---------------------+
110
-
111
- +------------+-------------+-----------+--------+
112
- | First Name | Last Name | Phones |
113
- +------------+-------------+-----------+--------+
114
- | Bob | Someone | 123 | 456 |
115
- | TJ | Holowaychuk | No phones |
116
- | Joe | Whatever | 4324 | 343242 |
117
- +------------+-------------+-----------+--------+
118
-
119
- +------------+-------+
120
- | | Lines |
121
- +------------+-------+
122
- | Lines | 100 |
123
- | Comments | 20 |
124
- | Ruby | 70 |
125
- | JavaScript | 30 |
126
- +------------+-------+
127
-
128
- +------------+-----+
129
- | Lines | 100 |
130
- | Comments | 20 |
131
- | Ruby | 70 |
132
- | JavaScript | 30 |
133
- +------------+-----+
134
-
135
- == License:
54
+
55
+ To add separators between rows:
56
+
57
+ table = Terminal::Table.new do |t|
58
+ t << ['One', 1]
59
+ t << :separator
60
+ t.add_row ['Two', 2]
61
+ t.add_separator
62
+ t.add_row ['Three', 3]
63
+ end
64
+
65
+ # > puts table
66
+ #
67
+ # +-------+---+
68
+ # | One | 1 |
69
+ # +-------+---+
70
+ # | Two | 2 |
71
+ # +-------+---+
72
+ # | Three | 3 |
73
+ # +-------+---+
74
+
75
+ Cells can handle multiline content:
76
+
77
+ table = Terminal::Table.new do |t|
78
+ t << ['One', 1]
79
+ t << :separator
80
+ t.add_row ["Two\nDouble", 2]
81
+ t.add_separator
82
+ t.add_row ['Three', 3]
83
+ end
84
+
85
+ # > puts table
86
+ #
87
+ # +--------+---+
88
+ # | One | 1 |
89
+ # +--------+---+
90
+ # | Two | 2 |
91
+ # | Double | |
92
+ # +--------+---+
93
+ # | Three | 3 |
94
+ # +--------+---+
95
+
96
+ === Head
97
+
98
+ To add a head to the table:
99
+
100
+ table = Terminal::Table.new :headings => ['Word', 'Number'], :rows => rows
101
+
102
+ # > puts table
103
+ #
104
+ # +-------+--------+
105
+ # | Word | Number |
106
+ # +-------+--------+
107
+ # | One | 1 |
108
+ # | Two | 2 |
109
+ # | Three | 3 |
110
+ # +-------+--------+
111
+
112
+ === Title
113
+
114
+ To add a title to the table:
115
+
116
+ table = Terminal::Table.new :title => "Cheatsheet", :headings => ['Word', 'Number'], :rows => rows
117
+
118
+ # > puts table
119
+ #
120
+ # +------------+--------+
121
+ # | Cheatsheet |
122
+ # +------------+--------+
123
+ # | Word | Number |
124
+ # +------------+--------+
125
+ # | One | 1 |
126
+ # | Two | 2 |
127
+ # | Three | 3 |
128
+ # +------------+--------+
129
+
130
+ === Alignment
131
+
132
+ To align the second column to the right:
133
+
134
+ table.align_column(1, :right)
135
+
136
+ # > puts table
137
+ #
138
+ # +-------+--------+
139
+ # | Word | Number |
140
+ # +-------+--------+
141
+ # | One | 1 |
142
+ # | Two | 2 |
143
+ # | Three | 3 |
144
+ # +-------+--------+
145
+
146
+ To align an individual cell, you specify the cell value in a hash along the alignment:
147
+
148
+ table << ["Four", {:value => 4.0, :alignment => :center}]
149
+
150
+ # > puts table
151
+ #
152
+ # +-------+--------+
153
+ # | Word | Number |
154
+ # +-------+--------+
155
+ # | One | 1 |
156
+ # | Two | 2 |
157
+ # | Three | 3 |
158
+ # | Four | 4.0 |
159
+ # +-------+--------+
160
+
161
+ === Style
162
+
163
+ To specifify style options:
164
+
165
+ table = Terminal::Table.new :headings => ['Word', 'Number'], :rows => rows, :style => {:width => 80}
166
+
167
+ # > puts table
168
+ #
169
+ # +--------------------------------------+---------------------------------------+
170
+ # | Word | Number |
171
+ # +--------------------------------------+---------------------------------------+
172
+ # | One | 1 |
173
+ # | Two | 2 |
174
+ # | Three | 3 |
175
+ # +--------------------------------------+---------------------------------------+
176
+
177
+ And change styles on the fly:
178
+
179
+ table.style = {:width => 40, :padding_left => 3, :border_x => "=", :border_i => "x"}
180
+
181
+ # > puts table
182
+ #
183
+ # x====================x=================x
184
+ # | Cheatsheet |
185
+ # x====================x=================x
186
+ # | Word | Number |
187
+ # x====================x=================x
188
+ # | One | 1 |
189
+ # | Two | 2 |
190
+ # | Three | 3 |
191
+ # x====================x=================x
192
+
193
+ To change the default style options:
194
+
195
+ Terminal::Style.defaults = {:width => 80}
196
+
197
+ All Table objects created afterwards will inherit these defaults.
198
+
199
+ === Constructor options and setter methods
200
+
201
+ Valid options for the constructor are :rows, :headings, :style and :title - and all options can also be set on the created table object by their setter method:
202
+
203
+ table = Terminal::Table.new
204
+ table.title = "Cheatsheet"
205
+ table.headings = ['Word', 'Number']
206
+ table.rows = rows
207
+ table.style = {:width => 40}
208
+
209
+ == More examples
210
+
211
+ For more examples, please see the examples/examples.rb file included in the source distribution.
212
+
213
+ == Author
214
+
215
+ TJ Holowaychuk <tj@vision-media.ca>
216
+
217
+ == License
136
218
 
137
219
  (The MIT License)
138
220
 
data/Todo.rdoc CHANGED
@@ -6,7 +6,6 @@
6
6
  == Minor:
7
7
 
8
8
  * Programmatically add separator rows
9
- * Arbitrary dimensions
10
9
  * Add multi-column sorting
11
10
  * Change; pre-create Cell and Heading objects to clean up Table a bit
12
11
 
data/examples/examples.rb CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  $:.unshift File.dirname(__FILE__) + '/../lib'
3
2
  require 'terminal-table/import'
4
3
 
@@ -7,6 +6,7 @@ puts table(['a', 'b'], [1, 2], [3, 4])
7
6
 
8
7
  puts
9
8
  t = table ['a', 'b']
9
+ t.style = {:padding_left => 2, :width => 80}
10
10
  t << [1, 2]
11
11
  t << [3, 4]
12
12
  t << :separator
@@ -14,11 +14,22 @@ t << [4, 6]
14
14
  puts t
15
15
 
16
16
  puts
17
- user_table = table do |t|
18
- t.headings = 'First Name', 'Last Name', 'Email'
19
- t << %w( TJ Holowaychuk tj@vision-media.ca )
20
- t << %w( Bob Someone bob@vision-media.ca )
21
- t << %w( Joe Whatever bob@vision-media.ca )
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 )
22
33
  end
23
34
  puts user_table
24
35
 
@@ -38,7 +49,9 @@ puts
38
49
  user_table = table do
39
50
  self.headings = ['First Name', 'Last Name', {:value => 'Phones', :colspan => 2, :alignment => :center}]
40
51
  add_row ['Bob', 'Someone', '123', '456']
41
- add_row ['TJ', 'Holowaychuk', {:value => "No phones", :colspan => 2, :alignment => :center}]
52
+ add_row :separator
53
+ add_row ['TJ', 'Holowaychuk', {:value => "No phones\navaiable", :colspan => 2, :alignment => :center}]
54
+ add_row :separator
42
55
  add_row ['Joe', 'Whatever', '4324', '343242']
43
56
  end
44
57
  puts user_table
@@ -22,10 +22,6 @@
22
22
  #++
23
23
 
24
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'
25
+ %w(version core_ext table cell row separator style table_helper).each do |file|
26
+ require "terminal-table/#{file}"
27
+ end
@@ -13,40 +13,75 @@ module Terminal
13
13
 
14
14
  attr_reader :value
15
15
 
16
- ##
17
- # Cell alignment.
18
-
19
- attr_reader :alignment
20
-
21
16
  ##
22
17
  # Column span.
23
18
 
24
19
  attr_reader :colspan
25
20
 
26
21
  ##
27
- # Initialize with _width_ and _options_.
22
+ # Initialize with _options_.
28
23
 
29
- def initialize width, options = nil
30
- @width = width
24
+ def initialize options = nil
31
25
  @value, options = options, {} unless Hash === options
32
26
  @value = options.fetch :value, value
33
- @alignment = options.fetch :alignment, :left
27
+ @alignment = options.fetch :alignment, nil
34
28
  @colspan = options.fetch :colspan, 1
29
+ @width = options.fetch :width, @value.to_s.size
30
+ @index = options.fetch :index
31
+ @table = options.fetch :table
32
+ end
33
+
34
+ def alignment?
35
+ !@alignment.nil?
36
+ end
37
+
38
+ def alignment
39
+ @alignment || :left
40
+ end
41
+
42
+ def alignment=(val)
43
+ supported = %w(left center right)
44
+ if supported.include?(val.to_s)
45
+ @alignment = val
46
+ else
47
+ raise "Aligment must be one of: #{supported.join(' ')}"
48
+ end
49
+ end
50
+
51
+ def lines
52
+ @value.to_s.split(/\n/)
35
53
  end
36
54
 
37
55
  ##
38
56
  # Render the cell.
39
57
 
40
- def render
41
- " #{value} ".align alignment, width + 2
58
+ def render(line = 0)
59
+ left = " " * @table.style.padding_left
60
+ right = " " * @table.style.padding_right
61
+ "#{left}#{lines[line]}#{right}".align(alignment, width + @table.cell_padding)
42
62
  end
43
63
  alias :to_s :render
44
64
 
45
65
  ##
46
- # Cell length.
66
+ # Returns the longest line in the cell and
67
+ # removes all ANSI escape sequences (e.g. color)
68
+
69
+ def value_for_column_width_recalc
70
+ str = lines.sort_by { |s| s.size }.last.to_s
71
+ str = str.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '')
72
+ str = str.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '')
73
+ str.gsub(/[\x03|\x1a]/, '')
74
+ end
75
+
76
+ ##
77
+ # Returns the width of this cell
47
78
 
48
- def length
49
- value.to_s.length + 2
79
+ def width
80
+ padding = (colspan - 1) * @table.cell_spacing
81
+ inner_width = (1..@colspan).to_a.inject(0) do |w, counter|
82
+ w + @table.column_width(@index + counter - 1)
83
+ end
84
+ inner_width + padding
50
85
  end
51
86
  end
52
87
  end