tty 0.1.2 → 0.1.3

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 (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,294 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'tty/table/validatable'
4
-
5
- module TTY
6
- class Table
7
- class Renderer
8
- # Renders table without any border styles.
9
- #
10
- # @api private
11
- class Basic
12
- include TTY::Table::Validatable
13
-
14
- # Table to be rendered
15
- #
16
- # @return [TTY::Table]
17
- #
18
- # @api public
19
- attr_reader :table
20
- # private :table
21
-
22
- # Table border to be rendered
23
- #
24
- # @return [TTY::Table::Border]
25
- #
26
- # @api private
27
- attr_accessor :border_class
28
-
29
- # The table enforced column widths
30
- #
31
- # @return [Array]
32
- #
33
- # @api public
34
- attr_writer :column_widths
35
-
36
- # The table column alignments
37
- #
38
- # @return [Array]
39
- #
40
- # @api private
41
- attr_accessor :column_aligns
42
-
43
- # The table operations applied to rows
44
- #
45
- # @api public
46
- attr_reader :operations
47
-
48
- # A callable object used for formatting field content
49
- #
50
- # @api public
51
- attr_accessor :filter
52
-
53
- # The table column span behaviour. When true the column's line breaks
54
- # cause the column to span multiple rows. By default set to false.
55
- #
56
- # @return [Boolean]
57
- #
58
- # @api public
59
- attr_accessor :multiline
60
-
61
- # The table indentation value
62
- #
63
- # @return [Integer]
64
- #
65
- # @api public
66
- attr_accessor :indent
67
-
68
- # The table totabl width
69
- #
70
- # @return [Integer]
71
- #
72
- # @api public
73
- attr_accessor :width
74
-
75
- # The table resizing behaviour. If true the algorithm will
76
- # automatically expand or shrink table to fit the terminal
77
- # width or specified width. By default its false.
78
- #
79
- # @return [Integer]
80
- #
81
- # @api public
82
- attr_accessor :resize
83
-
84
- # The table padding settings
85
- #
86
- # @return [TTY::Table::Padder]
87
- #
88
- # @api public
89
- attr_reader :padding
90
-
91
- # Initialize a Renderer
92
- #
93
- # @param [Hash] options
94
- # @option options [String] :column_aligns
95
- # used to format table individual column alignment
96
- # @option options [String] :column_widths
97
- # used to format table individula column width
98
- # @option options [Integer] :indent
99
- # indent the first column by indent value
100
- # @option options [Integer,Array] :padding
101
- # add padding to table fields
102
- #
103
- # @return [TTY::Table::Renderer::Basic]
104
- #
105
- # @api private
106
- def initialize(table, options = {})
107
- @table = assert_table_type(table)
108
- @multiline = options.fetch(:multiline) { false }
109
- @operations = TTY::Table::Operations.new(table)
110
- @operations.add(:escape, Operation::Escape.new)
111
- @border = TTY::Table::BorderOptions.from(options.delete(:border))
112
- @column_widths = options.fetch(:column_widths, nil)
113
- @column_aligns = Array(options.delete(:column_aligns)).map(&:to_sym)
114
- @filter = options.fetch(:filter) { proc { |val, _| val } }
115
- @width = options.fetch(:width) { TTY::Screen.width }
116
- @border_class = options.fetch(:border_class) { Border::Null }
117
- @indent = options.fetch(:indent) { 0 }
118
- @resize = options.fetch(:resize) { false }
119
- @padding = TTY::Table::Padder.parse(options[:padding])
120
- end
121
-
122
- # Parses supplied column widths, if not present
123
- # calculates natural widths.
124
- #
125
- # @return [Array[Integer]]
126
- #
127
- # @api public
128
- def column_widths
129
- @column_widths = ColumnSet.widths_from(table, @column_widths)
130
- end
131
-
132
- # Store border characters, style and separator for the table rendering
133
- #
134
- # @param [Hash, Table::BorderOptions] options
135
- #
136
- # @yield [Table::BorderOptions]
137
- # block representing border options
138
- #
139
- # @api public
140
- def border(options=(not_set=true), &block)
141
- @border = TTY::Table::BorderOptions.new unless @border
142
- if block_given?
143
- border_dsl = TTY::Table::BorderDSL.new(&block)
144
- @border = border_dsl.options
145
- elsif !not_set
146
- @border = TTY::Table::BorderOptions.from(options)
147
- end
148
- @border
149
- end
150
-
151
- # Initialize and add operations
152
- #
153
- # @api private
154
- def add_operations
155
- operations.add(:alignment, Operation::AlignmentSet.new(column_aligns,
156
- column_widths))
157
- operations.add(:filter, Operation::Filter.new(filter))
158
- operations.add(:truncation, Operation::Truncation.new(column_widths))
159
- operations.add(:wrapping, Operation::Wrapped.new(column_widths, padding))
160
- operations.add(:padding, Operation::Padding.new(padding, multiline))
161
- end
162
-
163
- # Initializes indentation
164
- #
165
- # @return [TTY::Table::Indentation]
166
- #
167
- # @api private
168
- def indentation
169
- @indentation ||= TTY::Table::Indentation.new(self)
170
- end
171
-
172
- # Delegate indentation insertion
173
- #
174
- # @api public
175
- def insert_indent(line)
176
- indentation.insert_indent(line)
177
- end
178
-
179
- # Return column contraints
180
- #
181
- # @api private
182
- def columns_constraints
183
- TTY::Table::Columns.new(self)
184
- end
185
-
186
- # Sets the output padding,
187
- #
188
- # @param [Integer] value
189
- # the amount of padding, not allowed to be zero
190
- #
191
- # @api public
192
- def padding=(value)
193
- @padding = TTY::Table::Padder.parse(value)
194
- end
195
-
196
- # Renders table
197
- #
198
- # @return [String] string representation of table
199
- #
200
- # @api public
201
- def render
202
- return if table.empty?
203
-
204
- operations.run_operations(:escape) unless multiline
205
- columns_constraints.enforce
206
- add_operations
207
- ops = [:alignment]
208
- ops << :padding unless padding.empty?
209
- multiline ? ops << :wrapping : ops << :truncation
210
- ops << :filter
211
- operations.run_operations(*ops)
212
-
213
- render_data.compact.join("\n")
214
- end
215
-
216
- private
217
-
218
- # Render table data
219
- #
220
- # @api private
221
- def render_data
222
- first_row = table.first
223
- data_border = border_class.new(column_widths, border)
224
- header = render_header(first_row, data_border)
225
- rows_with_border = render_rows(data_border)
226
- bottom_line = data_border.bottom_line
227
-
228
- insert_indent(bottom_line) if bottom_line
229
-
230
- [header, rows_with_border, bottom_line].compact
231
- end
232
-
233
- # Format the header if present
234
- #
235
- # @param [TTY::Table::Row, TTY::Table::Header] row
236
- # the first row in the table
237
- #
238
- # @param [TTY::Table::Border] data_boder
239
- # the border for this table
240
- #
241
- # @return [String]
242
- #
243
- # @api private
244
- def render_header(row, data_border)
245
- top_line = data_border.top_line
246
- if row.is_a?(TTY::Table::Header)
247
- header = [top_line, data_border.row_line(row), data_border.separator]
248
- insert_indent(header.compact)
249
- else
250
- top_line
251
- end
252
- end
253
-
254
- # Format the rows
255
- #
256
- # @param [TTY::Table::Border] data_boder
257
- # the border for this table
258
- #
259
- # @return [Arrays[String]]
260
- #
261
- # @api private
262
- def render_rows(data_border)
263
- rows = table.rows
264
- size = rows.size
265
- rows.each_with_index.map do |row, index|
266
- render_row(row, data_border, size != (index += 1))
267
- end
268
- end
269
-
270
- # Format a single row with border
271
- #
272
- # @param [Array] row
273
- # a row to decorate
274
- #
275
- # @param [TTY::Table::Border] data_boder
276
- # the border for this table
277
- #
278
- # @param [Boolean] is_last_row
279
- #
280
- # @api private
281
- def render_row(row, data_border, is_last_row)
282
- separator = data_border.separator
283
- row_line = data_border.row_line(row)
284
-
285
- if (border.separator == TTY::Table::Border::EACH_ROW) && is_last_row
286
- insert_indent([row_line, separator])
287
- else
288
- insert_indent(row_line)
289
- end
290
- end
291
- end # Basic
292
- end # Renderer
293
- end # Table
294
- 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,21 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module TTY
4
- class Table
5
- class Renderer
6
- # Unicode representation of table renderer
7
- #
8
- # @api private
9
- class Unicode < Basic
10
- # Create Unicode renderer
11
- #
12
- # @param [Table] table
13
- #
14
- # @api private
15
- def initialize(table, options = {})
16
- super(table, options.merge(border_class: TTY::Table::Border::Unicode))
17
- end
18
- end # Unicode
19
- end # Renderer
20
- end # Table
21
- end # TTY
@@ -1,101 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module TTY
4
- class Table
5
- # A class responsible for rendering tabular data
6
- #
7
- # Used internally by {Table} to render table content out.
8
- #
9
- # @api private
10
- class Renderer
11
- autoload :ASCII, 'tty/table/renderer/ascii'
12
- autoload :Basic, 'tty/table/renderer/basic'
13
- autoload :Color, 'tty/table/renderer/color'
14
- autoload :Unicode, 'tty/table/renderer/unicode'
15
-
16
- RENDERER_MAPPER = {
17
- ascii: TTY::Table::Renderer::ASCII,
18
- basic: TTY::Table::Renderer::Basic,
19
- color: TTY::Table::Renderer::Color,
20
- unicode: TTY::Table::Renderer::Unicode
21
- }
22
-
23
- # Select renderer class based on string name.
24
- #
25
- # The possible values for renderer are
26
- # [:basic, :ascii, :unicode, :color]
27
- #
28
- # @param [Symbol] renderer
29
- # the renderer used for displaying table
30
- #
31
- # @return [TTY::Table::Renderer]
32
- #
33
- # @api private
34
- def self.select(type)
35
- RENDERER_MAPPER[type || :basic]
36
- end
37
-
38
- # Raises an error if provided border class is of wrong type or has invalid
39
- # implementation
40
- #
41
- # @raise [TypeError]
42
- # raised when providing wrong class for border
43
- #
44
- # @raise [NoImplementationError]
45
- # raised when border class does not implement core methods
46
- #
47
- # @api public
48
- def self.assert_border_class(border_class)
49
- return unless border_class
50
- unless border_class <= TTY::Table::Border
51
- fail TypeError,
52
- "#{border_class} should inherit from TTY::Table::Border"
53
- end
54
- unless border_class.characters
55
- fail NoImplementationError,
56
- "#{border_class} should implement def_border"
57
- end
58
- end
59
-
60
- # Add custom border for the renderer
61
- #
62
- # @param [TTY::Table::Border] border_class
63
- #
64
- # @param [TTY::Table] table
65
- #
66
- # @param [Hash] options
67
- #
68
- # @raise [TypeError]
69
- # raised if the klass does not inherit from Table::Border
70
- #
71
- # @raise [NoImplemntationError]
72
- # raise if the klass does not implement def_border
73
- #
74
- # @api public
75
- def self.render_with(border_class, table, options = {}, &block)
76
- assert_border_class(border_class)
77
- options[:border_class] = border_class if border_class
78
- render(table, options, &block)
79
- end
80
-
81
- # Render a given table and return the string representation.
82
- #
83
- # @param [TTY::Table] table
84
- # the table to be rendered
85
- #
86
- # @param [Hash] options
87
- # the options to render the table with
88
- # @option options [String] :renderer
89
- # used to format table output
90
- #
91
- # @return [String]
92
- #
93
- # @api public
94
- def self.render(table, options = {}, &block)
95
- renderer = select(options[:renderer]).new(table, options)
96
- yield renderer if block_given?
97
- renderer.render
98
- end
99
- end # Renderer
100
- end # Table
101
- end # TTY
data/lib/tty/table/row.rb DELETED
@@ -1,217 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'tty/vector'
4
-
5
- module TTY
6
- class Table
7
- # Convert an Array row into Row
8
- #
9
- # @return [TTY::Table::Row]
10
- #
11
- # @api private
12
- def to_row(row, header = nil)
13
- Row.new(row, header)
14
- end
15
-
16
- # A class that represents a row in a table.
17
- #
18
- # Used internally by {Table} to store row represenation by converting
19
- # {Array} into {Row} instance.
20
- #
21
- # @api private
22
- class Row < Vector
23
- include Equatable
24
- extend Forwardable
25
-
26
- # The row attributes that describe each element
27
- #
28
- # @return [Array]
29
- #
30
- # @api private
31
- attr_reader :attributes
32
-
33
- # The row data
34
- #
35
- # @return [Hash]
36
- #
37
- # @api private
38
- attr_reader :data
39
-
40
- # The row fields
41
- #
42
- # @api public
43
- attr_reader :fields
44
-
45
- def_delegators :to_ary, :join
46
-
47
- # Initialize a Row
48
- #
49
- # @example
50
- # row = new TTY::Table::Row.new [1,2,3]
51
- # row[1] # => 2
52
- #
53
- # row = new TTY::Table::Row.new [1,2,3], %w[a b c]
54
- # row[0] # => 1
55
- # row['a'] # => 1
56
- #
57
- # row = new TTY::Table::Row.new {"a": 1, "b": 2, "c": 3}
58
- # row[0] # => 1
59
- # row['a'] # => 1
60
- #
61
- # @param [#to_ary] data
62
- # the row data
63
- #
64
- # @return [undefined]
65
- #
66
- # @api public
67
- def initialize(data, header = nil)
68
- case data
69
- when Array
70
- @attributes = (header || (0...data.length)).to_a
71
- @fields = coerce_to_fields(data)
72
- when Hash
73
- @data = data.dup
74
- @fields = coerce_to_fields(@data.values)
75
- @attributes = (header || data.keys).to_a
76
- end
77
- @data = Hash[@attributes.zip(fields)]
78
- end
79
-
80
- # Coerces values to field instances
81
- #
82
- # @param [Array[Object]] values
83
- #
84
- # @return [Array[TTY::Table::Field]]
85
- #
86
- # @api public
87
- def coerce_to_fields(values)
88
- values.reduce([]) { |acc, el| acc << to_field(el) }
89
- end
90
-
91
- # Instantiates a new field
92
- #
93
- # @api public
94
- def to_field(options = nil)
95
- Field.new(options)
96
- end
97
-
98
- # Lookup a value in the row given an attribute allowing for Array or
99
- # Hash like indexing
100
- #
101
- # @exmaple
102
- # row[1]
103
- # row[:id]
104
- # row.call(:id)
105
- #
106
- # @api public
107
- def [](attribute)
108
- case attribute
109
- when Integer
110
- data[attributes[attribute]].value
111
- else
112
- data.fetch(attribute) do |name|
113
- fail UnknownAttributeError, "the attribute #{name} is unkown"
114
- end.value
115
- end
116
- end
117
-
118
- # Lookup attribute without evaluation
119
- #
120
- # @api public
121
- def call(attribute)
122
- data[attributes[attribute]]
123
- end
124
-
125
- # Set value at index
126
- #
127
- # @example
128
- # row[attribute] = value
129
- #
130
- # @api public
131
- def []=(attribute, value)
132
- case attribute
133
- when Integer
134
- data[attributes[attribute]] = to_field(value)
135
- else
136
- data[attribute] = to_field(value)
137
- attributes << attribute unless attributes.include?(attribute)
138
- end
139
- end
140
-
141
- # Number of data items in a row
142
- #
143
- # @return [Integer]
144
- #
145
- # @api public
146
- def size
147
- data.size
148
- end
149
- alias :length :size
150
-
151
- # Find maximum row height
152
- #
153
- # @return [Integer]
154
- #
155
- # @api public
156
- def height
157
- fields.map(&:height).max
158
- end
159
-
160
- # Convert the Row into Array
161
- #
162
- # @example
163
- # array = row.to_ary
164
- #
165
- # @return [Array]
166
- #
167
- # @api public
168
- def to_ary
169
- to_hash.values_at(*attributes)
170
- end
171
-
172
- # Convert the Row into hash
173
- #
174
- # @return [Hash]
175
- #
176
- # @api public
177
- def to_hash
178
- hash = data.dup
179
- hash.update(hash) { |_, val| val.value if val }
180
- end
181
-
182
- # Check if this row is equivalent to another row
183
- #
184
- # @return [Boolean]
185
- #
186
- # @api public
187
- def ==(other)
188
- to_a == other.to_a
189
- end
190
- alias :eql? :==
191
-
192
- # Provide a unique hash value. If a row contains the same data as another
193
- # row, they will hash to the same value.
194
- #
195
- # @api public
196
- def hash
197
- to_a.hash
198
- end
199
-
200
- # Map field values
201
- #
202
- # @api public
203
- def map!(&block)
204
- data.values_at(*attributes).each do |field|
205
- field.value = block.call(field)
206
- end
207
- end
208
-
209
- # String representation of a row with its fields
210
- #
211
- # @api public
212
- def inspect
213
- "#<#{self.class.name} fields=#{to_a}>"
214
- end
215
- end # Row
216
- end # Table
217
- end # TTY
@@ -1,39 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module TTY
4
- class Table
5
- # A class for transforming table values
6
- #
7
- # Used internally by {Table}
8
- #
9
- # @api private
10
- class Transformation
11
- # Extract the header and row tuples from the value
12
- #
13
- # @param [Array] args
14
- #
15
- # @return [Object]
16
- #
17
- # @api public
18
- def self.extract_tuples(args)
19
- rows = args.pop
20
- header = args.size.zero? ? nil : args.first
21
- if rows.first.is_a?(Hash)
22
- header, rows = group_header_and_rows(rows)
23
- end
24
- { header: header, rows: rows }
25
- end
26
-
27
- # Group hash keys into header and values into rows
28
- #
29
- # @params [Hash] value
30
- #
31
- # @api public
32
- def self.group_header_and_rows(value)
33
- header = value.map(&:keys).flatten.uniq
34
- rows = value.reduce([]) { |arr, el| arr + el.values }
35
- [header, rows]
36
- end
37
- end # Transformation
38
- end # Table
39
- end # TTY