tty 0.0.10 → 0.0.11

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.
Files changed (56) hide show
  1. data/README.md +75 -19
  2. data/lib/tty.rb +7 -0
  3. data/lib/tty/shell/question.rb +3 -3
  4. data/lib/tty/shell/response.rb +5 -5
  5. data/lib/tty/table.rb +51 -19
  6. data/lib/tty/table/column_set.rb +42 -1
  7. data/lib/tty/table/columns.rb +167 -0
  8. data/lib/tty/table/field.rb +2 -2
  9. data/lib/tty/table/indentation.rb +54 -0
  10. data/lib/tty/table/operation/escape.rb +1 -1
  11. data/lib/tty/table/operation/filter.rb +0 -1
  12. data/lib/tty/table/operation/padding.rb +95 -0
  13. data/lib/tty/table/operation/wrapped.rb +6 -3
  14. data/lib/tty/table/operations.rb +3 -2
  15. data/lib/tty/table/orientation/horizontal.rb +27 -1
  16. data/lib/tty/table/orientation/vertical.rb +17 -1
  17. data/lib/tty/table/padder.rb +142 -0
  18. data/lib/tty/table/renderer/basic.rb +101 -31
  19. data/lib/tty/table/validatable.rb +0 -7
  20. data/lib/tty/terminal/color.rb +36 -20
  21. data/lib/tty/text/truncation.rb +16 -1
  22. data/lib/tty/text/wrapping.rb +31 -10
  23. data/lib/tty/version.rb +1 -1
  24. data/spec/tty/shell/question/argument_spec.rb +1 -1
  25. data/spec/tty/shell/question/modify_spec.rb +2 -2
  26. data/spec/tty/shell/response/read_email_spec.rb +0 -1
  27. data/spec/tty/table/border/ascii/rendering_spec.rb +34 -7
  28. data/spec/tty/table/border/null/rendering_spec.rb +34 -7
  29. data/spec/tty/table/column_set/extract_widths_spec.rb +1 -1
  30. data/spec/tty/table/column_set/widths_from_spec.rb +52 -0
  31. data/spec/tty/table/columns/enforce_spec.rb +68 -0
  32. data/spec/tty/table/columns/widths_spec.rb +33 -0
  33. data/spec/tty/table/indentation/insert_indent_spec.rb +27 -0
  34. data/spec/tty/table/operation/wrapped/call_spec.rb +2 -1
  35. data/spec/tty/table/operation/wrapped/wrap_spec.rb +3 -2
  36. data/spec/tty/table/operations/new_spec.rb +3 -5
  37. data/spec/tty/table/orientation_spec.rb +68 -22
  38. data/spec/tty/table/padder/parse_spec.rb +45 -0
  39. data/spec/tty/table/padding_spec.rb +120 -0
  40. data/spec/tty/table/renderer/ascii/indentation_spec.rb +41 -0
  41. data/spec/tty/table/renderer/ascii/padding_spec.rb +61 -0
  42. data/spec/tty/table/renderer/ascii/resizing_spec.rb +114 -0
  43. data/spec/tty/table/renderer/basic/alignment_spec.rb +18 -19
  44. data/spec/tty/table/renderer/basic/coloring_spec.rb +45 -0
  45. data/spec/tty/table/renderer/basic/indentation_spec.rb +46 -0
  46. data/spec/tty/table/renderer/basic/options_spec.rb +2 -2
  47. data/spec/tty/table/renderer/basic/padding_spec.rb +52 -0
  48. data/spec/tty/table/renderer/basic/resizing_spec.rb +96 -0
  49. data/spec/tty/table/renderer/render_spec.rb +36 -0
  50. data/spec/tty/table/renderer/unicode/indentation_spec.rb +41 -0
  51. data/spec/tty/table/renderer/unicode/padding_spec.rb +61 -0
  52. data/spec/tty/table/renderer_spec.rb +19 -0
  53. data/spec/tty/table/rotate_spec.rb +20 -6
  54. data/spec/tty/text/truncation/truncate_spec.rb +18 -3
  55. data/spec/tty/text/wrapping/wrap_spec.rb +24 -7
  56. metadata +56 -18
@@ -63,33 +63,79 @@ describe TTY::Table, 'orientation' do
63
63
 
64
64
  it { expect(subject.header).to be_empty }
65
65
 
66
- it 'rotates original rows' do
67
- rotated_rows = []
68
- (0..2).each {|n| rotated_rows << [header[n], rows[0][n], rows[1][n]] }
69
- expect(subject.to_a).to eql rotated_rows
70
- end
66
+ context 'with header' do
67
+ it 'rotates original rows' do
68
+ rotated_rows = [['h1','a1'],['h2','a2'],['h3','a3'], ['h1','b1'],['h2','b2'],['h3','b3']]
69
+ expect(subject.to_a).to eql rotated_rows
70
+ end
71
71
 
72
- context 'without border' do
73
- it 'displays table' do
74
- subject.to_s.should == <<-EOS.normalize
75
- h1 a1 b1
76
- h2 a2 b2
77
- h3 a3 b3
78
- EOS
72
+ context 'without border' do
73
+ it 'displays table' do
74
+ subject.to_s.should == <<-EOS.normalize
75
+ h1 a1
76
+ h2 a2
77
+ h3 a3
78
+ h1 b1
79
+ h2 b2
80
+ h3 b3
81
+ EOS
82
+ end
83
+ end
84
+
85
+ context 'with border' do
86
+ let(:renderer) { :ascii }
87
+
88
+ it 'diplays table' do
89
+ subject.render(renderer).should == <<-EOS.normalize
90
+ +--+--+
91
+ |h1|a1|
92
+ |h2|a2|
93
+ |h3|a3|
94
+ |h1|b1|
95
+ |h2|b2|
96
+ |h3|b3|
97
+ +--+--+
98
+ EOS
99
+ end
79
100
  end
80
101
  end
81
102
 
82
- context 'with border' do
83
- let(:renderer) { :ascii }
103
+ context 'without header' do
104
+ let(:header) { nil }
84
105
 
85
- it 'diplays table' do
86
- subject.render(renderer).should == <<-EOS.normalize
87
- +--+--+--+
88
- |h1|a1|b1|
89
- |h2|a2|b2|
90
- |h3|a3|b3|
91
- +--+--+--+
92
- EOS
106
+ it 'rotates original rows' do
107
+ rotated_rows = [['1','a1'],['2','a2'],['3','a3'], ['1','b1'],['2','b2'],['3','b3']]
108
+ expect(subject.to_a).to eql rotated_rows
109
+ end
110
+
111
+ context 'without border' do
112
+ it 'displays table' do
113
+ subject.to_s.should == <<-EOS.normalize
114
+ 1 a1
115
+ 2 a2
116
+ 3 a3
117
+ 1 b1
118
+ 2 b2
119
+ 3 b3
120
+ EOS
121
+ end
122
+ end
123
+
124
+ context 'with border' do
125
+ let(:renderer) { :ascii }
126
+
127
+ it 'diplays table' do
128
+ subject.render(renderer).should == <<-EOS.normalize
129
+ +-+--+
130
+ |1|a1|
131
+ |2|a2|
132
+ |3|a3|
133
+ |1|b1|
134
+ |2|b2|
135
+ |3|b3|
136
+ +-+--+
137
+ EOS
138
+ end
93
139
  end
94
140
  end
95
141
  end
@@ -0,0 +1,45 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Table::Padder, '#parse' do
6
+ let(:object) { described_class }
7
+
8
+ subject { object.parse(value).padding }
9
+
10
+ context 'when self' do
11
+ let(:value) { described_class.new([]) }
12
+
13
+ it { expect(subject).to eq([]) }
14
+ end
15
+
16
+ context 'when number' do
17
+ let(:value) { 5 }
18
+
19
+ it { expect(subject).to eq([5,5,5,5]) }
20
+ end
21
+
22
+ context 'when nil' do
23
+ let(:value) { nil }
24
+
25
+ it { expect(subject).to eq([]) }
26
+ end
27
+
28
+ context 'when 2-element array' do
29
+ let(:value) { [2,3] }
30
+
31
+ it { expect(subject).to eq([2,3,2,3]) }
32
+ end
33
+
34
+ context 'when 4-element array' do
35
+ let(:value) { [1,2,3,4] }
36
+
37
+ it { expect(subject).to eq([1,2,3,4]) }
38
+ end
39
+
40
+ context 'when unkown' do
41
+ let(:value) { :unkown }
42
+
43
+ it { expect { subject }.to raise_error }
44
+ end
45
+ end
@@ -0,0 +1,120 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Table, 'padding' 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
+
10
+ it 'sets specific padding' do
11
+ expect(table.render(:ascii) { |renderer|
12
+ renderer.multiline = true
13
+ renderer.padding.right = 2
14
+ renderer.padding.top = 1
15
+ }).to eq <<-EOS.normalize
16
+ +----+----+----+
17
+ | | | |
18
+ |h1 |h2 |h3 |
19
+ +----+----+----+
20
+ | | | |
21
+ |a1 |a2 |a3 |
22
+ | | | |
23
+ |b1 |b2 |b3 |
24
+ +----+----+----+
25
+ EOS
26
+ end
27
+
28
+ it 'sets padding for all' do
29
+ expect(table.render(:ascii) { |renderer|
30
+ renderer.multiline = true
31
+ renderer.padding= [1,2,1,2]
32
+ }).to eq <<-EOS.normalize
33
+ +------+------+------+
34
+ | | | |
35
+ | h1 | h2 | h3 |
36
+ | | | |
37
+ +------+------+------+
38
+ | | | |
39
+ | a1 | a2 | a3 |
40
+ | | | |
41
+ | | | |
42
+ | b1 | b2 | b3 |
43
+ | | | |
44
+ +------+------+------+
45
+ EOS
46
+ end
47
+
48
+ context 'with column width' do
49
+ let(:column_widths) { [4,4,4] }
50
+
51
+ it 'sets padding for all' do
52
+ expect(table.render(:ascii) { |renderer|
53
+ renderer.column_widths = column_widths
54
+ renderer.multiline = true
55
+ renderer.padding= [1,2,1,2]
56
+ }).to eq <<-EOS.normalize
57
+ +--------+--------+--------+
58
+ | | | |
59
+ | h1 | h2 | h3 |
60
+ | | | |
61
+ +--------+--------+--------+
62
+ | | | |
63
+ | a1 | a2 | a3 |
64
+ | | | |
65
+ | | | |
66
+ | b1 | b2 | b3 |
67
+ | | | |
68
+ +--------+--------+--------+
69
+ EOS
70
+ end
71
+ end
72
+
73
+ context 'with multi line text' do
74
+ let(:header) { ['head1', 'head2'] }
75
+ let(:rows) { [["Multi\nLine\nContent", "Text\nthat\nwraps",],
76
+ ["Some\nother\ntext", 'Simple']] }
77
+
78
+ context 'when wrapped' do
79
+ it 'sets padding for all' do
80
+ expect(table.render(:ascii) { |renderer|
81
+ renderer.multiline = true
82
+ renderer.padding= [1,2,1,2]
83
+ }).to eq <<-EOS.normalize
84
+ +-----------+----------+
85
+ | | |
86
+ | head1 | head2 |
87
+ | | |
88
+ +-----------+----------+
89
+ | | |
90
+ | Multi | Text |
91
+ | Line | that |
92
+ | Content | wraps |
93
+ | | |
94
+ | | |
95
+ | Some | Simple |
96
+ | other | |
97
+ | text | |
98
+ | | |
99
+ +-----------+----------+
100
+ EOS
101
+ end
102
+ end
103
+
104
+ context 'when escaped' do
105
+ it 'sets padding for all' do
106
+ expect(table.render(:ascii) { |renderer|
107
+ renderer.multiline = false
108
+ renderer.padding= [0,2,0,2]
109
+ }).to eq <<-EOS.normalize
110
+ +------------------------+---------------------+
111
+ | head1 | head2 |
112
+ +------------------------+---------------------+
113
+ | Multi\\nLine\\nContent | Text\\nthat\\nwraps |
114
+ | Some\\nother\\ntext | Simple |
115
+ +------------------------+---------------------+
116
+ EOS
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,41 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ 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
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ 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,114 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ 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