tty 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -1
- data/README.md +19 -7
- data/lib/tty.rb +10 -0
- data/lib/tty/support/equatable.rb +1 -0
- data/lib/tty/support/unicode.rb +36 -0
- data/lib/tty/table.rb +3 -3
- data/lib/tty/table/border.rb +104 -0
- data/lib/tty/table/border/ascii.rb +41 -0
- data/lib/tty/table/border/null.rb +49 -0
- data/lib/tty/table/border/unicode.rb +41 -0
- data/lib/tty/table/column_set.rb +77 -0
- data/lib/tty/table/operation/alignment.rb +2 -0
- data/lib/tty/table/operation/alignment_set.rb +4 -58
- data/lib/tty/table/operation/truncation.rb +15 -19
- data/lib/tty/table/operation/wrapped.rb +34 -0
- data/lib/tty/table/renderer.rb +8 -11
- data/lib/tty/table/renderer/ascii.rb +15 -0
- data/lib/tty/table/renderer/basic.rb +21 -34
- data/lib/tty/table/renderer/unicode.rb +2 -2
- data/lib/tty/vector.rb +117 -0
- data/lib/tty/version.rb +1 -1
- data/spec/tty/table/border/ascii/rendering_spec.rb +49 -0
- data/spec/tty/table/border/new_spec.rb +21 -0
- data/spec/tty/table/border/null/rendering_spec.rb +49 -0
- data/spec/tty/table/border/unicode/rendering_spec.rb +49 -0
- data/spec/tty/table/column_set/extract_widths_spec.rb +26 -0
- data/spec/tty/table/operation/alignment_set/align_rows_spec.rb +4 -4
- data/spec/tty/table/operation/alignment_set/new_spec.rb +1 -1
- data/spec/tty/table/operation/wrapped/wrap_spec.rb +22 -0
- data/spec/tty/table/properties_spec.rb +3 -0
- data/spec/tty/table/renderer/basic/render_spec.rb +144 -33
- data/spec/tty/table/renderer/pick_renderer_spec.rb +25 -0
- data/spec/tty/table/to_s_spec.rb +51 -0
- data/spec/tty/vector/new_spec.rb +47 -0
- metadata +35 -8
data/lib/tty/version.rb
CHANGED
@@ -0,0 +1,49 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe TTY::Table::Border::ASCII, '#rendering' do
|
6
|
+
|
7
|
+
subject { described_class.new row }
|
8
|
+
|
9
|
+
context 'with empty row' do
|
10
|
+
let(:row) { [] }
|
11
|
+
|
12
|
+
it 'draws top line' do
|
13
|
+
subject.top_line.should == "++"
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'draws middle line' do
|
17
|
+
subject.separator.should == "++"
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'draw bottom line' do
|
21
|
+
subject.bottom_line.should == "++"
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'draws row line' do
|
25
|
+
subject.row_line.should == "||"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'with row' do
|
30
|
+
let(:row) { ['a1', 'a2', 'a3'] }
|
31
|
+
|
32
|
+
it 'draws top line' do
|
33
|
+
subject.top_line.should == "+--+--+--+"
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'draw middle line' do
|
37
|
+
subject.separator.should == "+--+--+--+"
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'draw bottom line' do
|
41
|
+
subject.bottom_line.should == "+--+--+--+"
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'draws row line' do
|
45
|
+
subject.row_line.should == "|a1|a2|a3|"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe TTY::Table::Border, '#new' do
|
6
|
+
subject { klass.new }
|
7
|
+
|
8
|
+
context '' do
|
9
|
+
let(:klass) { described_class }
|
10
|
+
|
11
|
+
specify { expect {subject }.to raise_error(NotImplementedError) }
|
12
|
+
end
|
13
|
+
|
14
|
+
context '' do
|
15
|
+
let(:klass) { Class.new }
|
16
|
+
|
17
|
+
specify { expect {subject }.to_not raise_error(NotImplementedError) }
|
18
|
+
|
19
|
+
it { should be_instance_of klass }
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe TTY::Table::Border::Null, '#rendering' do
|
6
|
+
|
7
|
+
subject { described_class.new row }
|
8
|
+
|
9
|
+
context 'with empty row' do
|
10
|
+
let(:row) { [] }
|
11
|
+
|
12
|
+
it 'draws top line' do
|
13
|
+
subject.top_line.should be_nil
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'draws middle line' do
|
17
|
+
subject.separator.should be_nil
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'draw bottom line' do
|
21
|
+
subject.bottom_line.should be_nil
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'draws row line' do
|
25
|
+
subject.row_line.should == ''
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'with row' do
|
30
|
+
let(:row) { ['a1', 'a2', 'a3'] }
|
31
|
+
|
32
|
+
it 'draws top line' do
|
33
|
+
subject.top_line.should be_nil
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'draw middle line' do
|
37
|
+
subject.separator.should be_nil
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'draw bottom line' do
|
41
|
+
subject.bottom_line.should be_nil
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'draws row line' do
|
45
|
+
subject.row_line.should == 'a1 a2 a3'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe TTY::Table::Border::Unicode, '#rendering' do
|
6
|
+
|
7
|
+
subject { described_class.new row }
|
8
|
+
|
9
|
+
context 'with empty row' do
|
10
|
+
let(:row) { [] }
|
11
|
+
|
12
|
+
it 'draws top line' do
|
13
|
+
subject.top_line.should == "┌┐"
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'draws middle line' do
|
17
|
+
subject.separator.should == "├┤"
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'draw bottom line' do
|
21
|
+
subject.bottom_line.should == "└┘"
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'draws row line' do
|
25
|
+
subject.row_line.should == "││"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'with row' do
|
30
|
+
let(:row) { ['a1', 'a2', 'a3'] }
|
31
|
+
|
32
|
+
it 'draws top line' do
|
33
|
+
subject.top_line.should == "┌──┬──┬──┐"
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'draw middle line' do
|
37
|
+
subject.separator.should == "├──┼──┼──┤"
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'draw bottom line' do
|
41
|
+
subject.bottom_line.should == "└──┴──┴──┘"
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'draws row line' do
|
45
|
+
subject.row_line.should == "│a1│a2│a3│"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe TTY::Table::ColumnSet, '#extract_widths!' do
|
6
|
+
|
7
|
+
let(:header) { ['h1', 'h2', 'h3'] }
|
8
|
+
let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
|
9
|
+
|
10
|
+
let(:table) {
|
11
|
+
stub(:table, :column_widths => column_widths,
|
12
|
+
:header => header, :to_a => rows).as_null_object
|
13
|
+
}
|
14
|
+
|
15
|
+
subject { described_class.new table }
|
16
|
+
|
17
|
+
context 'with column_widths' do
|
18
|
+
let(:column_widths) { [5,5,5] }
|
19
|
+
|
20
|
+
it '' do
|
21
|
+
subject.extract_widths!
|
22
|
+
subject.column_widths.should == column_widths
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -14,7 +14,7 @@ describe TTY::Table::Operation::AlignmentSet, '#align_rows' do
|
|
14
14
|
|
15
15
|
it { should be_instance_of(Array) }
|
16
16
|
|
17
|
-
it { should == [['a1
|
17
|
+
it { should == [['a1 ', 'a2 '], ['b1 ', 'b2 ']] }
|
18
18
|
end
|
19
19
|
|
20
20
|
context 'aligned with column widths and alignments' do
|
@@ -23,7 +23,7 @@ describe TTY::Table::Operation::AlignmentSet, '#align_rows' do
|
|
23
23
|
|
24
24
|
it { should be_instance_of(Array) }
|
25
25
|
|
26
|
-
it { should == [[' a1
|
26
|
+
it { should == [[' a1', 'a2 '], [' b1', 'b2 ']] }
|
27
27
|
end
|
28
28
|
|
29
29
|
context 'aligned with no column widths and no alignments' do
|
@@ -32,7 +32,7 @@ describe TTY::Table::Operation::AlignmentSet, '#align_rows' do
|
|
32
32
|
|
33
33
|
it { should be_instance_of(Array) }
|
34
34
|
|
35
|
-
it { should == [['a1
|
35
|
+
it { should == [['a1', 'a2'], ['b1', 'b2']] }
|
36
36
|
end
|
37
37
|
|
38
38
|
context 'aligned with no column widths and alignments' do
|
@@ -41,6 +41,6 @@ describe TTY::Table::Operation::AlignmentSet, '#align_rows' do
|
|
41
41
|
|
42
42
|
it { should be_instance_of(Array) }
|
43
43
|
|
44
|
-
it { should == [['a1
|
44
|
+
it { should == [['a1', 'a2'], ['b1', 'b2']] }
|
45
45
|
end
|
46
46
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe TTY::Table::Operation::Wrapped, '#wrap' do
|
6
|
+
let(:instance) { described_class.new }
|
7
|
+
let(:text) { 'ラドクリフ、マラソン五輪代表に1万m出場にも含み' }
|
8
|
+
|
9
|
+
subject { instance.wrap(text, width) }
|
10
|
+
|
11
|
+
context 'without wrapping' do
|
12
|
+
let(:width) { 8 }
|
13
|
+
|
14
|
+
it { should == "ラドクリフ、マラ\nソン五輪代表に1\n万m出場にも含み" }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'with wrapping' do
|
18
|
+
let(:width) { 100 }
|
19
|
+
|
20
|
+
it { should == text }
|
21
|
+
end
|
22
|
+
end
|
@@ -14,6 +14,8 @@ describe TTY::Table, 'properties' do
|
|
14
14
|
|
15
15
|
its(:size) { should == [2,3] }
|
16
16
|
|
17
|
+
its(:width) { should == 6 }
|
18
|
+
|
17
19
|
context 'no size' do
|
18
20
|
let(:rows) { [] }
|
19
21
|
|
@@ -21,4 +23,5 @@ describe TTY::Table, 'properties' do
|
|
21
23
|
|
22
24
|
its(:column_size) { should == 0 }
|
23
25
|
end
|
26
|
+
|
24
27
|
end
|
@@ -6,43 +6,154 @@ 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
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
9
|
+
subject { described_class.new }
|
10
|
+
|
11
|
+
context 'without border' do
|
12
|
+
|
13
|
+
it 'displays table without styling' do
|
14
|
+
table = TTY::Table.new rows
|
15
|
+
subject.render(table).should == <<-EOS.normalize
|
16
|
+
a1 a2 a3
|
17
|
+
b1 b2 b3
|
18
|
+
EOS
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'displays table with header' do
|
22
|
+
table = TTY::Table.new header, rows
|
23
|
+
subject.render(table).should == <<-EOS.normalize
|
24
|
+
h1 h2 h3
|
25
|
+
a1 a2 a3
|
26
|
+
b1 b2 b3
|
27
|
+
EOS
|
28
|
+
end
|
29
|
+
|
30
|
+
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
|
35
|
+
h1 h2
|
36
|
+
aaa1 a2
|
37
|
+
b1 bb1
|
38
|
+
EOS
|
39
|
+
end
|
40
|
+
|
41
|
+
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
|
45
|
+
header1 header2 header3
|
46
|
+
a1 a2 a3
|
47
|
+
b1 b2 b3
|
48
|
+
EOS
|
49
|
+
end
|
17
50
|
|
18
|
-
it 'displays table with header' do
|
19
|
-
table = TTY::Table.new :header => header, :renderer => :basic
|
20
|
-
table << rows[0] << rows[1]
|
21
|
-
table.to_s.should == <<-EOS.normalize
|
22
|
-
h1 h2 h3
|
23
|
-
a1 a2 a3
|
24
|
-
b1 b2 b3
|
25
|
-
EOS
|
26
51
|
end
|
27
52
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
37
104
|
end
|
38
105
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
table
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
47
157
|
end
|
158
|
+
|
48
159
|
end
|