tty 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -3
- data/CHANGELOG.md +5 -0
- data/README.md +42 -306
- data/lib/tty/{text → shell}/distance.rb +5 -27
- data/lib/tty/shell/suggestion.rb +42 -22
- data/lib/tty/version.rb +1 -1
- data/lib/tty.rb +2 -42
- data/spec/tty/{text → shell}/distance/distance_spec.rb +4 -4
- data/spec/tty/shell/distance/initialize_spec.rb +14 -0
- data/tty.gemspec +4 -3
- metadata +37 -269
- data/benchmarks/table.rb +0 -40
- data/lib/tty/table/border/ascii.rb +0 -32
- data/lib/tty/table/border/null.rb +0 -37
- data/lib/tty/table/border/row_line.rb +0 -18
- data/lib/tty/table/border/unicode.rb +0 -32
- data/lib/tty/table/border.rb +0 -222
- data/lib/tty/table/border_dsl.rb +0 -251
- data/lib/tty/table/border_options.rb +0 -53
- data/lib/tty/table/column_set.rb +0 -121
- data/lib/tty/table/columns.rb +0 -170
- data/lib/tty/table/error.rb +0 -18
- data/lib/tty/table/field.rb +0 -139
- data/lib/tty/table/header.rb +0 -133
- data/lib/tty/table/indentation.rb +0 -52
- data/lib/tty/table/operation/alignment.rb +0 -107
- data/lib/tty/table/operation/alignment_set.rb +0 -77
- data/lib/tty/table/operation/escape.rb +0 -30
- data/lib/tty/table/operation/filter.rb +0 -34
- data/lib/tty/table/operation/padding.rb +0 -95
- data/lib/tty/table/operation/truncation.rb +0 -55
- data/lib/tty/table/operation/wrapped.rb +0 -58
- data/lib/tty/table/operations.rb +0 -69
- data/lib/tty/table/orientation/horizontal.rb +0 -48
- data/lib/tty/table/orientation/vertical.rb +0 -38
- data/lib/tty/table/orientation.rb +0 -57
- data/lib/tty/table/padder.rb +0 -180
- data/lib/tty/table/renderer/ascii.rb +0 -16
- data/lib/tty/table/renderer/basic.rb +0 -294
- data/lib/tty/table/renderer/color.rb +0 -12
- data/lib/tty/table/renderer/unicode.rb +0 -21
- data/lib/tty/table/renderer.rb +0 -101
- data/lib/tty/table/row.rb +0 -217
- data/lib/tty/table/transformation.rb +0 -39
- data/lib/tty/table/validatable.rb +0 -64
- data/lib/tty/table.rb +0 -476
- data/lib/tty/text/truncation.rb +0 -99
- data/lib/tty/text/wrapping.rb +0 -118
- data/lib/tty/text.rb +0 -98
- data/spec/tty/table/access_spec.rb +0 -86
- data/spec/tty/table/add_row_spec.rb +0 -28
- data/spec/tty/table/border/ascii/rendering_spec.rb +0 -90
- data/spec/tty/table/border/new_spec.rb +0 -27
- data/spec/tty/table/border/null/rendering_spec.rb +0 -130
- data/spec/tty/table/border/options/from_spec.rb +0 -38
- data/spec/tty/table/border/options/new_spec.rb +0 -14
- data/spec/tty/table/border/unicode/rendering_spec.rb +0 -63
- data/spec/tty/table/border_options/new_spec.rb +0 -20
- data/spec/tty/table/border_options/update_spec.rb +0 -18
- data/spec/tty/table/column_set/extract_widths_spec.rb +0 -15
- data/spec/tty/table/column_set/total_width_spec.rb +0 -15
- data/spec/tty/table/column_set/widths_from_spec.rb +0 -51
- data/spec/tty/table/columns/enforce_spec.rb +0 -68
- data/spec/tty/table/columns/widths_spec.rb +0 -35
- data/spec/tty/table/data_spec.rb +0 -14
- data/spec/tty/table/each_spec.rb +0 -41
- data/spec/tty/table/each_with_index_spec.rb +0 -57
- data/spec/tty/table/empty_spec.rb +0 -23
- data/spec/tty/table/eql_spec.rb +0 -34
- data/spec/tty/table/field/equality_spec.rb +0 -51
- data/spec/tty/table/field/length_spec.rb +0 -21
- data/spec/tty/table/field/lines_spec.rb +0 -21
- data/spec/tty/table/field/new_spec.rb +0 -29
- data/spec/tty/table/field/width_spec.rb +0 -23
- data/spec/tty/table/filter_spec.rb +0 -23
- data/spec/tty/table/header/call_spec.rb +0 -30
- data/spec/tty/table/header/height_spec.rb +0 -27
- data/spec/tty/table/header/new_spec.rb +0 -25
- data/spec/tty/table/header/set_spec.rb +0 -20
- data/spec/tty/table/header/to_ary_spec.rb +0 -14
- data/spec/tty/table/header_spec.rb +0 -13
- data/spec/tty/table/indentation/insert_indent_spec.rb +0 -27
- data/spec/tty/table/initialize_spec.rb +0 -88
- data/spec/tty/table/operation/alignment/format_spec.rb +0 -47
- data/spec/tty/table/operation/alignment/new_spec.rb +0 -31
- data/spec/tty/table/operation/alignment_set/call_spec.rb +0 -39
- data/spec/tty/table/operation/alignment_set/each_spec.rb +0 -17
- data/spec/tty/table/operation/alignment_set/new_spec.rb +0 -27
- data/spec/tty/table/operation/alignment_set/to_ary_spec.rb +0 -14
- data/spec/tty/table/operation/escape/call_spec.rb +0 -16
- data/spec/tty/table/operation/filter/call_spec.rb +0 -17
- data/spec/tty/table/operation/truncation/call_spec.rb +0 -32
- data/spec/tty/table/operation/truncation/truncate_spec.rb +0 -22
- data/spec/tty/table/operation/wrapped/call_spec.rb +0 -33
- data/spec/tty/table/operation/wrapped/wrap_spec.rb +0 -23
- data/spec/tty/table/operations/new_spec.rb +0 -30
- data/spec/tty/table/options_spec.rb +0 -25
- data/spec/tty/table/orientation_spec.rb +0 -145
- data/spec/tty/table/padder/parse_spec.rb +0 -45
- data/spec/tty/table/padder/to_s_spec.rb +0 -14
- data/spec/tty/table/padding_spec.rb +0 -120
- data/spec/tty/table/properties_spec.rb +0 -25
- data/spec/tty/table/render_spec.rb +0 -63
- data/spec/tty/table/render_with_spec.rb +0 -106
- data/spec/tty/table/renderer/ascii/indentation_spec.rb +0 -41
- data/spec/tty/table/renderer/ascii/padding_spec.rb +0 -61
- data/spec/tty/table/renderer/ascii/render_spec.rb +0 -68
- data/spec/tty/table/renderer/ascii/resizing_spec.rb +0 -114
- data/spec/tty/table/renderer/ascii/separator_spec.rb +0 -28
- data/spec/tty/table/renderer/basic/alignment_spec.rb +0 -88
- data/spec/tty/table/renderer/basic/coloring_spec.rb +0 -46
- data/spec/tty/table/renderer/basic/extract_column_widths_spec.rb +0 -28
- data/spec/tty/table/renderer/basic/filter_spec.rb +0 -53
- data/spec/tty/table/renderer/basic/indentation_spec.rb +0 -48
- data/spec/tty/table/renderer/basic/multiline_content_spec.rb +0 -135
- data/spec/tty/table/renderer/basic/new_spec.rb +0 -22
- data/spec/tty/table/renderer/basic/options_spec.rb +0 -48
- data/spec/tty/table/renderer/basic/padding_spec.rb +0 -52
- data/spec/tty/table/renderer/basic/render_spec.rb +0 -57
- data/spec/tty/table/renderer/basic/resizing_spec.rb +0 -96
- data/spec/tty/table/renderer/basic/separator_spec.rb +0 -43
- data/spec/tty/table/renderer/basic/truncation_spec.rb +0 -35
- data/spec/tty/table/renderer/basic/wrapping_spec.rb +0 -40
- data/spec/tty/table/renderer/basic_spec.rb +0 -0
- data/spec/tty/table/renderer/border_spec.rb +0 -104
- data/spec/tty/table/renderer/render_spec.rb +0 -36
- data/spec/tty/table/renderer/select_spec.rb +0 -22
- data/spec/tty/table/renderer/style_spec.rb +0 -72
- data/spec/tty/table/renderer/unicode/indentation_spec.rb +0 -41
- data/spec/tty/table/renderer/unicode/padding_spec.rb +0 -61
- data/spec/tty/table/renderer/unicode/render_spec.rb +0 -68
- data/spec/tty/table/renderer/unicode/separator_spec.rb +0 -26
- data/spec/tty/table/renderer_spec.rb +0 -19
- data/spec/tty/table/rotate_spec.rb +0 -86
- data/spec/tty/table/row/access_spec.rb +0 -25
- data/spec/tty/table/row/call_spec.rb +0 -41
- data/spec/tty/table/row/data_spec.rb +0 -26
- data/spec/tty/table/row/each_spec.rb +0 -31
- data/spec/tty/table/row/equality_spec.rb +0 -73
- data/spec/tty/table/row/height_spec.rb +0 -27
- data/spec/tty/table/row/new_spec.rb +0 -41
- data/spec/tty/table/row/to_ary_spec.rb +0 -14
- data/spec/tty/table/to_s_spec.rb +0 -63
- data/spec/tty/table/transformation/extract_tuples_spec.rb +0 -35
- data/spec/tty/table/validatable/validate_options_spec.rb +0 -33
- data/spec/tty/table/validatable_spec.rb +0 -32
- data/spec/tty/text/distance/initialize_spec.rb +0 -14
- data/spec/tty/text/distance_spec.rb +0 -11
- data/spec/tty/text/truncate_spec.rb +0 -12
- data/spec/tty/text/truncation/initialize_spec.rb +0 -29
- data/spec/tty/text/truncation/truncate_spec.rb +0 -94
- data/spec/tty/text/wrap_spec.rb +0 -13
- data/spec/tty/text/wrapping/initialize_spec.rb +0 -25
- data/spec/tty/text/wrapping/wrap_spec.rb +0 -97
@@ -1,64 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module TTY
|
4
|
-
class Table
|
5
|
-
# Mixin to provide validation for {Table}.
|
6
|
-
#
|
7
|
-
# Include this mixin to add validation for options.
|
8
|
-
#
|
9
|
-
# @api private
|
10
|
-
module Validatable
|
11
|
-
# Check if table rows are the equal size
|
12
|
-
#
|
13
|
-
# @raise [DimensionMismatchError]
|
14
|
-
# if the rows are not equal length
|
15
|
-
#
|
16
|
-
# @return [nil]
|
17
|
-
#
|
18
|
-
# @api private
|
19
|
-
def assert_row_sizes(rows)
|
20
|
-
size = (rows[0] || []).size
|
21
|
-
rows.each do |row|
|
22
|
-
next if row.size == size
|
23
|
-
fail TTY::Table::DimensionMismatchError,
|
24
|
-
"row size differs (#{row.size} should be #{size})"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
# Check if table type is provided
|
29
|
-
#
|
30
|
-
# @raise [ArgumentRequired]
|
31
|
-
#
|
32
|
-
# @return [Table]
|
33
|
-
#
|
34
|
-
# @api private
|
35
|
-
def assert_table_type(value)
|
36
|
-
return value if value.is_a?(TTY::Table)
|
37
|
-
fail ArgumentRequired,
|
38
|
-
"Expected TTY::Table instance, got #{value.inspect}"
|
39
|
-
end
|
40
|
-
|
41
|
-
# def assert_matching_widths(rows)
|
42
|
-
# end
|
43
|
-
#
|
44
|
-
# def assert_string_values(rows)
|
45
|
-
# end
|
46
|
-
|
47
|
-
# Check if options are of required type
|
48
|
-
#
|
49
|
-
# @api private
|
50
|
-
def validate_options!(options)
|
51
|
-
header = options[:header]
|
52
|
-
rows = options[:rows]
|
53
|
-
|
54
|
-
if header && (!header.is_a?(Array) || header.empty?)
|
55
|
-
fail InvalidArgument, ':header must be a non-empty array'
|
56
|
-
end
|
57
|
-
|
58
|
-
if rows && !(rows.is_a?(Array) || rows.is_a?(Hash))
|
59
|
-
fail InvalidArgument, ':rows must be a non-empty array or hash'
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end # Validatable
|
63
|
-
end # Table
|
64
|
-
end # TTY
|
data/lib/tty/table.rb
DELETED
@@ -1,476 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'forwardable'
|
4
|
-
require 'tty/table/renderer'
|
5
|
-
require 'tty/table/error'
|
6
|
-
require 'tty/table/validatable'
|
7
|
-
require 'tty/table/header'
|
8
|
-
require 'tty/table/row'
|
9
|
-
|
10
|
-
module TTY
|
11
|
-
# A core class intended for storing data in a structured, tabular form.
|
12
|
-
# Once the data is stored in a TTY::Table various operations can be performed
|
13
|
-
# before the information is dumped into a stdout.
|
14
|
-
class Table
|
15
|
-
include Comparable, Enumerable, Validatable, Equatable
|
16
|
-
extend Forwardable
|
17
|
-
|
18
|
-
# The table header
|
19
|
-
#
|
20
|
-
# @return [Enumerable]
|
21
|
-
#
|
22
|
-
# @api public
|
23
|
-
attr_reader :header
|
24
|
-
|
25
|
-
# The table rows
|
26
|
-
#
|
27
|
-
# @return [Enumerable]
|
28
|
-
#
|
29
|
-
# @api private
|
30
|
-
attr_reader :rows
|
31
|
-
# private :rows
|
32
|
-
|
33
|
-
# The table orientation out of :horizontal and :vertical
|
34
|
-
#
|
35
|
-
# @reutrn [TTY::Table::Orientation]
|
36
|
-
#
|
37
|
-
# @api public
|
38
|
-
attr_reader :orientation
|
39
|
-
|
40
|
-
# The table original row count
|
41
|
-
#
|
42
|
-
# @reutrn [Integer]
|
43
|
-
#
|
44
|
-
# @api public
|
45
|
-
attr_reader :original_rows
|
46
|
-
|
47
|
-
# The table original column count
|
48
|
-
#
|
49
|
-
# @reutrn [Integer]
|
50
|
-
#
|
51
|
-
# @api public
|
52
|
-
attr_reader :original_columns
|
53
|
-
|
54
|
-
# Subset of safe methods that both Array and Hash implement
|
55
|
-
def_delegators(:data, :assoc, :flatten, :include?, :index,
|
56
|
-
:length, :select, :to_a, :values_at, :pretty_print, :rassoc)
|
57
|
-
|
58
|
-
# Create a new Table where each argument is a row
|
59
|
-
#
|
60
|
-
# @example
|
61
|
-
# table = TTY::Table[['a1', 'a2'], ['b1', 'b2']]
|
62
|
-
#
|
63
|
-
# @api public
|
64
|
-
def self.[](*rows)
|
65
|
-
new(rows: rows)
|
66
|
-
end
|
67
|
-
|
68
|
-
# Instantiate a new Table
|
69
|
-
#
|
70
|
-
# @example of no header
|
71
|
-
# rows = [ ['a1', 'a2'], ['b1', 'b2'] ]
|
72
|
-
# table = Table.new rows
|
73
|
-
#
|
74
|
-
# @example of direct parameters
|
75
|
-
# rows = [ ['a1', 'a2'], ['b1', 'b2'] ]
|
76
|
-
# table = Table.new ['Header 1', 'Header 2'], rows
|
77
|
-
#
|
78
|
-
# @example of parameters passed as options
|
79
|
-
# rows = [ ['a1', 'a2'], ['b1', 'b2'] ]
|
80
|
-
# table = Table.new header: ['Header 1', 'Header 2'], rows: rows
|
81
|
-
#
|
82
|
-
# @example of parameters passed as hash
|
83
|
-
# Table.new [ {'Header1' => ['a1','a2'], 'Header2' => ['b1', 'b2'] }] }
|
84
|
-
#
|
85
|
-
# @param [Array[Symbol], Hash] *args
|
86
|
-
#
|
87
|
-
# @api public
|
88
|
-
def self.new(*args, &block)
|
89
|
-
options = Utils.extract_options!(args)
|
90
|
-
if args.size.nonzero?
|
91
|
-
super(Transformation.extract_tuples(args).merge(options), &block)
|
92
|
-
else
|
93
|
-
super(options, &block)
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
# Initialize a Table
|
98
|
-
#
|
99
|
-
# @param [Hash] options
|
100
|
-
# the options to create the table with
|
101
|
-
# @option options [String] :header
|
102
|
-
# column names to be displayed
|
103
|
-
# @option options [String] :rows
|
104
|
-
# Array of Arrays expressing the rows
|
105
|
-
# @option options [Symbol] :orientation
|
106
|
-
# used to transform table orientation
|
107
|
-
#
|
108
|
-
# @return [TTY::Table]
|
109
|
-
#
|
110
|
-
# @api private
|
111
|
-
def initialize(options = {}, &block)
|
112
|
-
validate_options! options
|
113
|
-
@converter = Necromancer.new
|
114
|
-
@header = (value = options[:header]) ? Header.new(value) : nil
|
115
|
-
@rows = coerce(options.fetch(:rows) { Row.new([]) })
|
116
|
-
@rotated = false
|
117
|
-
self.orientation = options.fetch(:orientation) { :horizontal }
|
118
|
-
|
119
|
-
assert_row_sizes @rows
|
120
|
-
orientation.transform(self)
|
121
|
-
|
122
|
-
yield_or_eval(&block) if block_given?
|
123
|
-
end
|
124
|
-
|
125
|
-
# Provides access to all table data
|
126
|
-
#
|
127
|
-
# @return [Array]
|
128
|
-
#
|
129
|
-
# @api public
|
130
|
-
def data
|
131
|
-
(header && !header.empty?) ? [header] + rows : rows
|
132
|
-
end
|
133
|
-
|
134
|
-
# Sets table orientation
|
135
|
-
#
|
136
|
-
# @param [String,Symbol] value
|
137
|
-
#
|
138
|
-
# @api public
|
139
|
-
def orientation=(value)
|
140
|
-
@orientation = Orientation.coerce(value)
|
141
|
-
end
|
142
|
-
|
143
|
-
# Marks this table as rotated
|
144
|
-
#
|
145
|
-
# @return [Boolean]
|
146
|
-
#
|
147
|
-
# @api public
|
148
|
-
def rotated?
|
149
|
-
@rotated
|
150
|
-
end
|
151
|
-
|
152
|
-
# Rotate the table between vertical and horizontal orientation
|
153
|
-
#
|
154
|
-
# @return [self]
|
155
|
-
#
|
156
|
-
# @api private
|
157
|
-
def rotate
|
158
|
-
orientation.transform(self)
|
159
|
-
self
|
160
|
-
end
|
161
|
-
|
162
|
-
# Rotate the table vertically
|
163
|
-
#
|
164
|
-
# @api private
|
165
|
-
def rotate_vertical
|
166
|
-
@original_columns = column_size
|
167
|
-
@original_rows = row_size
|
168
|
-
@rows = orientation.slice(self)
|
169
|
-
@header = [] if header
|
170
|
-
@rotated = true
|
171
|
-
end
|
172
|
-
|
173
|
-
# Rotate the table horizontally
|
174
|
-
#
|
175
|
-
# @api private
|
176
|
-
def rotate_horizontal
|
177
|
-
return unless rotated?
|
178
|
-
head, body = orientation.slice(self)
|
179
|
-
if header && header.empty?
|
180
|
-
@header = head[0]
|
181
|
-
@rows = body.map { |row| to_row(row, @header) }
|
182
|
-
else
|
183
|
-
@rows = body.map { |row| to_row(row) }
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
|
-
# Lookup element of the table given a row(i) and column(j)
|
188
|
-
#
|
189
|
-
# @api public
|
190
|
-
def [](row_index, column_index = false)
|
191
|
-
return row(row_index) unless column_index
|
192
|
-
if row_index >= 0 && column_index >= 0
|
193
|
-
rows.fetch(row_index) { return nil }[column_index]
|
194
|
-
else
|
195
|
-
fail TTY::Table::TupleMissing.new(row_index, column_index)
|
196
|
-
end
|
197
|
-
end
|
198
|
-
alias at []
|
199
|
-
alias element []
|
200
|
-
alias component []
|
201
|
-
|
202
|
-
# Set table value at row(i) and column(j)
|
203
|
-
#
|
204
|
-
# @api private
|
205
|
-
def []=(row_index, column_index, val)
|
206
|
-
@rows[row_index][column_index] = val
|
207
|
-
end
|
208
|
-
private :[]=
|
209
|
-
|
210
|
-
# Return a row number at the index of the table as an Array.
|
211
|
-
# When a block is given, the elements of that Array are iterated over.
|
212
|
-
#
|
213
|
-
# @example
|
214
|
-
# rows = [ ['a1', 'a2'], ['b1', 'b2'] ]
|
215
|
-
# table = TTY::Table.new :rows => rows
|
216
|
-
# table.row(1) { |element| ... }
|
217
|
-
#
|
218
|
-
# @param [Integer] index
|
219
|
-
#
|
220
|
-
# @yield []
|
221
|
-
# optional block to execute in the iteration operation
|
222
|
-
#
|
223
|
-
# @return [self]
|
224
|
-
#
|
225
|
-
# @api public
|
226
|
-
def row(index, &block)
|
227
|
-
if block_given?
|
228
|
-
rows.fetch(index) { return self }.each(&block)
|
229
|
-
self
|
230
|
-
else
|
231
|
-
rows.fetch(index) { return nil }
|
232
|
-
end
|
233
|
-
end
|
234
|
-
|
235
|
-
# Return a column number at the index of the table as an Array.
|
236
|
-
# If the table has a header then column can be searched by header name.
|
237
|
-
# When a block is given, the elements of that Array are iterated over.
|
238
|
-
#
|
239
|
-
# @example
|
240
|
-
# header = [:h1, :h2]
|
241
|
-
# rows = [ ['a1', 'a2'], ['b1', 'b2'] ]
|
242
|
-
# table = TTY::Table.new :rows => rows, :header => header
|
243
|
-
# table.column(1)
|
244
|
-
# table.column(1) { |element| ... }
|
245
|
-
# table.column(:h1)
|
246
|
-
# table.column(:h1) { |element| ... }
|
247
|
-
#
|
248
|
-
# @param [Integer, String, Symbol] index
|
249
|
-
#
|
250
|
-
# @yield []
|
251
|
-
# optional block to execute in the iteration operation
|
252
|
-
#
|
253
|
-
# @return [self]
|
254
|
-
#
|
255
|
-
# @api public
|
256
|
-
def column(index)
|
257
|
-
index_unknown = index.is_a?(Integer) && (index >= column_size || index < 0)
|
258
|
-
if block_given?
|
259
|
-
return self if index_unknown
|
260
|
-
rows.map { |row| yield row[index] }
|
261
|
-
else
|
262
|
-
return nil if index_unknown
|
263
|
-
rows.map { |row| row[index] }.compact
|
264
|
-
end
|
265
|
-
end
|
266
|
-
|
267
|
-
# Add row to table
|
268
|
-
#
|
269
|
-
# @param [Array] row
|
270
|
-
#
|
271
|
-
# @return [self]
|
272
|
-
#
|
273
|
-
# @api public
|
274
|
-
def <<(row)
|
275
|
-
rows_copy = rows.dup
|
276
|
-
assert_row_sizes rows_copy << row
|
277
|
-
rows << to_row(row)
|
278
|
-
self
|
279
|
-
end
|
280
|
-
|
281
|
-
# Iterate over each tuple in the set
|
282
|
-
#
|
283
|
-
# @example
|
284
|
-
# table = TTY::Table.new(header, tuples)
|
285
|
-
# table.each { |row| ... }
|
286
|
-
#
|
287
|
-
# @yield [Array[Array]]
|
288
|
-
#
|
289
|
-
# @return [self]
|
290
|
-
#
|
291
|
-
# @api public
|
292
|
-
def each
|
293
|
-
return to_enum unless block_given?
|
294
|
-
data.each { |row| yield row }
|
295
|
-
self
|
296
|
-
end
|
297
|
-
|
298
|
-
# Iterate over each element yielding in addition row and column index
|
299
|
-
#
|
300
|
-
# @example
|
301
|
-
# table = TTY::Table.new(header, tuples)
|
302
|
-
# table.each_with_index { |el, row, col| puts "#{el} at #{row},#{col}" }
|
303
|
-
#
|
304
|
-
# @return self
|
305
|
-
#
|
306
|
-
# @api public
|
307
|
-
def each_with_index
|
308
|
-
return to_enum unless block_given?
|
309
|
-
start_index = 0
|
310
|
-
if header && !header.empty?
|
311
|
-
header.attributes.each_with_index do |el, col_index|
|
312
|
-
yield el, 0, col_index
|
313
|
-
end
|
314
|
-
start_index = 1
|
315
|
-
end
|
316
|
-
|
317
|
-
rows.each_with_index do |row, row_index|
|
318
|
-
row.fields.each_with_index do |el, col_index|
|
319
|
-
yield el, row_index + start_index, col_index
|
320
|
-
end
|
321
|
-
end
|
322
|
-
self
|
323
|
-
end
|
324
|
-
|
325
|
-
# Return the number of columns
|
326
|
-
#
|
327
|
-
# @example
|
328
|
-
# table.column_size # => 5
|
329
|
-
#
|
330
|
-
# @return [Integer]
|
331
|
-
#
|
332
|
-
# @api public
|
333
|
-
# TODO: renmae to columns_size
|
334
|
-
def column_size
|
335
|
-
rows.size > 0 ? rows[0].size : 0
|
336
|
-
end
|
337
|
-
|
338
|
-
# Return the number of rows
|
339
|
-
#
|
340
|
-
# @example
|
341
|
-
# table.row_size # => 5
|
342
|
-
#
|
343
|
-
# @return [Integer]
|
344
|
-
#
|
345
|
-
# @api public
|
346
|
-
def row_size
|
347
|
-
rows.size
|
348
|
-
end
|
349
|
-
|
350
|
-
# Return the number of rows and columns
|
351
|
-
#
|
352
|
-
# @example
|
353
|
-
# table.size # => [3,5]
|
354
|
-
#
|
355
|
-
# @return [Array] row x columns
|
356
|
-
#
|
357
|
-
# @api public
|
358
|
-
def size
|
359
|
-
[row_size, column_size]
|
360
|
-
end
|
361
|
-
|
362
|
-
# Check table width
|
363
|
-
#
|
364
|
-
# @return [Integer] width
|
365
|
-
#
|
366
|
-
# @api public
|
367
|
-
def width
|
368
|
-
ColumnSet.new(self).total_width
|
369
|
-
end
|
370
|
-
|
371
|
-
# Return true if this is an empty table, i.e. if the number of
|
372
|
-
# rows or the number of columns is 0
|
373
|
-
#
|
374
|
-
# @return [Boolean]
|
375
|
-
#
|
376
|
-
# @api public
|
377
|
-
def empty?
|
378
|
-
column_size == 0 || row_size == 0
|
379
|
-
end
|
380
|
-
|
381
|
-
# Return string representation of table using basic renderer.
|
382
|
-
#
|
383
|
-
# @return [String]
|
384
|
-
#
|
385
|
-
# @api public
|
386
|
-
def to_s
|
387
|
-
render(:basic)
|
388
|
-
end
|
389
|
-
|
390
|
-
# Return renderer for this table
|
391
|
-
#
|
392
|
-
# @param [Symbol] type
|
393
|
-
# the renderer type
|
394
|
-
#
|
395
|
-
# @param [Hash] options
|
396
|
-
# the renderer options
|
397
|
-
#
|
398
|
-
def renderer(type = :basic, options = {})
|
399
|
-
@renderer ||= Renderer.select(type).new(self, options)
|
400
|
-
end
|
401
|
-
|
402
|
-
# Render a given table. This method takes options which will be passed
|
403
|
-
# to the renderer prior to rendering, which allows the caller to set any
|
404
|
-
# table rendering variables.
|
405
|
-
#
|
406
|
-
# @param [Symbol] renderer_type
|
407
|
-
# the renderer to be used
|
408
|
-
#
|
409
|
-
# @param [Hash] options
|
410
|
-
#
|
411
|
-
# @yield [renderer]
|
412
|
-
#
|
413
|
-
# @yieldparam [TTY::Table::Renderer] renderer
|
414
|
-
# the renderer for the table
|
415
|
-
#
|
416
|
-
# @return [String]
|
417
|
-
#
|
418
|
-
# @api public
|
419
|
-
def render(*args, &block)
|
420
|
-
render_with(nil, *args, &block)
|
421
|
-
end
|
422
|
-
|
423
|
-
# Render a given table using custom border class.
|
424
|
-
#
|
425
|
-
# @param [TTY::Table::Border]
|
426
|
-
#
|
427
|
-
# @param [Symbol] renderer_type
|
428
|
-
#
|
429
|
-
# @yield [renderer]
|
430
|
-
#
|
431
|
-
# @yieldparam [TTY::Table::Renderer] renderer
|
432
|
-
# the renderer for the table
|
433
|
-
#
|
434
|
-
# @return [String]
|
435
|
-
#
|
436
|
-
# @api public
|
437
|
-
def render_with(border_class, renderer_type=(not_set=true), options={}, &block)
|
438
|
-
unless not_set
|
439
|
-
if renderer_type.respond_to?(:to_hash)
|
440
|
-
options = renderer_type
|
441
|
-
else
|
442
|
-
options[:renderer] = renderer_type
|
443
|
-
end
|
444
|
-
end
|
445
|
-
|
446
|
-
Renderer.render_with(border_class, self, options, &block)
|
447
|
-
end
|
448
|
-
|
449
|
-
# Coerce an Enumerable into a Table
|
450
|
-
# This coercion mechanism is used by Table to handle Enumerable types
|
451
|
-
# and force them into array type.
|
452
|
-
#
|
453
|
-
# @param [Enumerable] object
|
454
|
-
# the object to coerce
|
455
|
-
#
|
456
|
-
# @return [Array]
|
457
|
-
#
|
458
|
-
# @api public
|
459
|
-
def coerce(rows)
|
460
|
-
rows = @converter.convert(rows).to(:array)
|
461
|
-
rows.map { |row| to_row(row, header) }
|
462
|
-
end
|
463
|
-
|
464
|
-
private
|
465
|
-
|
466
|
-
# Evaluate block
|
467
|
-
#
|
468
|
-
# @return [Table]
|
469
|
-
#
|
470
|
-
# @api private
|
471
|
-
def yield_or_eval(&block)
|
472
|
-
return unless block
|
473
|
-
block.arity > 0 ? yield(self) : instance_eval(&block)
|
474
|
-
end
|
475
|
-
end # Table
|
476
|
-
end # TTY
|
data/lib/tty/text/truncation.rb
DELETED
@@ -1,99 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module TTY
|
4
|
-
class Text
|
5
|
-
# A class responsible for text truncation operations
|
6
|
-
class Truncation
|
7
|
-
include Unicode
|
8
|
-
|
9
|
-
DEFAULT_TRAILING = '…'.freeze
|
10
|
-
|
11
|
-
DEFAULT_TRUNCATION_LENGTH = 30.freeze
|
12
|
-
|
13
|
-
attr_reader :text
|
14
|
-
|
15
|
-
attr_reader :length
|
16
|
-
|
17
|
-
attr_reader :separator
|
18
|
-
|
19
|
-
attr_reader :trailing
|
20
|
-
|
21
|
-
attr_reader :escape
|
22
|
-
|
23
|
-
attr_reader :color
|
24
|
-
|
25
|
-
# Initialize a Truncation
|
26
|
-
#
|
27
|
-
# @param [String] text
|
28
|
-
# the text to be truncated
|
29
|
-
#
|
30
|
-
# @overload new(text, value)
|
31
|
-
# truncates text at given value
|
32
|
-
#
|
33
|
-
# @param [Integer] value
|
34
|
-
#
|
35
|
-
# @overload new(text, value, options)
|
36
|
-
# @param [Integer] value
|
37
|
-
#
|
38
|
-
# @param [Hash] options
|
39
|
-
# @option options [Symbol] :length the desired length
|
40
|
-
# @option options [Symbol] :separator the character for splitting words
|
41
|
-
# @option options [Symbol] :trailing the character for ending sentence
|
42
|
-
# @option options [Symbol] :escape remove ANSI escape sequences
|
43
|
-
#
|
44
|
-
# @api private
|
45
|
-
def initialize(text, *args)
|
46
|
-
options = Utils.extract_options!(args)
|
47
|
-
@color = Pastel.new
|
48
|
-
@text = text
|
49
|
-
@length = options.fetch(:length) { DEFAULT_TRUNCATION_LENGTH }
|
50
|
-
@length = args[0] unless args.empty?
|
51
|
-
@separator = options.fetch(:separator) { nil }
|
52
|
-
@trailing = options.fetch(:trailing) { DEFAULT_TRAILING }
|
53
|
-
@escape = options.fetch(:escape) { true }
|
54
|
-
end
|
55
|
-
|
56
|
-
# Truncate a text
|
57
|
-
#
|
58
|
-
# @see TTY::Text.truncate
|
59
|
-
#
|
60
|
-
# @api private
|
61
|
-
def truncate
|
62
|
-
return text unless length && length > 0
|
63
|
-
|
64
|
-
as_unicode do
|
65
|
-
chars = (escape ? escape_text : text).chars.to_a
|
66
|
-
return chars.join if chars.length <= length
|
67
|
-
stop = chars[0, length_without_trailing].rindex(separator)
|
68
|
-
|
69
|
-
chars[0, stop || length_without_trailing].join + trailing
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
private
|
74
|
-
|
75
|
-
# Strip ANSI characters from the text
|
76
|
-
#
|
77
|
-
# @param [String] text
|
78
|
-
#
|
79
|
-
# @return [String]
|
80
|
-
#
|
81
|
-
# @api private
|
82
|
-
def escape_text
|
83
|
-
color.strip text.dup
|
84
|
-
end
|
85
|
-
|
86
|
-
# Leave space for the trailing characters
|
87
|
-
#
|
88
|
-
# @return [Integer]
|
89
|
-
#
|
90
|
-
# @api private
|
91
|
-
def length_without_trailing
|
92
|
-
as_unicode do
|
93
|
-
trailing_size = trailing.chars.to_a.size
|
94
|
-
length - trailing_size
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end # Truncation
|
98
|
-
end # Text
|
99
|
-
end # TTY
|