console_table 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -8
- data/README.md +54 -1
- data/lib/console_table.rb +29 -14
- data/test/test_console_table.rb +459 -198
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d29f00cea80b04a26dc82d97bfa2f6223a37966d
|
4
|
+
data.tar.gz: 66059cc9c33ec69fe028422c3fc707a53fb94f29
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17cd329d715430896670cd02dcaf547af1cd93199192cee28d6b85e594b196e9d3152c7d0a24656a5a48dd331fae4ef5e52bb387bc42002e73379fd337afa490
|
7
|
+
data.tar.gz: 6592e9a36084707eec3b715c90ff8118943be9a8c6d9b7ce7112a8503d5d511747e3aff97664164d07ccfe1053e777acc2ac0fd792356d81c8dceaafa702578b
|
data/.travis.yml
CHANGED
@@ -1,11 +1,3 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
3
|
- 1.9.3
|
4
|
-
deploy:
|
5
|
-
provider: rubygems
|
6
|
-
api_key:
|
7
|
-
secure: M1I+7IFuje77sZjtQbmvamzihT+NEJpCzb6Eco8AKbJu/swim+2DvojdJHJw3qpjBt+S2u6zPWWc8KqP6ME7YZzydhJKN/rftUow4lyPbJ6ut9wNg4J9EaZgawG/BOm7LESqtj6J5KWrEd2qRn86mLMHO6ZgLsvsKBWR+oMnb+U=
|
8
|
-
gem: console_table
|
9
|
-
on:
|
10
|
-
tags: true
|
11
|
-
repo: rodhilton/console_table
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# ConsoleTable
|
2
2
|
|
3
|
-
[![Build Status](https://travis-ci.org/rodhilton/console_table.svg?branch=master)](https://travis-ci.org/rodhilton/console_table)
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/console_table.svg)](https://rubygems.org/gems/console_table) [![Build Status](https://travis-ci.org/rodhilton/console_table.svg?branch=master)](https://travis-ci.org/rodhilton/console_table)
|
4
4
|
|
5
5
|
ConsoleTable is a helper class that allows you to print data to a console in a clean, table-like fashion. It's intended for use
|
6
6
|
in commandline applications with information-dense output. It checks your terminal window size (or COLUMNS environment variable) to ensure
|
@@ -30,6 +30,32 @@ Or install it yourself as:
|
|
30
30
|
|
31
31
|
$ gem install console_table
|
32
32
|
|
33
|
+
## Quick Start
|
34
|
+
|
35
|
+
Here's a short-and-sweet example of using ConsoleTable
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
require 'console_table'
|
39
|
+
|
40
|
+
ConsoleTable.define(["Name", "DOB", "Title"]) do |table|
|
41
|
+
table << ["Rod", "06-15-80", "Software Engineer"]
|
42
|
+
table << ["Julia", "04-25-81", "Piano Teacher"]
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
This program will output, on a console 80 characters wide:
|
47
|
+
|
48
|
+
```
|
49
|
+
================================================================================
|
50
|
+
Name DOB Title
|
51
|
+
--------------------------------------------------------------------------------
|
52
|
+
Rod 06-15-80 Software Engineer
|
53
|
+
Julia 04-25-81 Piano Teacher
|
54
|
+
================================================================================
|
55
|
+
```
|
56
|
+
|
57
|
+
This usage takes advantage of the fact that ConsoleTable infers a lot of defaults. For more details about how to exercise the real power of ConsoleTable, refer to the next section.
|
58
|
+
|
33
59
|
## Usage
|
34
60
|
|
35
61
|
ConsoleTable needs a lot of information to get going, and it can be somewhat awkward to work with.
|
@@ -371,6 +397,33 @@ Which will yield this, if you're into that sort of thing:
|
|
371
397
|
*======================================================*
|
372
398
|
```
|
373
399
|
|
400
|
+
A lot of times, supplying less information means that ConsoleTable will just guess at various defaults. For example, if you do not provide a key for the table config, ConsoleTable will simply number your keys with names like 'col1', 'col2', etc. If you do not provide a title, it will `capitalize` whatever key you are using for the column, or use something like "Column 1" if you do not supply one. If you do not supply a size, ConsoleTable will just assume you want "*" as the size (remember, you can have many "*"s and the space is divided equally).
|
401
|
+
|
402
|
+
Additionally, you can give ONLY the titles for columns by simply suppling an array of strings as the console config. With this ability, it's possible to define and use ConsoleTable in a remarkably abbreviated manner, where most of the control of the formatting is unexercised.
|
403
|
+
|
404
|
+
```ruby
|
405
|
+
require 'console_table'
|
406
|
+
|
407
|
+
ConsoleTable.define(["Name", "DOB", "Title"]) do |table|
|
408
|
+
table << ["Rod", "06-15-80", "Software Engineer"]
|
409
|
+
table << ["Julia", "04-25-81", "Piano Teacher"]
|
410
|
+
end
|
411
|
+
|
412
|
+
```
|
413
|
+
|
414
|
+
Which yields:
|
415
|
+
```
|
416
|
+
================================================================================
|
417
|
+
Name DOB Title
|
418
|
+
--------------------------------------------------------------------------------
|
419
|
+
Rod 06-15-80 Software Engineer
|
420
|
+
Julia 04-25-81 Piano Teacher
|
421
|
+
================================================================================
|
422
|
+
```
|
423
|
+
|
424
|
+
Which is the original Quick Start example up above.
|
425
|
+
|
426
|
+
For more examples of different ways of using ConsoleTable, refer to the test file in this repository, which has many many different examples of usage.
|
374
427
|
|
375
428
|
## Contributing
|
376
429
|
|
data/lib/console_table.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module ConsoleTable
|
2
|
-
VERSION = "0.1.
|
2
|
+
VERSION = "0.1.7"
|
3
3
|
|
4
4
|
def self.define(layout, options={}, &block)
|
5
5
|
table = ConsoleTableClass.new(layout, options)
|
@@ -19,18 +19,31 @@ module ConsoleTable
|
|
19
19
|
|
20
20
|
def initialize(column_layout, options={})
|
21
21
|
@original_column_layout = []
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
22
|
+
|
23
|
+
if column_layout.is_a? Fixnum
|
24
|
+
column_layout = (1..column_layout).collect{|i| "Column #{i}"}
|
25
|
+
end
|
26
|
+
|
27
|
+
if column_layout.is_a? Array
|
28
|
+
column_layout.each_with_index do |layout, i|
|
29
|
+
if layout.is_a? String
|
30
|
+
@original_column_layout << {:key => "col#{i+1}".to_sym, :title=>layout, :size=>"*"}
|
31
|
+
elsif layout.is_a? Fixnum
|
32
|
+
@original_column_layout << {:key => "col#{i+1}".to_sym, :title=>"Column #{i+1}", :size=>layout}
|
33
|
+
elsif layout.is_a? Float
|
34
|
+
@original_column_layout << {:key => "col#{i+1}".to_sym, :title=>"Column #{i+1}", :size=>layout}
|
35
|
+
elsif layout[:key].nil? and layout[:title].nil?
|
36
|
+
@original_column_layout << layout.merge({:key => "col#{i+1}".to_sym, :title=>"Column #{i+1}"})
|
37
|
+
elsif layout[:title].nil?
|
38
|
+
@original_column_layout << layout.merge({:title => layout[:key].to_s.capitalize})
|
39
|
+
elsif layout[:key].nil?
|
40
|
+
@original_column_layout << layout.merge({:key => "col#{i+1}".to_sym})
|
41
|
+
else
|
42
|
+
@original_column_layout << layout
|
43
|
+
end
|
33
44
|
end
|
45
|
+
else
|
46
|
+
raise("Column layout invalid, must be a num of columns or an array of column definitions")
|
34
47
|
end
|
35
48
|
|
36
49
|
#Mostly used for mocking/testing
|
@@ -41,6 +54,7 @@ module ConsoleTable
|
|
41
54
|
@borders = options[:borders] || false #Lines between every cell, implies outline
|
42
55
|
@left_margin = options[:left_margin] || 0
|
43
56
|
@right_margin = options[:right_margin] || 0
|
57
|
+
@headings = options[:headings].nil? ? true : options[:headings]
|
44
58
|
|
45
59
|
#Set outline, just the upper and lower lines
|
46
60
|
if @borders
|
@@ -169,7 +183,7 @@ module ConsoleTable
|
|
169
183
|
return
|
170
184
|
end
|
171
185
|
|
172
|
-
print_headings unless @headings_printed
|
186
|
+
print_headings unless @headings_printed or not @headings
|
173
187
|
|
174
188
|
if options.is_a? Array #If an array or something is supplied, infer the order from the heading order
|
175
189
|
munged_options = {}
|
@@ -180,7 +194,8 @@ module ConsoleTable
|
|
180
194
|
options = munged_options
|
181
195
|
end
|
182
196
|
|
183
|
-
|
197
|
+
|
198
|
+
print_line if @borders unless not @headings and @count == 0
|
184
199
|
|
185
200
|
@out.print " "*@left_margin
|
186
201
|
if @borders
|
data/test/test_console_table.rb
CHANGED
@@ -11,17 +11,13 @@ class ConsoleTableTest < Minitest::Test
|
|
11
11
|
@mock_out = StringIO.new
|
12
12
|
end
|
13
13
|
|
14
|
-
def teardown
|
15
|
-
# Do nothing
|
16
|
-
end
|
17
|
-
|
18
14
|
def test_basic
|
19
15
|
table_config = [
|
20
|
-
{:key
|
21
|
-
{:key
|
16
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
17
|
+
{:key => :col2, :size => 20, :title => "Column 2"},
|
22
18
|
]
|
23
19
|
|
24
|
-
ConsoleTable.define(table_config, :width=> 100, :output=>@mock_out) do |table|
|
20
|
+
ConsoleTable.define(table_config, :width => 100, :output=>@mock_out) do |table|
|
25
21
|
table << {
|
26
22
|
:col1 => "Row 1, Column 1",
|
27
23
|
:col2 => "Row 1, Column 2"
|
@@ -48,12 +44,12 @@ Row 2, Column 1 Row 2, Column 1
|
|
48
44
|
|
49
45
|
def test_unicode
|
50
46
|
table_config = [
|
51
|
-
{:key
|
52
|
-
{:key
|
53
|
-
{:key
|
47
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
48
|
+
{:key => :col2, :size => 20, :title => "Column 2"},
|
49
|
+
{:key => :col3, :size => 20, :title => "Column 3"},
|
54
50
|
]
|
55
51
|
|
56
|
-
ConsoleTable.define(table_config, :width=> 62, :output
|
52
|
+
ConsoleTable.define(table_config, :width => 62, :output => @mock_out) do |table|
|
57
53
|
table << {
|
58
54
|
:col1 => "I ♥ Unicode",
|
59
55
|
:col2 => "I ☂ Unicode",
|
@@ -67,9 +63,9 @@ Row 2, Column 1 Row 2, Column 1
|
|
67
63
|
}
|
68
64
|
|
69
65
|
table << {
|
70
|
-
:col1 => {:text => "I ⁂ Unicode", :justify
|
71
|
-
:col2 => {:text => "I \u2190 Unicode", :justify
|
72
|
-
:col3 => {:text => "I ✓ Unicode", :justify
|
66
|
+
:col1 => {:text => "I ⁂ Unicode", :justify => :right},
|
67
|
+
:col2 => {:text => "I \u2190 Unicode", :justify => :center},
|
68
|
+
:col3 => {:text => "I ✓ Unicode", :justify => :left}
|
73
69
|
}
|
74
70
|
end
|
75
71
|
|
@@ -88,12 +84,12 @@ I ⚂ Unicode Even Mor I ¥ Unicode Even Mor I µ Unicode Even Mor
|
|
88
84
|
|
89
85
|
def test_spacing_convention_sets_justification
|
90
86
|
table_config = [
|
91
|
-
{:key
|
92
|
-
{:key
|
93
|
-
{:key
|
87
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
88
|
+
{:key => :col2, :size => 20, :title => "Column 2"},
|
89
|
+
{:key => :col3, :size => 20, :title => "Column 3"},
|
94
90
|
]
|
95
91
|
|
96
|
-
ConsoleTable.define(table_config, :width=> 62, :output
|
92
|
+
ConsoleTable.define(table_config, :width => 62, :output => @mock_out) do |table|
|
97
93
|
table << [
|
98
94
|
"\tRight",
|
99
95
|
"\tCenter\t",
|
@@ -114,16 +110,16 @@ Column 1 Column 2 Column 3
|
|
114
110
|
|
115
111
|
def test_newlines_converted_to_spaces_in_middle_stripped_at_ends
|
116
112
|
table_config = [
|
117
|
-
{:key
|
118
|
-
{:key
|
119
|
-
{:key
|
113
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
114
|
+
{:key => :col2, :size => 20, :title => "Column 2"},
|
115
|
+
{:key => :col3, :size => 20, :title => "Column 3"},
|
120
116
|
]
|
121
117
|
|
122
|
-
ConsoleTable.define(table_config, :width=> 62, :output
|
118
|
+
ConsoleTable.define(table_config, :width => 62, :output => @mock_out) do |table|
|
123
119
|
table << [
|
124
|
-
{:text=>"Bl\nah", :justify
|
125
|
-
{:text=>"\nStuff", :justify
|
126
|
-
{:text=>"Junk\n", :justify
|
120
|
+
{:text => "Bl\nah", :justify => :left},
|
121
|
+
{:text => "\nStuff", :justify => :left},
|
122
|
+
{:text => "Junk\n", :justify => :right}
|
127
123
|
]
|
128
124
|
end
|
129
125
|
|
@@ -140,11 +136,11 @@ Bl ah Stuff Junk
|
|
140
136
|
|
141
137
|
def test_linebreak_inside_colorcode_still_resets
|
142
138
|
table_config = [
|
143
|
-
{:key
|
144
|
-
{:key
|
139
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
140
|
+
{:key => :col2, :size => 5, :title => "Column 2"},
|
145
141
|
]
|
146
142
|
|
147
|
-
ConsoleTable.define(table_config, :width=> 62, :output
|
143
|
+
ConsoleTable.define(table_config, :width => 62, :output => @mock_out) do |table|
|
148
144
|
table << [
|
149
145
|
"Bl\nah".blue,
|
150
146
|
"1234\nStuff".red
|
@@ -159,18 +155,18 @@ Bl ah 1234
|
|
159
155
|
==========================
|
160
156
|
END
|
161
157
|
|
162
|
-
assert_includes @mock_out.string, "\e[0;34;49mBl ah\e[0m"
|
163
|
-
assert_includes @mock_out.string, "\e[0;31;49m1234 \e[0m"
|
158
|
+
assert_includes @mock_out.string, "\e[0;34;49mBl ah\e[0m" #ensure the color got reset
|
159
|
+
assert_includes @mock_out.string, "\e[0;31;49m1234 \e[0m" #ensure the color got reset
|
164
160
|
|
165
161
|
assert_output_equal expected, @mock_out.string
|
166
162
|
end
|
167
163
|
|
168
164
|
def test_spacing_after_colors_is_preserved
|
169
165
|
table_config = [
|
170
|
-
{:key
|
166
|
+
{:key => :col1, :size => 60, :title => "Column 1"},
|
171
167
|
]
|
172
168
|
|
173
|
-
ConsoleTable.define(table_config, :width=> 60, :output
|
169
|
+
ConsoleTable.define(table_config, :width => 60, :output => @mock_out) do |table|
|
174
170
|
table << [
|
175
171
|
"hello - \e[0;34;49mTest\e[0m - this is a test"
|
176
172
|
]
|
@@ -199,19 +195,19 @@ blah and stuff - Test
|
|
199
195
|
|
200
196
|
def test_can_ellipsize_at_column_level
|
201
197
|
table_config = [
|
202
|
-
{:key
|
203
|
-
{:key
|
198
|
+
{:key => :col1, :size => 20, :title => "Column 1", :ellipsize => true},
|
199
|
+
{:key => :col2, :size => 20, :title => "Column 2"},
|
204
200
|
]
|
205
201
|
|
206
|
-
ConsoleTable.define(table_config, :width=> 62, :output
|
202
|
+
ConsoleTable.define(table_config, :width => 62, :output => @mock_out) do |table|
|
207
203
|
table << [
|
208
204
|
"This is way too long to fit here",
|
209
205
|
"This is way too long to fit here",
|
210
206
|
]
|
211
207
|
|
212
208
|
table << [
|
213
|
-
{text: "This is way too long to fit here", :ellipsize=>false},
|
214
|
-
{text: "This is way too long to fit here", :ellipsize=>true},
|
209
|
+
{text: "This is way too long to fit here", :ellipsize => false},
|
210
|
+
{text: "This is way too long to fit here", :ellipsize => true},
|
215
211
|
]
|
216
212
|
end
|
217
213
|
|
@@ -229,19 +225,19 @@ This is way too long This is way too l...
|
|
229
225
|
|
230
226
|
def test_justify_convention_followed_in_hash_text_but_overrideable
|
231
227
|
table_config = [
|
232
|
-
{:key
|
228
|
+
{:key => :col1, :size => 20, :title => "Column 1", :justify => :center},
|
233
229
|
]
|
234
230
|
|
235
|
-
ConsoleTable.define(table_config, :width=> 62, :output
|
231
|
+
ConsoleTable.define(table_config, :width => 62, :output => @mock_out) do |table|
|
236
232
|
table << ["Short"]
|
237
233
|
table << ["\tShort"]
|
238
234
|
table << ["Short\t"]
|
239
|
-
table << [{:text=>"Short", :justify
|
240
|
-
table << [{:text=>"Short", :justify
|
241
|
-
table << [{:text=>"\tShort"}]
|
242
|
-
table << [{:text=>"Short\t"}]
|
243
|
-
table << [{:text=>"\tShort", :justify
|
244
|
-
table << [{:text=>"Short\t", :justify
|
235
|
+
table << [{:text => "Short", :justify => :right}]
|
236
|
+
table << [{:text => "Short", :justify => :left}]
|
237
|
+
table << [{:text => "\tShort"}]
|
238
|
+
table << [{:text => "Short\t"}]
|
239
|
+
table << [{:text => "\tShort", :justify => :left}] #Override
|
240
|
+
table << [{:text => "Short\t", :justify => :right}] #Override
|
245
241
|
end
|
246
242
|
|
247
243
|
expected=<<-END
|
@@ -265,12 +261,12 @@ Short
|
|
265
261
|
|
266
262
|
def test_should_not_color_tabs_or_ignore_tab_justify_convention_if_inside_color
|
267
263
|
table_config = [
|
268
|
-
{:key
|
269
|
-
{:key
|
270
|
-
{:key
|
264
|
+
{:key => :col1, :size => 20, :title => "Column 1", :justify => :center},
|
265
|
+
{:key => :col2, :size => 20, :title => "Column 2", :justify => :right},
|
266
|
+
{:key => :col3, :size => 20, :title => "Column 3", :justify => :left},
|
271
267
|
]
|
272
268
|
|
273
|
-
ConsoleTable.define(table_config, :width=> 62, :output
|
269
|
+
ConsoleTable.define(table_config, :width => 62, :output => @mock_out) do |table|
|
274
270
|
table << [
|
275
271
|
"\tRight".blue,
|
276
272
|
"\tCenter\t".red,
|
@@ -286,25 +282,25 @@ Short
|
|
286
282
|
==============================================================
|
287
283
|
END
|
288
284
|
|
289
|
-
assert_includes @mock_out.string, " \e[0;34;49mRight\e[0m"
|
290
|
-
assert_includes @mock_out.string, " \e[0;35;49mLeft\e[0m "
|
291
|
-
assert_includes @mock_out.string, " \e[0;31;49mCenter\e[0m"
|
285
|
+
assert_includes @mock_out.string, " \e[0;34;49mRight\e[0m" #space is on outside of coor
|
286
|
+
assert_includes @mock_out.string, " \e[0;35;49mLeft\e[0m " #space is on outside of color
|
287
|
+
assert_includes @mock_out.string, " \e[0;31;49mCenter\e[0m" #this assert fails due to what I'm pretty sure is a bug in gsub(), but it's not the end of the world so I'm not doing a workaround
|
292
288
|
|
293
289
|
assert_output_equal expected, @mock_out.string
|
294
290
|
end
|
295
291
|
|
296
292
|
def test_spaces_preserved_in_middle_but_stripped_at_ends
|
297
293
|
table_config = [
|
298
|
-
{:key
|
299
|
-
{:key
|
300
|
-
{:key
|
294
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
295
|
+
{:key => :col2, :size => 20, :title => "Column 2"},
|
296
|
+
{:key => :col3, :size => 20, :title => "Column 3"},
|
301
297
|
]
|
302
298
|
|
303
|
-
ConsoleTable.define(table_config, :width=> 62, :output
|
299
|
+
ConsoleTable.define(table_config, :width => 62, :output => @mock_out) do |table|
|
304
300
|
table << [
|
305
|
-
{:text=>"Bl ah", :justify
|
306
|
-
{:text=>" Stuff", :justify
|
307
|
-
{:text=>"Junk ", :justify
|
301
|
+
{:text => "Bl ah", :justify => :left},
|
302
|
+
{:text => " Stuff", :justify => :left},
|
303
|
+
{:text => "Junk ", :justify => :right}
|
308
304
|
]
|
309
305
|
end
|
310
306
|
|
@@ -321,16 +317,16 @@ Bl ah Stuff Junk
|
|
321
317
|
|
322
318
|
def test_ignores_tabbing_convention_if_setting_justification_explicitly
|
323
319
|
table_config = [
|
324
|
-
{:key
|
325
|
-
{:key
|
326
|
-
{:key
|
320
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
321
|
+
{:key => :col2, :size => 20, :title => "Column 2"},
|
322
|
+
{:key => :col3, :size => 20, :title => "Column 3"},
|
327
323
|
]
|
328
324
|
|
329
|
-
ConsoleTable.define(table_config, :width=> 62, :output
|
325
|
+
ConsoleTable.define(table_config, :width => 62, :output => @mock_out) do |table|
|
330
326
|
table << [
|
331
|
-
{:text=>"\tBlah", :justify
|
332
|
-
{:text=>"\tStuff\t", :justify
|
333
|
-
{:text=>"\tJunk", :justify
|
327
|
+
{:text => "\tBlah", :justify => :left},
|
328
|
+
{:text => "\tStuff\t", :justify => :right},
|
329
|
+
{:text => "\tJunk", :justify => :center}
|
334
330
|
]
|
335
331
|
end
|
336
332
|
|
@@ -347,20 +343,20 @@ Blah Stuff Junk
|
|
347
343
|
|
348
344
|
def test_can_use_convenient_operator
|
349
345
|
table_config = [
|
350
|
-
{:key
|
351
|
-
{:key
|
346
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
347
|
+
{:key => :col2, :size => 20, :title => "Column 2"},
|
352
348
|
]
|
353
349
|
|
354
|
-
ConsoleTable.define(table_config, :width=>100, :output
|
350
|
+
ConsoleTable.define(table_config, :width => 100, :output => @mock_out) do |table|
|
355
351
|
table << {
|
356
|
-
|
357
|
-
|
358
|
-
|
352
|
+
:col1 => "Row 1, Column 1",
|
353
|
+
:col2 => "Row 1, Column 2"
|
354
|
+
}
|
359
355
|
|
360
356
|
table << {
|
361
|
-
|
362
|
-
|
363
|
-
|
357
|
+
:col1 => "Row 2, Column 1",
|
358
|
+
:col2 => "Row 2, Column 1"
|
359
|
+
}
|
364
360
|
end
|
365
361
|
|
366
362
|
expected=<<-END
|
@@ -377,19 +373,19 @@ Row 2, Column 1 Row 2, Column 1
|
|
377
373
|
|
378
374
|
def test_can_supply_array_and_order_is_inferred
|
379
375
|
table_config = [
|
380
|
-
{:key
|
381
|
-
{:key
|
376
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
377
|
+
{:key => :col2, :size => 20, :title => "Column 2"},
|
382
378
|
]
|
383
379
|
|
384
|
-
ConsoleTable.define(table_config, :width=> 100, :output
|
380
|
+
ConsoleTable.define(table_config, :width => 100, :output => @mock_out) do |table|
|
385
381
|
table << [
|
386
382
|
"Row 1, Column 1",
|
387
383
|
"Row 1, Column 2"
|
388
384
|
]
|
389
385
|
|
390
386
|
table << [
|
391
|
-
{:text=>"Row 2, Column 1", :justify
|
392
|
-
{:text=>"Row 2, Column 2", :justify
|
387
|
+
{:text => "Row 2, Column 1", :justify => :center},
|
388
|
+
{:text => "Row 2, Column 2", :justify => :right}
|
393
389
|
]
|
394
390
|
end
|
395
391
|
|
@@ -407,11 +403,11 @@ Row 1, Column 1 Row 1, Column 2
|
|
407
403
|
|
408
404
|
def test_percents
|
409
405
|
table_config = [
|
410
|
-
{:key
|
411
|
-
{:key
|
406
|
+
{:key => :col1, :size => 0.3, :title => "Column 1"},
|
407
|
+
{:key => :col2, :size => 0.7, :title => "Column 2"},
|
412
408
|
]
|
413
409
|
|
414
|
-
ConsoleTable.define(table_config, :width=> 100, :output
|
410
|
+
ConsoleTable.define(table_config, :width => 100, :output => @mock_out) do |table|
|
415
411
|
table << {
|
416
412
|
:col1 => "Row 1, Column 1",
|
417
413
|
:col2 => "Row 1, Column 2"
|
@@ -434,11 +430,11 @@ Row 2, Column 1 Row 2, Column 1
|
|
434
430
|
|
435
431
|
def test_star_fills_all_extra_space
|
436
432
|
table_config = [
|
437
|
-
{:key
|
438
|
-
{:key
|
433
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
434
|
+
{:key => :col2, :size => "*", :title => "Column 2"},
|
439
435
|
]
|
440
436
|
|
441
|
-
ConsoleTable.define(table_config, :width=> 100, :output
|
437
|
+
ConsoleTable.define(table_config, :width => 100, :output => @mock_out) do |table|
|
442
438
|
table << {
|
443
439
|
:col1 => "Row 1, Column 1",
|
444
440
|
:col2 => "Row 1, Column 2"
|
@@ -464,12 +460,12 @@ Row 2, Column 1 Row 2, Column 1
|
|
464
460
|
|
465
461
|
def test_multiple_stars_split_evenly
|
466
462
|
table_config = [
|
467
|
-
{:key
|
468
|
-
{:key
|
469
|
-
{:key
|
463
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
464
|
+
{:key => :col2, :size => "*", :title => "Column 2"},
|
465
|
+
{:key => :col3, :size => "*", :title => "Column 3"},
|
470
466
|
]
|
471
467
|
|
472
|
-
ConsoleTable.define(table_config, :width=> 100, :output
|
468
|
+
ConsoleTable.define(table_config, :width => 100, :output => @mock_out) do |table|
|
473
469
|
table << {
|
474
470
|
:col1 => "Row 1, Column 1",
|
475
471
|
:col2 => "Row 1, Column 2",
|
@@ -497,11 +493,11 @@ Row 2, Column 1 Row 2, Column 1 Row 2, Column 3
|
|
497
493
|
|
498
494
|
def test_no_size_assumed_to_be_star
|
499
495
|
table_config = [
|
500
|
-
{:key
|
501
|
-
{:key
|
496
|
+
{:key => :col1, :title => "Column 1"},
|
497
|
+
{:key => :col2, :title => "Column 2"},
|
502
498
|
]
|
503
499
|
|
504
|
-
ConsoleTable.define(table_config, :width=> 40, :output
|
500
|
+
ConsoleTable.define(table_config, :width => 40, :output => @mock_out) do |table|
|
505
501
|
table << {
|
506
502
|
:col1 => "Row 1, Column 1",
|
507
503
|
:col2 => "Row 1, Column 2",
|
@@ -522,11 +518,11 @@ Row 1, Column 1 Row 1, Column 2
|
|
522
518
|
|
523
519
|
def test_no_name_defaulted_to_capitalize_of_key_name
|
524
520
|
table_config = [
|
525
|
-
{:key
|
526
|
-
{:key
|
521
|
+
{:key => :col1},
|
522
|
+
{:key => :col2},
|
527
523
|
]
|
528
524
|
|
529
|
-
ConsoleTable.define(table_config, :width=> 40, :output
|
525
|
+
ConsoleTable.define(table_config, :width => 40, :output => @mock_out) do |table|
|
530
526
|
table << {
|
531
527
|
:col1 => "Row 1, Column 1",
|
532
528
|
:col2 => "Row 1, Column 2",
|
@@ -547,16 +543,16 @@ Row 1, Column 1 Row 1, Column 2
|
|
547
543
|
|
548
544
|
def test_can_combine_percentages_fixed_and_stars
|
549
545
|
table_config = [
|
550
|
-
{:key
|
551
|
-
{:key
|
552
|
-
{:key
|
553
|
-
{:key
|
554
|
-
{:key
|
555
|
-
{:key
|
556
|
-
{:key
|
546
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
547
|
+
{:key => :col2, :size => 0.3, :title => "Column 2"},
|
548
|
+
{:key => :col3, :size => 4, :title => "Column 3"},
|
549
|
+
{:key => :col4, :size => "*", :title => "Column 4"},
|
550
|
+
{:key => :col5, :size => 0.2, :title => "Column 5"},
|
551
|
+
{:key => :col6, :size => "*", :title => "Column 6"},
|
552
|
+
{:key => :col7, :size => 10, :title => "Column 7"}
|
557
553
|
]
|
558
554
|
|
559
|
-
ConsoleTable.define(table_config, :width=> 160, :output
|
555
|
+
ConsoleTable.define(table_config, :width => 160, :output => @mock_out) do |table|
|
560
556
|
table << {
|
561
557
|
:col1 => "Row 1, Column 1",
|
562
558
|
:col2 => "Row 1, Column 2",
|
@@ -592,17 +588,17 @@ Row 2, Column 1 Row 2, Column 2 Row Row 2, Column 4
|
|
592
588
|
|
593
589
|
def test_wont_create_layout_too_large_for_screen
|
594
590
|
table_config = [
|
595
|
-
{:key
|
596
|
-
{:key
|
591
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
592
|
+
{:key => :col2, :size => 20, :title => "Column 2"},
|
597
593
|
]
|
598
594
|
|
599
|
-
assert_raises(RuntimeError) { ConsoleTable.define(table_config, :width=>30) }
|
595
|
+
assert_raises(RuntimeError) { ConsoleTable.define(table_config, :width => 30) }
|
600
596
|
end
|
601
597
|
|
602
598
|
def test_wont_create_layout_with_more_than_100_percent
|
603
599
|
table_config = [
|
604
|
-
{:key
|
605
|
-
{:key
|
600
|
+
{:key => :col1, :size => 0.8, :title => "Column 1"},
|
601
|
+
{:key => :col2, :size => 0.3, :title => "Column 2"},
|
606
602
|
]
|
607
603
|
|
608
604
|
assert_raises(RuntimeError) { ConsoleTable.define(table_config) }
|
@@ -610,8 +606,8 @@ Row 2, Column 1 Row 2, Column 2 Row Row 2, Column 4
|
|
610
606
|
|
611
607
|
def test_wont_create_layout_with_invalid_size
|
612
608
|
table_config = [
|
613
|
-
{:key
|
614
|
-
{:key
|
609
|
+
{:key => :col1, :size => 0.8, :title => "Column 1"},
|
610
|
+
{:key => :col2, :size => "hello!", :title => "Column 2"},
|
615
611
|
]
|
616
612
|
|
617
613
|
assert_raises(RuntimeError) { ConsoleTable.define(table_config) }
|
@@ -619,34 +615,30 @@ Row 2, Column 1 Row 2, Column 2 Row Row 2, Column 4
|
|
619
615
|
|
620
616
|
def test_wont_allow_repeats_of_key_names
|
621
617
|
table_config = [
|
622
|
-
{:key
|
623
|
-
{:key
|
618
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
619
|
+
{:key => :col1, :size => 20, :title => "Column 2"},
|
624
620
|
]
|
625
621
|
|
626
622
|
assert_raises(RuntimeError) { ConsoleTable.define(table_config) }
|
627
623
|
end
|
628
624
|
|
629
|
-
def
|
630
|
-
|
631
|
-
|
632
|
-
{:size=>20, :title=>"Column 2"},
|
633
|
-
]
|
634
|
-
|
635
|
-
assert_raises(RuntimeError) { ConsoleTable.define(table_config) }
|
625
|
+
def test_wont_create_layout_when_column_layout_isnt_num_or_array
|
626
|
+
assert_raises(RuntimeError) { ConsoleTable.define("4", :width => 30) }
|
627
|
+
assert_raises(RuntimeError) { ConsoleTable.define({}, :width => 30) }
|
636
628
|
end
|
637
629
|
|
638
630
|
def test_can_truncate_output
|
639
631
|
table_config = [
|
640
|
-
{:key
|
632
|
+
{:key => :col1, :size => 20, :title => "Column 1"}
|
641
633
|
]
|
642
634
|
|
643
|
-
ConsoleTable.define(table_config, :width=> 100, :output
|
635
|
+
ConsoleTable.define(table_config, :width => 100, :output => @mock_out) do |table|
|
644
636
|
table << ["This is short"]
|
645
637
|
|
646
|
-
table << {:col1=>"This is way too long and it needs to get cut off"}
|
638
|
+
table << {:col1 => "This is way too long and it needs to get cut off"}
|
647
639
|
|
648
640
|
table << [
|
649
|
-
{:text=>"This is way too long and it needs to get cut off", :ellipsize=>true}
|
641
|
+
{:text => "This is way too long and it needs to get cut off", :ellipsize => true}
|
650
642
|
]
|
651
643
|
|
652
644
|
end
|
@@ -666,28 +658,28 @@ This is way too l...
|
|
666
658
|
|
667
659
|
def test_can_justify_columns_and_override_in_rows
|
668
660
|
table_config = [
|
669
|
-
{:key
|
670
|
-
{:key
|
671
|
-
{:key
|
661
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
662
|
+
{:key => :col2, :size => 20, :title => "Column 2", :justify => :center},
|
663
|
+
{:key => :col3, :size => 20, :title => "Column 3", :justify => :right}
|
672
664
|
]
|
673
665
|
|
674
|
-
ConsoleTable.define(table_config, :width=> 100, :output
|
666
|
+
ConsoleTable.define(table_config, :width => 100, :output => @mock_out) do |table|
|
675
667
|
table << {
|
676
|
-
|
677
|
-
|
678
|
-
|
668
|
+
:col1 => "Short1",
|
669
|
+
:col2 => "Short2",
|
670
|
+
:col3 => "Short3"
|
679
671
|
}
|
680
672
|
|
681
673
|
table << {
|
682
|
-
|
683
|
-
|
684
|
-
|
674
|
+
:col1 => {text: "Short1"},
|
675
|
+
:col2 => {text: "Short2"},
|
676
|
+
:col3 => {text: "Short3"}
|
685
677
|
}
|
686
678
|
|
687
679
|
table << {
|
688
|
-
:col1 => {text: "Short1", :justify
|
689
|
-
:col2 => {text: "Short2", :justify
|
690
|
-
:col3 => {text: "Short3", :justify
|
680
|
+
:col1 => {text: "Short1", :justify => :center},
|
681
|
+
:col2 => {text: "Short2", :justify => :right},
|
682
|
+
:col3 => {text: "Short3", :justify => :left}
|
691
683
|
}
|
692
684
|
end
|
693
685
|
|
@@ -707,32 +699,32 @@ Short1 Short2 Short3
|
|
707
699
|
def test_huge_example
|
708
700
|
|
709
701
|
table_config = [
|
710
|
-
{:key
|
711
|
-
{:key
|
712
|
-
{:key
|
713
|
-
{:key
|
702
|
+
{:key => :title, :size => 15, :title => "Movie Title"},
|
703
|
+
{:key => :name, :size => 15, :title => "Name"},
|
704
|
+
{:key => :release_date, :size => 8, :title => "Release Date Too Long"},
|
705
|
+
{:key => :tagline, :size => "*", :title => "Motto", :justify => :right},
|
714
706
|
]
|
715
707
|
|
716
|
-
ConsoleTable.define(table_config, :left_margin=>5, :right_margin=>10, :width=>80, :title=>"Movie Killers", :output
|
708
|
+
ConsoleTable.define(table_config, :left_margin => 5, :right_margin => 10, :width => 80, :title => "Movie Killers", :output => @mock_out) do |table|
|
717
709
|
table << {
|
718
|
-
:title=>{:text=>"Friday the 13th"},
|
719
|
-
:name=>{:text=>"Jason's Mom", :justify
|
720
|
-
:release_date=>{text: "05-09-80"},
|
721
|
-
:tagline=>{:text=>"They were warned...They are doomed...And on Friday the 13th, nothing will save them.", :ellipsize=>true}
|
710
|
+
:title => {:text => "Friday the 13th"},
|
711
|
+
:name => {:text => "Jason's Mom", :justify => :left},
|
712
|
+
:release_date => {text: "05-09-80"},
|
713
|
+
:tagline => {:text => "They were warned...They are doomed...And on Friday the 13th, nothing will save them.", :ellipsize => true}
|
722
714
|
}
|
723
715
|
|
724
716
|
table << {
|
725
|
-
:title=>{:text=>"Halloween"},
|
726
|
-
:name=>{:text=>"Michael Meyers", :justify
|
727
|
-
:release_date=>{text: "10-25-80"},
|
728
|
-
:tagline=>{:text=>"Everyone is entitled to one good scare", :ellipsize=>true}
|
717
|
+
:title => {:text => "Halloween"},
|
718
|
+
:name => {:text => "Michael Meyers", :justify => :left},
|
719
|
+
:release_date => {text: "10-25-80"},
|
720
|
+
:tagline => {:text => "Everyone is entitled to one good scare", :ellipsize => true}
|
729
721
|
}
|
730
722
|
|
731
723
|
table << {
|
732
|
-
:title=>{:text=>"Nightmare on Elm St."},
|
733
|
-
:name=>{:text=>"Freddy Krueger", :justify
|
734
|
-
:release_date=>{text: "11-16-84"},
|
735
|
-
:tagline=>{:text=>"A scream that wakes you up, might be your own", :ellipsize=>true}
|
724
|
+
:title => {:text => "Nightmare on Elm St."},
|
725
|
+
:name => {:text => "Freddy Krueger", :justify => :left},
|
726
|
+
:release_date => {text: "11-16-84"},
|
727
|
+
:tagline => {:text => "A scream that wakes you up, might be your own", :ellipsize => true}
|
736
728
|
}
|
737
729
|
|
738
730
|
table << ["Hellraiser", "Pinhead", "9-18-87", "Demon to some. Angel to others."]
|
@@ -763,11 +755,11 @@ Short1 Short2 Short3
|
|
763
755
|
|
764
756
|
def test_printing_a_single_string_does_full_line
|
765
757
|
table_config = [
|
766
|
-
{:key
|
767
|
-
{:key
|
758
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
759
|
+
{:key => :col2, :size => 20, :title => "Column 2"},
|
768
760
|
]
|
769
761
|
|
770
|
-
ConsoleTable.define(table_config, :width=> 100, :output
|
762
|
+
ConsoleTable.define(table_config, :width => 100, :output => @mock_out) do |table|
|
771
763
|
table << "This is just a string, it should ignore columns"
|
772
764
|
end
|
773
765
|
|
@@ -782,11 +774,11 @@ This is just a string, it should ignore c
|
|
782
774
|
|
783
775
|
def test_printing_a_single_after_data_makes_headings_show_up
|
784
776
|
table_config = [
|
785
|
-
{:key
|
786
|
-
{:key
|
777
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
778
|
+
{:key => :col2, :size => 20, :title => "Column 2"},
|
787
779
|
]
|
788
780
|
|
789
|
-
ConsoleTable.define(table_config, :width=> 100, :output
|
781
|
+
ConsoleTable.define(table_config, :width => 100, :output => @mock_out) do |table|
|
790
782
|
table << ["One", "Two"]
|
791
783
|
table << "This is just a string, it should ignore columns"
|
792
784
|
table << ["One", "Two"]
|
@@ -807,15 +799,15 @@ One Two
|
|
807
799
|
|
808
800
|
def test_can_have_a_bordered_table
|
809
801
|
table_config = [
|
810
|
-
{:key
|
811
|
-
{:key
|
812
|
-
{:key
|
813
|
-
{:key
|
802
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
803
|
+
{:key => :col2, :size => 0.3, :title => "Column 2"},
|
804
|
+
{:key => :col3, :size => 10, :title => "Column 3", :justify => :center},
|
805
|
+
{:key => :col4, :size => "*", :title => "Column 4", :justify => :center}
|
814
806
|
]
|
815
807
|
|
816
|
-
ConsoleTable.define(table_config, :left_margin=>10, :right_margin=>7, :width=> 100, :title=>"Test Title", :borders=>true, :output
|
808
|
+
ConsoleTable.define(table_config, :left_margin => 10, :right_margin => 7, :width => 100, :title => "Test Title", :borders => true, :output => @mock_out) do |table|
|
817
809
|
(1..5).each do |row|
|
818
|
-
table << (1..4).collect{|i| "Row #{row}, Column #{i}".red}
|
810
|
+
table << (1..4).collect { |i| "Row #{row}, Column #{i}".red }
|
819
811
|
end
|
820
812
|
|
821
813
|
table << "Plain line needs borders"
|
@@ -853,16 +845,16 @@ One Two
|
|
853
845
|
|
854
846
|
def test_outline_joins_only_when_no_footer_or_header
|
855
847
|
table_config = [
|
856
|
-
{:key
|
857
|
-
{:key
|
858
|
-
{:key
|
859
|
-
{:key
|
848
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
849
|
+
{:key => :col2, :size => 0.3, :title => "Column 2"},
|
850
|
+
{:key => :col3, :size => 10, :title => "Column 3", :justify => :center},
|
851
|
+
{:key => :col4, :size => "*", :title => "Column 4", :justify => :center}
|
860
852
|
]
|
861
853
|
|
862
854
|
#borders are true, so outline false should be ignored
|
863
|
-
ConsoleTable.define(table_config, :left_margin=>10, :right_margin=>7, :width=> 100, :borders=>true, :outline=>false, :output
|
855
|
+
ConsoleTable.define(table_config, :left_margin => 10, :right_margin => 7, :width => 100, :borders => true, :outline => false, :output => @mock_out) do |table|
|
864
856
|
(1..5).each do |row|
|
865
|
-
table << (1..4).collect{|i| "Row #{row}, Column #{i}"}
|
857
|
+
table << (1..4).collect { |i| "Row #{row}, Column #{i}" }
|
866
858
|
end
|
867
859
|
|
868
860
|
table << "Plain line needs borders"
|
@@ -892,13 +884,13 @@ One Two
|
|
892
884
|
|
893
885
|
def test_can_have_no_outline_if_requested
|
894
886
|
table_config = [
|
895
|
-
{:key
|
896
|
-
{:key
|
887
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
888
|
+
{:key => :col2, :size => 0.3, :title => "Column 2"},
|
897
889
|
]
|
898
890
|
|
899
|
-
ConsoleTable.define(table_config, :width=>60, :outline=>false, :title=>"Still has a title", :output
|
891
|
+
ConsoleTable.define(table_config, :width => 60, :outline => false, :title => "Still has a title", :output => @mock_out) do |table|
|
900
892
|
(1..5).each do |row|
|
901
|
-
table << (1..2).collect{|i| "Row #{row}, Column #{i}"}
|
893
|
+
table << (1..2).collect { |i| "Row #{row}, Column #{i}" }
|
902
894
|
end
|
903
895
|
|
904
896
|
end
|
@@ -919,26 +911,26 @@ Row 5, Column 1 Row 5, Colu
|
|
919
911
|
|
920
912
|
def test_can_use_colors_without_affecting_layout
|
921
913
|
table_config = [
|
922
|
-
{:key
|
923
|
-
{:key
|
924
|
-
{:key
|
914
|
+
{:key => :col1, :size => 10, :title => "Column 1", :justify => :left},
|
915
|
+
{:key => :col2, :size => 10, :title => "Column 2", :justify => :center},
|
916
|
+
{:key => :col3, :size => 10, :title => "Column 3", :justify => :right},
|
925
917
|
]
|
926
918
|
|
927
|
-
ConsoleTable.define(table_config, :width=> 120, :output
|
919
|
+
ConsoleTable.define(table_config, :width => 120, :output => @mock_out) do |table|
|
928
920
|
table << ["Short".blue, "Short".bold, "Short".red.on_blue]
|
929
921
|
|
930
922
|
table << ["Much much longer".blue, "Much much longer".bold, "Much much longer".red.on_blue]
|
931
923
|
|
932
924
|
table << [
|
933
|
-
{:text=>"Much much longer".blue, :ellipsize=>true},
|
934
|
-
{:text=>"Much much longer".underline, :ellipsize=>true},
|
935
|
-
{:text=>"Much much longer".on_magenta, :ellipsize=>true}
|
925
|
+
{:text => "Much much longer".blue, :ellipsize => true},
|
926
|
+
{:text => "Much much longer".underline, :ellipsize => true},
|
927
|
+
{:text => "Much much longer".on_magenta, :ellipsize => true}
|
936
928
|
]
|
937
929
|
|
938
930
|
table << [
|
939
|
-
{:text=>"Much much longer".yellow, :ellipsize=>true},
|
940
|
-
{:text=>"Normal, should reset", :ellipsize=>true},
|
941
|
-
{:text=>"Much much longer".bold, :ellipsize=>true}
|
931
|
+
{:text => "Much much longer".yellow, :ellipsize => true},
|
932
|
+
{:text => "Normal, should reset", :ellipsize => true},
|
933
|
+
{:text => "Much much longer".bold, :ellipsize => true}
|
942
934
|
]
|
943
935
|
end
|
944
936
|
|
@@ -953,23 +945,23 @@ Much mu... Normal,... Much mu...
|
|
953
945
|
================================
|
954
946
|
END
|
955
947
|
|
956
|
-
assert_includes @mock_out.string, "\e[1;39;49mShort\e[0m"
|
957
|
-
assert_includes @mock_out.string, "\e[0;33;49mMuch mu...\e[0m"
|
948
|
+
assert_includes @mock_out.string, "\e[1;39;49mShort\e[0m" #Should have normal color codes
|
949
|
+
assert_includes @mock_out.string, "\e[0;33;49mMuch mu...\e[0m" #the cut-off one should keep the color code for ellipses, then reset
|
958
950
|
|
959
951
|
assert_output_equal expected, @mock_out.string
|
960
952
|
end
|
961
953
|
|
962
954
|
def test_will_generate_default_column_keys_and_titles_and_sizes_if_not_provided
|
963
955
|
table_config = [
|
964
|
-
{:size=>20, :title=>"Column 1"},
|
965
|
-
{:key
|
966
|
-
{:size=>20, :title=>"Column 3"},
|
967
|
-
{:key
|
968
|
-
{:size=>20},
|
956
|
+
{:size => 20, :title => "Column 1"},
|
957
|
+
{:key => :col2, :size => 20, :title => "Column 2"},
|
958
|
+
{:size => 20, :title => "Column 3"},
|
959
|
+
{:key => :col4, :size => 20},
|
960
|
+
{:size => 20},
|
969
961
|
{}
|
970
962
|
]
|
971
963
|
|
972
|
-
ConsoleTable.define(table_config, :width=> 125, :output
|
964
|
+
ConsoleTable.define(table_config, :width => 125, :output => @mock_out) do |table|
|
973
965
|
table << ["A", "B", "C", "D", "E", "F"]
|
974
966
|
|
975
967
|
end
|
@@ -987,12 +979,12 @@ A B C D
|
|
987
979
|
|
988
980
|
def test_can_use_a_string_instead_of_hash_to_default_everything
|
989
981
|
table_config = [
|
990
|
-
{:size=>20, :title=>"Column 1"},
|
982
|
+
{:size => 20, :title => "Column 1"},
|
991
983
|
"Simple Column",
|
992
|
-
{:size=>20, :title=>"Column 3"},
|
984
|
+
{:size => 20, :title => "Column 3"},
|
993
985
|
]
|
994
986
|
|
995
|
-
ConsoleTable.define(table_config, :width=> 70, :output
|
987
|
+
ConsoleTable.define(table_config, :width => 70, :output => @mock_out) do |table|
|
996
988
|
table << ["A", "B", "C"]
|
997
989
|
end
|
998
990
|
|
@@ -1012,7 +1004,7 @@ A B C
|
|
1012
1004
|
"A Column", "B Column", "C Column"
|
1013
1005
|
]
|
1014
1006
|
|
1015
|
-
ConsoleTable.define(table_config, :width=> 30, :output
|
1007
|
+
ConsoleTable.define(table_config, :width => 30, :output => @mock_out) do |table|
|
1016
1008
|
table << ["A", "B", "C"]
|
1017
1009
|
end
|
1018
1010
|
|
@@ -1027,6 +1019,275 @@ A B C
|
|
1027
1019
|
assert_output_equal expected, @mock_out.string
|
1028
1020
|
end
|
1029
1021
|
|
1022
|
+
def test_can_disable_headings
|
1023
|
+
table_config = [
|
1024
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
1025
|
+
{:key => :col2, :size => 20, :title => "Column 2"},
|
1026
|
+
]
|
1027
|
+
|
1028
|
+
ConsoleTable.define(table_config, :width => 100, :headings => false, :output => @mock_out) do |table|
|
1029
|
+
table << {
|
1030
|
+
:col1 => "Row 1, Column 1",
|
1031
|
+
:col2 => "Row 1, Column 2"
|
1032
|
+
}
|
1033
|
+
|
1034
|
+
table << {
|
1035
|
+
:col1 => "Row 2, Column 1",
|
1036
|
+
:col2 => "Row 2, Column 1"
|
1037
|
+
}
|
1038
|
+
end
|
1039
|
+
|
1040
|
+
expected=<<-END
|
1041
|
+
=========================================
|
1042
|
+
Row 1, Column 1 Row 1, Column 2
|
1043
|
+
Row 2, Column 1 Row 2, Column 1
|
1044
|
+
=========================================
|
1045
|
+
END
|
1046
|
+
|
1047
|
+
assert_output_equal expected, @mock_out.string
|
1048
|
+
|
1049
|
+
end
|
1050
|
+
|
1051
|
+
def test_can_disable_headings_and_outline
|
1052
|
+
table_config = [
|
1053
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
1054
|
+
{:key => :col2, :size => 20, :title => "Column 2"},
|
1055
|
+
]
|
1056
|
+
|
1057
|
+
ConsoleTable.define(table_config, :width => 100, :headings => false, :outline=>false, :output => @mock_out) do |table|
|
1058
|
+
table << {
|
1059
|
+
:col1 => "Row 1, Column 1",
|
1060
|
+
:col2 => "Row 1, Column 2"
|
1061
|
+
}
|
1062
|
+
|
1063
|
+
table << {
|
1064
|
+
:col1 => "Row 2, Column 1",
|
1065
|
+
:col2 => "Row 2, Column 1"
|
1066
|
+
}
|
1067
|
+
end
|
1068
|
+
|
1069
|
+
expected=<<-END
|
1070
|
+
Row 1, Column 1 Row 1, Column 2
|
1071
|
+
Row 2, Column 1 Row 2, Column 1
|
1072
|
+
END
|
1073
|
+
|
1074
|
+
assert_output_equal expected, @mock_out.string
|
1075
|
+
|
1076
|
+
end
|
1077
|
+
|
1078
|
+
def test_can_disable_headings_with_borders
|
1079
|
+
table_config = [
|
1080
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
1081
|
+
{:key => :col2, :size => 20, :title => "Column 2"},
|
1082
|
+
]
|
1083
|
+
|
1084
|
+
ConsoleTable.define(table_config, :width => 100, :headings => false, :borders => true, :output => @mock_out) do |table|
|
1085
|
+
table << {
|
1086
|
+
:col1 => "Row 1, Column 1",
|
1087
|
+
:col2 => "Row 1, Column 2"
|
1088
|
+
}
|
1089
|
+
|
1090
|
+
table << {
|
1091
|
+
:col1 => "Row 2, Column 1",
|
1092
|
+
:col2 => "Row 2, Column 1"
|
1093
|
+
}
|
1094
|
+
end
|
1095
|
+
|
1096
|
+
expected=<<-END
|
1097
|
+
*====================*====================*
|
1098
|
+
|Row 1, Column 1 |Row 1, Column 2 |
|
1099
|
+
+--------------------+--------------------+
|
1100
|
+
|Row 2, Column 1 |Row 2, Column 1 |
|
1101
|
+
*====================*====================*
|
1102
|
+
END
|
1103
|
+
|
1104
|
+
assert_output_equal expected, @mock_out.string
|
1105
|
+
end
|
1106
|
+
|
1107
|
+
def test_can_define_a_table_with_just_numbers_for_size
|
1108
|
+
table_config = [
|
1109
|
+
{:key => :col1, :size => 20, :title => "Column 1"},
|
1110
|
+
15,
|
1111
|
+
]
|
1112
|
+
|
1113
|
+
ConsoleTable.define(table_config, :width => 100, :output => @mock_out) do |table|
|
1114
|
+
table << {
|
1115
|
+
:col1 => "Row 1, Column 1",
|
1116
|
+
:col2 => "Row 1, Column 2"
|
1117
|
+
}
|
1118
|
+
|
1119
|
+
table << {
|
1120
|
+
:col1 => "Row 2, Column 1",
|
1121
|
+
:col2 => "Row 2, Column 1"
|
1122
|
+
}
|
1123
|
+
end
|
1124
|
+
|
1125
|
+
expected=<<-END
|
1126
|
+
====================================
|
1127
|
+
Column 1 Column 2
|
1128
|
+
------------------------------------
|
1129
|
+
Row 1, Column 1 Row 1, Column 2
|
1130
|
+
Row 2, Column 1 Row 2, Column 1
|
1131
|
+
====================================
|
1132
|
+
END
|
1133
|
+
|
1134
|
+
assert_output_equal expected, @mock_out.string
|
1135
|
+
|
1136
|
+
end
|
1137
|
+
|
1138
|
+
def test_can_define_a_mix_and_match_of_floats_and_hashes_and_strings
|
1139
|
+
table_config = [
|
1140
|
+
0.4,
|
1141
|
+
{:key => :a_column, :size => 20, :title => "A Column"},
|
1142
|
+
15,
|
1143
|
+
"Title"
|
1144
|
+
]
|
1145
|
+
|
1146
|
+
ConsoleTable.define(table_config, :width => 100, :output => @mock_out) do |table|
|
1147
|
+
table << [
|
1148
|
+
"Row 1, Column 1",
|
1149
|
+
"Row 1, Column 2",
|
1150
|
+
"Row 1, Column 3",
|
1151
|
+
"Row 1, Column 4"
|
1152
|
+
]
|
1153
|
+
|
1154
|
+
table << [
|
1155
|
+
"Row 2, Column 1",
|
1156
|
+
"Row 2, Column 2",
|
1157
|
+
"Row 2, Column 3",
|
1158
|
+
"Row 2, Column 4"
|
1159
|
+
]
|
1160
|
+
end
|
1161
|
+
|
1162
|
+
expected=<<-END
|
1163
|
+
===================================================================================================
|
1164
|
+
Column 1 A Column Column 3 Title
|
1165
|
+
---------------------------------------------------------------------------------------------------
|
1166
|
+
Row 1, Column 1 Row 1, Column 2 Row 1, Column 3 Row 1, Column 4
|
1167
|
+
Row 2, Column 1 Row 2, Column 2 Row 2, Column 3 Row 2, Column 4
|
1168
|
+
===================================================================================================
|
1169
|
+
END
|
1170
|
+
|
1171
|
+
assert_output_equal expected, @mock_out.string
|
1172
|
+
|
1173
|
+
end
|
1174
|
+
|
1175
|
+
def test_can_define_a_group_of_equal_size_columns_with_just_the_number_of_columns
|
1176
|
+
ConsoleTable.define(4, :width => 100, :output => @mock_out) do |table|
|
1177
|
+
table << [
|
1178
|
+
"Row 1, Column 1",
|
1179
|
+
"Row 1, Column 2",
|
1180
|
+
"Row 1, Column 3",
|
1181
|
+
"Row 1, Column 4"
|
1182
|
+
]
|
1183
|
+
|
1184
|
+
table << [
|
1185
|
+
"Row 2, Column 1",
|
1186
|
+
"Row 2, Column 2",
|
1187
|
+
"Row 2, Column 3",
|
1188
|
+
"Row 2, Column 4"
|
1189
|
+
]
|
1190
|
+
end
|
1191
|
+
|
1192
|
+
expected=<<-END
|
1193
|
+
===================================================================================================
|
1194
|
+
Column 1 Column 2 Column 3 Column 4
|
1195
|
+
---------------------------------------------------------------------------------------------------
|
1196
|
+
Row 1, Column 1 Row 1, Column 2 Row 1, Column 3 Row 1, Column 4
|
1197
|
+
Row 2, Column 1 Row 2, Column 2 Row 2, Column 3 Row 2, Column 4
|
1198
|
+
===================================================================================================
|
1199
|
+
END
|
1200
|
+
|
1201
|
+
assert_output_equal expected, @mock_out.string
|
1202
|
+
|
1203
|
+
end
|
1204
|
+
|
1205
|
+
def test_can_define_a_table_as_just_array_of_numbers
|
1206
|
+
table_config = [
|
1207
|
+
20,
|
1208
|
+
15,
|
1209
|
+
]
|
1210
|
+
|
1211
|
+
ConsoleTable.define(table_config, :width => 100, :output => @mock_out) do |table|
|
1212
|
+
table << {
|
1213
|
+
:col1 => "Row 1, Column 1",
|
1214
|
+
:col2 => "Row 1, Column 2"
|
1215
|
+
}
|
1216
|
+
|
1217
|
+
table << {
|
1218
|
+
:col1 => "Row 2, Column 1",
|
1219
|
+
:col2 => "Row 2, Column 1"
|
1220
|
+
}
|
1221
|
+
end
|
1222
|
+
|
1223
|
+
expected=<<-END
|
1224
|
+
====================================
|
1225
|
+
Column 1 Column 2
|
1226
|
+
------------------------------------
|
1227
|
+
Row 1, Column 1 Row 1, Column 2
|
1228
|
+
Row 2, Column 1 Row 2, Column 1
|
1229
|
+
====================================
|
1230
|
+
END
|
1231
|
+
|
1232
|
+
assert_output_equal expected, @mock_out.string
|
1233
|
+
|
1234
|
+
end
|
1235
|
+
|
1236
|
+
def test_can_define_a_table_with_just_numbers_including_floats
|
1237
|
+
table_config = [
|
1238
|
+
0.5,
|
1239
|
+
15,
|
1240
|
+
]
|
1241
|
+
|
1242
|
+
ConsoleTable.define(table_config, :width => 100, :output => @mock_out) do |table|
|
1243
|
+
table << {
|
1244
|
+
:col1 => "Row 1, Column 1",
|
1245
|
+
:col2 => "Row 1, Column 2"
|
1246
|
+
}
|
1247
|
+
|
1248
|
+
table << {
|
1249
|
+
:col1 => "Row 2, Column 1",
|
1250
|
+
:col2 => "Row 2, Column 1"
|
1251
|
+
}
|
1252
|
+
end
|
1253
|
+
|
1254
|
+
expected=<<-END
|
1255
|
+
==========================================================
|
1256
|
+
Column 1 Column 2
|
1257
|
+
----------------------------------------------------------
|
1258
|
+
Row 1, Column 1 Row 1, Column 2
|
1259
|
+
Row 2, Column 1 Row 2, Column 1
|
1260
|
+
==========================================================
|
1261
|
+
END
|
1262
|
+
|
1263
|
+
puts @mock_out.string
|
1264
|
+
|
1265
|
+
assert_output_equal expected, @mock_out.string
|
1266
|
+
|
1267
|
+
end
|
1268
|
+
|
1269
|
+
def test_can_use_console_table_as_glorified_tab_replacement
|
1270
|
+
ConsoleTable.define(2, :width => 40, :headings=>false, :outline=>false, :output => @mock_out) do |table|
|
1271
|
+
table << [
|
1272
|
+
"Row 1, Column 1",
|
1273
|
+
"\tRow 1, Column 2"
|
1274
|
+
]
|
1275
|
+
|
1276
|
+
table << [
|
1277
|
+
"Row 2, Column 1",
|
1278
|
+
"\tRow 2, Column 2"
|
1279
|
+
]
|
1280
|
+
end
|
1281
|
+
|
1282
|
+
expected=<<-END
|
1283
|
+
Row 1, Column 1 Row 1, Column 2
|
1284
|
+
Row 2, Column 1 Row 2, Column 2
|
1285
|
+
END
|
1286
|
+
|
1287
|
+
assert_output_equal expected, @mock_out.string
|
1288
|
+
|
1289
|
+
end
|
1290
|
+
|
1030
1291
|
private
|
1031
1292
|
def assert_output_equal(expected, actual)
|
1032
1293
|
expected_lines = expected.split("\n")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: console_table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rod Hilton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|