tty 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. data/.rspec +2 -1
  2. data/.travis.yml +3 -6
  3. data/README.md +232 -134
  4. data/lib/tty/plugins/plugin.rb +56 -0
  5. data/lib/tty/plugins.rb +75 -0
  6. data/lib/tty/shell/suggestion.rb +102 -0
  7. data/lib/tty/shell.rb +41 -14
  8. data/lib/tty/system/editor.rb +111 -0
  9. data/lib/tty/system/which.rb +13 -1
  10. data/lib/tty/system.rb +44 -28
  11. data/lib/tty/table/border/null.rb +0 -9
  12. data/lib/tty/table/border/row_line.rb +21 -0
  13. data/lib/tty/table/border.rb +63 -32
  14. data/lib/tty/table/border_dsl.rb +1 -1
  15. data/lib/tty/table/column_set.rb +16 -17
  16. data/lib/tty/table/field.rb +27 -7
  17. data/lib/tty/table/header.rb +18 -9
  18. data/lib/tty/table/operation/alignment_set.rb +20 -25
  19. data/lib/tty/table/operation/escape.rb +30 -0
  20. data/lib/tty/table/operation/filter.rb +36 -0
  21. data/lib/tty/table/operation/truncation.rb +22 -11
  22. data/lib/tty/table/operation/wrapped.rb +21 -10
  23. data/lib/tty/table/operations.rb +10 -8
  24. data/lib/tty/table/orientation/horizontal.rb +1 -1
  25. data/lib/tty/table/renderer/ascii.rb +3 -3
  26. data/lib/tty/table/renderer/basic.rb +135 -65
  27. data/lib/tty/table/renderer/color.rb +1 -4
  28. data/lib/tty/table/renderer/unicode.rb +3 -3
  29. data/lib/tty/table/renderer.rb +48 -61
  30. data/lib/tty/table/row.rb +30 -3
  31. data/lib/tty/table/transformation.rb +38 -0
  32. data/lib/tty/table/validatable.rb +7 -5
  33. data/lib/tty/table.rb +78 -99
  34. data/lib/tty/terminal/color.rb +2 -2
  35. data/lib/tty/terminal/echo.rb +1 -1
  36. data/lib/tty/terminal/pager/basic.rb +52 -0
  37. data/lib/tty/terminal/pager/system.rb +39 -0
  38. data/lib/tty/terminal/pager.rb +95 -0
  39. data/lib/tty/terminal.rb +30 -1
  40. data/lib/tty/version.rb +1 -1
  41. data/lib/tty.rb +41 -1
  42. data/spec/spec_helper.rb +20 -0
  43. data/spec/tty/plugins/find_spec.rb +28 -0
  44. data/spec/tty/plugins/load_spec.rb +20 -0
  45. data/spec/tty/plugins/plugin/load_spec.rb +30 -0
  46. data/spec/tty/plugins/plugin/new_spec.rb +18 -0
  47. data/spec/tty/shell/suggest_spec.rb +50 -0
  48. data/spec/tty/support/conversion_spec.rb +3 -3
  49. data/spec/tty/support/delegatable_spec.rb +1 -1
  50. data/spec/tty/support/equatable_spec.rb +6 -9
  51. data/spec/tty/system/editor/available_spec.rb +40 -0
  52. data/spec/tty/system/editor/build_spec.rb +40 -0
  53. data/spec/tty/system/editor/command_spec.rb +16 -0
  54. data/spec/tty/system/editor/executables_spec.rb +13 -0
  55. data/spec/tty/system/editor/invoke_spec.rb +38 -0
  56. data/spec/tty/system/editor/open_spec.rb +27 -0
  57. data/spec/tty/system/platform_spec.rb +4 -6
  58. data/spec/tty/system/which/which_spec.rb +48 -0
  59. data/spec/tty/system/which_spec.rb +8 -34
  60. data/spec/tty/table/border/ascii/rendering_spec.rb +19 -5
  61. data/spec/tty/table/border/new_spec.rb +1 -1
  62. data/spec/tty/table/border/null/rendering_spec.rb +24 -8
  63. data/spec/tty/table/border/unicode/rendering_spec.rb +19 -5
  64. data/spec/tty/table/column_set/extract_widths_spec.rb +4 -15
  65. data/spec/tty/table/column_set/total_width_spec.rb +15 -0
  66. data/spec/tty/table/data_spec.rb +14 -0
  67. data/spec/tty/table/each_spec.rb +17 -4
  68. data/spec/tty/table/each_with_index_spec.rb +34 -6
  69. data/spec/tty/table/field/length_spec.rb +21 -0
  70. data/spec/tty/table/field/lines_spec.rb +21 -0
  71. data/spec/tty/table/filter_spec.rb +23 -0
  72. data/spec/tty/table/header/call_spec.rb +1 -1
  73. data/spec/tty/table/header/height_spec.rb +27 -0
  74. data/spec/tty/table/initialize_spec.rb +6 -6
  75. data/spec/tty/table/operation/alignment_set/call_spec.rb +39 -0
  76. data/spec/tty/table/operation/escape/call_spec.rb +16 -0
  77. data/spec/tty/table/operation/filter/call_spec.rb +17 -0
  78. data/spec/tty/table/operation/truncation/call_spec.rb +15 -10
  79. data/spec/tty/table/operation/truncation/truncate_spec.rb +1 -1
  80. data/spec/tty/table/operation/wrapped/call_spec.rb +15 -10
  81. data/spec/tty/table/operation/wrapped/wrap_spec.rb +1 -1
  82. data/spec/tty/table/operations/new_spec.rb +4 -4
  83. data/spec/tty/table/options_spec.rb +0 -28
  84. data/spec/tty/table/orientation_spec.rb +5 -6
  85. data/spec/tty/table/properties_spec.rb +1 -4
  86. data/spec/tty/table/render_spec.rb +57 -0
  87. data/spec/tty/table/{renders_with_spec.rb → render_with_spec.rb} +29 -10
  88. data/spec/tty/table/renderer/ascii/render_spec.rb +68 -0
  89. data/spec/tty/table/renderer/ascii/separator_spec.rb +28 -0
  90. data/spec/tty/table/renderer/basic/alignment_spec.rb +18 -16
  91. data/spec/tty/table/renderer/basic/extract_column_widths_spec.rb +17 -12
  92. data/spec/tty/table/renderer/basic/filter_spec.rb +53 -0
  93. data/spec/tty/table/renderer/basic/multiline_content_spec.rb +135 -0
  94. data/spec/tty/table/renderer/basic/new_spec.rb +13 -2
  95. data/spec/tty/table/renderer/basic/options_spec.rb +48 -0
  96. data/spec/tty/table/renderer/basic/render_spec.rb +19 -121
  97. data/spec/tty/table/renderer/basic/separator_spec.rb +14 -48
  98. data/spec/tty/table/renderer/basic/truncation_spec.rb +35 -0
  99. data/spec/tty/table/renderer/basic/wrapping_spec.rb +40 -0
  100. data/spec/tty/table/{border_spec.rb → renderer/border_spec.rb} +17 -20
  101. data/spec/tty/table/renderer/select_spec.rb +22 -0
  102. data/spec/tty/table/{border → renderer}/style_spec.rb +13 -14
  103. data/spec/tty/table/renderer/unicode/render_spec.rb +68 -0
  104. data/spec/tty/table/renderer/unicode/separator_spec.rb +26 -0
  105. data/spec/tty/table/rotate_spec.rb +2 -3
  106. data/spec/tty/table/row/call_spec.rb +1 -1
  107. data/spec/tty/table/row/each_spec.rb +31 -0
  108. data/spec/tty/table/row/height_spec.rb +27 -0
  109. data/spec/tty/table/to_s_spec.rb +3 -3
  110. data/spec/tty/table/transformation/extract_tuples_spec.rb +35 -0
  111. data/spec/tty/table/validatable/validate_options_spec.rb +1 -2
  112. data/spec/tty/terminal/home_spec.rb +3 -3
  113. data/spec/tty/terminal/page_spec.rb +13 -0
  114. data/spec/tty/terminal/pager/available_spec.rb +40 -0
  115. data/spec/tty/terminal/pager/basic/page_spec.rb +54 -0
  116. data/spec/tty/terminal/pager/command_spec.rb +16 -0
  117. data/spec/tty/terminal/pager/executables_spec.rb +13 -0
  118. data/spec/tty/terminal/pager/page_spec.rb +47 -0
  119. data/spec/tty/terminal/pager/system/page_spec.rb +29 -0
  120. data/spec/tty/text/distance/distance_spec.rb +12 -0
  121. data/tty.gemspec +7 -3
  122. metadata +160 -27
  123. data/spec/tty/table/operation/alignment_set/align_rows_spec.rb +0 -53
  124. data/spec/tty/table/renderer/pick_renderer_spec.rb +0 -25
  125. data/spec/tty/table/renderer_spec.rb +0 -49
@@ -5,155 +5,53 @@ require 'spec_helper'
5
5
  describe TTY::Table::Renderer::Basic, '#render' do
6
6
  let(:header) { ['h1', 'h2', 'h3'] }
7
7
  let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
+ let(:table) { TTY::Table.new header, rows }
8
9
 
9
- subject { described_class.new }
10
+ subject { described_class.new(table) }
10
11
 
11
- context 'without border' do
12
+ context 'with rows' do
13
+ let(:table) { TTY::Table.new rows }
12
14
 
13
15
  it 'displays table without styling' do
14
- table = TTY::Table.new rows
15
- subject.render(table).should == <<-EOS.normalize
16
+ subject.render.should == <<-EOS.normalize
16
17
  a1 a2 a3
17
18
  b1 b2 b3
18
19
  EOS
19
20
  end
21
+ end
20
22
 
23
+ context 'with header and rows' do
21
24
  it 'displays table with header' do
22
- table = TTY::Table.new header, rows
23
- subject.render(table).should == <<-EOS.normalize
25
+ subject.render.should == <<-EOS.normalize
24
26
  h1 h2 h3
25
27
  a1 a2 a3
26
28
  b1 b2 b3
27
29
  EOS
28
30
  end
31
+ end
32
+
33
+ context 'with short header' do
34
+ let(:header) { ['h1', 'h2'] }
35
+ let(:rows) { [['aaa1', 'a2'], ['b1', 'bb1']] }
29
36
 
30
37
  it 'displays table according to widths' do
31
- header = ['h1', 'h2']
32
- rows = [['aaa1', 'a2'], ['b1', 'bb1']]
33
- table = TTY::Table.new header, rows
34
- subject.render(table).should == <<-EOS.normalize
38
+ subject.render.should == <<-EOS.normalize
35
39
  h1 h2
36
40
  aaa1 a2
37
41
  b1 bb1
38
42
  EOS
39
43
  end
44
+ end
45
+
46
+ context 'with long header' do
47
+ let(:header) { ['header1', 'header2', 'header3'] }
40
48
 
41
49
  it 'header greater than row sizes' do
42
- header = ['header1', 'header2', 'header3']
43
- table = TTY::Table.new header, rows
44
- subject.render(table).should == <<-EOS.normalize
50
+ subject.render.should == <<-EOS.normalize
45
51
  header1 header2 header3
46
52
  a1 a2 a3
47
53
  b1 b2 b3
48
54
  EOS
49
55
  end
50
-
51
56
  end
52
-
53
- context 'with ASCII border' do
54
- let(:border) { TTY::Table::Border::ASCII }
55
-
56
- it 'display table rows' do
57
- table = TTY::Table.new rows
58
- subject.render(table, border).should == <<-EOS.normalize
59
- +--+--+--+
60
- |a1|a2|a3|
61
- |b1|b2|b3|
62
- +--+--+--+
63
- EOS
64
- end
65
-
66
- it 'displays table with header' do
67
- table = TTY::Table.new header, rows
68
- subject.render(table, border).should == <<-EOS.normalize
69
- +--+--+--+
70
- |h1|h2|h3|
71
- +--+--+--+
72
- |a1|a2|a3|
73
- |b1|b2|b3|
74
- +--+--+--+
75
- EOS
76
- end
77
-
78
- it 'displays table according to widths' do
79
- header = ['h1', 'h2']
80
- rows = [['aaa1', 'a2'], ['b1', 'bb1']]
81
- table = TTY::Table.new header, rows
82
- subject.render(table, border).should == <<-EOS.normalize
83
- +----+---+
84
- |h1 |h2 |
85
- +----+---+
86
- |aaa1|a2 |
87
- |b1 |bb1|
88
- +----+---+
89
- EOS
90
- end
91
-
92
- it 'header greater than row sizes' do
93
- header = ['header1', 'header2', 'header3']
94
- table = TTY::Table.new header, rows
95
- subject.render(table, border).should == <<-EOS.normalize
96
- +-------+-------+-------+
97
- |header1|header2|header3|
98
- +-------+-------+-------+
99
- |a1 |a2 |a3 |
100
- |b1 |b2 |b3 |
101
- +-------+-------+-------+
102
- EOS
103
- end
104
- end
105
-
106
- context 'with Unicode border' do
107
- let(:border) { TTY::Table::Border::Unicode }
108
-
109
- it 'display table rows' do
110
- table = TTY::Table.new rows
111
- subject.render(table, border).should == <<-EOS.normalize
112
- ┌──┬──┬──┐
113
- │a1│a2│a3│
114
- │b1│b2│b3│
115
- └──┴──┴──┘
116
- EOS
117
- end
118
-
119
- it 'displays table with header' do
120
- table = TTY::Table.new header, rows
121
- subject.render(table, border).should == <<-EOS.normalize
122
- ┌──┬──┬──┐
123
- │h1│h2│h3│
124
- ├──┼──┼──┤
125
- │a1│a2│a3│
126
- │b1│b2│b3│
127
- └──┴──┴──┘
128
- EOS
129
- end
130
-
131
- it 'displays table according to widths' do
132
- header = ['h1', 'h2']
133
- rows = [['aaa1', 'a2'], ['b1', 'bb1']]
134
- table = TTY::Table.new header, rows
135
- subject.render(table, border).should == <<-EOS.normalize
136
- ┌────┬───┐
137
- │h1 │h2 │
138
- ├────┼───┤
139
- │aaa1│a2 │
140
- │b1 │bb1│
141
- └────┴───┘
142
- EOS
143
- end
144
-
145
- it 'header greater than row sizes' do
146
- header = ['header1', 'header2', 'header3']
147
- table = TTY::Table.new header, rows
148
- subject.render(table, border).to_s.should == <<-EOS.normalize
149
- ┌───────┬───────┬───────┐
150
- │header1│header2│header3│
151
- ├───────┼───────┼───────┤
152
- │a1 │a2 │a3 │
153
- │b1 │b2 │b3 │
154
- └───────┴───────┴───────┘
155
- EOS
156
- end
157
- end
158
-
159
57
  end
@@ -2,35 +2,36 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe TTY::Table, 'with separator' do
5
+ describe TTY::Table::Renderer::Basic, 'with separator' do
6
6
  let(:header) { ['h1', 'h2', 'h3'] }
7
- let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
7
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
+ let(:table) { TTY::Table.new(header, rows) }
8
9
 
9
- subject(:table) { described_class.new header, rows, :renderer => renderer }
10
+ let(:object) { described_class.new table }
10
11
 
11
- context 'when default' do
12
- let(:renderer) { :basic }
12
+ subject(:renderer) { object }
13
13
 
14
+ context 'when default' do
14
15
  it "sets through hash" do
15
- table.border :separator => :each_row
16
- expect(table.border.separator).to eql(:each_row)
16
+ renderer.border :separator => :each_row
17
+ expect(renderer.border.separator).to eql(:each_row)
17
18
  end
18
19
 
19
20
  it "sets through attribute" do
20
- table.border.separator= :each_row
21
- expect(table.border.separator).to eql(:each_row)
21
+ renderer.border.separator= :each_row
22
+ expect(renderer.border.separator).to eql(:each_row)
22
23
  end
23
24
 
24
25
  it "sets through block" do
25
- table.border do
26
+ renderer.border do
26
27
  separator :each_row
27
28
  end
28
- expect(table.border.separator).to eql(:each_row)
29
+ expect(renderer.border.separator).to eql(:each_row)
29
30
  end
30
31
 
31
32
  it "renders each row" do
32
- table.border.separator= :each_row
33
- table.to_s.should == <<-EOS.normalize
33
+ renderer.border.separator= :each_row
34
+ renderer.render.should == <<-EOS.normalize
34
35
  h1 h2 h3
35
36
 
36
37
  a1 a2 a3
@@ -39,39 +40,4 @@ describe TTY::Table, 'with separator' do
39
40
  EOS
40
41
  end
41
42
  end
42
-
43
- context 'when ascii' do
44
- let(:renderer) { :ascii }
45
-
46
- it "renders each row" do
47
- table.border.separator= :each_row
48
- table.to_s.should == <<-EOS.normalize
49
- +--+--+--+
50
- |h1|h2|h3|
51
- +--+--+--+
52
- |a1|a2|a3|
53
- +--+--+--+
54
- |b1|b2|b3|
55
- +--+--+--+
56
- EOS
57
- end
58
- end
59
-
60
- context 'when unicode' do
61
- let(:renderer) { :unicode }
62
-
63
- it "renders each row" do
64
- table.border.separator= :each_row
65
- table.to_s.should == <<-EOS.normalize
66
- ┌──┬──┬──┐
67
- │h1│h2│h3│
68
- ├──┼──┼──┤
69
- │a1│a2│a3│
70
- ├──┼──┼──┤
71
- │b1│b2│b3│
72
- └──┴──┴──┘
73
- EOS
74
- end
75
- end
76
-
77
43
  end
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Table::Renderer::Basic, 'truncation' do
6
+ let(:header) { ['header1', 'head2', 'h3'] }
7
+ let(:rows) { [['a1111111', 'a222', 'a3333333'], ['b111', 'b2222222', 'b333333']]}
8
+ let(:table) { TTY::Table.new header, rows }
9
+
10
+ subject { described_class.new(table, options) }
11
+
12
+ context 'without column widths' do
13
+ let(:options) { {} }
14
+
15
+ it "doesn't shorten the fields" do
16
+ subject.render.should == <<-EOS.normalize
17
+ header1 head2 h3
18
+ a1111111 a222 a3333333
19
+ b111 b2222222 b333333
20
+ EOS
21
+ end
22
+ end
23
+
24
+ context 'with column widths' do
25
+ let(:options) { { column_widths: [3, 5, 7] } }
26
+
27
+ it 'shortens the fields' do
28
+ subject.render.should == <<-EOS.normalize
29
+ he… head2 h3
30
+ a1… a222 a33333…
31
+ b1… b222… b333333
32
+ EOS
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,40 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Table::Renderer::Basic, 'wrapping' do
6
+ let(:header) { ['header1', 'head2', 'h3'] }
7
+ let(:rows) { [['a1111111', 'a222', 'a3333333'], ['b111', 'b2222222', 'b333333']]}
8
+ let(:table) { TTY::Table.new header, rows }
9
+
10
+ subject { described_class.new(table, options) }
11
+
12
+ context 'without column widths' do
13
+ let(:options) { {multiline: true} }
14
+
15
+ it 'doesn\'t wrap the fields' do
16
+ subject.render.should == <<-EOS.normalize
17
+ header1 head2 h3
18
+ a1111111 a222 a3333333
19
+ b111 b2222222 b333333
20
+ EOS
21
+ end
22
+ end
23
+
24
+ context 'with column widths' do
25
+ let(:options) { { column_widths: [3, 5, 7], multiline: true} }
26
+
27
+ it 'wraps the fields' do
28
+ subject.render.should == <<-EOS.normalize
29
+ hea head2 h3
30
+ der
31
+ 1
32
+ a11 a222 a333333
33
+ 111 3
34
+ 11
35
+ b11 b2222 b333333
36
+ 1 222
37
+ EOS
38
+ end
39
+ end
40
+ end
@@ -2,38 +2,36 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe TTY::Table, '#border' do
5
+ describe TTY::Table::Renderer, '#border' do
6
6
  let(:header) { ['h1', 'h2', 'h3'] }
7
- let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
7
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
8
  let(:border) { nil }
9
9
 
10
- subject(:table) { described_class.new header, rows, :renderer => renderer, :border => border }
10
+ let(:table) { TTY::Table.new(header, rows) }
11
+
12
+ subject { described_class.select(renderer).new(table) }
11
13
 
12
14
  context 'when default' do
13
15
  let(:renderer) { :basic }
14
16
  let(:border) { { :characters => {'top' => '-'}, :style => :red } }
15
17
 
16
- it 'specifies border at initialization' do
17
- expect(table.border.style).to eql(:red)
18
- end
19
-
20
18
  it 'specifies border in hash' do
21
- table.border border
22
- expect(table.border.characters['top']).to eql('-')
19
+ subject.border border
20
+ expect(subject.border.characters['top']).to eql('-')
23
21
  end
24
22
 
25
23
  it 'specifies border in characters attribute' do
26
- table.border.characters = {'top' => '-'}
27
- expect(table.border.characters['top']).to eql('-')
24
+ subject.border.characters = {'top' => '*'}
25
+ expect(subject.border.characters['top']).to eql('*')
28
26
  end
29
27
 
30
28
  it 'specifies border in block' do
31
- table.border do
29
+ subject.border do
32
30
  mid '='
33
31
  mid_mid ' '
34
32
  end
35
33
 
36
- table.to_s.should == <<-EOS.normalize
34
+ subject.render.should == <<-EOS.normalize
37
35
  h1 h2 h3
38
36
  == == ==
39
37
  a1 a2 a3
@@ -46,14 +44,14 @@ describe TTY::Table, '#border' do
46
44
  let(:renderer) { :ascii }
47
45
 
48
46
  it 'specifies border in block' do
49
- table.border do
47
+ subject.border do
50
48
  mid '='
51
49
  mid_mid '='
52
50
  mid_left '='
53
51
  mid_right '='
54
52
  end
55
53
 
56
- table.to_s.should == <<-EOS.normalize
54
+ subject.render.should == <<-EOS.normalize
57
55
  +--+--+--+
58
56
  |h1|h2|h3|
59
57
  ==========
@@ -64,14 +62,14 @@ describe TTY::Table, '#border' do
64
62
  end
65
63
 
66
64
  it 'specifies border as hash' do
67
- table.border({ :characters => {
65
+ subject.border({ :characters => {
68
66
  'mid' => '=',
69
67
  'mid_mid' => '=',
70
68
  'mid_left' => '=',
71
69
  'mid_right' => '=',
72
70
  }})
73
71
 
74
- table.to_s.should == <<-EOS.normalize
72
+ subject.render.should == <<-EOS.normalize
75
73
  +--+--+--+
76
74
  |h1|h2|h3|
77
75
  ==========
@@ -86,14 +84,14 @@ describe TTY::Table, '#border' do
86
84
  let(:renderer) { :unicode }
87
85
 
88
86
  it 'specifies border in block' do
89
- table.border do
87
+ subject.border do
90
88
  mid '='
91
89
  mid_mid '='
92
90
  mid_left '='
93
91
  mid_right '='
94
92
  end
95
93
 
96
- table.to_s.should == <<-EOS.normalize
94
+ subject.render.should == <<-EOS.normalize
97
95
  ┌──┬──┬──┐
98
96
  │h1│h2│h3│
99
97
  ==========
@@ -103,5 +101,4 @@ describe TTY::Table, '#border' do
103
101
  EOS
104
102
  end
105
103
  end
106
-
107
104
  end # border
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Table::Renderer, '#select' do
6
+ let(:klass) { ::Class.new }
7
+ let(:instance) { described_class }
8
+
9
+ subject { instance.select(renderer) }
10
+
11
+ context 'with basic' do
12
+ let(:renderer) { :basic }
13
+
14
+ it { should be(TTY::Table::Renderer::Basic) }
15
+ end
16
+
17
+ context 'with unicode' do
18
+ let(:renderer) { :unicode }
19
+
20
+ it { should be(TTY::Table::Renderer::Unicode) }
21
+ end
22
+ end
@@ -2,27 +2,28 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe TTY::Table, 'with style' do
5
+ describe TTY::Table::Renderer, 'with style' do
6
6
  let(:header) { ['h1', 'h2', 'h3'] }
7
- let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
7
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
+ let(:table) { TTY::Table.new(header, rows) }
8
9
 
9
- subject(:table) { described_class.new header, rows, :renderer => renderer }
10
+ subject { described_class.select(renderer).new(table) }
10
11
 
11
12
  context 'when default' do
12
13
  let(:renderer) { :basic }
13
14
 
14
15
  it "sets through hash" do
15
- table.border :style => :red
16
- expect(table.border.style).to eql :red
16
+ subject.border style: :red
17
+ expect(subject.border.style).to eql(:red)
17
18
  end
18
19
 
19
20
  it "sets through attribute" do
20
- table.border.style = :red
21
- expect(table.border.style).to eql :red
21
+ subject.border.style = :red
22
+ expect(subject.border.style).to eql :red
22
23
  end
23
24
 
24
25
  it "renders without color" do
25
- table.to_s.should == <<-EOS.normalize
26
+ subject.render.should == <<-EOS.normalize
26
27
  h1 h2 h3
27
28
  a1 a2 a3
28
29
  b1 b2 b3
@@ -36,9 +37,8 @@ describe TTY::Table, 'with style' do
36
37
  let(:clear) { "\e[0m" }
37
38
 
38
39
  it "renders border in color" do
39
- table.border.style= :red
40
-
41
- table.to_s.should == <<-EOS.normalize
40
+ subject.border.style= :red
41
+ subject.render.should == <<-EOS.normalize
42
42
  #{red}+--+--+--+#{clear}
43
43
  #{red}|#{clear}h1#{red}|#{clear}h2#{red}|#{clear}h3#{red}|#{clear}
44
44
  #{red}+--+--+--+#{clear}
@@ -55,8 +55,8 @@ describe TTY::Table, 'with style' do
55
55
  let(:clear) { "\e[0m" }
56
56
 
57
57
  it "renders each row" do
58
- table.border.style= :red
59
- table.to_s.should == <<-EOS.normalize
58
+ subject.border.style= :red
59
+ subject.render.should == <<-EOS.normalize
60
60
  #{red}┌──┬──┬──┐#{clear}
61
61
  #{red}│#{clear}h1#{red}│#{clear}h2#{red}│#{clear}h3#{red}│#{clear}
62
62
  #{red}├──┼──┼──┤#{clear}
@@ -66,5 +66,4 @@ describe TTY::Table, 'with style' do
66
66
  EOS
67
67
  end
68
68
  end
69
-
70
69
  end
@@ -0,0 +1,68 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Table::Renderer::Unicode, '#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 { described_class.new(table) }
11
+
12
+ context 'with rows only' do
13
+ let(:table) { TTY::Table.new rows }
14
+
15
+ it 'display table rows' do
16
+ subject.render.should == <<-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
+ subject.render.should == <<-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
+ subject.render.should == <<-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
+ subject.render.to_s.should == <<-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,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Table::Renderer::Unicode, '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
+ it "renders each row" do
15
+ renderer.border.separator= :each_row
16
+ renderer.render.should == <<-EOS.normalize
17
+ ┌──┬──┬──┐
18
+ │h1│h2│h3│
19
+ ├──┼──┼──┤
20
+ │a1│a2│a3│
21
+ ├──┼──┼──┤
22
+ │b1│b2│b3│
23
+ └──┴──┴──┘
24
+ EOS
25
+ end
26
+ end
@@ -22,8 +22,7 @@ describe TTY::Table, '#rotate' do
22
22
 
23
23
  context 'with header' do
24
24
  it 'preserves orientation' do
25
- expect(subject.header).to eql header
26
- expect(subject.rotate.to_a).to eql rows
25
+ expect(subject.rotate.to_a).to eql [header] + rows
27
26
  end
28
27
  end
29
28
  end
@@ -66,7 +65,7 @@ describe TTY::Table, '#rotate' do
66
65
  subject.rotate
67
66
  expect(subject.orientation).to be_a TTY::Table::Orientation::Vertical
68
67
  subject.orientation = :horizontal
69
- expect(subject.rotate.to_a).to eql rows
68
+ expect(subject.rotate.to_a).to eql [header] + rows
70
69
  expect(subject.header).to eql header
71
70
  end
72
71
  end
@@ -5,7 +5,7 @@ require 'spec_helper'
5
5
  describe TTY::Table::Row, '#call' do
6
6
  let(:object) { described_class.new(data) }
7
7
 
8
- subject { object.call(attribute) }
8
+ subject { object[attribute] }
9
9
 
10
10
  context 'when integer' do
11
11
  let(:data) { ['a', 'b'] }