tty-table 0.7.0 → 0.12.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 (151) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +57 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +165 -94
  5. data/lib/tty-table.rb +1 -5
  6. data/lib/tty/table.rb +102 -42
  7. data/lib/tty/table/alignment_set.rb +3 -6
  8. data/lib/tty/table/border.rb +57 -68
  9. data/lib/tty/table/border/ascii.rb +17 -17
  10. data/lib/tty/table/border/null.rb +20 -22
  11. data/lib/tty/table/border/row_line.rb +4 -3
  12. data/lib/tty/table/border/unicode.rb +17 -17
  13. data/lib/tty/table/border_dsl.rb +28 -25
  14. data/lib/tty/table/border_options.rb +50 -25
  15. data/lib/tty/table/column_constraint.rb +29 -17
  16. data/lib/tty/table/{column_set.rb → columns.rb} +44 -69
  17. data/lib/tty/table/empty.rb +2 -0
  18. data/lib/tty/table/error.rb +1 -1
  19. data/lib/tty/table/field.rb +43 -35
  20. data/lib/tty/table/header.rb +7 -5
  21. data/lib/tty/table/indentation.rb +16 -25
  22. data/lib/tty/table/operation/alignment.rb +10 -8
  23. data/lib/tty/table/operation/escape.rb +2 -3
  24. data/lib/tty/table/operation/filter.rb +1 -1
  25. data/lib/tty/table/operation/padding.rb +4 -12
  26. data/lib/tty/table/operation/truncation.rb +4 -4
  27. data/lib/tty/table/operation/wrapped.rb +4 -4
  28. data/lib/tty/table/operations.rb +4 -15
  29. data/lib/tty/table/options.rb +2 -4
  30. data/lib/tty/table/orientation.rb +5 -5
  31. data/lib/tty/table/orientation/horizontal.rb +1 -1
  32. data/lib/tty/table/orientation/vertical.rb +1 -1
  33. data/lib/tty/table/renderer.rb +40 -35
  34. data/lib/tty/table/renderer/ascii.rb +4 -4
  35. data/lib/tty/table/renderer/basic.rb +85 -88
  36. data/lib/tty/table/renderer/unicode.rb +3 -3
  37. data/lib/tty/table/row.rb +10 -8
  38. data/lib/tty/table/transformation.rb +2 -2
  39. data/lib/tty/table/validatable.rb +25 -7
  40. data/lib/tty/table/version.rb +3 -3
  41. metadata +33 -283
  42. data/.gitignore +0 -14
  43. data/.rspec +0 -3
  44. data/.travis.yml +0 -24
  45. data/CODE_OF_CONDUCT.md +0 -49
  46. data/Gemfile +0 -18
  47. data/Rakefile +0 -8
  48. data/benchmarks/speed.rb +0 -41
  49. data/spec/spec_helper.rb +0 -50
  50. data/spec/unit/access_spec.rb +0 -86
  51. data/spec/unit/add_row_spec.rb +0 -28
  52. data/spec/unit/alignment_set/each_spec.rb +0 -17
  53. data/spec/unit/alignment_set/new_spec.rb +0 -27
  54. data/spec/unit/alignment_set/to_ary_spec.rb +0 -14
  55. data/spec/unit/alignment_spec.rb +0 -71
  56. data/spec/unit/border/ascii/rendering_spec.rb +0 -90
  57. data/spec/unit/border/new_spec.rb +0 -27
  58. data/spec/unit/border/null/rendering_spec.rb +0 -130
  59. data/spec/unit/border/options/from_spec.rb +0 -38
  60. data/spec/unit/border/options/new_spec.rb +0 -14
  61. data/spec/unit/border/unicode/rendering_spec.rb +0 -63
  62. data/spec/unit/border_options/new_spec.rb +0 -20
  63. data/spec/unit/border_options/update_spec.rb +0 -18
  64. data/spec/unit/column_constraint/enforce_spec.rb +0 -70
  65. data/spec/unit/column_constraint/widths_spec.rb +0 -35
  66. data/spec/unit/column_set/extract_widths_spec.rb +0 -48
  67. data/spec/unit/column_set/total_width_spec.rb +0 -15
  68. data/spec/unit/column_set/widths_from_spec.rb +0 -51
  69. data/spec/unit/data_spec.rb +0 -12
  70. data/spec/unit/each_spec.rb +0 -26
  71. data/spec/unit/each_with_index_spec.rb +0 -51
  72. data/spec/unit/empty_spec.rb +0 -23
  73. data/spec/unit/eql_spec.rb +0 -34
  74. data/spec/unit/field/equality_spec.rb +0 -51
  75. data/spec/unit/field/length_spec.rb +0 -35
  76. data/spec/unit/field/lines_spec.rb +0 -21
  77. data/spec/unit/field/new_spec.rb +0 -29
  78. data/spec/unit/field/width_spec.rb +0 -23
  79. data/spec/unit/filter_spec.rb +0 -22
  80. data/spec/unit/header/call_spec.rb +0 -30
  81. data/spec/unit/header/color_spec.rb +0 -19
  82. data/spec/unit/header/equality_spec.rb +0 -51
  83. data/spec/unit/header/height_spec.rb +0 -27
  84. data/spec/unit/header/new_spec.rb +0 -16
  85. data/spec/unit/header/set_spec.rb +0 -20
  86. data/spec/unit/header/to_ary_spec.rb +0 -14
  87. data/spec/unit/header_spec.rb +0 -13
  88. data/spec/unit/indentation/indent_spec.rb +0 -21
  89. data/spec/unit/new_spec.rb +0 -73
  90. data/spec/unit/operation/alignment/call_spec.rb +0 -39
  91. data/spec/unit/operation/escape/call_spec.rb +0 -15
  92. data/spec/unit/operation/filter/call_spec.rb +0 -16
  93. data/spec/unit/operation/truncation/call_spec.rb +0 -30
  94. data/spec/unit/operation/wrapped/call_spec.rb +0 -40
  95. data/spec/unit/operations/new_spec.rb +0 -30
  96. data/spec/unit/options/access_spec.rb +0 -14
  97. data/spec/unit/options_spec.rb +0 -25
  98. data/spec/unit/orientation_spec.rb +0 -145
  99. data/spec/unit/padding_spec.rb +0 -117
  100. data/spec/unit/properties_spec.rb +0 -25
  101. data/spec/unit/render_repeat_spec.rb +0 -42
  102. data/spec/unit/render_spec.rb +0 -63
  103. data/spec/unit/render_with_spec.rb +0 -106
  104. data/spec/unit/renderer/ascii/coloring_spec.rb +0 -70
  105. data/spec/unit/renderer/ascii/indentation_spec.rb +0 -41
  106. data/spec/unit/renderer/ascii/multiline_spec.rb +0 -101
  107. data/spec/unit/renderer/ascii/padding_spec.rb +0 -88
  108. data/spec/unit/renderer/ascii/render_spec.rb +0 -68
  109. data/spec/unit/renderer/ascii/resizing_spec.rb +0 -114
  110. data/spec/unit/renderer/ascii/separator_spec.rb +0 -28
  111. data/spec/unit/renderer/basic/alignment_spec.rb +0 -88
  112. data/spec/unit/renderer/basic/coloring_spec.rb +0 -61
  113. data/spec/unit/renderer/basic/extract_column_widths_spec.rb +0 -28
  114. data/spec/unit/renderer/basic/filter_spec.rb +0 -53
  115. data/spec/unit/renderer/basic/indentation_spec.rb +0 -48
  116. data/spec/unit/renderer/basic/multiline_spec.rb +0 -74
  117. data/spec/unit/renderer/basic/new_spec.rb +0 -26
  118. data/spec/unit/renderer/basic/options_spec.rb +0 -52
  119. data/spec/unit/renderer/basic/padding_spec.rb +0 -76
  120. data/spec/unit/renderer/basic/render_spec.rb +0 -57
  121. data/spec/unit/renderer/basic/resizing_spec.rb +0 -96
  122. data/spec/unit/renderer/basic/separator_spec.rb +0 -43
  123. data/spec/unit/renderer/basic/truncation_spec.rb +0 -35
  124. data/spec/unit/renderer/basic/wrapping_spec.rb +0 -40
  125. data/spec/unit/renderer/border_spec.rb +0 -104
  126. data/spec/unit/renderer/render_spec.rb +0 -36
  127. data/spec/unit/renderer/select_spec.rb +0 -22
  128. data/spec/unit/renderer/unicode/coloring_spec.rb +0 -70
  129. data/spec/unit/renderer/unicode/indentation_spec.rb +0 -41
  130. data/spec/unit/renderer/unicode/padding_spec.rb +0 -61
  131. data/spec/unit/renderer/unicode/render_spec.rb +0 -68
  132. data/spec/unit/renderer/unicode/separator_spec.rb +0 -26
  133. data/spec/unit/renderer_spec.rb +0 -19
  134. data/spec/unit/rotate_spec.rb +0 -86
  135. data/spec/unit/row/access_spec.rb +0 -25
  136. data/spec/unit/row/call_spec.rb +0 -45
  137. data/spec/unit/row/data_spec.rb +0 -26
  138. data/spec/unit/row/each_spec.rb +0 -31
  139. data/spec/unit/row/equality_spec.rb +0 -73
  140. data/spec/unit/row/height_spec.rb +0 -27
  141. data/spec/unit/row/new_spec.rb +0 -41
  142. data/spec/unit/row/to_ary_spec.rb +0 -14
  143. data/spec/unit/to_s_spec.rb +0 -56
  144. data/spec/unit/transformation/extract_tuples_spec.rb +0 -35
  145. data/spec/unit/utf_spec.rb +0 -33
  146. data/spec/unit/validatable/validate_options_spec.rb +0 -33
  147. data/spec/unit/validatable_spec.rb +0 -32
  148. data/tasks/console.rake +0 -10
  149. data/tasks/coverage.rake +0 -11
  150. data/tasks/spec.rake +0 -29
  151. data/tty-table.gemspec +0 -30
@@ -1,6 +1,6 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
- require 'tty/table/border'
3
+ require_relative "../border"
4
4
 
5
5
  module TTY
6
6
  class Table
@@ -11,21 +11,21 @@ module TTY
11
11
  class ASCII < Border
12
12
 
13
13
  def_border do
14
- top '-'
15
- top_mid '+'
16
- top_left '+'
17
- top_right '+'
18
- bottom '-'
19
- bottom_mid '+'
20
- bottom_left '+'
21
- bottom_right '+'
22
- mid '-'
23
- mid_mid '+'
24
- mid_left '+'
25
- mid_right '+'
26
- left '|'
27
- center '|'
28
- right '|'
14
+ top "-"
15
+ top_mid "+"
16
+ top_left "+"
17
+ top_right "+"
18
+ bottom "-"
19
+ bottom_mid "+"
20
+ bottom_left "+"
21
+ bottom_right "+"
22
+ mid "-"
23
+ mid_mid "+"
24
+ mid_left "+"
25
+ mid_right "+"
26
+ left "|"
27
+ center "|"
28
+ right "|"
29
29
  end
30
30
 
31
31
  end # ASCII
@@ -1,37 +1,35 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
- require 'tty/table/border'
3
+ require_relative "../border"
4
4
 
5
5
  module TTY
6
6
  class Table
7
7
  class Border
8
8
  # A class that represents no border.
9
9
  class Null < Border
10
-
11
10
  def_border do
12
- center SPACE_CHAR
13
- end
14
-
15
- # A stub top line
16
- #
17
- # @api private
18
- def top_line
19
- border_options ? super : nil
20
- end
21
-
22
- # A stub separator line
23
- #
24
- # @api private
25
- def separator
26
- return [] if border_options.separator == EACH_ROW
27
- border_options ? super : nil
11
+ top EMPTY_CHAR
12
+ top_mid EMPTY_CHAR
13
+ top_left EMPTY_CHAR
14
+ top_right EMPTY_CHAR
15
+ bottom EMPTY_CHAR
16
+ bottom_mid EMPTY_CHAR
17
+ bottom_left EMPTY_CHAR
18
+ bottom_right EMPTY_CHAR
19
+ mid EMPTY_CHAR
20
+ mid_mid EMPTY_CHAR
21
+ mid_left EMPTY_CHAR
22
+ mid_right EMPTY_CHAR
23
+ left EMPTY_CHAR
24
+ center SPACE_CHAR
25
+ right EMPTY_CHAR
28
26
  end
29
27
 
30
- # A stub bottom line
28
+ # A stub middle line
31
29
  #
32
30
  # @api private
33
- def bottom_line
34
- border_options ? super : nil
31
+ def middle_line
32
+ border_options.separator ? "" : super
35
33
  end
36
34
  end # Null
37
35
  end # Border
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module TTY
4
4
  class Table
@@ -9,8 +9,9 @@ module TTY
9
9
  #
10
10
  # @api public
11
11
  def colorize(border, style)
12
- colorized_chars = border.set_color(style, right, center, left)
13
- self.right, self.center, self.left = colorized_chars
12
+ self.right = border.set_color(style, right)
13
+ self.center = border.set_color(style, center)
14
+ self.left = border.set_color(style, left)
14
15
  end
15
16
  end # RowLine
16
17
  end # Border
@@ -1,6 +1,6 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
- require 'tty/table/border'
3
+ require_relative "../border"
4
4
 
5
5
  module TTY
6
6
  class Table
@@ -11,21 +11,21 @@ module TTY
11
11
  class Unicode < Border
12
12
 
13
13
  def_border do
14
- top ''
15
- top_mid ''
16
- top_left ''
17
- top_right ''
18
- bottom ''
19
- bottom_mid ''
20
- bottom_left ''
21
- bottom_right ''
22
- mid ''
23
- mid_mid ''
24
- mid_left ''
25
- mid_right ''
26
- left ''
27
- center ''
28
- right ''
14
+ top ""
15
+ top_mid ""
16
+ top_left ""
17
+ top_right ""
18
+ bottom ""
19
+ bottom_mid ""
20
+ bottom_left ""
21
+ bottom_right ""
22
+ mid ""
23
+ mid_mid ""
24
+ mid_left ""
25
+ mid_right ""
26
+ left ""
27
+ center ""
28
+ right ""
29
29
  end
30
30
 
31
31
  end # Unicode
@@ -1,7 +1,8 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
- require 'forwardable'
4
- require 'tty/table/border_options'
3
+ require "forwardable"
4
+
5
+ require_relative "border_options"
5
6
 
6
7
  module TTY
7
8
  class Table
@@ -28,15 +29,14 @@ module TTY
28
29
  # @return [undefined]
29
30
  #
30
31
  # @api private
31
- def initialize(characters = nil, &block)
32
- @options = TTY::Table::BorderOptions.new
33
- @options.characters = characters if characters
32
+ def initialize(border_opts = nil, &block)
33
+ @options = TTY::Table::BorderOptions.from(border_opts)
34
34
  yield_or_eval(&block) if block_given?
35
35
  end
36
36
 
37
37
  # Apply style color to the border
38
38
  #
39
- # @param [Symbol] style
39
+ # @param [Symbol] value
40
40
  # the style color for the border
41
41
  #
42
42
  # @return [undefined]
@@ -44,19 +44,22 @@ module TTY
44
44
  # @api public
45
45
  def style(value = (not_set = true))
46
46
  return options.style if not_set
47
+
47
48
  options.style = value
48
49
  end
49
50
 
50
51
  # Apply table tuple separator
51
52
  #
52
- # @param [Symbol] separator
53
+ # @param [Symbol] value
53
54
  # the table tuple separator
54
55
  #
55
56
  # @return [undefined]
56
57
  #
57
58
  # @api public
58
- def separator(value)
59
- value ? options.separator = value : options.separator
59
+ def separator(value = (not_set = true))
60
+ return options.separator if not_set
61
+
62
+ options.separator = value
60
63
  end
61
64
 
62
65
  # Set top border character
@@ -68,7 +71,7 @@ module TTY
68
71
  #
69
72
  # @api public
70
73
  def top(value)
71
- options.characters['top'] = value
74
+ characters["top"] = value
72
75
  end
73
76
 
74
77
  # Set top middle border character
@@ -80,7 +83,7 @@ module TTY
80
83
  #
81
84
  # @api public
82
85
  def top_mid(value)
83
- options.characters['top_mid'] = value
86
+ characters["top_mid"] = value
84
87
  end
85
88
 
86
89
  # Set top left corner border character
@@ -92,7 +95,7 @@ module TTY
92
95
  #
93
96
  # @api public
94
97
  def top_left(value)
95
- options.characters['top_left'] = value
98
+ characters["top_left"] = value
96
99
  end
97
100
 
98
101
  # Set top right corner border character
@@ -104,7 +107,7 @@ module TTY
104
107
  #
105
108
  # @api public
106
109
  def top_right(value)
107
- options.characters['top_right'] = value
110
+ characters["top_right"] = value
108
111
  end
109
112
 
110
113
  # Set bottom border character
@@ -116,7 +119,7 @@ module TTY
116
119
  #
117
120
  # @api public
118
121
  def bottom(value)
119
- options.characters['bottom'] = value
122
+ characters["bottom"] = value
120
123
  end
121
124
 
122
125
  # Set bottom middle border character
@@ -128,7 +131,7 @@ module TTY
128
131
  #
129
132
  # @api public
130
133
  def bottom_mid(value)
131
- options.characters['bottom_mid'] = value
134
+ characters["bottom_mid"] = value
132
135
  end
133
136
 
134
137
  # Set bottom left corner border character
@@ -140,7 +143,7 @@ module TTY
140
143
  #
141
144
  # @api public
142
145
  def bottom_left(value)
143
- options.characters['bottom_left'] = value
146
+ characters["bottom_left"] = value
144
147
  end
145
148
 
146
149
  # Set bottom right corner border character
@@ -152,7 +155,7 @@ module TTY
152
155
  #
153
156
  # @api public
154
157
  def bottom_right(value)
155
- options.characters['bottom_right'] = value
158
+ characters["bottom_right"] = value
156
159
  end
157
160
 
158
161
  # Set middle border character
@@ -164,7 +167,7 @@ module TTY
164
167
  #
165
168
  # @api public
166
169
  def mid(value)
167
- options.characters['mid'] = value
170
+ characters["mid"] = value
168
171
  end
169
172
 
170
173
  # Set middle border character
@@ -176,7 +179,7 @@ module TTY
176
179
  #
177
180
  # @api public
178
181
  def mid_mid(value)
179
- options.characters['mid_mid'] = value
182
+ characters["mid_mid"] = value
180
183
  end
181
184
 
182
185
  # Set middle left corner border character
@@ -188,7 +191,7 @@ module TTY
188
191
  #
189
192
  # @api public
190
193
  def mid_left(value)
191
- options.characters['mid_left'] = value
194
+ characters["mid_left"] = value
192
195
  end
193
196
 
194
197
  # Set middle right corner border character
@@ -200,7 +203,7 @@ module TTY
200
203
  #
201
204
  # @api public
202
205
  def mid_right(value)
203
- options.characters['mid_right'] = value
206
+ characters["mid_right"] = value
204
207
  end
205
208
 
206
209
  # Set left border character
@@ -212,7 +215,7 @@ module TTY
212
215
  #
213
216
  # @api public
214
217
  def left(value)
215
- options.characters['left'] = value
218
+ characters["left"] = value
216
219
  end
217
220
 
218
221
  # Set center border character
@@ -224,7 +227,7 @@ module TTY
224
227
  #
225
228
  # @api public
226
229
  def center(value)
227
- options.characters['center'] = value
230
+ characters["center"] = value
228
231
  end
229
232
 
230
233
  # Set right border character
@@ -236,7 +239,7 @@ module TTY
236
239
  #
237
240
  # @api public
238
241
  def right(value)
239
- options.characters['right'] = value
242
+ characters["right"] = value
240
243
  end
241
244
 
242
245
  private
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module TTY
4
4
  class Table
@@ -7,46 +7,71 @@ module TTY
7
7
  # Used internally by {Table::Border} to manage options such as style
8
8
  #
9
9
  # @api private
10
- class BorderOptions < Struct.new(:characters, :separator, :style)
11
- # Initialize a BorderOptions
10
+ class BorderOptions
11
+ # Create options instance from hash
12
12
  #
13
13
  # @api public
14
- def initialize(*args)
15
- super(*args)
16
- self.characters = {} unless characters
14
+ def self.from(options)
15
+ return new if options.nil?
16
+
17
+ opts = case options
18
+ when self.class
19
+ options.to_hash
20
+ else
21
+ options
22
+ end
23
+ new(**opts)
17
24
  end
18
25
 
19
- # Create options instance from hash
26
+ attr_accessor :characters
27
+
28
+ attr_accessor :separator
29
+
30
+ attr_accessor :style
31
+
32
+ # Initialize a BorderOptions
33
+ #
34
+ # @param [String] style
35
+ # the style like :red
36
+ # @param [String] separator
37
+ # the separator character
38
+ # @param [Hash] characters
39
+ # the border characters
20
40
  #
21
41
  # @api public
22
- def self.from(value)
23
- value ? new.update(value) : new
42
+ def initialize(characters: {}, separator: nil, style: nil)
43
+ @characters = characters
44
+ @separator = separator
45
+ @style = style
24
46
  end
25
47
 
26
- # Set all accessors with hash attributes
27
- #
28
- # @param [Hash, BorderOptions] obj
48
+ # Convert to hash
29
49
  #
30
- # @return [BorderOptions]
50
+ # @return [Hash]
31
51
  #
32
52
  # @api public
33
- def update(obj)
34
- obj.each_pair do |key, value|
35
- send("#{key}=", value)
36
- end
37
- self
53
+ def to_hash
54
+ { characters: characters, separator: separator, style: style }
38
55
  end
39
56
 
40
- # Convert to hash
57
+ # Check if there should be a separator AFTER this line
58
+ #
59
+ # @param [Integer] line
60
+ #
61
+ # @return [Boolean]
41
62
  #
42
63
  # @api public
43
- def to_hash
44
- hash = {}
45
- members.each do |key|
46
- value = send(key)
47
- hash[key.to_sym] = value if value
64
+ def separator?(line)
65
+ case separator
66
+ when TTY::Table::Border::EACH_ROW
67
+ true
68
+ when Array
69
+ separator.include?(line)
70
+ when Proc
71
+ separator.call(line)
72
+ else
73
+ false
48
74
  end
49
- hash
50
75
  end
51
76
  end # BorderOptions
52
77
  end # Table