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
@@ -1,6 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "strings"
|
4
4
|
|
5
5
|
module TTY
|
6
6
|
class Table
|
@@ -19,10 +19,14 @@ module TTY
|
|
19
19
|
|
20
20
|
# Evaluate alignment of the provided row
|
21
21
|
#
|
22
|
+
# @param [TTY::Table::Field] field
|
23
|
+
# the table field
|
24
|
+
#
|
22
25
|
# @param [Array] row
|
23
26
|
# the table row
|
24
|
-
#
|
25
|
-
#
|
27
|
+
#
|
28
|
+
# @param [Integer] col
|
29
|
+
# the table column index
|
26
30
|
#
|
27
31
|
# @return [TTY::Table::Field]
|
28
32
|
#
|
@@ -42,18 +46,16 @@ module TTY
|
|
42
46
|
# @param [TTY::Table::Field] field
|
43
47
|
# the table field
|
44
48
|
#
|
45
|
-
# @param [Integer]
|
49
|
+
# @param [Integer] col
|
46
50
|
# the table column index
|
47
51
|
#
|
48
|
-
# @param [Hash] options
|
49
|
-
#
|
50
52
|
# @return [TTY::Table::Field]
|
51
53
|
#
|
52
54
|
# @api private
|
53
55
|
def align_field(field, col)
|
54
56
|
column_width = widths[col]
|
55
57
|
direction = field.alignment || alignments[col] || DEFAULT
|
56
|
-
|
58
|
+
Strings.align(field.content, column_width, direction: direction)
|
57
59
|
end
|
58
60
|
end # Alignment
|
59
61
|
end # Operation
|
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
@@ -7,7 +7,6 @@ module TTY
|
|
7
7
|
#
|
8
8
|
# @api private
|
9
9
|
class Escape
|
10
|
-
|
11
10
|
# Escape special characters in a table field
|
12
11
|
#
|
13
12
|
# @param [TTY::Table::Field] field
|
@@ -21,7 +20,7 @@ module TTY
|
|
21
20
|
# @api public
|
22
21
|
def call(field, row, col)
|
23
22
|
field.content.gsub(/(\t|\r|\n)/) do |val|
|
24
|
-
val.dump.gsub('"',
|
23
|
+
val.dump.gsub('"', "")
|
25
24
|
end
|
26
25
|
end
|
27
26
|
end # Escape
|
@@ -1,6 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "strings"
|
4
4
|
|
5
5
|
module TTY
|
6
6
|
class Table
|
@@ -11,9 +11,7 @@ module TTY
|
|
11
11
|
class Padding
|
12
12
|
# Initialize a Padding operation
|
13
13
|
#
|
14
|
-
# @param [
|
15
|
-
#
|
16
|
-
# @param [Array[Integer]] widths
|
14
|
+
# @param [Strings::Padder] padding
|
17
15
|
#
|
18
16
|
# @api public
|
19
17
|
def initialize(padding)
|
@@ -25,17 +23,11 @@ module TTY
|
|
25
23
|
# @param [TTY::Table::Field] field
|
26
24
|
# the table field
|
27
25
|
#
|
28
|
-
# @param [Integer] row
|
29
|
-
# the field row index
|
30
|
-
#
|
31
|
-
# @param [Integer] col
|
32
|
-
# the field column index
|
33
|
-
#
|
34
26
|
# @return [TTY::Table::Field]
|
35
27
|
#
|
36
28
|
# @api public
|
37
29
|
def call(field, *)
|
38
|
-
|
30
|
+
Strings.pad(field.content, padding)
|
39
31
|
end
|
40
32
|
|
41
33
|
protected
|
@@ -1,6 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "strings"
|
4
4
|
|
5
5
|
module TTY
|
6
6
|
class Table
|
@@ -34,8 +34,8 @@ module TTY
|
|
34
34
|
#
|
35
35
|
# @api public
|
36
36
|
def call(field, row, col)
|
37
|
-
|
38
|
-
|
37
|
+
column_width = widths[col] || field.width
|
38
|
+
Strings.truncate(field.content, column_width)
|
39
39
|
end
|
40
40
|
end # Truncation
|
41
41
|
end # Operation
|
@@ -1,6 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "strings"
|
4
4
|
|
5
5
|
module TTY
|
6
6
|
class Table
|
@@ -33,8 +33,8 @@ module TTY
|
|
33
33
|
#
|
34
34
|
# @api public
|
35
35
|
def call(field, row, col)
|
36
|
-
|
37
|
-
|
36
|
+
column_width = widths[col] || field.width
|
37
|
+
Strings.wrap(field.content, column_width)
|
38
38
|
end
|
39
39
|
end # Wrapped
|
40
40
|
end # Operation
|
data/lib/tty/table/operations.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
@@ -8,14 +8,8 @@ module TTY
|
|
8
8
|
class Operations
|
9
9
|
# Initialize Operations
|
10
10
|
#
|
11
|
-
# @param [TTY::Table] table
|
12
|
-
# the table to perform operations on
|
13
|
-
#
|
14
|
-
# @return [Object]
|
15
|
-
#
|
16
11
|
# @api public
|
17
|
-
def initialize
|
18
|
-
@table = table
|
12
|
+
def initialize
|
19
13
|
@operations = Hash.new { |hash, key| hash[key] = [] }
|
20
14
|
end
|
21
15
|
|
@@ -55,14 +49,14 @@ module TTY
|
|
55
49
|
# @return [TTY::Table]
|
56
50
|
#
|
57
51
|
# @api public
|
58
|
-
def
|
52
|
+
def apply_to(table, *args)
|
59
53
|
operation_types = args
|
60
54
|
table.data.each_with_index do |row, row_i|
|
61
55
|
row.fields.each_with_index do |field, col_i|
|
62
56
|
field.reset!
|
63
57
|
operation_types.each do |type|
|
64
58
|
operations[type].each do |operation|
|
65
|
-
field.content = operation.
|
59
|
+
field.content = operation.(field, row_i, col_i)
|
66
60
|
end
|
67
61
|
end
|
68
62
|
end
|
@@ -71,11 +65,6 @@ module TTY
|
|
71
65
|
|
72
66
|
protected
|
73
67
|
|
74
|
-
# The table
|
75
|
-
#
|
76
|
-
# @api private
|
77
|
-
attr_reader :table
|
78
|
-
|
79
68
|
# Available operations
|
80
69
|
#
|
81
70
|
# @return [Hash]
|
data/lib/tty/table/options.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "delegate"
|
4
4
|
|
5
5
|
module TTY
|
6
6
|
class Table
|
7
|
-
|
8
7
|
# Structure for holding table options with indifferent access
|
9
8
|
class Options < DelegateClass(Hash)
|
10
9
|
|
@@ -25,6 +24,5 @@ module TTY
|
|
25
24
|
end
|
26
25
|
|
27
26
|
end # Options
|
28
|
-
|
29
27
|
end # Table
|
30
28
|
end # TTY
|
@@ -1,7 +1,7 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
require_relative "orientation/horizontal"
|
4
|
+
require_relative "orientation/vertical"
|
5
5
|
|
6
6
|
module TTY
|
7
7
|
class Table
|
@@ -33,8 +33,8 @@ module TTY
|
|
33
33
|
when /v|ert(ical)?/i
|
34
34
|
Vertical.new :vertical
|
35
35
|
else
|
36
|
-
|
37
|
-
|
36
|
+
raise InvalidOrientationError,
|
37
|
+
"orientation must be one of :horizontal, :vertical"
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
data/lib/tty/table/renderer.rb
CHANGED
@@ -1,17 +1,18 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
require_relative "error"
|
4
|
+
require_relative "renderer/basic"
|
5
|
+
require_relative "renderer/ascii"
|
6
|
+
require_relative "renderer/unicode"
|
6
7
|
|
7
8
|
module TTY
|
8
9
|
class Table
|
9
|
-
# A
|
10
|
+
# A module responsible for selecting tabule data renderer
|
10
11
|
#
|
11
12
|
# Used internally by {Table} to render table content out.
|
12
13
|
#
|
13
14
|
# @api private
|
14
|
-
|
15
|
+
module Renderer
|
15
16
|
RENDERER_MAPPER = {
|
16
17
|
ascii: TTY::Table::Renderer::ASCII,
|
17
18
|
basic: TTY::Table::Renderer::Basic,
|
@@ -20,18 +21,19 @@ module TTY
|
|
20
21
|
|
21
22
|
# Select renderer class based on string name.
|
22
23
|
#
|
23
|
-
# The possible values for
|
24
|
+
# The possible values for type are
|
24
25
|
# [:basic, :ascii, :unicode]
|
25
26
|
#
|
26
|
-
# @param [Symbol]
|
27
|
-
# the renderer used for displaying table
|
27
|
+
# @param [Symbol] type
|
28
|
+
# the renderer type used for displaying table
|
28
29
|
#
|
29
30
|
# @return [TTY::Table::Renderer]
|
30
31
|
#
|
31
32
|
# @api private
|
32
|
-
def
|
33
|
+
def select(type)
|
33
34
|
RENDERER_MAPPER[type || :basic]
|
34
35
|
end
|
36
|
+
module_function :select
|
35
37
|
|
36
38
|
# Raises an error if provided border class is of wrong type or has invalid
|
37
39
|
# implementation
|
@@ -43,17 +45,38 @@ module TTY
|
|
43
45
|
# raised when border class does not implement core methods
|
44
46
|
#
|
45
47
|
# @api public
|
46
|
-
def
|
48
|
+
def assert_border_class(border_class)
|
47
49
|
return unless border_class
|
48
50
|
unless border_class <= TTY::Table::Border
|
49
|
-
|
50
|
-
|
51
|
+
raise TypeError,
|
52
|
+
"#{border_class} should inherit from TTY::Table::Border"
|
51
53
|
end
|
52
54
|
unless border_class.characters
|
53
|
-
|
54
|
-
|
55
|
+
raise NoImplementationError,
|
56
|
+
"#{border_class} should implement def_border"
|
55
57
|
end
|
56
58
|
end
|
59
|
+
module_function :assert_border_class
|
60
|
+
|
61
|
+
# Render a given table and return the string representation.
|
62
|
+
#
|
63
|
+
# @param [TTY::Table] table
|
64
|
+
# the table to be rendered
|
65
|
+
#
|
66
|
+
# @param [Hash] options
|
67
|
+
# the options to render the table with
|
68
|
+
# @option options [String] :renderer
|
69
|
+
# used to format table output
|
70
|
+
#
|
71
|
+
# @return [String]
|
72
|
+
#
|
73
|
+
# @api public
|
74
|
+
def render(table, options = {}, &block)
|
75
|
+
renderer = select(options[:renderer]).new(table, options)
|
76
|
+
yield renderer if block_given?
|
77
|
+
renderer.render
|
78
|
+
end
|
79
|
+
module_function :render
|
57
80
|
|
58
81
|
# Add custom border for the renderer
|
59
82
|
#
|
@@ -70,30 +93,12 @@ module TTY
|
|
70
93
|
# raise if the klass does not implement def_border
|
71
94
|
#
|
72
95
|
# @api public
|
73
|
-
def
|
96
|
+
def render_with(border_class, table, options = {}, &block)
|
74
97
|
assert_border_class(border_class)
|
75
98
|
options[:border_class] = border_class if border_class
|
76
99
|
render(table, options, &block)
|
77
100
|
end
|
78
|
-
|
79
|
-
# Render a given table and return the string representation.
|
80
|
-
#
|
81
|
-
# @param [TTY::Table] table
|
82
|
-
# the table to be rendered
|
83
|
-
#
|
84
|
-
# @param [Hash] options
|
85
|
-
# the options to render the table with
|
86
|
-
# @option options [String] :renderer
|
87
|
-
# used to format table output
|
88
|
-
#
|
89
|
-
# @return [String]
|
90
|
-
#
|
91
|
-
# @api public
|
92
|
-
def self.render(table, options = {}, &block)
|
93
|
-
renderer = select(options[:renderer]).new(table, options)
|
94
|
-
yield renderer if block_given?
|
95
|
-
renderer.render
|
96
|
-
end
|
101
|
+
module_function :render_with
|
97
102
|
end # Renderer
|
98
103
|
end # Table
|
99
104
|
end # TTY
|
@@ -1,11 +1,11 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
require_relative "../border/ascii"
|
4
|
+
require_relative "../renderer/basic"
|
5
5
|
|
6
6
|
module TTY
|
7
7
|
class Table
|
8
|
-
|
8
|
+
module Renderer
|
9
9
|
class ASCII < Basic
|
10
10
|
# Create ASCII renderer
|
11
11
|
#
|
@@ -1,28 +1,27 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
require 'tty/table/validatable'
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "strings"
|
4
|
+
require "tty-screen"
|
5
|
+
|
6
|
+
require_relative "../alignment_set"
|
7
|
+
require_relative "../border_dsl"
|
8
|
+
require_relative "../border/null"
|
9
|
+
require_relative "../column_constraint"
|
10
|
+
require_relative "../columns"
|
11
|
+
require_relative "../header"
|
12
|
+
require_relative "../indentation"
|
13
|
+
require_relative "../operations"
|
14
|
+
require_relative "../operation/alignment"
|
15
|
+
require_relative "../operation/truncation"
|
16
|
+
require_relative "../operation/wrapped"
|
17
|
+
require_relative "../operation/filter"
|
18
|
+
require_relative "../operation/escape"
|
19
|
+
require_relative "../operation/padding"
|
20
|
+
require_relative "../validatable"
|
22
21
|
|
23
22
|
module TTY
|
24
23
|
class Table
|
25
|
-
|
24
|
+
module Renderer
|
26
25
|
# Renders table without any border styles.
|
27
26
|
#
|
28
27
|
# @api private
|
@@ -70,7 +69,7 @@ module TTY
|
|
70
69
|
# @api public
|
71
70
|
attr_reader :indent
|
72
71
|
|
73
|
-
# The table
|
72
|
+
# The table total width
|
74
73
|
#
|
75
74
|
# @return [Integer]
|
76
75
|
#
|
@@ -113,16 +112,19 @@ module TTY
|
|
113
112
|
def initialize(table, options = {})
|
114
113
|
@table = assert_table_type(table)
|
115
114
|
@multiline = options.fetch(:multiline) { false }
|
116
|
-
@border =
|
115
|
+
@border = BorderDSL.new(options.delete(:border)).options
|
116
|
+
unless @table.separators.empty?
|
117
|
+
@border.separator ||= @table.separators
|
118
|
+
end
|
117
119
|
@column_widths = options.fetch(:column_widths, nil)
|
118
120
|
alignment = Array(options[:alignment]) * table.columns_size
|
119
|
-
@alignments =
|
121
|
+
@alignments = AlignmentSet.new(options[:alignments] || alignment)
|
120
122
|
@filter = options.fetch(:filter) { proc { |val, _| val } }
|
121
123
|
@width = options.fetch(:width) { TTY::Screen.width }
|
122
124
|
@border_class = options.fetch(:border_class) { Border::Null }
|
123
125
|
@indent = options.fetch(:indent) { 0 }
|
124
126
|
@resize = options.fetch(:resize) { false }
|
125
|
-
@padding =
|
127
|
+
@padding = Strings::Padder.parse(options[:padding])
|
126
128
|
end
|
127
129
|
|
128
130
|
# Parses supplied column widths, if not present
|
@@ -132,7 +134,7 @@ module TTY
|
|
132
134
|
#
|
133
135
|
# @api public
|
134
136
|
def column_widths
|
135
|
-
@column_widths =
|
137
|
+
@column_widths = Columns.widths_from(table, @column_widths)
|
136
138
|
end
|
137
139
|
|
138
140
|
# Store border characters, style and separator for the table rendering
|
@@ -143,17 +145,13 @@ module TTY
|
|
143
145
|
# block representing border options
|
144
146
|
#
|
145
147
|
# @api public
|
146
|
-
def border(
|
147
|
-
@border
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
elsif !not_set
|
152
|
-
@border = TTY::Table::BorderOptions.from(options)
|
153
|
-
end
|
154
|
-
@border
|
148
|
+
def border(border_opts = (not_set = true), &block)
|
149
|
+
return @border if not_set && !block_given?
|
150
|
+
|
151
|
+
border_opts = {} if not_set
|
152
|
+
@border = BorderDSL.new(border_opts, &block).options
|
155
153
|
end
|
156
|
-
|
154
|
+
alias border= border
|
157
155
|
|
158
156
|
# Change the value of indentation
|
159
157
|
#
|
@@ -162,7 +160,7 @@ module TTY
|
|
162
160
|
#
|
163
161
|
# @api public
|
164
162
|
def indent=(value)
|
165
|
-
@
|
163
|
+
@indent = value
|
166
164
|
end
|
167
165
|
|
168
166
|
# Sets the output padding,
|
@@ -172,7 +170,7 @@ module TTY
|
|
172
170
|
#
|
173
171
|
# @api public
|
174
172
|
def padding=(value)
|
175
|
-
@padding =
|
173
|
+
@padding = Strings::Padder.parse(value)
|
176
174
|
end
|
177
175
|
|
178
176
|
# Renders table as string with border
|
@@ -188,32 +186,48 @@ module TTY
|
|
188
186
|
def render
|
189
187
|
return if table.empty?
|
190
188
|
|
191
|
-
|
192
|
-
|
193
|
-
|
189
|
+
operations = TTY::Table::Operations.new
|
190
|
+
operations.add(:escape, Operation::Escape.new)
|
191
|
+
operations.apply_to(table, :escape) unless multiline
|
192
|
+
|
194
193
|
column_constraint = ColumnConstraint.new(table, self)
|
195
194
|
@column_widths = column_constraint.enforce
|
196
|
-
|
195
|
+
widths_without_padding = @column_widths.map do |_width|
|
196
|
+
_width - padding.left - padding.right
|
197
|
+
end
|
198
|
+
create_operations(widths_without_padding).each do |op|
|
199
|
+
operations.add(*op)
|
200
|
+
end
|
201
|
+
operations.apply_to(table, *select_operations)
|
202
|
+
|
203
|
+
render_data.compact.join("\n")
|
204
|
+
end
|
205
|
+
|
206
|
+
# Select applicable operations for this table
|
207
|
+
#
|
208
|
+
# @api private
|
209
|
+
def select_operations
|
197
210
|
ops = []
|
198
211
|
ops << :escape unless multiline
|
199
212
|
ops << :alignment
|
200
213
|
ops << (multiline ? :wrapping : :truncation)
|
201
214
|
ops << :padding
|
202
215
|
ops << :filter
|
203
|
-
@operations.run_operations(*ops)
|
204
|
-
|
205
|
-
render_data.compact.join("\n")
|
206
216
|
end
|
207
217
|
|
208
|
-
# Initialize
|
218
|
+
# Initialize operations
|
219
|
+
#
|
220
|
+
# @return [Array[String, Operation]]
|
209
221
|
#
|
210
222
|
# @api private
|
211
|
-
def
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
223
|
+
def create_operations(widths)
|
224
|
+
[
|
225
|
+
[:alignment, Operation::Alignment.new(alignments, widths)],
|
226
|
+
[:filter, Operation::Filter.new(filter)],
|
227
|
+
[:truncation, Operation::Truncation.new(widths)],
|
228
|
+
[:wrapping, Operation::Wrapped.new(widths)],
|
229
|
+
[:padding, Operation::Padding.new(padding)]
|
230
|
+
]
|
217
231
|
end
|
218
232
|
|
219
233
|
protected
|
@@ -225,33 +239,17 @@ module TTY
|
|
225
239
|
# @api public
|
226
240
|
attr_reader :table
|
227
241
|
|
228
|
-
# Initializes indentation
|
229
|
-
#
|
230
|
-
# @return [TTY::Table::Indentation]
|
231
|
-
#
|
232
|
-
# @api private
|
233
|
-
def indentation
|
234
|
-
@indentation ||= TTY::Table::Indentation.new(indent)
|
235
|
-
end
|
236
|
-
|
237
|
-
# Delegate indentation insertion
|
238
|
-
#
|
239
|
-
# @api public
|
240
|
-
def insert_indent(line)
|
241
|
-
indentation.indent(line)
|
242
|
-
end
|
243
|
-
|
244
242
|
# Render table data
|
245
243
|
#
|
246
244
|
# @api private
|
247
245
|
def render_data
|
248
246
|
first_row = table.first
|
249
|
-
data_border = border_class.new(column_widths,
|
247
|
+
data_border = border_class.new(column_widths, border)
|
250
248
|
header = render_header(first_row, data_border)
|
251
249
|
rows_with_border = render_rows(data_border)
|
252
250
|
bottom_line = data_border.bottom_line
|
253
251
|
|
254
|
-
|
252
|
+
bottom_line = Indentation.indent(bottom_line, @indent) if bottom_line
|
255
253
|
|
256
254
|
[header, rows_with_border, bottom_line].compact
|
257
255
|
end
|
@@ -261,7 +259,7 @@ module TTY
|
|
261
259
|
# @param [TTY::Table::Row, TTY::Table::Header] row
|
262
260
|
# the first row in the table
|
263
261
|
#
|
264
|
-
# @param [TTY::Table::Border]
|
262
|
+
# @param [TTY::Table::Border] data_border
|
265
263
|
# the border for this table
|
266
264
|
#
|
267
265
|
# @return [String]
|
@@ -269,17 +267,17 @@ module TTY
|
|
269
267
|
# @api private
|
270
268
|
def render_header(row, data_border)
|
271
269
|
top_line = data_border.top_line
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
top_line
|
270
|
+
return top_line unless row.is_a?(TTY::Table::Header)
|
271
|
+
header = [top_line, data_border.row_line(row)]
|
272
|
+
if !border.separator || border.separator?(0)
|
273
|
+
header << data_border.middle_line
|
277
274
|
end
|
275
|
+
Indentation.indent(header.compact, @indent)
|
278
276
|
end
|
279
277
|
|
280
278
|
# Format the rows
|
281
279
|
#
|
282
|
-
# @param [TTY::Table::Border]
|
280
|
+
# @param [TTY::Table::Border] data_border
|
283
281
|
# the border for this table
|
284
282
|
#
|
285
283
|
# @return [Arrays[String]]
|
@@ -288,8 +286,9 @@ module TTY
|
|
288
286
|
def render_rows(data_border)
|
289
287
|
rows = table.rows
|
290
288
|
size = rows.size
|
289
|
+
offset = table.first.is_a?(TTY::Table::Header) ? 1 : 0
|
291
290
|
rows.each_with_index.map do |row, index|
|
292
|
-
render_row(row, data_border, size != (index
|
291
|
+
render_row(row, index+offset, data_border, size != (index + 1))
|
293
292
|
end
|
294
293
|
end
|
295
294
|
|
@@ -298,21 +297,19 @@ module TTY
|
|
298
297
|
# @param [Array] row
|
299
298
|
# a row to decorate
|
300
299
|
#
|
301
|
-
# @param [TTY::Table::Border]
|
300
|
+
# @param [TTY::Table::Border] data_border
|
302
301
|
# the border for this table
|
303
302
|
#
|
304
|
-
# @param [Boolean]
|
303
|
+
# @param [Boolean] is_not_last_row
|
305
304
|
#
|
306
305
|
# @api private
|
307
|
-
def render_row(row, data_border,
|
308
|
-
separator = data_border.separator
|
306
|
+
def render_row(row, index, data_border, is_not_last_row)
|
309
307
|
row_line = data_border.row_line(row)
|
310
|
-
|
311
|
-
if
|
312
|
-
|
313
|
-
else
|
314
|
-
insert_indent(row_line)
|
308
|
+
line = [row_line]
|
309
|
+
if border.separator?(index) && is_not_last_row
|
310
|
+
line << data_border.middle_line
|
315
311
|
end
|
312
|
+
Indentation.indent(line, @indent)
|
316
313
|
end
|
317
314
|
end # Basic
|
318
315
|
end # Renderer
|