tty-table 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (148) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/.rspec +3 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +22 -0
  6. data/Gemfile +19 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +389 -0
  9. data/Rakefile +8 -0
  10. data/benchmarks/speed.rb +36 -0
  11. data/lib/tty/table/border/ascii.rb +32 -0
  12. data/lib/tty/table/border/null.rb +37 -0
  13. data/lib/tty/table/border/row_line.rb +18 -0
  14. data/lib/tty/table/border/unicode.rb +32 -0
  15. data/lib/tty/table/border.rb +219 -0
  16. data/lib/tty/table/border_dsl.rb +251 -0
  17. data/lib/tty/table/border_options.rb +53 -0
  18. data/lib/tty/table/column_set.rb +121 -0
  19. data/lib/tty/table/columns.rb +170 -0
  20. data/lib/tty/table/empty.rb +26 -0
  21. data/lib/tty/table/error.rb +39 -0
  22. data/lib/tty/table/field.rb +139 -0
  23. data/lib/tty/table/header.rb +163 -0
  24. data/lib/tty/table/indentation.rb +52 -0
  25. data/lib/tty/table/operation/alignment_set.rb +103 -0
  26. data/lib/tty/table/operation/escape.rb +30 -0
  27. data/lib/tty/table/operation/filter.rb +34 -0
  28. data/lib/tty/table/operation/padding.rb +95 -0
  29. data/lib/tty/table/operation/truncation.rb +41 -0
  30. data/lib/tty/table/operation/wrapped.rb +43 -0
  31. data/lib/tty/table/operations.rb +69 -0
  32. data/lib/tty/table/options.rb +30 -0
  33. data/lib/tty/table/orientation/horizontal.rb +48 -0
  34. data/lib/tty/table/orientation/vertical.rb +38 -0
  35. data/lib/tty/table/orientation.rb +57 -0
  36. data/lib/tty/table/padder.rb +180 -0
  37. data/lib/tty/table/renderer/ascii.rb +16 -0
  38. data/lib/tty/table/renderer/basic.rb +294 -0
  39. data/lib/tty/table/renderer/color.rb +12 -0
  40. data/lib/tty/table/renderer/unicode.rb +21 -0
  41. data/lib/tty/table/renderer.rb +101 -0
  42. data/lib/tty/table/row.rb +248 -0
  43. data/lib/tty/table/transformation.rb +39 -0
  44. data/lib/tty/table/validatable.rb +64 -0
  45. data/lib/tty/table/version.rb +7 -0
  46. data/lib/tty/table.rb +469 -0
  47. data/lib/tty-table.rb +48 -0
  48. data/spec/spec_helper.rb +51 -0
  49. data/spec/unit/access_spec.rb +86 -0
  50. data/spec/unit/add_row_spec.rb +28 -0
  51. data/spec/unit/border/ascii/rendering_spec.rb +90 -0
  52. data/spec/unit/border/new_spec.rb +27 -0
  53. data/spec/unit/border/null/rendering_spec.rb +130 -0
  54. data/spec/unit/border/options/from_spec.rb +38 -0
  55. data/spec/unit/border/options/new_spec.rb +14 -0
  56. data/spec/unit/border/unicode/rendering_spec.rb +63 -0
  57. data/spec/unit/border_options/new_spec.rb +20 -0
  58. data/spec/unit/border_options/update_spec.rb +18 -0
  59. data/spec/unit/column_set/extract_widths_spec.rb +15 -0
  60. data/spec/unit/column_set/total_width_spec.rb +15 -0
  61. data/spec/unit/column_set/widths_from_spec.rb +51 -0
  62. data/spec/unit/columns/enforce_spec.rb +67 -0
  63. data/spec/unit/columns/widths_spec.rb +35 -0
  64. data/spec/unit/data_spec.rb +14 -0
  65. data/spec/unit/each_spec.rb +41 -0
  66. data/spec/unit/each_with_index_spec.rb +57 -0
  67. data/spec/unit/empty_spec.rb +23 -0
  68. data/spec/unit/eql_spec.rb +34 -0
  69. data/spec/unit/field/equality_spec.rb +51 -0
  70. data/spec/unit/field/length_spec.rb +21 -0
  71. data/spec/unit/field/lines_spec.rb +21 -0
  72. data/spec/unit/field/new_spec.rb +29 -0
  73. data/spec/unit/field/width_spec.rb +23 -0
  74. data/spec/unit/filter_spec.rb +23 -0
  75. data/spec/unit/header/call_spec.rb +30 -0
  76. data/spec/unit/header/color_spec.rb +19 -0
  77. data/spec/unit/header/equality_spec.rb +51 -0
  78. data/spec/unit/header/height_spec.rb +27 -0
  79. data/spec/unit/header/new_spec.rb +25 -0
  80. data/spec/unit/header/set_spec.rb +20 -0
  81. data/spec/unit/header/to_ary_spec.rb +14 -0
  82. data/spec/unit/header_spec.rb +13 -0
  83. data/spec/unit/indentation/insert_indent_spec.rb +27 -0
  84. data/spec/unit/initialize_spec.rb +88 -0
  85. data/spec/unit/operation/alignment_set/call_spec.rb +39 -0
  86. data/spec/unit/operation/alignment_set/each_spec.rb +17 -0
  87. data/spec/unit/operation/alignment_set/new_spec.rb +27 -0
  88. data/spec/unit/operation/alignment_set/to_ary_spec.rb +14 -0
  89. data/spec/unit/operation/escape/call_spec.rb +16 -0
  90. data/spec/unit/operation/filter/call_spec.rb +17 -0
  91. data/spec/unit/operation/truncation/call_spec.rb +32 -0
  92. data/spec/unit/operation/wrapped/call_spec.rb +33 -0
  93. data/spec/unit/operations/new_spec.rb +30 -0
  94. data/spec/unit/options/access_spec.rb +14 -0
  95. data/spec/unit/options_spec.rb +25 -0
  96. data/spec/unit/orientation_spec.rb +145 -0
  97. data/spec/unit/padder/parse_spec.rb +45 -0
  98. data/spec/unit/padder/to_s_spec.rb +14 -0
  99. data/spec/unit/padding_spec.rb +120 -0
  100. data/spec/unit/properties_spec.rb +25 -0
  101. data/spec/unit/render_spec.rb +63 -0
  102. data/spec/unit/render_with_spec.rb +106 -0
  103. data/spec/unit/renderer/ascii/indentation_spec.rb +41 -0
  104. data/spec/unit/renderer/ascii/padding_spec.rb +61 -0
  105. data/spec/unit/renderer/ascii/render_spec.rb +68 -0
  106. data/spec/unit/renderer/ascii/resizing_spec.rb +114 -0
  107. data/spec/unit/renderer/ascii/separator_spec.rb +28 -0
  108. data/spec/unit/renderer/basic/alignment_spec.rb +88 -0
  109. data/spec/unit/renderer/basic/coloring_spec.rb +46 -0
  110. data/spec/unit/renderer/basic/extract_column_widths_spec.rb +28 -0
  111. data/spec/unit/renderer/basic/filter_spec.rb +53 -0
  112. data/spec/unit/renderer/basic/indentation_spec.rb +48 -0
  113. data/spec/unit/renderer/basic/multiline_content_spec.rb +135 -0
  114. data/spec/unit/renderer/basic/new_spec.rb +26 -0
  115. data/spec/unit/renderer/basic/options_spec.rb +52 -0
  116. data/spec/unit/renderer/basic/padding_spec.rb +52 -0
  117. data/spec/unit/renderer/basic/render_spec.rb +57 -0
  118. data/spec/unit/renderer/basic/resizing_spec.rb +96 -0
  119. data/spec/unit/renderer/basic/separator_spec.rb +43 -0
  120. data/spec/unit/renderer/basic/truncation_spec.rb +35 -0
  121. data/spec/unit/renderer/basic/wrapping_spec.rb +40 -0
  122. data/spec/unit/renderer/border_spec.rb +104 -0
  123. data/spec/unit/renderer/render_spec.rb +36 -0
  124. data/spec/unit/renderer/select_spec.rb +22 -0
  125. data/spec/unit/renderer/style_spec.rb +72 -0
  126. data/spec/unit/renderer/unicode/indentation_spec.rb +41 -0
  127. data/spec/unit/renderer/unicode/padding_spec.rb +61 -0
  128. data/spec/unit/renderer/unicode/render_spec.rb +68 -0
  129. data/spec/unit/renderer/unicode/separator_spec.rb +26 -0
  130. data/spec/unit/renderer_spec.rb +19 -0
  131. data/spec/unit/rotate_spec.rb +86 -0
  132. data/spec/unit/row/access_spec.rb +25 -0
  133. data/spec/unit/row/call_spec.rb +45 -0
  134. data/spec/unit/row/data_spec.rb +26 -0
  135. data/spec/unit/row/each_spec.rb +31 -0
  136. data/spec/unit/row/equality_spec.rb +73 -0
  137. data/spec/unit/row/height_spec.rb +27 -0
  138. data/spec/unit/row/new_spec.rb +41 -0
  139. data/spec/unit/row/to_ary_spec.rb +14 -0
  140. data/spec/unit/to_s_spec.rb +63 -0
  141. data/spec/unit/transformation/extract_tuples_spec.rb +35 -0
  142. data/spec/unit/validatable/validate_options_spec.rb +33 -0
  143. data/spec/unit/validatable_spec.rb +32 -0
  144. data/tasks/console.rake +10 -0
  145. data/tasks/coverage.rake +11 -0
  146. data/tasks/spec.rake +29 -0
  147. data/tty-table.gemspec +28 -0
  148. metadata +371 -0
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table, 'properties' do
6
+ let(:rows) {[['a1', 'a2', 'a3'], ['b1', 'b2', 'c3']] }
7
+
8
+ subject(:table) { described_class.new rows }
9
+
10
+ it { expect(table.width).to eq(6) }
11
+
12
+ it { expect(table.row_size).to eq(2) }
13
+
14
+ it { expect(table.column_size).to eq(3) }
15
+
16
+ it { expect(table.size).to eq([2,3]) }
17
+
18
+ context 'no size' do
19
+ let(:rows) { [] }
20
+
21
+ it { expect(table.row_size).to eq(0) }
22
+
23
+ it { expect(table.column_size).to eq(0) }
24
+ end
25
+ end
@@ -0,0 +1,63 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table, '#render' do
6
+ let(:object) { described_class }
7
+ let(:header) { ['h1', 'h2'] }
8
+ let(:rows) { [['a1', 'a2'], ['b1', 'b2']] }
9
+ let(:color) { Pastel.new(enabled: true) }
10
+ let(:basic_renderer) { TTY::Table::Renderer::Basic }
11
+ let(:ascii_renderer) { TTY::Table::Renderer::ASCII }
12
+
13
+ subject(:table) { object.new header, rows }
14
+
15
+ before { allow(Pastel).to receive(:new).and_return(color) }
16
+
17
+ it { is_expected.to respond_to(:render) }
18
+
19
+ context 'with block' do
20
+ it 'allows to modify renderer in a block' do
21
+ expected = nil
22
+ block = lambda { |renderer| expected = renderer }
23
+ table.render(&block)
24
+ expect(expected).to be_kind_of(basic_renderer)
25
+ end
26
+
27
+ it 'sets renderer as block parameter' do
28
+ expected = nil
29
+ block = lambda { |renderer| expected = renderer }
30
+ table.render(:ascii, &block)
31
+ expect(expected).to be_kind_of(ascii_renderer)
32
+ end
33
+
34
+ it 'sets parameter on renderer' do
35
+ result = table.render :ascii do |renderer|
36
+ renderer.border.style = :red
37
+ end
38
+ expect(result).to eq <<-EOS.normalize
39
+ \e[31m+--+--+\e[0m
40
+ \e[31m|\e[0mh1\e[31m|\e[0mh2\e[31m|\e[0m
41
+ \e[31m+--+--+\e[0m
42
+ \e[31m|\e[0ma1\e[31m|\e[0ma2\e[31m|\e[0m
43
+ \e[31m|\e[0mb1\e[31m|\e[0mb2\e[31m|\e[0m
44
+ \e[31m+--+--+\e[0m
45
+ EOS
46
+ end
47
+ end
48
+
49
+ context 'with params' do
50
+ it 'sets params without renderer' do
51
+ allow(TTY::Table::Renderer).to receive(:render)
52
+ table.render(:basic)
53
+ expect(TTY::Table::Renderer).to have_received(:render).
54
+ with(table, {renderer: :basic})
55
+ end
56
+
57
+ it 'sets params with renderer' do
58
+ allow(TTY::Table::Renderer).to receive(:render)
59
+ table.render
60
+ expect(TTY::Table::Renderer).to have_received(:render).with(table, {})
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,106 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table, '#render_with' do
6
+ let(:header) { ['h1', 'h2', 'h3'] }
7
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
+ let(:table) { described_class.new header, rows }
9
+ let(:color) { Pastel.new(enabled: true) }
10
+
11
+ before { allow(Pastel).to receive(:new).and_return(color) }
12
+
13
+ context 'with invalid border class' do
14
+ it "doesn't inherit from TTY::Table::Border" do
15
+ expect { table.render_with String }.to raise_error(TTY::Table::TypeError)
16
+ end
17
+
18
+ it "doesn't implement def_border" do
19
+ klass = Class.new(TTY::Table::Border)
20
+ expect { table.render_with klass }.
21
+ to raise_error(TTY::Table::NoImplementationError)
22
+ end
23
+ end
24
+
25
+ context 'with complete border' do
26
+ before {
27
+ class MyBorder < TTY::Table::Border
28
+ def_border do
29
+ top '='
30
+ top_mid '*'
31
+ top_left '*'
32
+ top_right '*'
33
+ bottom '='
34
+ bottom_mid '*'
35
+ bottom_left '*'
36
+ bottom_right '*'
37
+ mid '='
38
+ mid_mid '*'
39
+ mid_left '*'
40
+ mid_right '*'
41
+ left '$'
42
+ center '$'
43
+ right '$'
44
+ end
45
+ end
46
+ }
47
+
48
+ it 'displays custom border' do
49
+ expect(table.render_with(MyBorder)).to eq <<-EOS.normalize
50
+ *==*==*==*
51
+ $h1$h2$h3$
52
+ *==*==*==*
53
+ $a1$a2$a3$
54
+ $b1$b2$b3$
55
+ *==*==*==*
56
+ EOS
57
+ end
58
+ end
59
+
60
+ context 'with incomplete border' do
61
+ before {
62
+ class MyBorder < TTY::Table::Border
63
+ def_border do
64
+ bottom ' '
65
+ bottom_mid '*'
66
+ bottom_left '*'
67
+ bottom_right '*'
68
+ left '$'
69
+ center '$'
70
+ right '$'
71
+ end
72
+ end
73
+ }
74
+
75
+ it 'displays border' do
76
+ expect(table.render_with(MyBorder)).to eq <<-EOS.normalize
77
+ $h1$h2$h3$
78
+ $a1$a2$a3$
79
+ $b1$b2$b3$
80
+ * * * *
81
+ EOS
82
+ end
83
+ end
84
+
85
+ context 'with renderer' do
86
+ before {
87
+ class MyBorder < TTY::Table::Border
88
+ def_border do
89
+ left '|'
90
+ right '|'
91
+ end
92
+ end
93
+ }
94
+
95
+ it 'displays border' do
96
+ result = table.render_with MyBorder do |renderer|
97
+ renderer.border.style = :red
98
+ end
99
+ expect(result).to eq <<-EOS.normalize
100
+ \e[31m|\e[0mh1h2h3\e[31m|\e[0m
101
+ \e[31m|\e[0ma1a2a3\e[31m|\e[0m
102
+ \e[31m|\e[0mb1b2b3\e[31m|\e[0m
103
+ EOS
104
+ end
105
+ end
106
+ end # render_with
@@ -0,0 +1,41 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::ASCII, 'indentation' do
6
+ let(:header) { ['h1', 'h2', 'h3'] }
7
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
+ let(:table) { TTY::Table.new(header, rows) }
9
+ let(:indent) { 2 }
10
+ let(:options) { {indent: indent } }
11
+
12
+ subject(:renderer) { described_class.new(table, options)}
13
+
14
+ context 'when default' do
15
+ it 'indents by value' do
16
+ expect(renderer.render).to eql <<-EOS.chomp
17
+ +--+--+--+
18
+ |h1|h2|h3|
19
+ +--+--+--+
20
+ |a1|a2|a3|
21
+ |b1|b2|b3|
22
+ +--+--+--+
23
+ EOS
24
+ end
25
+ end
26
+
27
+ context 'when each row' do
28
+ it 'indents by value' do
29
+ renderer.border.separator = :each_row
30
+ expect(renderer.render).to eql <<-EOS.chomp
31
+ +--+--+--+
32
+ |h1|h2|h3|
33
+ +--+--+--+
34
+ |a1|a2|a3|
35
+ +--+--+--+
36
+ |b1|b2|b3|
37
+ +--+--+--+
38
+ EOS
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,61 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::ASCII, 'padding' do
6
+ let(:header) { ['Field', 'Type', 'Null', 'Key', 'Default', 'Extra'] }
7
+ let(:rows) { [['id', 'int(11)', 'YES', 'nil', 'NULL', '']] }
8
+ let(:table) { TTY::Table.new(header, rows) }
9
+
10
+ subject(:renderer) { described_class.new(table, options) }
11
+
12
+ context 'with left & right padding' do
13
+ let(:options) { {padding: [0,1,0,1]} }
14
+
15
+ it 'pads each field' do
16
+ expect(renderer.render).to eql <<-EOS.chomp
17
+ +-------+---------+------+-----+---------+-------+
18
+ | Field | Type | Null | Key | Default | Extra |
19
+ +-------+---------+------+-----+---------+-------+
20
+ | id | int(11) | YES | nil | NULL | |
21
+ +-------+---------+------+-----+---------+-------+
22
+ EOS
23
+ end
24
+ end
25
+
26
+ context 'with top & bottom padding' do
27
+ let(:options) { {padding: [1,0,1,0], multiline: true} }
28
+
29
+ it 'pads each field' do
30
+ expect(renderer.render).to eql <<-EOS.chomp
31
+ +-----+-------+----+---+-------+-----+
32
+ | | | | | | |
33
+ |Field|Type |Null|Key|Default|Extra|
34
+ | | | | | | |
35
+ +-----+-------+----+---+-------+-----+
36
+ | | | | | | |
37
+ |id |int(11)|YES |nil|NULL | |
38
+ | | | | | | |
39
+ +-----+-------+----+---+-------+-----+
40
+ EOS
41
+ end
42
+ end
43
+
44
+ context 'with full padding' do
45
+ let(:options) { {padding: [1,1,1,1], multiline: true} }
46
+
47
+ it 'pads each field' do
48
+ expect(renderer.render).to eql <<-EOS.chomp
49
+ +-------+---------+------+-----+---------+-------+
50
+ | | | | | | |
51
+ | Field | Type | Null | Key | Default | Extra |
52
+ | | | | | | |
53
+ +-------+---------+------+-----+---------+-------+
54
+ | | | | | | |
55
+ | id | int(11) | YES | nil | NULL | |
56
+ | | | | | | |
57
+ +-------+---------+------+-----+---------+-------+
58
+ EOS
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,68 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::ASCII, '#render' do
6
+ let(:header) { ['h1', 'h2', 'h3'] }
7
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
+ let(:table) { TTY::Table.new header, rows }
9
+
10
+ subject(:renderer) { described_class.new(table) }
11
+
12
+ context 'only rows' do
13
+ let(:table) { TTY::Table.new rows }
14
+
15
+ it 'display table rows' do
16
+ expect(renderer.render).to eq <<-EOS.normalize
17
+ +--+--+--+
18
+ |a1|a2|a3|
19
+ |b1|b2|b3|
20
+ +--+--+--+
21
+ EOS
22
+ end
23
+ end
24
+
25
+ context 'with header' do
26
+ it 'displays table with header' do
27
+ expect(renderer.render).to eq <<-EOS.normalize
28
+ +--+--+--+
29
+ |h1|h2|h3|
30
+ +--+--+--+
31
+ |a1|a2|a3|
32
+ |b1|b2|b3|
33
+ +--+--+--+
34
+ EOS
35
+ end
36
+ end
37
+
38
+ context 'with short header' do
39
+ let(:header) { ['h1', 'h2'] }
40
+ let(:rows) { [['aaa1', 'a2'], ['b1', 'bb1']] }
41
+
42
+ it 'displays table according to widths' do
43
+ expect(renderer.render).to eq <<-EOS.normalize
44
+ +----+---+
45
+ |h1 |h2 |
46
+ +----+---+
47
+ |aaa1|a2 |
48
+ |b1 |bb1|
49
+ +----+---+
50
+ EOS
51
+ end
52
+ end
53
+
54
+ context 'with long header' do
55
+ let(:header) { ['header1', 'header2', 'header3'] }
56
+
57
+ it 'header greater than row sizes' do
58
+ expect(renderer.render).to eq <<-EOS.normalize
59
+ +-------+-------+-------+
60
+ |header1|header2|header3|
61
+ +-------+-------+-------+
62
+ |a1 |a2 |a3 |
63
+ |b1 |b2 |b3 |
64
+ +-------+-------+-------+
65
+ EOS
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,114 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::ASCII, 'resizing' do
6
+ let(:header) { ['h1', 'h2', 'h3'] }
7
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
+ let(:table) { TTY::Table.new(header, rows) }
9
+
10
+ subject(:renderer) { described_class.new(table, options) }
11
+
12
+ context 'when expanding' do
13
+ context 'even columns' do
14
+ let(:options) { {width: 16, resize: true} }
15
+
16
+ it 'resizes each column' do
17
+ expect(renderer.render).to eql <<-EOS.normalize
18
+ +----+----+----+
19
+ |h1 |h2 |h3 |
20
+ +----+----+----+
21
+ |a1 |a2 |a3 |
22
+ |b1 |b2 |b3 |
23
+ +----+----+----+
24
+ EOS
25
+ end
26
+ end
27
+
28
+ context 'even columns with extra width' do
29
+ let(:header) { ['h1', 'h2', 'h3', 'h4'] }
30
+ let(:rows) { [['a1','a2','a3','a4'], ['b1','b2','b3','b4']] }
31
+ let(:options) { {width: 21, resize: true} }
32
+
33
+ it 'resizes each column' do
34
+ expect(renderer.render).to eql <<-EOS.normalize
35
+ +----+----+----+----+
36
+ |h1 |h2 |h3 |h4 |
37
+ +----+----+----+----+
38
+ |a1 |a2 |a3 |a4 |
39
+ |b1 |b2 |b3 |b4 |
40
+ +----+----+----+----+
41
+ EOS
42
+ end
43
+ end
44
+
45
+ context 'uneven columns' do
46
+ let(:header) { ['h1', 'h2', 'h3'] }
47
+ let(:rows) { [['aaa1', 'aa2', 'aaaaaaa3'], ['b1', 'b2', 'b3']] }
48
+ let(:options) { {width: 32, resize: true} }
49
+
50
+ it 'resizes each column' do
51
+ expect(renderer.render).to eql <<-EOS.normalize
52
+ +---------+-------+------------+
53
+ |h1 |h2 |h3 |
54
+ +---------+-------+------------+
55
+ |aaa1 |aa2 |aaaaaaa3 |
56
+ |b1 |b2 |b3 |
57
+ +---------+-------+------------+
58
+ EOS
59
+ end
60
+ end
61
+ end
62
+
63
+ context 'when shrinking' do
64
+ let(:header) { ['head1', 'head2'] }
65
+ let(:rows) { [['aaaa1','aaaa2',], ['bbbb1','bbbb2']] }
66
+
67
+ context 'even columns' do
68
+ let(:options) { {width: 7, resize: true} }
69
+
70
+ it 'resizes each column' do
71
+ expect(renderer.render).to eql <<-EOS.normalize
72
+ +--+--+
73
+ |h…|h…|
74
+ +--+--+
75
+ |a…|a…|
76
+ |b…|b…|
77
+ +--+--+
78
+ EOS
79
+ end
80
+ end
81
+
82
+ context 'even columns with extra width' do
83
+ let(:options) { {width: 8, resize: true} }
84
+
85
+ it 'resizes each column' do
86
+ expect(renderer.render).to eql <<-EOS.normalize
87
+ +---+--+
88
+ |he…|h…|
89
+ +---+--+
90
+ |aa…|a…|
91
+ |bb…|b…|
92
+ +---+--+
93
+ EOS
94
+ end
95
+ end
96
+
97
+ context 'uneven columns' do
98
+ let(:header) { ['head1', 'head2', 'head3'] }
99
+ let(:rows) { [['aaa1', 'aa2', 'aaaaaaa3'], ['b1', 'b2', 'b3']] }
100
+ let(:options) { {width: 15, resize: true} }
101
+
102
+ it 'resizes each column' do
103
+ expect(renderer.render).to eql <<-EOS.normalize
104
+ +---+---+-----+
105
+ |he…|he…|head3|
106
+ +---+---+-----+
107
+ |aa…|aa2|aaaa…|
108
+ |b1 |b2 |b3 |
109
+ +---+---+-----+
110
+ EOS
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::ASCII, 'with separator' do
6
+ let(:header) { ['h1', 'h2', 'h3'] }
7
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
+ let(:table) { TTY::Table.new(header, rows) }
9
+
10
+ let(:object) { described_class.new table }
11
+
12
+ subject(:renderer) { object }
13
+
14
+ context 'when ascii' do
15
+ it "renders each row" do
16
+ renderer.border.separator= :each_row
17
+ expect(renderer.render).to eq <<-EOS.normalize
18
+ +--+--+--+
19
+ |h1|h2|h3|
20
+ +--+--+--+
21
+ |a1|a2|a3|
22
+ +--+--+--+
23
+ |b1|b2|b3|
24
+ +--+--+--+
25
+ EOS
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,88 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::Basic, 'alignment' do
6
+ let(:header) { ['h1', 'h2', 'h3'] }
7
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
+ let(:options) { { column_aligns: column_aligns } }
9
+ let(:table) { TTY::Table.new(header, rows) }
10
+
11
+ subject(:renderer) { described_class.new table, options }
12
+
13
+ context 'with default' do
14
+ let(:header) { ['h1', 'h2'] }
15
+ let(:rows) { [['aaaaa', 'a'], ['b', 'bbbbb']] }
16
+ let(:column_aligns) { nil }
17
+
18
+ it 'aligns left by default' do
19
+ expect(renderer.render).to eql <<-EOS.normalize
20
+ h1 h2
21
+ aaaaa a
22
+ b bbbbb
23
+ EOS
24
+ end
25
+ end
26
+
27
+ context 'with different headers' do
28
+ let(:header) { ['header1', 'head2', 'h3'] }
29
+ let(:column_aligns) { [:left, :center, :right] }
30
+
31
+ it 'aligns headers' do
32
+ expect(renderer.render).to eql <<-EOS.normalize
33
+ header1 head2 h3
34
+ a1 a2 a3
35
+ b1 b2 b3
36
+ EOS
37
+ end
38
+ end
39
+
40
+ context 'with different aligns' do
41
+ let(:header) { nil }
42
+ let(:rows) { [['aaaaa', 'a'], ['b', 'bbbbb']] }
43
+ let(:column_aligns) { [:left, :right] }
44
+
45
+ it 'aligns table rows' do
46
+ expect(renderer.render.to_s).to eql <<-EOS.normalize
47
+ aaaaa a
48
+ b bbbbb
49
+ EOS
50
+ end
51
+ end
52
+
53
+ context 'with individual field aligns' do
54
+ let(:header) { ['header1', 'header2', 'header3'] }
55
+ let(:column_aligns) { [:left, :center, :right] }
56
+ let(:options) { {column_aligns: column_aligns} }
57
+ let(:table) {
58
+ TTY::Table.new header: header do |t|
59
+ t << ['a1', 'a2', 'a3']
60
+ t << ['b1', {value: 'b2', align: :right}, 'b3']
61
+ t << ['c1', 'c2', {value: 'c3', align: :center}]
62
+ end
63
+ }
64
+
65
+ it "takes individual fields over global aligns" do
66
+ expect(renderer.render).to eq <<-EOS.normalize
67
+ header1 header2 header3
68
+ a1 a2 a3
69
+ b1 b2 b3
70
+ c1 c2 c3
71
+ EOS
72
+ end
73
+ end
74
+
75
+ context 'with aligned header' do
76
+ let(:rows) { [['aaaaa1', 'a2', 'aaa3'], ['b1', 'bbbb2', 'bb3']] }
77
+ let(:header) {['h1', {:value => 'h2', :align => :right}, {:value => 'h3', :align => :center}] }
78
+ let(:options) { { renderer: :basic } }
79
+
80
+ it "aligns headres" do
81
+ expect(renderer.render).to eq <<-EOS.normalize
82
+ h1 h2 h3
83
+ aaaaa1 a2 aaa3
84
+ b1 bbbb2 bb3
85
+ EOS
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,46 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::Basic, 'coloring' do
6
+ let(:header) { ['h1', 'h2'] }
7
+ let(:rows) { [['a1', 'a2'], ['b1', 'b2']] }
8
+ let(:clear) { "\e[0m" }
9
+ let(:options) { {filter: filter } }
10
+ let(:table) { TTY::Table.new(header, rows) }
11
+ let(:color) { Pastel.new(enabled: true) }
12
+
13
+ subject(:renderer) { described_class.new(table, options) }
14
+
15
+ before { allow(Pastel).to receive(:new).and_return(color) }
16
+
17
+ context 'with filter on all fields' do
18
+ let(:filter) {
19
+ proc { |val, row, col| color.decorate val, :blue, :on_green }
20
+ }
21
+
22
+ it 'colors all elements' do
23
+ expect(renderer.render).to eql <<-EOS.normalize
24
+ \e[34;42mh1#{clear} \e[34;42mh2#{clear}
25
+ \e[34;42ma1#{clear} \e[34;42ma2#{clear}
26
+ \e[34;42mb1#{clear} \e[34;42mb2#{clear}
27
+ EOS
28
+ end
29
+ end
30
+
31
+ context 'with filter only on header' do
32
+ let(:filter) {
33
+ proc { |val, row, col|
34
+ row.zero? ? color.decorate(val, :blue, :on_green) : val
35
+ }
36
+ }
37
+
38
+ it 'colors only header' do
39
+ expect(renderer.render).to eql <<-EOS.normalize
40
+ \e[34;42mh1#{clear} \e[34;42mh2#{clear}
41
+ a1 a2
42
+ b1 b2
43
+ EOS
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::Basic, '#column_widths' do
6
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
7
+ let(:renderer) { described_class.new(table) }
8
+
9
+ subject { renderer.render }
10
+
11
+ context 'with rows only' do
12
+ let(:rows) { [['a1a', 'a2a2a2'], ['b1b1b', 'b2b2']] }
13
+ let(:table) { TTY::Table.new rows }
14
+
15
+ it 'calculates column widths' do
16
+ expect(renderer.column_widths).to eq([5,6])
17
+ end
18
+ end
19
+
20
+ context 'with header' do
21
+ let(:header) { ['header1', 'head2', 'h3'] }
22
+ let(:table) { TTY::Table.new header, rows }
23
+
24
+ it 'calcualtes column widths with header' do
25
+ expect(renderer.column_widths).to eq([7,5,2])
26
+ end
27
+ end
28
+ end