tty-table 0.7.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +57 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +165 -94
  5. data/lib/tty-table.rb +1 -5
  6. data/lib/tty/table.rb +102 -42
  7. data/lib/tty/table/alignment_set.rb +3 -6
  8. data/lib/tty/table/border.rb +57 -68
  9. data/lib/tty/table/border/ascii.rb +17 -17
  10. data/lib/tty/table/border/null.rb +20 -22
  11. data/lib/tty/table/border/row_line.rb +4 -3
  12. data/lib/tty/table/border/unicode.rb +17 -17
  13. data/lib/tty/table/border_dsl.rb +28 -25
  14. data/lib/tty/table/border_options.rb +50 -25
  15. data/lib/tty/table/column_constraint.rb +29 -17
  16. data/lib/tty/table/{column_set.rb → columns.rb} +44 -69
  17. data/lib/tty/table/empty.rb +2 -0
  18. data/lib/tty/table/error.rb +1 -1
  19. data/lib/tty/table/field.rb +43 -35
  20. data/lib/tty/table/header.rb +7 -5
  21. data/lib/tty/table/indentation.rb +16 -25
  22. data/lib/tty/table/operation/alignment.rb +10 -8
  23. data/lib/tty/table/operation/escape.rb +2 -3
  24. data/lib/tty/table/operation/filter.rb +1 -1
  25. data/lib/tty/table/operation/padding.rb +4 -12
  26. data/lib/tty/table/operation/truncation.rb +4 -4
  27. data/lib/tty/table/operation/wrapped.rb +4 -4
  28. data/lib/tty/table/operations.rb +4 -15
  29. data/lib/tty/table/options.rb +2 -4
  30. data/lib/tty/table/orientation.rb +5 -5
  31. data/lib/tty/table/orientation/horizontal.rb +1 -1
  32. data/lib/tty/table/orientation/vertical.rb +1 -1
  33. data/lib/tty/table/renderer.rb +40 -35
  34. data/lib/tty/table/renderer/ascii.rb +4 -4
  35. data/lib/tty/table/renderer/basic.rb +85 -88
  36. data/lib/tty/table/renderer/unicode.rb +3 -3
  37. data/lib/tty/table/row.rb +10 -8
  38. data/lib/tty/table/transformation.rb +2 -2
  39. data/lib/tty/table/validatable.rb +25 -7
  40. data/lib/tty/table/version.rb +3 -3
  41. metadata +33 -283
  42. data/.gitignore +0 -14
  43. data/.rspec +0 -3
  44. data/.travis.yml +0 -24
  45. data/CODE_OF_CONDUCT.md +0 -49
  46. data/Gemfile +0 -18
  47. data/Rakefile +0 -8
  48. data/benchmarks/speed.rb +0 -41
  49. data/spec/spec_helper.rb +0 -50
  50. data/spec/unit/access_spec.rb +0 -86
  51. data/spec/unit/add_row_spec.rb +0 -28
  52. data/spec/unit/alignment_set/each_spec.rb +0 -17
  53. data/spec/unit/alignment_set/new_spec.rb +0 -27
  54. data/spec/unit/alignment_set/to_ary_spec.rb +0 -14
  55. data/spec/unit/alignment_spec.rb +0 -71
  56. data/spec/unit/border/ascii/rendering_spec.rb +0 -90
  57. data/spec/unit/border/new_spec.rb +0 -27
  58. data/spec/unit/border/null/rendering_spec.rb +0 -130
  59. data/spec/unit/border/options/from_spec.rb +0 -38
  60. data/spec/unit/border/options/new_spec.rb +0 -14
  61. data/spec/unit/border/unicode/rendering_spec.rb +0 -63
  62. data/spec/unit/border_options/new_spec.rb +0 -20
  63. data/spec/unit/border_options/update_spec.rb +0 -18
  64. data/spec/unit/column_constraint/enforce_spec.rb +0 -70
  65. data/spec/unit/column_constraint/widths_spec.rb +0 -35
  66. data/spec/unit/column_set/extract_widths_spec.rb +0 -48
  67. data/spec/unit/column_set/total_width_spec.rb +0 -15
  68. data/spec/unit/column_set/widths_from_spec.rb +0 -51
  69. data/spec/unit/data_spec.rb +0 -12
  70. data/spec/unit/each_spec.rb +0 -26
  71. data/spec/unit/each_with_index_spec.rb +0 -51
  72. data/spec/unit/empty_spec.rb +0 -23
  73. data/spec/unit/eql_spec.rb +0 -34
  74. data/spec/unit/field/equality_spec.rb +0 -51
  75. data/spec/unit/field/length_spec.rb +0 -35
  76. data/spec/unit/field/lines_spec.rb +0 -21
  77. data/spec/unit/field/new_spec.rb +0 -29
  78. data/spec/unit/field/width_spec.rb +0 -23
  79. data/spec/unit/filter_spec.rb +0 -22
  80. data/spec/unit/header/call_spec.rb +0 -30
  81. data/spec/unit/header/color_spec.rb +0 -19
  82. data/spec/unit/header/equality_spec.rb +0 -51
  83. data/spec/unit/header/height_spec.rb +0 -27
  84. data/spec/unit/header/new_spec.rb +0 -16
  85. data/spec/unit/header/set_spec.rb +0 -20
  86. data/spec/unit/header/to_ary_spec.rb +0 -14
  87. data/spec/unit/header_spec.rb +0 -13
  88. data/spec/unit/indentation/indent_spec.rb +0 -21
  89. data/spec/unit/new_spec.rb +0 -73
  90. data/spec/unit/operation/alignment/call_spec.rb +0 -39
  91. data/spec/unit/operation/escape/call_spec.rb +0 -15
  92. data/spec/unit/operation/filter/call_spec.rb +0 -16
  93. data/spec/unit/operation/truncation/call_spec.rb +0 -30
  94. data/spec/unit/operation/wrapped/call_spec.rb +0 -40
  95. data/spec/unit/operations/new_spec.rb +0 -30
  96. data/spec/unit/options/access_spec.rb +0 -14
  97. data/spec/unit/options_spec.rb +0 -25
  98. data/spec/unit/orientation_spec.rb +0 -145
  99. data/spec/unit/padding_spec.rb +0 -117
  100. data/spec/unit/properties_spec.rb +0 -25
  101. data/spec/unit/render_repeat_spec.rb +0 -42
  102. data/spec/unit/render_spec.rb +0 -63
  103. data/spec/unit/render_with_spec.rb +0 -106
  104. data/spec/unit/renderer/ascii/coloring_spec.rb +0 -70
  105. data/spec/unit/renderer/ascii/indentation_spec.rb +0 -41
  106. data/spec/unit/renderer/ascii/multiline_spec.rb +0 -101
  107. data/spec/unit/renderer/ascii/padding_spec.rb +0 -88
  108. data/spec/unit/renderer/ascii/render_spec.rb +0 -68
  109. data/spec/unit/renderer/ascii/resizing_spec.rb +0 -114
  110. data/spec/unit/renderer/ascii/separator_spec.rb +0 -28
  111. data/spec/unit/renderer/basic/alignment_spec.rb +0 -88
  112. data/spec/unit/renderer/basic/coloring_spec.rb +0 -61
  113. data/spec/unit/renderer/basic/extract_column_widths_spec.rb +0 -28
  114. data/spec/unit/renderer/basic/filter_spec.rb +0 -53
  115. data/spec/unit/renderer/basic/indentation_spec.rb +0 -48
  116. data/spec/unit/renderer/basic/multiline_spec.rb +0 -74
  117. data/spec/unit/renderer/basic/new_spec.rb +0 -26
  118. data/spec/unit/renderer/basic/options_spec.rb +0 -52
  119. data/spec/unit/renderer/basic/padding_spec.rb +0 -76
  120. data/spec/unit/renderer/basic/render_spec.rb +0 -57
  121. data/spec/unit/renderer/basic/resizing_spec.rb +0 -96
  122. data/spec/unit/renderer/basic/separator_spec.rb +0 -43
  123. data/spec/unit/renderer/basic/truncation_spec.rb +0 -35
  124. data/spec/unit/renderer/basic/wrapping_spec.rb +0 -40
  125. data/spec/unit/renderer/border_spec.rb +0 -104
  126. data/spec/unit/renderer/render_spec.rb +0 -36
  127. data/spec/unit/renderer/select_spec.rb +0 -22
  128. data/spec/unit/renderer/unicode/coloring_spec.rb +0 -70
  129. data/spec/unit/renderer/unicode/indentation_spec.rb +0 -41
  130. data/spec/unit/renderer/unicode/padding_spec.rb +0 -61
  131. data/spec/unit/renderer/unicode/render_spec.rb +0 -68
  132. data/spec/unit/renderer/unicode/separator_spec.rb +0 -26
  133. data/spec/unit/renderer_spec.rb +0 -19
  134. data/spec/unit/rotate_spec.rb +0 -86
  135. data/spec/unit/row/access_spec.rb +0 -25
  136. data/spec/unit/row/call_spec.rb +0 -45
  137. data/spec/unit/row/data_spec.rb +0 -26
  138. data/spec/unit/row/each_spec.rb +0 -31
  139. data/spec/unit/row/equality_spec.rb +0 -73
  140. data/spec/unit/row/height_spec.rb +0 -27
  141. data/spec/unit/row/new_spec.rb +0 -41
  142. data/spec/unit/row/to_ary_spec.rb +0 -14
  143. data/spec/unit/to_s_spec.rb +0 -56
  144. data/spec/unit/transformation/extract_tuples_spec.rb +0 -35
  145. data/spec/unit/utf_spec.rb +0 -33
  146. data/spec/unit/validatable/validate_options_spec.rb +0 -33
  147. data/spec/unit/validatable_spec.rb +0 -32
  148. data/tasks/console.rake +0 -10
  149. data/tasks/coverage.rake +0 -11
  150. data/tasks/spec.rake +0 -29
  151. data/tty-table.gemspec +0 -30
@@ -1,7 +1,8 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
- require 'tty/table/column_set'
4
- require 'tty/table/border/null'
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) * (table.columns_size - 1)
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 ? expand : renderer.column_widths
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 'The table size exceeds the currently set width.' \
107
- 'To avoid error either. Defaulting to vertical ' \
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
- ColumnSet.widths_from(table)
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 expand
118
- column_size = table.columns_size
119
- ratio = ((renderer.width - natural_width) / column_size.to_f).floor
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...column_size).reduce([]) do |lengths, col|
122
- lengths + [renderer.column_widths[col] + ratio]
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
- lengths + [renderer.column_widths[col] - ratio]
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
- fail ResizeError, "Table's width is too small to contain the content " \
149
- "(min width #{minimum_width}, currently set #{width})"
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
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
- require 'equatable'
3
+ require_relative "error"
4
4
 
5
5
  module TTY
6
6
  class Table
7
- # A class that represents table column properties.
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
- class ColumnSet
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
- find_maximas(colcount)
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
- # Assert data integrity for column widths
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] table_widths
40
+ # @param [Integer] index
41
+ # the column index
48
42
  #
49
- # @raise [TTY::InvalidArgument]
43
+ # @return [Integer]
50
44
  #
51
- # @api public
52
- def self.assert_widths(column_widths, table_widths)
53
- if column_widths.empty?
54
- fail InvalidArgument, 'Value for :column_widths must be ' \
55
- 'a non-empty array'
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 self.widths_from(table, column_widths = nil)
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
- new(table).extract_widths
70
+ extract_widths(table.data)
81
71
  else
82
- fail TypeError, 'Invalid type for column widths'
72
+ raise TypeError, "Invalid type for column widths"
83
73
  end
84
74
  end
75
+ module_function :widths_from
85
76
 
86
- private
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
- # @api private
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] index
112
- # the column index
81
+ # @param [Integer] table_widths
113
82
  #
114
- # @return [Integer]
83
+ # @raise [TTY::InvalidArgument]
115
84
  #
116
- # @api private
117
- def find_maximum(index)
118
- data.map do |row|
119
- (field = row.call(index)) ? field.length : 0
120
- end.max
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
- end # ColumnSet
96
+ module_function :assert_widths
97
+ end # Columns
123
98
  end # Table
124
99
  end # TTY
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module TTY
2
4
  class Table
3
5
  class Empty < TTY::Table
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module TTY
4
4
  class Table
@@ -1,8 +1,6 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
- require 'equatable'
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
- attr_reader :value
17
+ attr_accessor :value
22
18
 
23
19
  # The formatted value inside the field used for display
24
20
  #
25
21
  # @api public
26
- attr_reader :content
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 'a1'
42
+ # field = TTY::Table::Field.new "a1"
62
43
  # field.value # => a1
63
44
  #
64
45
  # @example
65
- # field = TTY::Table::Field.new value: 'a1'
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: 'a1', alignment: :center
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) { nil }
79
- @colspan = options.fetch(:colspan) { 1 }
80
- @rowspan = options.fetch(:rowspan) { 1 }
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(self.class.color.strip(line))
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 self.color
158
- @color ||= Pastel.new(enabled: true)
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 display_width(string)
163
- Unicode::DisplayWidth.of(string)
170
+ def hash
171
+ [self.class, value].hash
164
172
  end
165
173
  end # Field
166
174
  end # Table
@@ -1,7 +1,9 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
- require 'forwardable'
4
- require 'tty/table/field'
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
- fail UnknownAttributeError,
80
- "the header '#{header_name.value}' is unknown"
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
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module TTY
4
4
  class Table
5
- # A class responsible for indenting table representation
6
- class Indentation
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 [#to_a, #to_s] line
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 = line.is_a?(Array) ? line[0] : 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