tty 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +4 -3
  3. data/CHANGELOG.md +5 -0
  4. data/README.md +42 -306
  5. data/lib/tty/{text → shell}/distance.rb +5 -27
  6. data/lib/tty/shell/suggestion.rb +42 -22
  7. data/lib/tty/version.rb +1 -1
  8. data/lib/tty.rb +2 -42
  9. data/spec/tty/{text → shell}/distance/distance_spec.rb +4 -4
  10. data/spec/tty/shell/distance/initialize_spec.rb +14 -0
  11. data/tty.gemspec +4 -3
  12. metadata +37 -269
  13. data/benchmarks/table.rb +0 -40
  14. data/lib/tty/table/border/ascii.rb +0 -32
  15. data/lib/tty/table/border/null.rb +0 -37
  16. data/lib/tty/table/border/row_line.rb +0 -18
  17. data/lib/tty/table/border/unicode.rb +0 -32
  18. data/lib/tty/table/border.rb +0 -222
  19. data/lib/tty/table/border_dsl.rb +0 -251
  20. data/lib/tty/table/border_options.rb +0 -53
  21. data/lib/tty/table/column_set.rb +0 -121
  22. data/lib/tty/table/columns.rb +0 -170
  23. data/lib/tty/table/error.rb +0 -18
  24. data/lib/tty/table/field.rb +0 -139
  25. data/lib/tty/table/header.rb +0 -133
  26. data/lib/tty/table/indentation.rb +0 -52
  27. data/lib/tty/table/operation/alignment.rb +0 -107
  28. data/lib/tty/table/operation/alignment_set.rb +0 -77
  29. data/lib/tty/table/operation/escape.rb +0 -30
  30. data/lib/tty/table/operation/filter.rb +0 -34
  31. data/lib/tty/table/operation/padding.rb +0 -95
  32. data/lib/tty/table/operation/truncation.rb +0 -55
  33. data/lib/tty/table/operation/wrapped.rb +0 -58
  34. data/lib/tty/table/operations.rb +0 -69
  35. data/lib/tty/table/orientation/horizontal.rb +0 -48
  36. data/lib/tty/table/orientation/vertical.rb +0 -38
  37. data/lib/tty/table/orientation.rb +0 -57
  38. data/lib/tty/table/padder.rb +0 -180
  39. data/lib/tty/table/renderer/ascii.rb +0 -16
  40. data/lib/tty/table/renderer/basic.rb +0 -294
  41. data/lib/tty/table/renderer/color.rb +0 -12
  42. data/lib/tty/table/renderer/unicode.rb +0 -21
  43. data/lib/tty/table/renderer.rb +0 -101
  44. data/lib/tty/table/row.rb +0 -217
  45. data/lib/tty/table/transformation.rb +0 -39
  46. data/lib/tty/table/validatable.rb +0 -64
  47. data/lib/tty/table.rb +0 -476
  48. data/lib/tty/text/truncation.rb +0 -99
  49. data/lib/tty/text/wrapping.rb +0 -118
  50. data/lib/tty/text.rb +0 -98
  51. data/spec/tty/table/access_spec.rb +0 -86
  52. data/spec/tty/table/add_row_spec.rb +0 -28
  53. data/spec/tty/table/border/ascii/rendering_spec.rb +0 -90
  54. data/spec/tty/table/border/new_spec.rb +0 -27
  55. data/spec/tty/table/border/null/rendering_spec.rb +0 -130
  56. data/spec/tty/table/border/options/from_spec.rb +0 -38
  57. data/spec/tty/table/border/options/new_spec.rb +0 -14
  58. data/spec/tty/table/border/unicode/rendering_spec.rb +0 -63
  59. data/spec/tty/table/border_options/new_spec.rb +0 -20
  60. data/spec/tty/table/border_options/update_spec.rb +0 -18
  61. data/spec/tty/table/column_set/extract_widths_spec.rb +0 -15
  62. data/spec/tty/table/column_set/total_width_spec.rb +0 -15
  63. data/spec/tty/table/column_set/widths_from_spec.rb +0 -51
  64. data/spec/tty/table/columns/enforce_spec.rb +0 -68
  65. data/spec/tty/table/columns/widths_spec.rb +0 -35
  66. data/spec/tty/table/data_spec.rb +0 -14
  67. data/spec/tty/table/each_spec.rb +0 -41
  68. data/spec/tty/table/each_with_index_spec.rb +0 -57
  69. data/spec/tty/table/empty_spec.rb +0 -23
  70. data/spec/tty/table/eql_spec.rb +0 -34
  71. data/spec/tty/table/field/equality_spec.rb +0 -51
  72. data/spec/tty/table/field/length_spec.rb +0 -21
  73. data/spec/tty/table/field/lines_spec.rb +0 -21
  74. data/spec/tty/table/field/new_spec.rb +0 -29
  75. data/spec/tty/table/field/width_spec.rb +0 -23
  76. data/spec/tty/table/filter_spec.rb +0 -23
  77. data/spec/tty/table/header/call_spec.rb +0 -30
  78. data/spec/tty/table/header/height_spec.rb +0 -27
  79. data/spec/tty/table/header/new_spec.rb +0 -25
  80. data/spec/tty/table/header/set_spec.rb +0 -20
  81. data/spec/tty/table/header/to_ary_spec.rb +0 -14
  82. data/spec/tty/table/header_spec.rb +0 -13
  83. data/spec/tty/table/indentation/insert_indent_spec.rb +0 -27
  84. data/spec/tty/table/initialize_spec.rb +0 -88
  85. data/spec/tty/table/operation/alignment/format_spec.rb +0 -47
  86. data/spec/tty/table/operation/alignment/new_spec.rb +0 -31
  87. data/spec/tty/table/operation/alignment_set/call_spec.rb +0 -39
  88. data/spec/tty/table/operation/alignment_set/each_spec.rb +0 -17
  89. data/spec/tty/table/operation/alignment_set/new_spec.rb +0 -27
  90. data/spec/tty/table/operation/alignment_set/to_ary_spec.rb +0 -14
  91. data/spec/tty/table/operation/escape/call_spec.rb +0 -16
  92. data/spec/tty/table/operation/filter/call_spec.rb +0 -17
  93. data/spec/tty/table/operation/truncation/call_spec.rb +0 -32
  94. data/spec/tty/table/operation/truncation/truncate_spec.rb +0 -22
  95. data/spec/tty/table/operation/wrapped/call_spec.rb +0 -33
  96. data/spec/tty/table/operation/wrapped/wrap_spec.rb +0 -23
  97. data/spec/tty/table/operations/new_spec.rb +0 -30
  98. data/spec/tty/table/options_spec.rb +0 -25
  99. data/spec/tty/table/orientation_spec.rb +0 -145
  100. data/spec/tty/table/padder/parse_spec.rb +0 -45
  101. data/spec/tty/table/padder/to_s_spec.rb +0 -14
  102. data/spec/tty/table/padding_spec.rb +0 -120
  103. data/spec/tty/table/properties_spec.rb +0 -25
  104. data/spec/tty/table/render_spec.rb +0 -63
  105. data/spec/tty/table/render_with_spec.rb +0 -106
  106. data/spec/tty/table/renderer/ascii/indentation_spec.rb +0 -41
  107. data/spec/tty/table/renderer/ascii/padding_spec.rb +0 -61
  108. data/spec/tty/table/renderer/ascii/render_spec.rb +0 -68
  109. data/spec/tty/table/renderer/ascii/resizing_spec.rb +0 -114
  110. data/spec/tty/table/renderer/ascii/separator_spec.rb +0 -28
  111. data/spec/tty/table/renderer/basic/alignment_spec.rb +0 -88
  112. data/spec/tty/table/renderer/basic/coloring_spec.rb +0 -46
  113. data/spec/tty/table/renderer/basic/extract_column_widths_spec.rb +0 -28
  114. data/spec/tty/table/renderer/basic/filter_spec.rb +0 -53
  115. data/spec/tty/table/renderer/basic/indentation_spec.rb +0 -48
  116. data/spec/tty/table/renderer/basic/multiline_content_spec.rb +0 -135
  117. data/spec/tty/table/renderer/basic/new_spec.rb +0 -22
  118. data/spec/tty/table/renderer/basic/options_spec.rb +0 -48
  119. data/spec/tty/table/renderer/basic/padding_spec.rb +0 -52
  120. data/spec/tty/table/renderer/basic/render_spec.rb +0 -57
  121. data/spec/tty/table/renderer/basic/resizing_spec.rb +0 -96
  122. data/spec/tty/table/renderer/basic/separator_spec.rb +0 -43
  123. data/spec/tty/table/renderer/basic/truncation_spec.rb +0 -35
  124. data/spec/tty/table/renderer/basic/wrapping_spec.rb +0 -40
  125. data/spec/tty/table/renderer/basic_spec.rb +0 -0
  126. data/spec/tty/table/renderer/border_spec.rb +0 -104
  127. data/spec/tty/table/renderer/render_spec.rb +0 -36
  128. data/spec/tty/table/renderer/select_spec.rb +0 -22
  129. data/spec/tty/table/renderer/style_spec.rb +0 -72
  130. data/spec/tty/table/renderer/unicode/indentation_spec.rb +0 -41
  131. data/spec/tty/table/renderer/unicode/padding_spec.rb +0 -61
  132. data/spec/tty/table/renderer/unicode/render_spec.rb +0 -68
  133. data/spec/tty/table/renderer/unicode/separator_spec.rb +0 -26
  134. data/spec/tty/table/renderer_spec.rb +0 -19
  135. data/spec/tty/table/rotate_spec.rb +0 -86
  136. data/spec/tty/table/row/access_spec.rb +0 -25
  137. data/spec/tty/table/row/call_spec.rb +0 -41
  138. data/spec/tty/table/row/data_spec.rb +0 -26
  139. data/spec/tty/table/row/each_spec.rb +0 -31
  140. data/spec/tty/table/row/equality_spec.rb +0 -73
  141. data/spec/tty/table/row/height_spec.rb +0 -27
  142. data/spec/tty/table/row/new_spec.rb +0 -41
  143. data/spec/tty/table/row/to_ary_spec.rb +0 -14
  144. data/spec/tty/table/to_s_spec.rb +0 -63
  145. data/spec/tty/table/transformation/extract_tuples_spec.rb +0 -35
  146. data/spec/tty/table/validatable/validate_options_spec.rb +0 -33
  147. data/spec/tty/table/validatable_spec.rb +0 -32
  148. data/spec/tty/text/distance/initialize_spec.rb +0 -14
  149. data/spec/tty/text/distance_spec.rb +0 -11
  150. data/spec/tty/text/truncate_spec.rb +0 -12
  151. data/spec/tty/text/truncation/initialize_spec.rb +0 -29
  152. data/spec/tty/text/truncation/truncate_spec.rb +0 -94
  153. data/spec/tty/text/wrap_spec.rb +0 -13
  154. data/spec/tty/text/wrapping/initialize_spec.rb +0 -25
  155. data/spec/tty/text/wrapping/wrap_spec.rb +0 -97
@@ -1,18 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module TTY
4
- class Table
5
- class Border
6
- # A class for a table row line chars manipulation
7
- class RowLine < Struct.new(:left, :center, :right)
8
- # Colorize characters with a given style
9
- #
10
- # @api public
11
- def colorize(border, style)
12
- colorized_chars = border.set_color(style, right, center, left)
13
- self.right, self.center, self.left = colorized_chars
14
- end
15
- end # RowLine
16
- end # Border
17
- end # Table
18
- end # TTY
@@ -1,32 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module TTY
4
- class Table
5
- class Border
6
- # A class that represents a unicode border.
7
- #
8
- # @api private
9
- class Unicode < Border
10
-
11
- def_border do
12
- top '─'
13
- top_mid '┬'
14
- top_left '┌'
15
- top_right '┐'
16
- bottom '─'
17
- bottom_mid '┴'
18
- bottom_left '└'
19
- bottom_right '┘'
20
- mid '─'
21
- mid_mid '┼'
22
- mid_left '├'
23
- mid_right '┤'
24
- left '│'
25
- center '│'
26
- right '│'
27
- end
28
-
29
- end # Unicode
30
- end # Border
31
- end # Table
32
- end # TTY
@@ -1,222 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module TTY
4
- class Table
5
- # Abstract base class that is responsible for building the table border.
6
- class Border
7
- include Unicode
8
- include Equatable
9
-
10
- EMPTY_CHAR = ''.freeze
11
-
12
- SPACE_CHAR = ' '.freeze
13
-
14
- # Represent a separtor on each row
15
- EACH_ROW = :each_row
16
-
17
- # The row field widths
18
- #
19
- # @api private
20
- attr_reader :widths
21
- private :widths
22
-
23
- # The table custom border characters
24
- attr_reader :border
25
-
26
- # The table
27
-
28
- class << self
29
- # Store characters for border
30
- #
31
- # @api private
32
- attr_accessor :characters
33
- end
34
-
35
- # Instantiate a new object
36
- #
37
- # @param [Array] column_widths
38
- # the table column widths
39
- #
40
- # @param [BorderOptions] options
41
- #
42
- # @return [Object]
43
- #
44
- # @api private
45
- def initialize(column_widths, options = nil)
46
- if self.class == Border
47
- fail NotImplementedError, "#{self} is an abstract class"
48
- else
49
- @widths = column_widths
50
- @border = TTY::Table::BorderOptions.from options
51
- @color = Pastel.new
52
- end
53
- end
54
-
55
- # Define border characters
56
- #
57
- # @param [Hash] characters
58
- # the border characters
59
- #
60
- # @return [Hash]
61
- #
62
- # @api public
63
- def self.def_border(characters=(not_set=true), &block)
64
- return self.characters = characters unless not_set
65
-
66
- dsl = TTY::Table::BorderDSL.new(&block)
67
- self.characters = dsl.characters
68
- end
69
-
70
- # Retrive individula character by type
71
- #
72
- # @param [String] type
73
- # the character type
74
- #
75
- # @return [String]
76
- #
77
- # @api private
78
- def [](type)
79
- characters = self.class.characters
80
- chars = border.nil? ? characters : characters.merge(border.characters)
81
- chars[type] || EMPTY_CHAR
82
- end
83
-
84
- # Check if border color is set
85
- #
86
- # @return [Boolean]
87
- #
88
- # @api public
89
- def color?
90
- border && border.style
91
- end
92
-
93
- # Set color on characters
94
- #
95
- # @param [Symbol] color
96
- #
97
- # @param [Array[String]] array of strings
98
- #
99
- # @return [Array[String]]
100
- #
101
- # @api public
102
- def set_color(color, *strings)
103
- strings.map { |string| @color.decorate(string, color) }
104
- end
105
-
106
- # A line spanning all columns marking top of a table.
107
- #
108
- # @return [String]
109
- #
110
- # @api private
111
- def top_line
112
- (result = render(:top)).empty? ? nil : result
113
- end
114
-
115
- # A line spannig all columns marking bottom of a table.
116
- #
117
- # @return [String]
118
- #
119
- # @api private
120
- def bottom_line
121
- (result = render(:bottom)).empty? ? nil : result
122
- end
123
-
124
- # A line spanning all columns delemeting rows in a table.
125
- #
126
- # @return [String]
127
- #
128
- # @api private
129
- def separator
130
- (result = render(:mid)).empty? ? nil : result
131
- end
132
-
133
- # A line spanning all columns delemeting fields in a row.
134
- #
135
- # @param [TTY::Table::Row] row
136
- # the table row
137
- #
138
- # @return [String]
139
- #
140
- # @api public
141
- def row_line(row)
142
- line = RowLine.new(self['left'], self['center'], self['right'])
143
- line.colorize(self, border.style) if color?
144
-
145
- result = row_heights(row, line)
146
- result.empty? ? EMPTY_CHAR : result
147
- end
148
-
149
- protected
150
-
151
- # Separate multiline string into individual rows with border.
152
- #
153
- # @param [TTY::Table::Row] row
154
- # the table row
155
- #
156
- # @param [TTY::Table::Border::RowLine] line
157
- #
158
- # @api private
159
- def row_heights(row, line)
160
- if row.size > 0
161
- row.height.times.map do |line_index|
162
- row_height_line(row, line_index, line)
163
- end.join("\n")
164
- else
165
- line.left + line.right
166
- end
167
- end
168
-
169
- # Generate border for a given multiline row
170
- #
171
- # @param [TTY::Table::Row] row
172
- # the table row
173
- #
174
- # @param [Integer] line
175
- # the index for current line inside multiline
176
- #
177
- # @param [TTY::Table::Border::RowLine] line
178
- #
179
- # @return [String]
180
- #
181
- # @api private
182
- def row_height_line(row, line_index, line)
183
- line.left + row.fields.each_with_index.map do |field, index|
184
- (field.lines[line_index] || EMPTY_CHAR).ljust(widths[index])
185
- end.join(line.center) + line.right
186
- end
187
-
188
- # Generate particular border type
189
- #
190
- # @param [String] type
191
- # border type one of [:top, :bottom, :mid]
192
- #
193
- # @api private
194
- def render(type)
195
- type = type.to_s
196
- border_char = self[type]
197
- line = render_line(border_char,
198
- self["#{type}_left"] || border_char,
199
- self["#{type}_right"] || border_char,
200
- self["#{type}_mid"])
201
-
202
- if color? && !line.empty?
203
- line = set_color(border.style, line)
204
- end
205
- line
206
- end
207
-
208
- # Generate a border string
209
- #
210
- # @param [String] line
211
- #
212
- # @return [String]
213
- #
214
- # @api private
215
- def render_line(line, left, right, intersection)
216
- as_unicode do
217
- left + widths.map { |width| line * width }.join(intersection) + right
218
- end
219
- end
220
- end # Border
221
- end # Table
222
- end # TTY
@@ -1,251 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module TTY
4
- class Table
5
- # A class responsible for bulding and modifying border
6
- #
7
- # Used internally by {Table::Border} to allow for building custom border
8
- # through DSL
9
- # @api private
10
- class BorderDSL
11
- extend TTY::Delegatable
12
-
13
- # Border options
14
- #
15
- # @return [Table::BorderOptions]
16
- attr_reader :options
17
-
18
- delegatable_method :options, :characters, :style
19
-
20
- # Initialize a BorderDSL
21
- #
22
- # @param [Hash] characters
23
- # the border characters
24
- #
25
- # @return [undefined]
26
- #
27
- # @api private
28
- def initialize(characters = nil, &block)
29
- @options = TTY::Table::BorderOptions.new
30
- @options.characters = characters if characters
31
- yield_or_eval(&block) if block_given?
32
- end
33
-
34
- # Apply style color to the border
35
- #
36
- # @param [Symbol] style
37
- # the style color for the border
38
- #
39
- # @return [undefined]
40
- #
41
- # @api public
42
- def style(value)
43
- options.style = value
44
- end
45
-
46
- # Apply table tuple separator
47
- #
48
- # @param [Symbol] separator
49
- # the table tuple separator
50
- #
51
- # @return [undefined]
52
- #
53
- # @api public
54
- def separator(value)
55
- value ? options.separator = value : options.separator
56
- end
57
-
58
- # Set top border character
59
- #
60
- # @param [String] value
61
- # the character value
62
- #
63
- # @return [undefined]
64
- #
65
- # @api public
66
- def top(value)
67
- options.characters['top'] = value
68
- end
69
-
70
- # Set top middle border character
71
- #
72
- # @param [String] value
73
- # the character value
74
- #
75
- # @return [undefined]
76
- #
77
- # @api public
78
- def top_mid(value)
79
- options.characters['top_mid'] = value
80
- end
81
-
82
- # Set top left corner border character
83
- #
84
- # @param [String] value
85
- # the character value
86
- #
87
- # @return [undefined]
88
- #
89
- # @api public
90
- def top_left(value)
91
- options.characters['top_left'] = value
92
- end
93
-
94
- # Set top right corner border character
95
- #
96
- # @param [String] value
97
- # the character value
98
- #
99
- # @return [undefined]
100
- #
101
- # @api public
102
- def top_right(value)
103
- options.characters['top_right'] = value
104
- end
105
-
106
- # Set bottom border character
107
- #
108
- # @param [String] value
109
- # the character value
110
- #
111
- # @return [undefined]
112
- #
113
- # @api public
114
- def bottom(value)
115
- options.characters['bottom'] = value
116
- end
117
-
118
- # Set bottom middle border character
119
- #
120
- # @param [String] value
121
- # the character value
122
- #
123
- # @return [undefined]
124
- #
125
- # @api public
126
- def bottom_mid(value)
127
- options.characters['bottom_mid'] = value
128
- end
129
-
130
- # Set bottom left corner border character
131
- #
132
- # @param [String] value
133
- # the character value
134
- #
135
- # @return [undefined]
136
- #
137
- # @api public
138
- def bottom_left(value)
139
- options.characters['bottom_left'] = value
140
- end
141
-
142
- # Set bottom right corner border character
143
- #
144
- # @param [String] value
145
- # the character value
146
- #
147
- # @return [undefined]
148
- #
149
- # @api public
150
- def bottom_right(value)
151
- options.characters['bottom_right'] = value
152
- end
153
-
154
- # Set middle border character
155
- #
156
- # @param [String] value
157
- # the character value
158
- #
159
- # @return [undefined]
160
- #
161
- # @api public
162
- def mid(value)
163
- options.characters['mid'] = value
164
- end
165
-
166
- # Set middle border character
167
- #
168
- # @param [String] value
169
- # the character value
170
- #
171
- # @return [undefined]
172
- #
173
- # @api public
174
- def mid_mid(value)
175
- options.characters['mid_mid'] = value
176
- end
177
-
178
- # Set middle left corner border character
179
- #
180
- # @param [String] value
181
- # the character value
182
- #
183
- # @return [undefined]
184
- #
185
- # @api public
186
- def mid_left(value)
187
- options.characters['mid_left'] = value
188
- end
189
-
190
- # Set middle right corner border character
191
- #
192
- # @param [String] value
193
- # the character value
194
- #
195
- # @return [undefined]
196
- #
197
- # @api public
198
- def mid_right(value)
199
- options.characters['mid_right'] = value
200
- end
201
-
202
- # Set left border character
203
- #
204
- # @param [String] value
205
- # the character value
206
- #
207
- # @return [undefined]
208
- #
209
- # @api public
210
- def left(value)
211
- options.characters['left'] = value
212
- end
213
-
214
- # Set center border character
215
- #
216
- # @param [String] value
217
- # the character value
218
- #
219
- # @return [undefined]
220
- #
221
- # @api public
222
- def center(value)
223
- options.characters['center'] = value
224
- end
225
-
226
- # Set right border character
227
- #
228
- # @param [String] value
229
- # the character value
230
- #
231
- # @return [undefined]
232
- #
233
- # @api public
234
- def right(value)
235
- options.characters['right'] = value
236
- end
237
-
238
- private
239
-
240
- # Evaluate block
241
- #
242
- # @return [Table]
243
- #
244
- # @api private
245
- def yield_or_eval(&block)
246
- return unless block
247
- block.arity > 0 ? yield(self) : instance_eval(&block)
248
- end
249
- end # BorderDSL
250
- end # Table
251
- end # TTY
@@ -1,53 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module TTY
4
- class Table
5
- # A class that represents table border options
6
- #
7
- # Used internally by {Table::Border} to manage options such as style
8
- #
9
- # @api private
10
- class BorderOptions < Struct.new(:characters, :separator, :style)
11
- # Initialize a BorderOptions
12
- #
13
- # @api public
14
- def initialize(*args)
15
- super(*args)
16
- self.characters = {} unless characters
17
- end
18
-
19
- # Create options instance from hash
20
- #
21
- # @api public
22
- def self.from(value)
23
- value ? new.update(value) : new
24
- end
25
-
26
- # Set all accessors with hash attributes
27
- #
28
- # @param [Hash, BorderOptions] obj
29
- #
30
- # @return [BorderOptions]
31
- #
32
- # @api public
33
- def update(obj)
34
- obj.each_pair do |key, value|
35
- send("#{key}=", value)
36
- end
37
- self
38
- end
39
-
40
- # Convert to hash
41
- #
42
- # @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
48
- end
49
- hash
50
- end
51
- end # BorderOptions
52
- end # Table
53
- end # TTY
@@ -1,121 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module TTY
4
- class Table
5
- # A class that represents table column properties.
6
- #
7
- # Used by {Table} to manage column sizing.
8
- #
9
- # @api private
10
- class ColumnSet
11
- include Equatable
12
-
13
- attr_reader :table
14
-
15
- # Initialize a ColumnSet
16
- #
17
- # @api public
18
- def initialize(table)
19
- @table = table
20
- end
21
-
22
- # Calculate total table width
23
- #
24
- # @return [Integer]
25
- #
26
- # @api public
27
- def total_width
28
- extract_widths.reduce(:+)
29
- end
30
-
31
- # Calcualte maximum column widths
32
- #
33
- # @return [Array] column widths
34
- #
35
- # @api private
36
- def extract_widths
37
- data = table.data
38
- colcount = data.max { |row_a, row_b| row_a.size <=> row_b.size }.size
39
-
40
- self.class.find_maximas(colcount, data)
41
- end
42
-
43
- # Assert data integrity for column widths
44
- #
45
- # @param [Array] column_widths
46
- #
47
- # @param [Integer] table_widths
48
- #
49
- # @raise [TTY::InvalidArgument]
50
- #
51
- # @api public
52
- def self.assert_widths(column_widths, table_widths)
53
- if column_widths.empty?
54
- fail InvalidArgument, 'Value for :column_widths must be ' \
55
- 'a non-empty array'
56
- end
57
- if column_widths.size != table_widths
58
- fail InvalidArgument, 'Value for :column_widths must match ' \
59
- 'table column count'
60
- end
61
- end
62
-
63
- # Converts column widths to array format or infers default widths
64
- #
65
- # @param [TTY::Table] table
66
- #
67
- # @param [Array, Numeric, NilClass] column_widths
68
- #
69
- # @return [Array[Integer]]
70
- #
71
- # @api public
72
- def self.widths_from(table, column_widths = nil)
73
- case column_widths
74
- when Array
75
- assert_widths(column_widths, table.column_size)
76
- Array(column_widths).map(&:to_i)
77
- when Numeric
78
- Array.new(table.column_size, column_widths)
79
- when NilClass
80
- new(table).extract_widths
81
- else
82
- fail TypeError, 'Invalid type for column widths'
83
- end
84
- end
85
-
86
- private
87
-
88
- # Find maximum widths for each row and header if present.
89
- #
90
- # @param [Integer] colcount
91
- # number of columns
92
- # @param [Array[Array]] data
93
- # the table's header and rows
94
- #
95
- # @api private
96
- def self.find_maximas(colcount, data)
97
- maximas = []
98
- start = 0
99
-
100
- start.upto(colcount - 1) do |col_index|
101
- maximas << find_maximum(data, col_index)
102
- end
103
- maximas
104
- end
105
-
106
- # Find a maximum column width. The calculation takes into account
107
- # wether the content is escaped or not.
108
- #
109
- # @param [Array] data
110
- # the table data
111
- #
112
- # @param [Integer] index
113
- # the column index
114
- #
115
- # @api private
116
- def self.find_maximum(data, index)
117
- data.map { |row| (value = row.call(index)) ? value.length : 0 }.max
118
- end
119
- end # ColumnSet
120
- end # Table
121
- end # TTY