tty-table 0.7.0 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +57 -0
- data/LICENSE.txt +1 -1
- data/README.md +165 -94
- data/lib/tty-table.rb +1 -5
- data/lib/tty/table.rb +102 -42
- data/lib/tty/table/alignment_set.rb +3 -6
- data/lib/tty/table/border.rb +57 -68
- data/lib/tty/table/border/ascii.rb +17 -17
- data/lib/tty/table/border/null.rb +20 -22
- data/lib/tty/table/border/row_line.rb +4 -3
- data/lib/tty/table/border/unicode.rb +17 -17
- data/lib/tty/table/border_dsl.rb +28 -25
- data/lib/tty/table/border_options.rb +50 -25
- data/lib/tty/table/column_constraint.rb +29 -17
- data/lib/tty/table/{column_set.rb → columns.rb} +44 -69
- data/lib/tty/table/empty.rb +2 -0
- data/lib/tty/table/error.rb +1 -1
- data/lib/tty/table/field.rb +43 -35
- data/lib/tty/table/header.rb +7 -5
- data/lib/tty/table/indentation.rb +16 -25
- data/lib/tty/table/operation/alignment.rb +10 -8
- data/lib/tty/table/operation/escape.rb +2 -3
- data/lib/tty/table/operation/filter.rb +1 -1
- data/lib/tty/table/operation/padding.rb +4 -12
- data/lib/tty/table/operation/truncation.rb +4 -4
- data/lib/tty/table/operation/wrapped.rb +4 -4
- data/lib/tty/table/operations.rb +4 -15
- data/lib/tty/table/options.rb +2 -4
- data/lib/tty/table/orientation.rb +5 -5
- data/lib/tty/table/orientation/horizontal.rb +1 -1
- data/lib/tty/table/orientation/vertical.rb +1 -1
- data/lib/tty/table/renderer.rb +40 -35
- data/lib/tty/table/renderer/ascii.rb +4 -4
- data/lib/tty/table/renderer/basic.rb +85 -88
- data/lib/tty/table/renderer/unicode.rb +3 -3
- data/lib/tty/table/row.rb +10 -8
- data/lib/tty/table/transformation.rb +2 -2
- data/lib/tty/table/validatable.rb +25 -7
- data/lib/tty/table/version.rb +3 -3
- metadata +33 -283
- data/.gitignore +0 -14
- data/.rspec +0 -3
- data/.travis.yml +0 -24
- data/CODE_OF_CONDUCT.md +0 -49
- data/Gemfile +0 -18
- data/Rakefile +0 -8
- data/benchmarks/speed.rb +0 -41
- data/spec/spec_helper.rb +0 -50
- data/spec/unit/access_spec.rb +0 -86
- data/spec/unit/add_row_spec.rb +0 -28
- data/spec/unit/alignment_set/each_spec.rb +0 -17
- data/spec/unit/alignment_set/new_spec.rb +0 -27
- data/spec/unit/alignment_set/to_ary_spec.rb +0 -14
- data/spec/unit/alignment_spec.rb +0 -71
- data/spec/unit/border/ascii/rendering_spec.rb +0 -90
- data/spec/unit/border/new_spec.rb +0 -27
- data/spec/unit/border/null/rendering_spec.rb +0 -130
- data/spec/unit/border/options/from_spec.rb +0 -38
- data/spec/unit/border/options/new_spec.rb +0 -14
- data/spec/unit/border/unicode/rendering_spec.rb +0 -63
- data/spec/unit/border_options/new_spec.rb +0 -20
- data/spec/unit/border_options/update_spec.rb +0 -18
- data/spec/unit/column_constraint/enforce_spec.rb +0 -70
- data/spec/unit/column_constraint/widths_spec.rb +0 -35
- data/spec/unit/column_set/extract_widths_spec.rb +0 -48
- data/spec/unit/column_set/total_width_spec.rb +0 -15
- data/spec/unit/column_set/widths_from_spec.rb +0 -51
- data/spec/unit/data_spec.rb +0 -12
- data/spec/unit/each_spec.rb +0 -26
- data/spec/unit/each_with_index_spec.rb +0 -51
- data/spec/unit/empty_spec.rb +0 -23
- data/spec/unit/eql_spec.rb +0 -34
- data/spec/unit/field/equality_spec.rb +0 -51
- data/spec/unit/field/length_spec.rb +0 -35
- data/spec/unit/field/lines_spec.rb +0 -21
- data/spec/unit/field/new_spec.rb +0 -29
- data/spec/unit/field/width_spec.rb +0 -23
- data/spec/unit/filter_spec.rb +0 -22
- data/spec/unit/header/call_spec.rb +0 -30
- data/spec/unit/header/color_spec.rb +0 -19
- data/spec/unit/header/equality_spec.rb +0 -51
- data/spec/unit/header/height_spec.rb +0 -27
- data/spec/unit/header/new_spec.rb +0 -16
- data/spec/unit/header/set_spec.rb +0 -20
- data/spec/unit/header/to_ary_spec.rb +0 -14
- data/spec/unit/header_spec.rb +0 -13
- data/spec/unit/indentation/indent_spec.rb +0 -21
- data/spec/unit/new_spec.rb +0 -73
- data/spec/unit/operation/alignment/call_spec.rb +0 -39
- data/spec/unit/operation/escape/call_spec.rb +0 -15
- data/spec/unit/operation/filter/call_spec.rb +0 -16
- data/spec/unit/operation/truncation/call_spec.rb +0 -30
- data/spec/unit/operation/wrapped/call_spec.rb +0 -40
- data/spec/unit/operations/new_spec.rb +0 -30
- data/spec/unit/options/access_spec.rb +0 -14
- data/spec/unit/options_spec.rb +0 -25
- data/spec/unit/orientation_spec.rb +0 -145
- data/spec/unit/padding_spec.rb +0 -117
- data/spec/unit/properties_spec.rb +0 -25
- data/spec/unit/render_repeat_spec.rb +0 -42
- data/spec/unit/render_spec.rb +0 -63
- data/spec/unit/render_with_spec.rb +0 -106
- data/spec/unit/renderer/ascii/coloring_spec.rb +0 -70
- data/spec/unit/renderer/ascii/indentation_spec.rb +0 -41
- data/spec/unit/renderer/ascii/multiline_spec.rb +0 -101
- data/spec/unit/renderer/ascii/padding_spec.rb +0 -88
- data/spec/unit/renderer/ascii/render_spec.rb +0 -68
- data/spec/unit/renderer/ascii/resizing_spec.rb +0 -114
- data/spec/unit/renderer/ascii/separator_spec.rb +0 -28
- data/spec/unit/renderer/basic/alignment_spec.rb +0 -88
- data/spec/unit/renderer/basic/coloring_spec.rb +0 -61
- data/spec/unit/renderer/basic/extract_column_widths_spec.rb +0 -28
- data/spec/unit/renderer/basic/filter_spec.rb +0 -53
- data/spec/unit/renderer/basic/indentation_spec.rb +0 -48
- data/spec/unit/renderer/basic/multiline_spec.rb +0 -74
- data/spec/unit/renderer/basic/new_spec.rb +0 -26
- data/spec/unit/renderer/basic/options_spec.rb +0 -52
- data/spec/unit/renderer/basic/padding_spec.rb +0 -76
- data/spec/unit/renderer/basic/render_spec.rb +0 -57
- data/spec/unit/renderer/basic/resizing_spec.rb +0 -96
- data/spec/unit/renderer/basic/separator_spec.rb +0 -43
- data/spec/unit/renderer/basic/truncation_spec.rb +0 -35
- data/spec/unit/renderer/basic/wrapping_spec.rb +0 -40
- data/spec/unit/renderer/border_spec.rb +0 -104
- data/spec/unit/renderer/render_spec.rb +0 -36
- data/spec/unit/renderer/select_spec.rb +0 -22
- data/spec/unit/renderer/unicode/coloring_spec.rb +0 -70
- data/spec/unit/renderer/unicode/indentation_spec.rb +0 -41
- data/spec/unit/renderer/unicode/padding_spec.rb +0 -61
- data/spec/unit/renderer/unicode/render_spec.rb +0 -68
- data/spec/unit/renderer/unicode/separator_spec.rb +0 -26
- data/spec/unit/renderer_spec.rb +0 -19
- data/spec/unit/rotate_spec.rb +0 -86
- data/spec/unit/row/access_spec.rb +0 -25
- data/spec/unit/row/call_spec.rb +0 -45
- data/spec/unit/row/data_spec.rb +0 -26
- data/spec/unit/row/each_spec.rb +0 -31
- data/spec/unit/row/equality_spec.rb +0 -73
- data/spec/unit/row/height_spec.rb +0 -27
- data/spec/unit/row/new_spec.rb +0 -41
- data/spec/unit/row/to_ary_spec.rb +0 -14
- data/spec/unit/to_s_spec.rb +0 -56
- data/spec/unit/transformation/extract_tuples_spec.rb +0 -35
- data/spec/unit/utf_spec.rb +0 -33
- data/spec/unit/validatable/validate_options_spec.rb +0 -33
- data/spec/unit/validatable_spec.rb +0 -32
- data/tasks/console.rake +0 -10
- data/tasks/coverage.rake +0 -11
- data/tasks/spec.rake +0 -29
- data/tty-table.gemspec +0 -30
data/lib/tty-table.rb
CHANGED
data/lib/tty/table.rb
CHANGED
@@ -1,23 +1,22 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require 'forwardable'
|
5
|
-
require 'necromancer'
|
3
|
+
require "forwardable"
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
5
|
+
require_relative "table/columns"
|
6
|
+
require_relative "table/header"
|
7
|
+
require_relative "table/orientation"
|
8
|
+
require_relative "table/row"
|
9
|
+
require_relative "table/renderer"
|
10
|
+
require_relative "table/transformation"
|
11
|
+
require_relative "table/validatable"
|
12
|
+
require_relative "table/version"
|
14
13
|
|
15
14
|
module TTY
|
16
15
|
# A core class intended for storing data in a structured, tabular form.
|
17
16
|
# Once the data is stored in a TTY::Table various operations can be performed
|
18
17
|
# before the information is dumped into a stdout.
|
19
18
|
class Table
|
20
|
-
include Comparable, Enumerable, Validatable
|
19
|
+
include Comparable, Enumerable, Validatable
|
21
20
|
extend Forwardable
|
22
21
|
|
23
22
|
# The table header
|
@@ -37,21 +36,21 @@ module TTY
|
|
37
36
|
|
38
37
|
# The table orientation out of :horizontal and :vertical
|
39
38
|
#
|
40
|
-
# @
|
39
|
+
# @return [TTY::Table::Orientation]
|
41
40
|
#
|
42
41
|
# @api public
|
43
42
|
attr_reader :orientation
|
44
43
|
|
45
44
|
# The table original row count
|
46
45
|
#
|
47
|
-
# @
|
46
|
+
# @return [Integer]
|
48
47
|
#
|
49
48
|
# @api public
|
50
49
|
attr_reader :original_rows
|
51
50
|
|
52
51
|
# The table original column count
|
53
52
|
#
|
54
|
-
# @
|
53
|
+
# @return [Integer]
|
55
54
|
#
|
56
55
|
# @api public
|
57
56
|
attr_reader :original_columns
|
@@ -63,7 +62,7 @@ module TTY
|
|
63
62
|
# Create a new Table where each argument is a row
|
64
63
|
#
|
65
64
|
# @example
|
66
|
-
# table = TTY::Table[[
|
65
|
+
# table = TTY::Table[["a1", "a2"], ["b1", "b2"]]
|
67
66
|
#
|
68
67
|
# @api public
|
69
68
|
def self.[](*rows)
|
@@ -73,20 +72,20 @@ module TTY
|
|
73
72
|
# Instantiate a new Table
|
74
73
|
#
|
75
74
|
# @example of no header
|
76
|
-
# table = Table.new [[
|
75
|
+
# table = Table.new [["a1", "a2"], ["b1", "b2"]]
|
77
76
|
#
|
78
77
|
# @example of direct parameters
|
79
|
-
# rows = [[
|
80
|
-
# table = Table.new [
|
78
|
+
# rows = [["a1", "a2"], ["b1", "b2"]]
|
79
|
+
# table = Table.new ["Header 1", "Header 2"], rows
|
81
80
|
#
|
82
81
|
# @example of parameters passed as options
|
83
|
-
# rows = [[
|
84
|
-
# table = Table.new header: [
|
82
|
+
# rows = [["a1", "a2"], ["b1", "b2"]]
|
83
|
+
# table = Table.new header: ["Header 1", "Header 2"], rows: rows
|
85
84
|
#
|
86
85
|
# @example of parameters passed as hash
|
87
|
-
# Table.new [{
|
86
|
+
# Table.new [{"Header1" => ["a1","a2"], "Header2" => ["b1", "b2"] }]}
|
88
87
|
#
|
89
|
-
# @param [Array[Symbol], Hash]
|
88
|
+
# @param [Array[Symbol], Hash] args
|
90
89
|
#
|
91
90
|
# @api public
|
92
91
|
def self.new(*args, &block)
|
@@ -114,7 +113,6 @@ module TTY
|
|
114
113
|
# @api private
|
115
114
|
def initialize(options = {}, &block)
|
116
115
|
validate_options! options
|
117
|
-
@converter = Necromancer.new
|
118
116
|
@header = (value = options[:header]) ? Header.new(value) : nil
|
119
117
|
@rows = coerce(options.fetch(:rows) { Row.new([]) })
|
120
118
|
@rotated = false
|
@@ -126,6 +124,15 @@ module TTY
|
|
126
124
|
yield_or_eval(&block) if block_given?
|
127
125
|
end
|
128
126
|
|
127
|
+
# Provides a list of rows to have separations applied
|
128
|
+
#
|
129
|
+
# @return [Array]
|
130
|
+
#
|
131
|
+
# @api public
|
132
|
+
def separators
|
133
|
+
@separators ||= []
|
134
|
+
end
|
135
|
+
|
129
136
|
# Provides access to all table data
|
130
137
|
#
|
131
138
|
# @return [Array]
|
@@ -194,10 +201,10 @@ module TTY
|
|
194
201
|
# @param [Integer] column_index
|
195
202
|
#
|
196
203
|
# @example
|
197
|
-
# table = TTY::Table.new [[
|
198
|
-
# table[0] # => [
|
199
|
-
# table[0,0] # =>
|
200
|
-
# table[-1] # => [
|
204
|
+
# table = TTY::Table.new [["a1","a2"], ["b1","b2"]]
|
205
|
+
# table[0] # => ["a1","a2"]
|
206
|
+
# table[0,0] # => "a1"
|
207
|
+
# table[-1] # => ["b1","b2"]
|
201
208
|
#
|
202
209
|
# @api public
|
203
210
|
def [](row_index, column_index = false)
|
@@ -205,12 +212,12 @@ module TTY
|
|
205
212
|
if row_index >= 0 && column_index >= 0
|
206
213
|
rows.fetch(row_index) { return nil }[column_index]
|
207
214
|
else
|
208
|
-
|
215
|
+
raise TTY::Table::TupleMissing.new(row_index, column_index)
|
209
216
|
end
|
210
217
|
end
|
211
|
-
|
212
|
-
|
213
|
-
|
218
|
+
alias at []
|
219
|
+
alias element []
|
220
|
+
alias component []
|
214
221
|
|
215
222
|
# Set table value at row(i) and column(j)
|
216
223
|
#
|
@@ -224,7 +231,7 @@ module TTY
|
|
224
231
|
# When a block is given, the elements of that Array are iterated over.
|
225
232
|
#
|
226
233
|
# @example
|
227
|
-
# rows = [[
|
234
|
+
# rows = [["a1", "a2"], ["b1", "b2"]]
|
228
235
|
# table = TTY::Table.new rows: rows
|
229
236
|
# table.row(1) { |row| ... }
|
230
237
|
#
|
@@ -251,7 +258,7 @@ module TTY
|
|
251
258
|
#
|
252
259
|
# @example
|
253
260
|
# header = [:h1, :h2]
|
254
|
-
# rows = [ [
|
261
|
+
# rows = [ ["a1", "a2"], ["b1", "b2"] ]
|
255
262
|
# table = TTY::Table.new :rows => rows, :header => header
|
256
263
|
# table.column(1)
|
257
264
|
# table.column(1) { |element| ... }
|
@@ -285,9 +292,12 @@ module TTY
|
|
285
292
|
#
|
286
293
|
# @api public
|
287
294
|
def <<(row)
|
288
|
-
|
289
|
-
|
290
|
-
|
295
|
+
if row == Border::SEPARATOR
|
296
|
+
separators << columns_size - (header ? 0 : 2)
|
297
|
+
else
|
298
|
+
assert_row_size(row, rows)
|
299
|
+
rows << to_row(row)
|
300
|
+
end
|
291
301
|
self
|
292
302
|
end
|
293
303
|
|
@@ -339,6 +349,7 @@ module TTY
|
|
339
349
|
def columns_size
|
340
350
|
rows.size > 0 ? rows[0].size : 0
|
341
351
|
end
|
352
|
+
alias columns_count columns_size
|
342
353
|
|
343
354
|
# Return the number of rows
|
344
355
|
#
|
@@ -370,7 +381,7 @@ module TTY
|
|
370
381
|
#
|
371
382
|
# @api public
|
372
383
|
def width
|
373
|
-
|
384
|
+
Columns.total_width(data)
|
374
385
|
end
|
375
386
|
|
376
387
|
# Return true if this is an empty table, i.e. if the number of
|
@@ -427,10 +438,12 @@ module TTY
|
|
427
438
|
|
428
439
|
# Render a given table using custom border class.
|
429
440
|
#
|
430
|
-
# @param [TTY::Table::Border]
|
441
|
+
# @param [TTY::Table::Border] border_class
|
431
442
|
#
|
432
443
|
# @param [Symbol] renderer_type
|
433
444
|
#
|
445
|
+
# @param [Hash] options
|
446
|
+
#
|
434
447
|
# @yield [renderer]
|
435
448
|
#
|
436
449
|
# @yieldparam [TTY::Table::Renderer] renderer
|
@@ -455,15 +468,62 @@ module TTY
|
|
455
468
|
# This coercion mechanism is used by Table to handle Enumerable types
|
456
469
|
# and force them into array type.
|
457
470
|
#
|
458
|
-
# @param [Enumerable]
|
471
|
+
# @param [Enumerable] rows
|
459
472
|
# the object to coerce
|
460
473
|
#
|
461
474
|
# @return [Array]
|
462
475
|
#
|
463
476
|
# @api public
|
464
477
|
def coerce(rows)
|
465
|
-
|
466
|
-
rows.
|
478
|
+
coerced_rows = []
|
479
|
+
Array(rows).each do |row|
|
480
|
+
if row == Border::SEPARATOR
|
481
|
+
separators << coerced_rows.length - (header ? 0 : 1)
|
482
|
+
else
|
483
|
+
coerced_rows << to_row(row, header)
|
484
|
+
end
|
485
|
+
end
|
486
|
+
coerced_rows
|
487
|
+
end
|
488
|
+
|
489
|
+
# Compare table for equality of header and rows attributes
|
490
|
+
#
|
491
|
+
# @return [Boolean]
|
492
|
+
#
|
493
|
+
# @api public
|
494
|
+
def eql?(other)
|
495
|
+
instance_of?(other.class) &&
|
496
|
+
header.eql?(other.header) && rows.eql?(other.rows)
|
497
|
+
end
|
498
|
+
|
499
|
+
# Compare table for equivalence of header and rows attributes
|
500
|
+
#
|
501
|
+
# @return [Boolean]
|
502
|
+
#
|
503
|
+
# @api public
|
504
|
+
def ==(other)
|
505
|
+
other.is_a?(self.class) &&
|
506
|
+
header == other.header && rows == other.rows
|
507
|
+
end
|
508
|
+
|
509
|
+
# Inspect this instance attributes
|
510
|
+
#
|
511
|
+
# @return [String]
|
512
|
+
#
|
513
|
+
# @api public
|
514
|
+
def inspect
|
515
|
+
"#<#{self.class.name} header=#{header.inspect} rows=#{rows.inspect} " \
|
516
|
+
"original_rows=#{original_rows.inspect} " \
|
517
|
+
"original_columns=#{original_columns.inspect}>"
|
518
|
+
end
|
519
|
+
|
520
|
+
# Hash for this instance and its attributes
|
521
|
+
#
|
522
|
+
# @return [Numeric]
|
523
|
+
#
|
524
|
+
# @api public
|
525
|
+
def hash
|
526
|
+
[self.class, header, rows].hash
|
467
527
|
end
|
468
528
|
|
469
529
|
private
|
@@ -1,6 +1,4 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
require 'necromancer'
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
3
|
module TTY
|
6
4
|
class Table
|
@@ -19,8 +17,7 @@ module TTY
|
|
19
17
|
#
|
20
18
|
# @api private
|
21
19
|
def initialize(alignments)
|
22
|
-
@
|
23
|
-
@alignments = @converter.convert(alignments).to(:array).map(&:to_sym)
|
20
|
+
@alignments = Array(alignments).map(&:to_sym)
|
24
21
|
end
|
25
22
|
|
26
23
|
# Iterate over each element in the alignment set
|
@@ -40,7 +37,7 @@ module TTY
|
|
40
37
|
|
41
38
|
# Lookup an alignment by index
|
42
39
|
#
|
43
|
-
# @param [Integer]
|
40
|
+
# @param [Integer] index
|
44
41
|
#
|
45
42
|
# @return [Symbol] alignment
|
46
43
|
#
|
data/lib/tty/table/border.rb
CHANGED
@@ -1,26 +1,26 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require 'verse'
|
3
|
+
require "pastel"
|
4
|
+
require "strings"
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
require_relative "border_dsl"
|
7
|
+
require_relative "border/row_line"
|
8
|
+
require_relative "error"
|
10
9
|
|
11
10
|
module TTY
|
12
11
|
class Table
|
13
12
|
# Abstract base class that is responsible for building the table border.
|
14
13
|
class Border
|
15
|
-
|
14
|
+
EMPTY_CHAR = ""
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
SPACE_CHAR = ' '.freeze
|
16
|
+
SPACE_CHAR = " "
|
20
17
|
|
21
18
|
# Represent a separtor on each row
|
22
19
|
EACH_ROW = :each_row
|
23
20
|
|
21
|
+
# specify a separator as a row
|
22
|
+
SEPARATOR = :separator
|
23
|
+
|
24
24
|
class << self
|
25
25
|
# Store characters for border
|
26
26
|
#
|
@@ -28,40 +28,40 @@ module TTY
|
|
28
28
|
attr_accessor :characters
|
29
29
|
end
|
30
30
|
|
31
|
+
# Define border characters
|
32
|
+
#
|
33
|
+
# @param [Hash] characters
|
34
|
+
# the border characters
|
35
|
+
#
|
36
|
+
# @return [Hash]
|
37
|
+
#
|
38
|
+
# @api public
|
39
|
+
def self.def_border(characters = (not_set = true), &block)
|
40
|
+
return self.characters = characters unless not_set
|
41
|
+
|
42
|
+
dsl = BorderDSL.new(&block)
|
43
|
+
self.characters = dsl.characters
|
44
|
+
end
|
45
|
+
|
31
46
|
# Instantiate a new object
|
32
47
|
#
|
33
48
|
# @param [Array] column_widths
|
34
49
|
# the table column widths
|
35
50
|
#
|
36
|
-
# @param [BorderOptions]
|
51
|
+
# @param [BorderOptions] border_opts
|
37
52
|
#
|
38
53
|
# @return [Object]
|
39
54
|
#
|
40
55
|
# @api private
|
41
|
-
def initialize(column_widths,
|
56
|
+
def initialize(column_widths, border_opts = nil)
|
42
57
|
if self.class == Border
|
43
|
-
|
44
|
-
else
|
45
|
-
@widths = column_widths
|
46
|
-
@padding = Verse::Padder.parse(padding)
|
47
|
-
@border_options = TTY::Table::BorderOptions.from options
|
48
|
-
@color = Pastel.new
|
58
|
+
raise NotImplementedError, "#{self} is an abstract class"
|
49
59
|
end
|
50
|
-
end
|
51
|
-
|
52
|
-
# Define border characters
|
53
|
-
#
|
54
|
-
# @param [Hash] characters
|
55
|
-
# the border characters
|
56
|
-
#
|
57
|
-
# @return [Hash]
|
58
|
-
#
|
59
|
-
# @api public
|
60
|
-
def self.def_border(characters=(not_set=true), &block)
|
61
|
-
return self.characters = characters unless not_set
|
62
60
|
|
63
|
-
|
64
|
-
|
61
|
+
@widths = column_widths
|
62
|
+
@dsl = BorderDSL.new(border_opts)
|
63
|
+
@characters = self.class.characters.merge(@dsl.characters)
|
64
|
+
@color = Pastel.new
|
65
65
|
end
|
66
66
|
|
67
67
|
# Retrive individual character by type
|
@@ -73,13 +73,7 @@ module TTY
|
|
73
73
|
#
|
74
74
|
# @api private
|
75
75
|
def [](type)
|
76
|
-
characters
|
77
|
-
chars = if border_options.nil?
|
78
|
-
characters
|
79
|
-
else
|
80
|
-
characters.merge(border_options.characters)
|
81
|
-
end
|
82
|
-
chars[type] || EMPTY_CHAR
|
76
|
+
@characters[type] || EMPTY_CHAR
|
83
77
|
end
|
84
78
|
|
85
79
|
# Check if border color is set
|
@@ -88,7 +82,7 @@ module TTY
|
|
88
82
|
#
|
89
83
|
# @api public
|
90
84
|
def color?
|
91
|
-
|
85
|
+
!!@dsl.style
|
92
86
|
end
|
93
87
|
|
94
88
|
# A line spanning all columns marking top of a table.
|
@@ -114,7 +108,7 @@ module TTY
|
|
114
108
|
# @return [String]
|
115
109
|
#
|
116
110
|
# @api private
|
117
|
-
def
|
111
|
+
def middle_line
|
118
112
|
(result = render(:mid)).empty? ? nil : result
|
119
113
|
end
|
120
114
|
|
@@ -127,30 +121,27 @@ module TTY
|
|
127
121
|
#
|
128
122
|
# @api public
|
129
123
|
def row_line(row)
|
130
|
-
line = RowLine.new(self[
|
131
|
-
line.colorize(self,
|
124
|
+
line = RowLine.new(self["left"], self["center"], self["right"])
|
125
|
+
line.colorize(self, @dsl.style) if color?
|
132
126
|
|
133
127
|
result = row_heights(row, line)
|
134
128
|
result.empty? ? EMPTY_CHAR : result
|
135
129
|
end
|
136
130
|
|
137
|
-
# Set color
|
131
|
+
# Set color for a string
|
138
132
|
#
|
139
133
|
# @param [Symbol] color
|
140
134
|
#
|
141
|
-
# @param [
|
135
|
+
# @param [String] string
|
136
|
+
# the string to color
|
142
137
|
#
|
143
|
-
# @return [
|
138
|
+
# @return [String]
|
144
139
|
#
|
145
140
|
# @api public
|
146
|
-
def set_color(color,
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
else
|
151
|
-
@color.decorate(string, color)
|
152
|
-
end
|
153
|
-
end
|
141
|
+
def set_color(color, string)
|
142
|
+
return string if string.gsub(/\s+/, EMPTY_CHAR).empty?
|
143
|
+
|
144
|
+
@color.decorate(string, color)
|
154
145
|
end
|
155
146
|
|
156
147
|
protected
|
@@ -163,12 +154,9 @@ module TTY
|
|
163
154
|
# The table custom border options including styling
|
164
155
|
#
|
165
156
|
# @api private
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
#
|
170
|
-
# @api private
|
171
|
-
attr_reader :padding
|
157
|
+
def border_options
|
158
|
+
@dsl.options
|
159
|
+
end
|
172
160
|
|
173
161
|
# Separate multiline string into individual rows with border.
|
174
162
|
#
|
@@ -193,7 +181,7 @@ module TTY
|
|
193
181
|
# @param [TTY::Table::Row] row
|
194
182
|
# the table row
|
195
183
|
#
|
196
|
-
# @param [Integer]
|
184
|
+
# @param [Integer] line_index
|
197
185
|
# the index for current line inside multiline
|
198
186
|
#
|
199
187
|
# @param [TTY::Table::Border::RowLine] line
|
@@ -205,14 +193,16 @@ module TTY
|
|
205
193
|
line.left + row.fields.each_with_index.map do |field, index|
|
206
194
|
direction = field.alignment || :left
|
207
195
|
field_content = field.lines[line_index] || SPACE_CHAR * field.length
|
208
|
-
|
196
|
+
Strings.align(field_content, widths[index], direction: direction)
|
209
197
|
end.join(line.center) + line.right
|
210
198
|
end
|
211
199
|
|
212
200
|
# Generate particular border type
|
213
201
|
#
|
214
202
|
# @param [String] type
|
215
|
-
# border type one of
|
203
|
+
# border type one of :top, :bottom and :mid
|
204
|
+
#
|
205
|
+
# @return [String]
|
216
206
|
#
|
217
207
|
# @api private
|
218
208
|
def render(type)
|
@@ -224,7 +214,8 @@ module TTY
|
|
224
214
|
self["#{type}_mid"])
|
225
215
|
|
226
216
|
return line unless color?
|
227
|
-
|
217
|
+
|
218
|
+
set_color(@dsl.style, line)
|
228
219
|
end
|
229
220
|
|
230
221
|
# Generate a border string
|
@@ -236,9 +227,7 @@ module TTY
|
|
236
227
|
#
|
237
228
|
# @api private
|
238
229
|
def render_line(line, left, right, intersection)
|
239
|
-
left + widths.map
|
240
|
-
line * (padding.left + width + padding.right)
|
241
|
-
end.join(intersection) + right
|
230
|
+
left + widths.map { |width| line * width }.join(intersection) + right
|
242
231
|
end
|
243
232
|
end # Border
|
244
233
|
end # Table
|