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,7 +1,8 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
require_relative "border/null"
|
4
|
+
require_relative "columns"
|
5
|
+
require_relative "error"
|
5
6
|
|
6
7
|
module TTY
|
7
8
|
class Table
|
@@ -56,7 +57,7 @@ module TTY
|
|
56
57
|
# @api public
|
57
58
|
def padding_size
|
58
59
|
padding = renderer.padding
|
59
|
-
(padding.left + padding.right) *
|
60
|
+
(padding.left + padding.right) * table.columns_count
|
60
61
|
end
|
61
62
|
|
62
63
|
# Estimate minimum table width to be able to display content
|
@@ -85,9 +86,16 @@ module TTY
|
|
85
86
|
# @api public
|
86
87
|
def enforce
|
87
88
|
assert_minimum_width
|
89
|
+
padding = renderer.padding
|
88
90
|
|
89
91
|
if natural_width <= renderer.width
|
90
|
-
renderer.resize
|
92
|
+
if renderer.resize
|
93
|
+
expand_column_widths
|
94
|
+
else
|
95
|
+
renderer.column_widths.map do |width|
|
96
|
+
padding.left + width + padding.right
|
97
|
+
end
|
98
|
+
end
|
91
99
|
else
|
92
100
|
if renderer.resize
|
93
101
|
shrink
|
@@ -103,23 +111,23 @@ module TTY
|
|
103
111
|
#
|
104
112
|
# @api private
|
105
113
|
def rotate
|
106
|
-
Kernel.warn
|
107
|
-
|
108
|
-
'orientation.'
|
114
|
+
Kernel.warn "The table size exceeds the currently set width." \
|
115
|
+
"Defaulting to vertical orientation."
|
109
116
|
table.orientation = :vertical
|
110
117
|
table.rotate
|
111
|
-
|
118
|
+
Columns.widths_from(table)
|
112
119
|
end
|
113
120
|
|
114
121
|
# Expand column widths to match the requested width
|
115
122
|
#
|
116
123
|
# @api private
|
117
|
-
def
|
118
|
-
|
119
|
-
|
124
|
+
def expand_column_widths
|
125
|
+
columns_count = table.columns_count
|
126
|
+
max_width = renderer.width
|
127
|
+
extra_column_width = ((max_width - natural_width) / columns_count.to_f).floor
|
120
128
|
|
121
|
-
widths = (0...
|
122
|
-
lengths
|
129
|
+
widths = (0...columns_count).reduce([]) do |lengths, col|
|
130
|
+
lengths << renderer.column_widths[col] + extra_column_width
|
123
131
|
end
|
124
132
|
distribute_extra_width(widths)
|
125
133
|
end
|
@@ -132,7 +140,11 @@ module TTY
|
|
132
140
|
ratio = ((natural_width - renderer.width) / column_size.to_f).ceil
|
133
141
|
|
134
142
|
widths = (0...column_size).reduce([]) do |lengths, col|
|
135
|
-
|
143
|
+
width = (renderer.column_widths[col] - ratio)
|
144
|
+
# basically ruby 2.4 Numeric#clamp
|
145
|
+
width = width < minimum_width ? minimum_width : width
|
146
|
+
width = width > renderer.width ? renderer.width : width
|
147
|
+
lengths << width
|
136
148
|
end
|
137
149
|
distribute_extra_width(widths)
|
138
150
|
end
|
@@ -145,8 +157,8 @@ module TTY
|
|
145
157
|
def assert_minimum_width
|
146
158
|
width = renderer.width
|
147
159
|
return unless width <= minimum_width
|
148
|
-
|
149
|
-
|
160
|
+
raise ResizeError, "Table's width is too small to contain the content " \
|
161
|
+
"(min width #{minimum_width}, currently set #{width})"
|
150
162
|
end
|
151
163
|
|
152
164
|
# Distribute remaining width to meet the total width requirement.
|
@@ -1,64 +1,54 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require_relative "error"
|
4
4
|
|
5
5
|
module TTY
|
6
6
|
class Table
|
7
|
-
# A
|
7
|
+
# A module for calculating table data column widths
|
8
8
|
#
|
9
9
|
# Used by {Table} to manage column sizing.
|
10
10
|
#
|
11
11
|
# @api private
|
12
|
-
|
13
|
-
include Equatable
|
14
|
-
|
15
|
-
# Initialize a ColumnSet
|
16
|
-
#
|
17
|
-
# @param [Array[Array[Object]]] data
|
18
|
-
#
|
19
|
-
# @api public
|
20
|
-
def initialize(table)
|
21
|
-
@data = table.data
|
22
|
-
end
|
23
|
-
|
12
|
+
module Columns
|
24
13
|
# Calculate total table width
|
25
14
|
#
|
26
15
|
# @return [Integer]
|
27
16
|
#
|
28
17
|
# @api public
|
29
|
-
def total_width
|
30
|
-
extract_widths.reduce(:+)
|
18
|
+
def total_width(data)
|
19
|
+
extract_widths(data).reduce(:+)
|
31
20
|
end
|
21
|
+
module_function :total_width
|
32
22
|
|
33
23
|
# Calcualte maximum column widths
|
34
24
|
#
|
35
25
|
# @return [Array] column widths
|
36
26
|
#
|
37
27
|
# @api private
|
38
|
-
def extract_widths
|
28
|
+
def extract_widths(data)
|
39
29
|
colcount = data.max { |row_a, row_b| row_a.size <=> row_b.size }.size
|
40
|
-
|
30
|
+
(0...colcount).reduce([]) do |maximas, col_index|
|
31
|
+
maximas << find_maximum(data, col_index)
|
32
|
+
maximas
|
33
|
+
end
|
41
34
|
end
|
35
|
+
module_function :extract_widths
|
42
36
|
|
43
|
-
#
|
44
|
-
#
|
45
|
-
# @param [Array] column_widths
|
37
|
+
# Find a maximum column width. The calculation takes into account
|
38
|
+
# wether the content is escaped or not.
|
46
39
|
#
|
47
|
-
# @param [Integer]
|
40
|
+
# @param [Integer] index
|
41
|
+
# the column index
|
48
42
|
#
|
49
|
-
# @
|
43
|
+
# @return [Integer]
|
50
44
|
#
|
51
|
-
# @api
|
52
|
-
def
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
57
|
-
if column_widths.size != table_widths
|
58
|
-
fail InvalidArgument, 'Value for :column_widths must match ' \
|
59
|
-
'table column count'
|
60
|
-
end
|
45
|
+
# @api private
|
46
|
+
def find_maximum(data, index)
|
47
|
+
data.map do |row|
|
48
|
+
(field = row.call(index)) ? field.length : 0
|
49
|
+
end.max
|
61
50
|
end
|
51
|
+
module_function :find_maximum
|
62
52
|
|
63
53
|
# Converts column widths to array format or infers default widths
|
64
54
|
#
|
@@ -69,7 +59,7 @@ module TTY
|
|
69
59
|
# @return [Array[Integer]]
|
70
60
|
#
|
71
61
|
# @api public
|
72
|
-
def
|
62
|
+
def widths_from(table, column_widths = nil)
|
73
63
|
case column_widths
|
74
64
|
when Array
|
75
65
|
assert_widths(column_widths, table.columns_size)
|
@@ -77,48 +67,33 @@ module TTY
|
|
77
67
|
when Numeric
|
78
68
|
Array.new(table.columns_size, column_widths)
|
79
69
|
when NilClass
|
80
|
-
|
70
|
+
extract_widths(table.data)
|
81
71
|
else
|
82
|
-
|
72
|
+
raise TypeError, "Invalid type for column widths"
|
83
73
|
end
|
84
74
|
end
|
75
|
+
module_function :widths_from
|
85
76
|
|
86
|
-
|
87
|
-
|
88
|
-
attr_reader :data
|
89
|
-
|
90
|
-
# Find maximum widths for each row and header if present.
|
91
|
-
#
|
92
|
-
# @param [Integer] colcount
|
93
|
-
# number of columns
|
94
|
-
#
|
95
|
-
# @return [Array[Integer]]
|
77
|
+
# Assert data integrity for column widths
|
96
78
|
#
|
97
|
-
# @
|
98
|
-
def find_maximas(colcount)
|
99
|
-
maximas = []
|
100
|
-
start = 0
|
101
|
-
|
102
|
-
start.upto(colcount - 1) do |col_index|
|
103
|
-
maximas << find_maximum(col_index)
|
104
|
-
end
|
105
|
-
maximas
|
106
|
-
end
|
107
|
-
|
108
|
-
# Find a maximum column width. The calculation takes into account
|
109
|
-
# wether the content is escaped or not.
|
79
|
+
# @param [Array] column_widths
|
110
80
|
#
|
111
|
-
# @param [Integer]
|
112
|
-
# the column index
|
81
|
+
# @param [Integer] table_widths
|
113
82
|
#
|
114
|
-
# @
|
83
|
+
# @raise [TTY::InvalidArgument]
|
115
84
|
#
|
116
|
-
# @api
|
117
|
-
def
|
118
|
-
|
119
|
-
|
120
|
-
|
85
|
+
# @api public
|
86
|
+
def assert_widths(column_widths, table_widths)
|
87
|
+
if column_widths.empty?
|
88
|
+
raise InvalidArgument, "Value for :column_widths must be " \
|
89
|
+
"a non-empty array"
|
90
|
+
end
|
91
|
+
if column_widths.size != table_widths
|
92
|
+
raise InvalidArgument, "Value for :column_widths must match " \
|
93
|
+
"table column count"
|
94
|
+
end
|
121
95
|
end
|
122
|
-
|
96
|
+
module_function :assert_widths
|
97
|
+
end # Columns
|
123
98
|
end # Table
|
124
99
|
end # TTY
|
data/lib/tty/table/empty.rb
CHANGED
data/lib/tty/table/error.rb
CHANGED
data/lib/tty/table/field.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require 'pastel'
|
5
|
-
require 'unicode/display_width'
|
3
|
+
require "strings"
|
6
4
|
|
7
5
|
module TTY
|
8
6
|
class Table
|
@@ -13,32 +11,15 @@ module TTY
|
|
13
11
|
#
|
14
12
|
# @api private
|
15
13
|
class Field
|
16
|
-
include Equatable
|
17
|
-
|
18
14
|
# The value inside the field
|
19
15
|
#
|
20
16
|
# @api public
|
21
|
-
|
17
|
+
attr_accessor :value
|
22
18
|
|
23
19
|
# The formatted value inside the field used for display
|
24
20
|
#
|
25
21
|
# @api public
|
26
|
-
|
27
|
-
|
28
|
-
# The name for the value
|
29
|
-
#
|
30
|
-
# @api public
|
31
|
-
attr_reader :name
|
32
|
-
|
33
|
-
# The actual value
|
34
|
-
#
|
35
|
-
# @api public
|
36
|
-
attr_writer :value
|
37
|
-
|
38
|
-
# The formatted string
|
39
|
-
#
|
40
|
-
# @api public
|
41
|
-
attr_writer :content
|
22
|
+
attr_accessor :content
|
42
23
|
|
43
24
|
# Number of columns this field spans. Defaults to 1.
|
44
25
|
#
|
@@ -58,15 +39,15 @@ module TTY
|
|
58
39
|
# Initialize a Field
|
59
40
|
#
|
60
41
|
# @example
|
61
|
-
# field = TTY::Table::Field.new
|
42
|
+
# field = TTY::Table::Field.new "a1"
|
62
43
|
# field.value # => a1
|
63
44
|
#
|
64
45
|
# @example
|
65
|
-
# field = TTY::Table::Field.new value:
|
46
|
+
# field = TTY::Table::Field.new value: "a1"
|
66
47
|
# field.value # => a1
|
67
48
|
#
|
68
49
|
# @example
|
69
|
-
# field = TTY::Table::Field.new value:
|
50
|
+
# field = TTY::Table::Field.new value: "a1", alignment: :center
|
70
51
|
# field.value # => a1
|
71
52
|
# field.alignment # => :center
|
72
53
|
#
|
@@ -75,9 +56,9 @@ module TTY
|
|
75
56
|
@value, options = extract_options(value)
|
76
57
|
@content = @value.to_s
|
77
58
|
@width = options[:width]
|
78
|
-
@alignment = options.fetch(:alignment
|
79
|
-
@colspan = options.fetch(:colspan
|
80
|
-
@rowspan = options.fetch(:rowspan
|
59
|
+
@alignment = options.fetch(:alignment, nil)
|
60
|
+
@colspan = options.fetch(:colspan, 1)
|
61
|
+
@rowspan = options.fetch(:rowspan, 1)
|
81
62
|
end
|
82
63
|
|
83
64
|
# Extract options and set value
|
@@ -104,7 +85,7 @@ module TTY
|
|
104
85
|
#
|
105
86
|
# @api public
|
106
87
|
def width
|
107
|
-
@width || display_width(@content)
|
88
|
+
@width || Strings::Align.display_width(@content)
|
108
89
|
end
|
109
90
|
|
110
91
|
# Return number of lines this value spans.
|
@@ -127,7 +108,7 @@ module TTY
|
|
127
108
|
# @api public
|
128
109
|
def length
|
129
110
|
(lines.map do |line|
|
130
|
-
display_width(
|
111
|
+
Strings::Align.display_width(line)
|
131
112
|
end << 0).max
|
132
113
|
end
|
133
114
|
|
@@ -153,14 +134,41 @@ module TTY
|
|
153
134
|
content
|
154
135
|
end
|
155
136
|
|
137
|
+
# Compare fields for equality of value attribute
|
138
|
+
#
|
139
|
+
# @return [Boolean]
|
140
|
+
#
|
156
141
|
# @api public
|
157
|
-
def
|
158
|
-
|
142
|
+
def eql?(other)
|
143
|
+
instance_of?(other.class) && value.eql?(other.value)
|
159
144
|
end
|
160
145
|
|
146
|
+
# Compare fields for equivalence of value attribute
|
147
|
+
#
|
148
|
+
# @return [Boolean]
|
149
|
+
#
|
150
|
+
# @api public
|
151
|
+
def ==(other)
|
152
|
+
other.is_a?(self.class) && value == other.value
|
153
|
+
end
|
154
|
+
|
155
|
+
# Inspect this instance attributes
|
156
|
+
#
|
157
|
+
# @return [String]
|
158
|
+
#
|
159
|
+
# @api public
|
160
|
+
def inspect
|
161
|
+
"#<#{self.class.name} value=#{value.inspect} " \
|
162
|
+
"rowspan=#{rowspan.inspect} colspan=#{colspan.inspect}>"
|
163
|
+
end
|
164
|
+
|
165
|
+
# Hash for this instance and its attributes
|
166
|
+
#
|
167
|
+
# @return [Numeric]
|
168
|
+
#
|
161
169
|
# @api public
|
162
|
-
def
|
163
|
-
|
170
|
+
def hash
|
171
|
+
[self.class, value].hash
|
164
172
|
end
|
165
173
|
end # Field
|
166
174
|
end # Table
|
data/lib/tty/table/header.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
|
3
|
+
require "forwardable"
|
4
|
+
|
5
|
+
require_relative "error"
|
6
|
+
require_relative "field"
|
5
7
|
|
6
8
|
module TTY
|
7
9
|
class Table
|
@@ -76,8 +78,8 @@ module TTY
|
|
76
78
|
@attributes[attribute].value
|
77
79
|
else
|
78
80
|
@attribute_for.fetch(to_field(attribute)) do |header_name|
|
79
|
-
|
80
|
-
|
81
|
+
raise UnknownAttributeError,
|
82
|
+
"the header '#{header_name.value}' is unknown"
|
81
83
|
end
|
82
84
|
end
|
83
85
|
end
|
@@ -1,47 +1,38 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
|
-
# A
|
6
|
-
|
7
|
-
# The amount of indentation
|
8
|
-
#
|
9
|
-
# @api public
|
10
|
-
attr_accessor :indentation
|
11
|
-
|
12
|
-
# Initialize an Indentation
|
13
|
-
#
|
14
|
-
# @api public
|
15
|
-
def initialize(indentation)
|
16
|
-
@indentation = indentation
|
17
|
-
end
|
18
|
-
|
5
|
+
# A module responsible for indenting table representation
|
6
|
+
module Indentation
|
19
7
|
# Return a table part with indentation inserted
|
20
8
|
#
|
21
9
|
# @param [#map, #to_s] part
|
22
10
|
# the rendered table part
|
23
11
|
#
|
24
12
|
# @api public
|
25
|
-
def indent(part)
|
26
|
-
if part.respond_to?(:to_a)
|
27
|
-
part.map { |line| insert_indentation(line) }
|
13
|
+
def indent(part, indentation)
|
14
|
+
if part.is_a?(Enumerable) && part.respond_to?(:to_a)
|
15
|
+
part.map { |line| insert_indentation(line, indentation) }
|
28
16
|
else
|
29
|
-
insert_indentation(part)
|
17
|
+
insert_indentation(part, indentation)
|
30
18
|
end
|
31
19
|
end
|
32
|
-
|
33
|
-
private
|
20
|
+
module_function :indent
|
34
21
|
|
35
22
|
# Insert indentation into a table renderd line
|
36
23
|
#
|
37
|
-
# @param [
|
24
|
+
# @param [String] line
|
38
25
|
# the rendered table line
|
26
|
+
# @param [Integer] indentation
|
27
|
+
# the amount of indentation to apply
|
28
|
+
#
|
29
|
+
# @return [String]
|
39
30
|
#
|
40
31
|
# @api public
|
41
|
-
def insert_indentation(line)
|
42
|
-
line
|
43
|
-
line.insert(0, ' ' * indentation) if line
|
32
|
+
def insert_indentation(line, indentation)
|
33
|
+
line ? " " * indentation + line.to_s : ""
|
44
34
|
end
|
35
|
+
module_function :insert_indentation
|
45
36
|
end # Indentation
|
46
37
|
end # Table
|
47
38
|
end # TTY
|