console_table 0.1.0 → 0.1.1
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.
- checksums.yaml +4 -4
- data/lib/console_table.rb +85 -23
- data/test/test_console_table.rb +134 -1
- data/todo.txt +1 -3
- 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: 62dec8c67df25306a62e7c1ccaa8341a5ca603f0
|
4
|
+
data.tar.gz: 7592bb485b9e110eab9395df2da50063625cad73
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3331fee285ff870bcdd5e7ac8e44f7cb75a1b943a6ef2cbe656078a0ba44ea31bbad0c87c6e724dcad6a22ffd0788816bc789c031e5958dd5aaa3369d83d7f48
|
7
|
+
data.tar.gz: 4738d8b0d88d6764df4e5e32e0314fa2829a0a79cfbb5df9fdc0367efa64d3a00e69fc9ed9ff37ab013dbff42337212a2805f8c074901203f6f26ba82b63f93d
|
data/lib/console_table.rb
CHANGED
@@ -2,7 +2,7 @@ require 'colorize'
|
|
2
2
|
require 'terminfo'
|
3
3
|
|
4
4
|
module ConsoleTable
|
5
|
-
VERSION = "0.1.
|
5
|
+
VERSION = "0.1.1"
|
6
6
|
|
7
7
|
def self.define(layout, options={}, &block)
|
8
8
|
table = ConsoleTableClass.new(layout, options)
|
@@ -28,6 +28,16 @@ module ConsoleTable
|
|
28
28
|
@out = options[:output] || $stdout
|
29
29
|
@title = options[:title]
|
30
30
|
@set_width = options[:width]
|
31
|
+
@borders = options[:borders] || false #Lines between every cell, implies outline
|
32
|
+
|
33
|
+
#Set outline, just the upper and lower lines
|
34
|
+
if @borders
|
35
|
+
@outline = true
|
36
|
+
elsif not options[:outline].nil?
|
37
|
+
@outline = options[:outline]
|
38
|
+
else
|
39
|
+
@outline = true
|
40
|
+
end
|
31
41
|
|
32
42
|
@footer = []
|
33
43
|
|
@@ -39,67 +49,105 @@ module ConsoleTable
|
|
39
49
|
end
|
40
50
|
|
41
51
|
def print_header()
|
42
|
-
@
|
43
|
-
|
44
|
-
|
52
|
+
if @title.nil?
|
53
|
+
print_line("=", "*", false)
|
54
|
+
else
|
55
|
+
print_line("=", "*", true)
|
56
|
+
end if @outline
|
45
57
|
|
46
58
|
if not @title.nil? and @title.length <= @working_width
|
47
59
|
@out.print " "*@left_margin
|
60
|
+
@out.print "|" if @borders
|
48
61
|
left_side = (@working_width - @title.uncolorize.length)/2
|
49
62
|
right_side = (@working_width - @title.uncolorize.length) - left_side
|
50
63
|
@out.print " "*left_side
|
51
64
|
@out.print @title
|
52
65
|
@out.print " "*right_side
|
66
|
+
@out.print "|" if @borders
|
53
67
|
@out.print "\n"
|
68
|
+
print_line if @borders
|
54
69
|
end
|
55
70
|
end
|
56
71
|
|
57
72
|
def print_headings()
|
58
73
|
@headings_printed = true
|
59
74
|
@out.print " "*@left_margin
|
75
|
+
if @borders
|
76
|
+
@out.print "|"
|
77
|
+
end
|
60
78
|
|
61
79
|
@column_widths.each_with_index do |column, i|
|
62
80
|
justify = column[:justify] || :left
|
63
81
|
title = (column[:title] || column[:key].to_s.capitalize).strip
|
64
82
|
@out.print format(column[:size], title, false, justify)
|
65
|
-
|
83
|
+
|
84
|
+
if @borders
|
85
|
+
@out.print "|"
|
86
|
+
else
|
87
|
+
@out.print " " if i < @column_widths.size-1
|
88
|
+
end
|
66
89
|
end
|
67
90
|
@out.print "\n"
|
68
91
|
|
69
|
-
@out
|
70
|
-
@out.print "-" * @working_width
|
71
|
-
@out.print "\n"
|
92
|
+
print_line unless @borders #this line will be printed when the NEXT LINE prints out if borders are on
|
72
93
|
end
|
73
94
|
|
74
|
-
def
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
95
|
+
def print_line(char="-", join_char="+", edge_join_only=false)
|
96
|
+
if @borders #use +'s to join columns
|
97
|
+
@out.print " " * @left_margin
|
98
|
+
@out.print join_char
|
99
|
+
@column_widths.each_with_index do |column, i|
|
100
|
+
@out.print char*column[:size]
|
101
|
+
if(edge_join_only and i < @column_widths.length - 1)
|
102
|
+
@out.print char
|
103
|
+
else
|
104
|
+
@out.print join_char
|
105
|
+
end
|
80
106
|
end
|
107
|
+
@out.print "\n"
|
108
|
+
else #just print long lines
|
109
|
+
@out.print " " * @left_margin
|
110
|
+
@out.print "#{char}" * (@working_width + (@borders ? 2 : 0))
|
111
|
+
@out.print "\n"
|
81
112
|
end
|
113
|
+
end
|
82
114
|
|
83
|
-
|
84
|
-
|
115
|
+
def print_footer()
|
85
116
|
if should_print_footer
|
86
|
-
|
87
|
-
@out.print "-" * @working_width
|
88
|
-
@out.print "\n"
|
117
|
+
print_line
|
89
118
|
end
|
90
119
|
|
91
120
|
footer_lines.each do |line|
|
92
121
|
if line.uncolorize.length <= @working_width
|
93
122
|
@out.print " " * @left_margin
|
123
|
+
@out.print "|" if @borders
|
94
124
|
@out.print " " * (@working_width - line.uncolorize.length)
|
95
125
|
@out.print line
|
126
|
+
@out.print "|" if @borders
|
96
127
|
@out.print "\n"
|
97
128
|
end
|
98
129
|
end
|
99
130
|
|
100
|
-
|
101
|
-
|
102
|
-
|
131
|
+
if should_print_footer
|
132
|
+
print_line("=", "*", true)
|
133
|
+
else
|
134
|
+
print_line("=", "*", false)
|
135
|
+
end if @outline
|
136
|
+
end
|
137
|
+
|
138
|
+
def should_print_footer
|
139
|
+
footer_lines.length > 0 && footer_lines.any? { |l| l.uncolorize.length <= @working_width }
|
140
|
+
end
|
141
|
+
|
142
|
+
def footer_lines
|
143
|
+
footer_lines = []
|
144
|
+
@footer.each do |line|
|
145
|
+
lines = line.split("\n")
|
146
|
+
lines.each do |l|
|
147
|
+
footer_lines << l.strip unless l.nil? or l.uncolorize.strip == ""
|
148
|
+
end
|
149
|
+
end
|
150
|
+
footer_lines
|
103
151
|
end
|
104
152
|
|
105
153
|
def print(options)
|
@@ -120,7 +168,12 @@ module ConsoleTable
|
|
120
168
|
options = munged_options
|
121
169
|
end
|
122
170
|
|
171
|
+
print_line if @borders
|
172
|
+
|
123
173
|
@out.print " "*@left_margin
|
174
|
+
if @borders
|
175
|
+
@out.print "|"
|
176
|
+
end
|
124
177
|
#column order is set, so go through each column and look up values in the incoming options
|
125
178
|
@column_widths.each_with_index do |column, i|
|
126
179
|
to_print = options[column[:key]] || ""
|
@@ -163,7 +216,11 @@ module ConsoleTable
|
|
163
216
|
@out.print format(column[:size], normalize(to_print.to_s))
|
164
217
|
end
|
165
218
|
|
166
|
-
|
219
|
+
if @borders
|
220
|
+
@out.print "|"
|
221
|
+
else
|
222
|
+
@out.print " " if i < @column_widths.size-1
|
223
|
+
end
|
167
224
|
end
|
168
225
|
@out.print "\n"
|
169
226
|
|
@@ -194,6 +251,7 @@ module ConsoleTable
|
|
194
251
|
end
|
195
252
|
|
196
253
|
num_spacers = @original_column_layout.length - 1
|
254
|
+
num_spacers = num_spacers + 2 if @borders
|
197
255
|
set_sizes = @original_column_layout.collect { |x| x[:size] }.find_all { |x| x.is_a? Integer }
|
198
256
|
used_up = set_sizes.inject(:+) || 0
|
199
257
|
available = total_width - used_up - @left_margin - @right_margin - num_spacers
|
@@ -251,7 +309,10 @@ module ConsoleTable
|
|
251
309
|
end
|
252
310
|
|
253
311
|
def print_plain(to_print)
|
312
|
+
print_line if @borders
|
313
|
+
|
254
314
|
@out.print " "*@left_margin
|
315
|
+
@out.print "|" if @borders
|
255
316
|
|
256
317
|
if to_print.is_a? String
|
257
318
|
@out.print format(@working_width, normalize(to_print))
|
@@ -270,6 +331,7 @@ module ConsoleTable
|
|
270
331
|
@out.print formatted
|
271
332
|
end
|
272
333
|
|
334
|
+
@out.print "|" if @borders
|
273
335
|
@out.print "\n"
|
274
336
|
end
|
275
337
|
end
|
data/test/test_console_table.rb
CHANGED
@@ -470,13 +470,146 @@ Short1 Short2 Short3
|
|
470
470
|
|
471
471
|
expected=<<-END
|
472
472
|
=========================================
|
473
|
+
This is just a string, it should ignore c
|
474
|
+
=========================================
|
475
|
+
END
|
476
|
+
|
477
|
+
assert_output_equal expected, @mock_out.string
|
478
|
+
end
|
479
|
+
|
480
|
+
def test_printing_a_single_after_data_makes_headings_show_up
|
481
|
+
table_config = [
|
482
|
+
{:key=>:col1, :size=>20, :title=>"Column 1"},
|
483
|
+
{:key=>:col2, :size=>20, :title=>"Column 2"},
|
484
|
+
]
|
485
|
+
|
486
|
+
ConsoleTable.define(table_config, :width=> 100, :output=>@mock_out) do |table|
|
487
|
+
table << ["One", "Two"]
|
488
|
+
table << "This is just a string, it should ignore columns"
|
489
|
+
table << ["One", "Two"]
|
490
|
+
end
|
491
|
+
|
492
|
+
expected=<<-END
|
493
|
+
=========================================
|
473
494
|
Column 1 Column 2
|
474
495
|
-----------------------------------------
|
496
|
+
One Two
|
475
497
|
This is just a string, it should ignore c
|
498
|
+
One Two
|
476
499
|
=========================================
|
477
500
|
END
|
478
501
|
|
479
|
-
|
502
|
+
assert_output_equal expected, @mock_out.string
|
503
|
+
end
|
504
|
+
|
505
|
+
def test_can_have_a_bordered_table
|
506
|
+
table_config = [
|
507
|
+
{:key=>:col1, :size=>20, :title=>"Column 1"},
|
508
|
+
{:key=>:col2, :size=>0.3, :title=>"Column 2"},
|
509
|
+
{:key=>:col3, :size=>10, :title=>"Column 3", :justify=>:center},
|
510
|
+
{:key=>:col4, :size=>"*", :title=>"Column 4", :justify=>:center}
|
511
|
+
]
|
512
|
+
|
513
|
+
ConsoleTable.define(table_config, :left_margin=>10, :right_margin=>7, :width=> 100, :title=>"Test Title", :borders=>true, :output=>@mock_out) do |table|
|
514
|
+
(1..5).each do |row|
|
515
|
+
table << (1..4).collect{|i| "Row #{row}, Column #{i}"}
|
516
|
+
end
|
517
|
+
|
518
|
+
table << "Plain line needs borders"
|
519
|
+
table.footer << "Footer needs borders"
|
520
|
+
table.footer << "Footer still \n needs borders"
|
521
|
+
|
522
|
+
end
|
523
|
+
|
524
|
+
expected=<<-END
|
525
|
+
*================================================================================*
|
526
|
+
| Test Title |
|
527
|
+
+--------------------+--------------+----------+---------------------------------+
|
528
|
+
|Column 1 |Column 2 | Column 3 | Column 4 |
|
529
|
+
+--------------------+--------------+----------+---------------------------------+
|
530
|
+
|Row 1, Column 1 |Row 1, Column |Row 1, Col| Row 1, Column 4 |
|
531
|
+
+--------------------+--------------+----------+---------------------------------+
|
532
|
+
|Row 2, Column 1 |Row 2, Column |Row 2, Col| Row 2, Column 4 |
|
533
|
+
+--------------------+--------------+----------+---------------------------------+
|
534
|
+
|Row 3, Column 1 |Row 3, Column |Row 3, Col| Row 3, Column 4 |
|
535
|
+
+--------------------+--------------+----------+---------------------------------+
|
536
|
+
|Row 4, Column 1 |Row 4, Column |Row 4, Col| Row 4, Column 4 |
|
537
|
+
+--------------------+--------------+----------+---------------------------------+
|
538
|
+
|Row 5, Column 1 |Row 5, Column |Row 5, Col| Row 5, Column 4 |
|
539
|
+
+--------------------+--------------+----------+---------------------------------+
|
540
|
+
|Plain line needs borders |
|
541
|
+
+--------------------+--------------+----------+---------------------------------+
|
542
|
+
| Footer needs borders|
|
543
|
+
| Footer still|
|
544
|
+
| needs borders|
|
545
|
+
*================================================================================*
|
546
|
+
END
|
547
|
+
|
548
|
+
assert_output_equal expected, @mock_out.string
|
549
|
+
end
|
550
|
+
|
551
|
+
def test_outline_joins_only_when_no_footer_or_header
|
552
|
+
table_config = [
|
553
|
+
{:key=>:col1, :size=>20, :title=>"Column 1"},
|
554
|
+
{:key=>:col2, :size=>0.3, :title=>"Column 2"},
|
555
|
+
{:key=>:col3, :size=>10, :title=>"Column 3", :justify=>:center},
|
556
|
+
{:key=>:col4, :size=>"*", :title=>"Column 4", :justify=>:center}
|
557
|
+
]
|
558
|
+
|
559
|
+
#borders are true, so outline false should be ignored
|
560
|
+
ConsoleTable.define(table_config, :left_margin=>10, :right_margin=>7, :width=> 100, :borders=>true, :outline=>false, :output=>@mock_out) do |table|
|
561
|
+
(1..5).each do |row|
|
562
|
+
table << (1..4).collect{|i| "Row #{row}, Column #{i}"}
|
563
|
+
end
|
564
|
+
|
565
|
+
table << "Plain line needs borders"
|
566
|
+
|
567
|
+
end
|
568
|
+
|
569
|
+
expected=<<-END
|
570
|
+
*====================*==============*==========*=================================*
|
571
|
+
|Column 1 |Column 2 | Column 3 | Column 4 |
|
572
|
+
+--------------------+--------------+----------+---------------------------------+
|
573
|
+
|Row 1, Column 1 |Row 1, Column |Row 1, Col| Row 1, Column 4 |
|
574
|
+
+--------------------+--------------+----------+---------------------------------+
|
575
|
+
|Row 2, Column 1 |Row 2, Column |Row 2, Col| Row 2, Column 4 |
|
576
|
+
+--------------------+--------------+----------+---------------------------------+
|
577
|
+
|Row 3, Column 1 |Row 3, Column |Row 3, Col| Row 3, Column 4 |
|
578
|
+
+--------------------+--------------+----------+---------------------------------+
|
579
|
+
|Row 4, Column 1 |Row 4, Column |Row 4, Col| Row 4, Column 4 |
|
580
|
+
+--------------------+--------------+----------+---------------------------------+
|
581
|
+
|Row 5, Column 1 |Row 5, Column |Row 5, Col| Row 5, Column 4 |
|
582
|
+
+--------------------+--------------+----------+---------------------------------+
|
583
|
+
|Plain line needs borders |
|
584
|
+
*====================*==============*==========*=================================*
|
585
|
+
END
|
586
|
+
|
587
|
+
assert_output_equal expected, @mock_out.string
|
588
|
+
end
|
589
|
+
|
590
|
+
def test_can_have_no_outline_if_requested
|
591
|
+
table_config = [
|
592
|
+
{:key=>:col1, :size=>20, :title=>"Column 1"},
|
593
|
+
{:key=>:col2, :size=>0.3, :title=>"Column 2"},
|
594
|
+
]
|
595
|
+
|
596
|
+
ConsoleTable.define(table_config, :width=>60, :outline=>false, :title=>"Still has a title", :output=>@mock_out) do |table|
|
597
|
+
(1..5).each do |row|
|
598
|
+
table << (1..2).collect{|i| "Row #{row}, Column #{i}"}
|
599
|
+
end
|
600
|
+
|
601
|
+
end
|
602
|
+
|
603
|
+
expected=<<-END
|
604
|
+
Still has a title
|
605
|
+
Column 1 Column 2
|
606
|
+
--------------------------------
|
607
|
+
Row 1, Column 1 Row 1, Colu
|
608
|
+
Row 2, Column 1 Row 2, Colu
|
609
|
+
Row 3, Column 1 Row 3, Colu
|
610
|
+
Row 4, Column 1 Row 4, Colu
|
611
|
+
Row 5, Column 1 Row 5, Colu
|
612
|
+
END
|
480
613
|
|
481
614
|
assert_output_equal expected, @mock_out.string
|
482
615
|
end
|
data/todo.txt
CHANGED
@@ -1,3 +1 @@
|
|
1
|
-
|
2
|
-
perhaps :border=>:thin, :thick, thin does above, thick also adds a row of --'s between, or even smartly uses +'s for joins
|
3
|
-
note the dangerousness of this with the current impl - Total width available is always the screen width, so if you make a table with 2 20 char columns and set a border, where does the final | go? it should go at the end of the 40ish character, but it'd go at the end of the line I think. might not even want to bite this off
|
1
|
+
better handling of colors. should you be able to supply colored strings and the spacing is smart enough to ignore control characters?
|
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.1
|
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-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|