tty-table 0.1.0 → 0.2.0

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 (94) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +24 -0
  3. data/README.md +458 -142
  4. data/lib/tty-table.rb +6 -6
  5. data/lib/tty/table.rb +34 -34
  6. data/lib/tty/table/alignment_set.rb +73 -0
  7. data/lib/tty/table/border.rb +54 -36
  8. data/lib/tty/table/border/null.rb +4 -4
  9. data/lib/tty/table/{columns.rb → column_constraint.rb} +30 -32
  10. data/lib/tty/table/column_set.rb +18 -17
  11. data/lib/tty/table/field.rb +50 -25
  12. data/lib/tty/table/header.rb +6 -2
  13. data/lib/tty/table/indentation.rb +7 -12
  14. data/lib/tty/table/operation/alignment.rb +59 -0
  15. data/lib/tty/table/operation/escape.rb +1 -1
  16. data/lib/tty/table/operation/filter.rb +1 -1
  17. data/lib/tty/table/operation/padding.rb +12 -61
  18. data/lib/tty/table/operation/truncation.rb +2 -2
  19. data/lib/tty/table/operation/wrapped.rb +2 -5
  20. data/lib/tty/table/operations.rb +35 -17
  21. data/lib/tty/table/orientation/vertical.rb +4 -4
  22. data/lib/tty/table/renderer.rb +1 -7
  23. data/lib/tty/table/renderer/basic.rb +69 -63
  24. data/lib/tty/table/version.rb +1 -1
  25. data/spec/spec_helper.rb +3 -4
  26. data/spec/unit/access_spec.rb +8 -8
  27. data/spec/unit/{operation/alignment_set → alignment_set}/each_spec.rb +1 -1
  28. data/spec/unit/{operation/alignment_set → alignment_set}/new_spec.rb +4 -4
  29. data/spec/unit/{operation/alignment_set → alignment_set}/to_ary_spec.rb +1 -1
  30. data/spec/unit/alignment_spec.rb +71 -0
  31. data/spec/unit/border/ascii/rendering_spec.rb +12 -12
  32. data/spec/unit/border/new_spec.rb +2 -2
  33. data/spec/unit/border/null/rendering_spec.rb +2 -2
  34. data/spec/unit/border/unicode/rendering_spec.rb +10 -10
  35. data/spec/unit/{columns → column_constraint}/enforce_spec.rb +15 -12
  36. data/spec/unit/{columns → column_constraint}/widths_spec.rb +6 -6
  37. data/spec/unit/column_set/extract_widths_spec.rb +39 -6
  38. data/spec/unit/data_spec.rb +4 -6
  39. data/spec/unit/each_spec.rb +8 -23
  40. data/spec/unit/each_with_index_spec.rb +27 -33
  41. data/spec/unit/field/length_spec.rb +23 -9
  42. data/spec/unit/field/width_spec.rb +1 -1
  43. data/spec/unit/filter_spec.rb +7 -8
  44. data/spec/unit/header/new_spec.rb +6 -15
  45. data/spec/unit/indentation/indent_spec.rb +21 -0
  46. data/spec/unit/new_spec.rb +73 -0
  47. data/spec/unit/operation/{alignment_set → alignment}/call_spec.rb +1 -1
  48. data/spec/unit/operation/escape/call_spec.rb +2 -3
  49. data/spec/unit/operation/filter/call_spec.rb +2 -3
  50. data/spec/unit/operation/truncation/call_spec.rb +6 -8
  51. data/spec/unit/operation/wrapped/call_spec.rb +15 -8
  52. data/spec/unit/operations/new_spec.rb +1 -1
  53. data/spec/unit/orientation_spec.rb +6 -6
  54. data/spec/unit/padding_spec.rb +29 -32
  55. data/spec/unit/properties_spec.rb +4 -4
  56. data/spec/unit/render_repeat_spec.rb +42 -0
  57. data/spec/unit/render_spec.rb +1 -1
  58. data/spec/unit/render_with_spec.rb +3 -3
  59. data/spec/unit/renderer/ascii/coloring_spec.rb +70 -0
  60. data/spec/unit/renderer/ascii/multiline_spec.rb +101 -0
  61. data/spec/unit/renderer/ascii/padding_spec.rb +37 -10
  62. data/spec/unit/renderer/ascii/render_spec.rb +4 -4
  63. data/spec/unit/renderer/ascii/resizing_spec.rb +22 -22
  64. data/spec/unit/renderer/ascii/separator_spec.rb +1 -1
  65. data/spec/unit/renderer/basic/alignment_spec.rb +20 -20
  66. data/spec/unit/renderer/basic/coloring_spec.rb +43 -28
  67. data/spec/unit/renderer/basic/filter_spec.rb +3 -3
  68. data/spec/unit/renderer/basic/multiline_spec.rb +74 -0
  69. data/spec/unit/renderer/basic/options_spec.rb +9 -9
  70. data/spec/unit/renderer/basic/padding_spec.rb +26 -2
  71. data/spec/unit/renderer/basic/render_spec.rb +4 -4
  72. data/spec/unit/renderer/basic/resizing_spec.rb +18 -18
  73. data/spec/unit/renderer/basic/separator_spec.rb +1 -1
  74. data/spec/unit/renderer/basic/truncation_spec.rb +6 -6
  75. data/spec/unit/renderer/basic/wrapping_spec.rb +3 -3
  76. data/spec/unit/renderer/border_spec.rb +4 -4
  77. data/spec/unit/renderer/unicode/coloring_spec.rb +70 -0
  78. data/spec/unit/renderer/unicode/indentation_spec.rb +1 -1
  79. data/spec/unit/renderer/unicode/padding_spec.rb +26 -26
  80. data/spec/unit/renderer/unicode/render_spec.rb +4 -4
  81. data/spec/unit/renderer/unicode/separator_spec.rb +1 -1
  82. data/spec/unit/to_s_spec.rb +4 -11
  83. data/spec/unit/utf_spec.rb +33 -0
  84. data/tty-table.gemspec +2 -1
  85. metadata +52 -32
  86. data/lib/tty/table/operation/alignment_set.rb +0 -103
  87. data/lib/tty/table/padder.rb +0 -180
  88. data/lib/tty/table/renderer/color.rb +0 -12
  89. data/spec/unit/indentation/insert_indent_spec.rb +0 -27
  90. data/spec/unit/initialize_spec.rb +0 -88
  91. data/spec/unit/padder/parse_spec.rb +0 -45
  92. data/spec/unit/padder/to_s_spec.rb +0 -14
  93. data/spec/unit/renderer/basic/multiline_content_spec.rb +0 -135
  94. data/spec/unit/renderer/style_spec.rb +0 -72
@@ -1,180 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module TTY
4
- class Table
5
- # A class responsible for processing table field padding
6
- #
7
- # Used internally by {Table::Renderer}
8
- #
9
- # @api private
10
- class Padder
11
- include Equatable
12
-
13
- # Padding for the table cells
14
- #
15
- # @return [Array[Integer]]
16
- attr_reader :padding
17
-
18
- # Initialize a Padder
19
- #
20
- # @api public
21
- def initialize(padding)
22
- @padding = padding
23
- end
24
-
25
- # Parse padding options
26
- #
27
- # Turn possible values into 4 element array
28
- #
29
- # @example
30
- # padder = TTY::Table::Padder.parse(5)
31
- # padder.padding # => [5, 5, 5, 5]
32
- #
33
- # @param [Object] value
34
- #
35
- # @return [TTY::Padder]
36
- # the new padder with padding values
37
- #
38
- # @api public
39
- def self.parse(value = nil)
40
- return value if value.is_a?(self)
41
-
42
- new(convert_to_ary(value))
43
- end
44
-
45
- # Convert value to 4 element array
46
- #
47
- # @return [Array[Integer]]
48
- # the 4 element padding array
49
- #
50
- # @api private
51
- def self.convert_to_ary(value)
52
- if value.class <= Numeric
53
- [value, value, value, value]
54
- elsif value.nil?
55
- []
56
- elsif value.size == 2
57
- [value[0], value[1], value[0], value[1]]
58
- elsif value.size == 4
59
- value
60
- else
61
- fail ArgumentError, 'Wrong :padding parameter, must be an array'
62
- end
63
- end
64
-
65
- # Top padding
66
- #
67
- # @return [Integer]
68
- #
69
- # @api public
70
- def top
71
- @padding[0].to_i
72
- end
73
-
74
- # Set top padding
75
- #
76
- # @param [Integer] val
77
- #
78
- # @return [nil]
79
- #
80
- # @api public
81
- def top=(value)
82
- @padding[0] = value
83
- end
84
-
85
- # Right padding
86
- #
87
- # @return [Integer]
88
- #
89
- # @api public
90
- def right
91
- @padding[1].to_i
92
- end
93
-
94
- # Set right padding
95
- #
96
- # @param [Integer] val
97
- #
98
- # @api public
99
- def right=(value)
100
- @padding[1] = value
101
- end
102
-
103
- # Bottom padding
104
- #
105
- # @return [Integer]
106
- #
107
- # @api public
108
- def bottom
109
- @padding[2].to_i
110
- end
111
-
112
- # Set bottom padding
113
- #
114
- # @param [Integer] value
115
- #
116
- # @return [nil]
117
- #
118
- # @api public
119
- def bottom=(value)
120
- @padding[2] = value
121
- end
122
-
123
- # Left padding
124
- #
125
- # @return [Integer]
126
- #
127
- # @api public
128
- def left
129
- @padding[3].to_i
130
- end
131
-
132
- # Set left padding
133
- #
134
- # @param [Integer] value
135
- #
136
- # @return [nil]
137
- #
138
- # @api public
139
- def left=(value)
140
- @padding[3] = value
141
- end
142
-
143
- # Check if padding is set
144
- #
145
- # @return [Boolean]
146
- #
147
- # @api public
148
- def empty?
149
- padding.empty?
150
- end
151
-
152
- # Check if vertical padding is applied
153
- #
154
- # @return [Boolean]
155
- #
156
- # @api public
157
- def vertical?
158
- top.nonzero? || bottom.nonzero?
159
- end
160
-
161
- # Check if horizontal padding is applied
162
- #
163
- # @return [Boolean]
164
- #
165
- # @api public
166
- def horizontal?
167
- left.nonzero? || right.nonzero?
168
- end
169
-
170
- # String represenation of this padder with padding values
171
- #
172
- # @return [String]
173
- #
174
- # @api public
175
- def to_s
176
- inspect
177
- end
178
- end # Padder
179
- end # Table
180
- end # TTY
@@ -1,12 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- module TTY
4
- class Table
5
- class Renderer
6
- class Color < Basic
7
-
8
-
9
- end # Color
10
- end # Renderer
11
- end # Table
12
- end # TTY
@@ -1,27 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe TTY::Table::Indentation, '#insert_indent' do
6
- let(:indent) { 2 }
7
- let(:renderer) { double(:renderer, indent: indent) }
8
- let(:object) { described_class.new(renderer) }
9
-
10
- subject { object.insert_indent(part) }
11
-
12
- context 'when enumerable' do
13
- let(:part) { ['line1'] }
14
-
15
- it 'inserts indentation for each element' do
16
- expect(subject[0]).to eql(' line1')
17
- end
18
- end
19
-
20
- context 'when string' do
21
- let(:part) { 'line1' }
22
-
23
- it 'inserts indentation' do
24
- expect(subject).to eql(' line1')
25
- end
26
- end
27
- end
@@ -1,88 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe TTY::Table, '#initialize' do
6
- let(:header) { ['Header1', 'Header2'] }
7
- let(:rows) { [['a1', 'a2'], ['b1', 'b2']] }
8
-
9
- it { is_expected.to be_kind_of(Enumerable) }
10
-
11
- it { is_expected.to be_kind_of(Comparable) }
12
-
13
- it { expect(Enumerable === subject).to eq(true) }
14
-
15
- context 'with rows only' do
16
- it 'initializes with rows as arguments' do
17
- table = TTY::Table[*rows]
18
- expect(table.to_a).to eql(rows)
19
- end
20
-
21
- it 'initializes with rows' do
22
- table = TTY::Table.new rows
23
- expect(table.to_a).to eql(rows)
24
- end
25
-
26
- it 'initializes table rows as an option' do
27
- table = TTY::Table.new rows: rows
28
- expect(table.to_a).to eq(rows)
29
- end
30
-
31
- it 'initializes table rows in a block with param' do
32
- table = TTY::Table.new do |t|
33
- t << rows[0]
34
- t << rows[1]
35
- end
36
- expect(table.to_a).to eq(rows)
37
- end
38
-
39
- it 'initializes table and adds rows' do
40
- table = TTY::Table.new
41
- table << rows[0]
42
- table << rows[1]
43
- expect(table.to_a).to eq(rows)
44
- end
45
-
46
- it 'chains rows' do
47
- table = TTY::Table.new
48
- table << rows[0] << rows[1]
49
- expect(table.to_a).to eq(rows)
50
- end
51
-
52
- context 'with data as hash' do
53
- let(:data) { [ {'Header1' => ['a1','a2'], 'Header2' => ['b1', 'b2'] }] }
54
-
55
- it 'extracts rows' do
56
- table = TTY::Table.new data
57
- expect(table.to_a).to include rows[0]
58
- expect(table.to_a).to include rows[1]
59
- end
60
-
61
- it 'extracts header' do
62
- table = TTY::Table.new data
63
- expect(table.header).to include header[0]
64
- expect(table.header).to include header[1]
65
- end
66
- end
67
- end
68
-
69
- context 'with header and rows' do
70
- it 'initializes header as an option' do
71
- table = TTY::Table.new header: header
72
- expect(table.header).to eql(header)
73
- end
74
-
75
- it 'initializes table rows as an argument' do
76
- table = TTY::Table.new header, rows
77
- expect(table.header).to eql(header)
78
- expect(table.rows).to eql(rows)
79
- end
80
- end
81
-
82
- context 'coercion' do
83
- it 'converts row arguments from hash to array' do
84
- table = TTY::Table.new rows: {a: 1, b: 2}
85
- expect(table.to_a).to include [:a, 1 ]
86
- end
87
- end
88
- end
@@ -1,45 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.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
@@ -1,14 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe TTY::Table::Padder, '.to_s' do
6
- let(:object) { described_class }
7
- let(:value) { [1,2,3,4] }
8
-
9
- subject { object.parse(value) }
10
-
11
- it 'prints string representation' do
12
- expect(subject.to_s).to eq('#<TTY::Table::Padder padding=[1, 2, 3, 4]>')
13
- end
14
- end
@@ -1,135 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe TTY::Table::Renderer::Basic, 'with multiline content' do
6
- let(:header) { nil }
7
- let(:object) { described_class }
8
- let(:table) { TTY::Table.new header, rows }
9
-
10
- subject(:renderer) { object.new table }
11
-
12
- context 'with escaping' do
13
- let(:rows) { [ ["First", '1'], ["Multiline\nContent", '2'], ["Third", '3']] }
14
-
15
- context 'without border' do
16
- it "renders single line" do
17
- expect(table.render(multiline: false)).to eq <<-EOS.normalize
18
- First 1
19
- Multiline\\nContent 2
20
- Third 3
21
- EOS
22
- end
23
- end
24
-
25
- context 'with column widths' do
26
- it "renders single line" do
27
- expect(table.render(multiline: false, column_widths: [8,1])).to eq <<-EOS.normalize
28
- First 1
29
- Multili… 2
30
- Third 3
31
- EOS
32
- end
33
- end
34
-
35
- context 'with border' do
36
- it "renders single line" do
37
- expect(table.render(:ascii, multiline: false)).to eq <<-EOS.normalize
38
- +------------------+-+
39
- |First |1|
40
- |Multiline\\nContent|2|
41
- |Third |3|
42
- +------------------+-+
43
- EOS
44
- end
45
- end
46
-
47
- context 'with header' do
48
- let(:header) { ["Multi\nHeader", "header2"] }
49
-
50
- it "renders header" do
51
- expect(table.render(:ascii, multiline: false)).to eq <<-EOS.normalize
52
- +------------------+-------+
53
- |Multi\\nHeader |header2|
54
- +------------------+-------+
55
- |First |1 |
56
- |Multiline\\nContent|2 |
57
- |Third |3 |
58
- +------------------+-------+
59
- EOS
60
- end
61
- end
62
- end
63
-
64
- context 'without escaping' do
65
- let(:rows) { [ ["First", '1'], ["Multi\nLine\nContent", '2'], ["Third", '3']] }
66
-
67
- context 'without border' do
68
- it "renders every line" do
69
- expect(table.render(multiline: true)).to eq <<-EOS.normalize
70
- First 1
71
- Multi 2
72
- Line
73
- Content
74
- Third 3
75
- EOS
76
- end
77
- end
78
-
79
- context 'with column widths' do
80
- it "renders multiline" do
81
- expect(table.render(multiline: true, column_widths: [8,1])).to eq <<-EOS.normalize
82
- First 1
83
- Multi 2
84
- Line
85
- Content
86
- Third 3
87
- EOS
88
- end
89
-
90
- it 'wraps multi line' do
91
- expect(table.render(multiline: true, column_widths: [5,1])).to eq <<-EOS.normalize
92
- First 1
93
- Multi 2
94
- Line
95
- Conte
96
- nt
97
- Third 3
98
- EOS
99
- end
100
- end
101
-
102
- context 'with border' do
103
- it "renders every line" do
104
- expect(table.render(:ascii, multiline: true)).to eq <<-EOS.normalize
105
- +-------+-+
106
- |First |1|
107
- |Multi |2|
108
- |Line | |
109
- |Content| |
110
- |Third |3|
111
- +-------+-+
112
- EOS
113
- end
114
- end
115
-
116
- context 'with header' do
117
- let(:header) { ["Multi\nHeader", "header2"] }
118
-
119
- it "renders header" do
120
- expect(table.render(:ascii, multiline: true)).to eq <<-EOS.normalize
121
- +-------+-------+
122
- |Multi |header2|
123
- |Header | |
124
- +-------+-------+
125
- |First |1 |
126
- |Multi |2 |
127
- |Line | |
128
- |Content| |
129
- |Third |3 |
130
- +-------+-------+
131
- EOS
132
- end
133
- end
134
- end
135
- end