tty 0.0.11 → 0.1.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.
- checksums.yaml +7 -0
- data/.rspec +1 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +16 -7
- data/CHANGELOG.md +6 -0
- data/Gemfile +18 -1
- data/README.md +55 -28
- data/Rakefile +5 -9
- data/images/tty.png +0 -0
- data/lib/tty.rb +7 -1
- data/lib/tty/coercer/boolean.rb +6 -9
- data/lib/tty/coercer/float.rb +14 -17
- data/lib/tty/coercer/integer.rb +14 -17
- data/lib/tty/coercer/range.rb +6 -7
- data/lib/tty/logger.rb +23 -9
- data/lib/tty/plugins.rb +3 -6
- data/lib/tty/plugins/plugin.rb +4 -7
- data/lib/tty/shell.rb +20 -12
- data/lib/tty/shell/question.rb +41 -48
- data/lib/tty/shell/question/modifier.rb +5 -8
- data/lib/tty/shell/question/validation.rb +8 -7
- data/lib/tty/shell/reader.rb +28 -19
- data/lib/tty/shell/response.rb +26 -22
- data/lib/tty/shell/statement.rb +13 -9
- data/lib/tty/shell/suggestion.rb +17 -13
- data/lib/tty/support/coercion.rb +5 -6
- data/lib/tty/support/conversion.rb +7 -6
- data/lib/tty/support/delegatable.rb +5 -5
- data/lib/tty/support/unicode.rb +3 -4
- data/lib/tty/support/utils.rb +1 -2
- data/lib/tty/system.rb +13 -6
- data/lib/tty/system/editor.rb +21 -18
- data/lib/tty/system/which.rb +3 -5
- data/lib/tty/table.rb +18 -22
- data/lib/tty/table/border.rb +2 -4
- data/lib/tty/table/border/ascii.rb +3 -2
- data/lib/tty/table/border/null.rb +1 -3
- data/lib/tty/table/border/row_line.rb +1 -4
- data/lib/tty/table/border/unicode.rb +3 -2
- data/lib/tty/table/border_dsl.rb +21 -17
- data/lib/tty/table/border_options.rb +6 -5
- data/lib/tty/table/column_set.rb +11 -7
- data/lib/tty/table/columns.rb +19 -16
- data/lib/tty/table/error.rb +1 -3
- data/lib/tty/table/field.rb +30 -12
- data/lib/tty/table/header.rb +18 -9
- data/lib/tty/table/indentation.rb +1 -3
- data/lib/tty/table/operation/alignment.rb +8 -9
- data/lib/tty/table/operation/alignment_set.rb +6 -5
- data/lib/tty/table/operation/escape.rb +3 -3
- data/lib/tty/table/operation/filter.rb +3 -4
- data/lib/tty/table/operation/truncation.rb +3 -3
- data/lib/tty/table/operation/wrapped.rb +3 -4
- data/lib/tty/table/operations.rb +19 -22
- data/lib/tty/table/orientation.rb +5 -5
- data/lib/tty/table/orientation/horizontal.rb +9 -6
- data/lib/tty/table/orientation/vertical.rb +10 -7
- data/lib/tty/table/padder.rb +46 -8
- data/lib/tty/table/renderer.rb +21 -14
- data/lib/tty/table/renderer/ascii.rb +6 -5
- data/lib/tty/table/renderer/basic.rb +18 -19
- data/lib/tty/table/renderer/unicode.rb +11 -5
- data/lib/tty/table/row.rb +36 -20
- data/lib/tty/table/transformation.rb +6 -5
- data/lib/tty/table/validatable.rb +32 -17
- data/lib/tty/terminal.rb +26 -42
- data/lib/tty/terminal/color.rb +10 -12
- data/lib/tty/terminal/echo.rb +4 -7
- data/lib/tty/terminal/home.rb +3 -10
- data/lib/tty/terminal/pager.rb +11 -10
- data/lib/tty/terminal/pager/basic.rb +3 -6
- data/lib/tty/terminal/pager/system.rb +2 -5
- data/lib/tty/text.rb +7 -10
- data/lib/tty/text/distance.rb +2 -4
- data/lib/tty/text/truncation.rb +1 -3
- data/lib/tty/text/wrapping.rb +1 -3
- data/lib/tty/vector.rb +3 -5
- data/lib/tty/version.rb +2 -2
- data/spec/spec_helper.rb +4 -11
- data/spec/tty/coercer/boolean/coerce_spec.rb +19 -16
- data/spec/tty/coercer/float/coerce_spec.rb +6 -4
- data/spec/tty/coercer/integer/coerce_spec.rb +5 -4
- data/spec/tty/coercer/range/coerce_spec.rb +12 -11
- data/spec/tty/logger/log_spec.rb +23 -0
- data/spec/tty/logger/new_spec.rb +14 -14
- data/spec/tty/logger/valid_level_spec.rb +6 -6
- data/spec/tty/plugins/find_spec.rb +4 -4
- data/spec/tty/plugins/load_spec.rb +4 -5
- data/spec/tty/plugins/plugin/load_spec.rb +10 -8
- data/spec/tty/plugins/plugin/new_spec.rb +7 -7
- data/spec/tty/shell/ask_spec.rb +6 -7
- data/spec/tty/shell/error_spec.rb +1 -2
- data/spec/tty/shell/print_table_spec.rb +1 -2
- data/spec/tty/shell/question/argument_spec.rb +1 -1
- data/spec/tty/shell/question/character_spec.rb +11 -3
- data/spec/tty/shell/question/default_spec.rb +3 -3
- data/spec/tty/shell/question/in_spec.rb +1 -1
- data/spec/tty/shell/question/initialize_spec.rb +8 -11
- data/spec/tty/shell/question/modifier/apply_to_spec.rb +8 -4
- data/spec/tty/shell/question/modifier/letter_case_spec.rb +4 -4
- data/spec/tty/shell/question/modifier/whitespace_spec.rb +5 -5
- data/spec/tty/shell/question/modify_spec.rb +1 -1
- data/spec/tty/shell/question/valid_spec.rb +4 -4
- data/spec/tty/shell/question/validate_spec.rb +4 -4
- data/spec/tty/shell/question/validation/coerce_spec.rb +2 -3
- data/spec/tty/shell/question/validation/valid_value_spec.rb +3 -3
- data/spec/tty/shell/reader/getc_spec.rb +16 -14
- data/spec/tty/shell/response/read_bool_spec.rb +1 -1
- data/spec/tty/shell/response/read_char_spec.rb +1 -2
- data/spec/tty/shell/response/read_date_spec.rb +1 -1
- data/spec/tty/shell/response/read_email_spec.rb +1 -1
- data/spec/tty/shell/response/read_multiple_spec.rb +3 -4
- data/spec/tty/shell/response/read_number_spec.rb +1 -2
- data/spec/tty/shell/response/read_range_spec.rb +2 -2
- data/spec/tty/shell/response/read_spec.rb +7 -7
- data/spec/tty/shell/response/read_string_spec.rb +2 -2
- data/spec/tty/shell/say_spec.rb +1 -1
- data/spec/tty/shell/statement/initialize_spec.rb +3 -3
- data/spec/tty/shell/suggest_spec.rb +1 -1
- data/spec/tty/shell/warn_spec.rb +1 -2
- data/spec/tty/support/coercion_spec.rb +10 -10
- data/spec/tty/support/conversion_spec.rb +9 -9
- data/spec/tty/support/delegatable_spec.rb +7 -5
- data/spec/tty/support/equatable_spec.rb +32 -34
- data/spec/tty/support/fixtures/classes.rb +4 -2
- data/spec/tty/system/editor/available_spec.rb +12 -12
- data/spec/tty/system/editor/build_spec.rb +8 -16
- data/spec/tty/system/editor/command_spec.rb +3 -2
- data/spec/tty/system/editor/executables_spec.rb +3 -3
- data/spec/tty/system/editor/invoke_spec.rb +10 -14
- data/spec/tty/system/editor/open_spec.rb +9 -7
- data/spec/tty/system/platform_spec.rb +32 -13
- data/spec/tty/system/which/which_spec.rb +9 -9
- data/spec/tty/system/which_spec.rb +4 -4
- data/spec/tty/table/access_spec.rb +24 -21
- data/spec/tty/table/add_row_spec.rb +2 -2
- data/spec/tty/table/border/ascii/rendering_spec.rb +13 -13
- data/spec/tty/table/border/new_spec.rb +5 -4
- data/spec/tty/table/border/null/rendering_spec.rb +12 -12
- data/spec/tty/table/border/options/from_spec.rb +2 -3
- data/spec/tty/table/border/options/new_spec.rb +4 -5
- data/spec/tty/table/border/unicode/rendering_spec.rb +10 -10
- data/spec/tty/table/border_options/new_spec.rb +20 -0
- data/spec/tty/table/border_options/update_spec.rb +18 -0
- data/spec/tty/table/column_set/extract_widths_spec.rb +1 -1
- data/spec/tty/table/column_set/total_width_spec.rb +2 -2
- data/spec/tty/table/column_set/widths_from_spec.rb +1 -2
- data/spec/tty/table/columns/enforce_spec.rb +2 -2
- data/spec/tty/table/columns/widths_spec.rb +2 -0
- data/spec/tty/table/data_spec.rb +3 -3
- data/spec/tty/table/each_spec.rb +9 -11
- data/spec/tty/table/each_with_index_spec.rb +8 -9
- data/spec/tty/table/empty_spec.rb +3 -3
- data/spec/tty/table/eql_spec.rb +6 -6
- data/spec/tty/table/field/equality_spec.rb +11 -11
- data/spec/tty/table/field/length_spec.rb +4 -4
- data/spec/tty/table/field/lines_spec.rb +3 -3
- data/spec/tty/table/field/new_spec.rb +7 -7
- data/spec/tty/table/field/width_spec.rb +6 -4
- data/spec/tty/table/filter_spec.rb +3 -3
- data/spec/tty/table/header/call_spec.rb +3 -3
- data/spec/tty/table/header/height_spec.rb +1 -1
- data/spec/tty/table/header/new_spec.rb +5 -5
- data/spec/tty/table/header/set_spec.rb +9 -4
- data/spec/tty/table/header/to_ary_spec.rb +4 -4
- data/spec/tty/table/header_spec.rb +3 -4
- data/spec/tty/table/indentation/insert_indent_spec.rb +1 -1
- data/spec/tty/table/initialize_spec.rb +13 -13
- data/spec/tty/table/operation/alignment/format_spec.rb +5 -5
- data/spec/tty/table/operation/alignment/new_spec.rb +4 -4
- data/spec/tty/table/operation/alignment_set/call_spec.rb +11 -11
- data/spec/tty/table/operation/alignment_set/each_spec.rb +3 -3
- data/spec/tty/table/operation/alignment_set/new_spec.rb +6 -6
- data/spec/tty/table/operation/alignment_set/to_ary_spec.rb +4 -4
- data/spec/tty/table/operation/escape/call_spec.rb +1 -1
- data/spec/tty/table/operation/filter/call_spec.rb +1 -1
- data/spec/tty/table/operation/truncation/call_spec.rb +1 -1
- data/spec/tty/table/operation/truncation/truncate_spec.rb +4 -4
- data/spec/tty/table/operation/wrapped/call_spec.rb +1 -1
- data/spec/tty/table/operation/wrapped/wrap_spec.rb +3 -3
- data/spec/tty/table/operations/new_spec.rb +1 -1
- data/spec/tty/table/options_spec.rb +8 -8
- data/spec/tty/table/orientation_spec.rb +15 -13
- data/spec/tty/table/padder/parse_spec.rb +1 -1
- data/spec/tty/table/padder/to_s_spec.rb +14 -0
- data/spec/tty/table/padding_spec.rb +1 -1
- data/spec/tty/table/properties_spec.rb +9 -8
- data/spec/tty/table/render_spec.rb +8 -5
- data/spec/tty/table/render_with_spec.rb +5 -4
- data/spec/tty/table/renderer/ascii/indentation_spec.rb +1 -1
- data/spec/tty/table/renderer/ascii/padding_spec.rb +1 -1
- data/spec/tty/table/renderer/ascii/render_spec.rb +7 -7
- data/spec/tty/table/renderer/ascii/resizing_spec.rb +1 -1
- data/spec/tty/table/renderer/ascii/separator_spec.rb +2 -2
- data/spec/tty/table/renderer/basic/alignment_spec.rb +5 -5
- data/spec/tty/table/renderer/basic/coloring_spec.rb +1 -1
- data/spec/tty/table/renderer/basic/extract_column_widths_spec.rb +6 -6
- data/spec/tty/table/renderer/basic/filter_spec.rb +4 -4
- data/spec/tty/table/renderer/basic/indentation_spec.rb +5 -3
- data/spec/tty/table/renderer/basic/multiline_content_spec.rb +10 -10
- data/spec/tty/table/renderer/basic/new_spec.rb +2 -2
- data/spec/tty/table/renderer/basic/options_spec.rb +9 -9
- data/spec/tty/table/renderer/basic/padding_spec.rb +1 -1
- data/spec/tty/table/renderer/basic/render_spec.rb +8 -8
- data/spec/tty/table/renderer/basic/resizing_spec.rb +1 -1
- data/spec/tty/table/renderer/basic/separator_spec.rb +2 -2
- data/spec/tty/table/renderer/basic/truncation_spec.rb +4 -4
- data/spec/tty/table/renderer/basic/wrapping_spec.rb +4 -4
- data/spec/tty/table/renderer/border_spec.rb +21 -21
- data/spec/tty/table/renderer/render_spec.rb +7 -7
- data/spec/tty/table/renderer/select_spec.rb +4 -4
- data/spec/tty/table/renderer/style_spec.rb +19 -19
- data/spec/tty/table/renderer/unicode/indentation_spec.rb +1 -1
- data/spec/tty/table/renderer/unicode/padding_spec.rb +1 -1
- data/spec/tty/table/renderer/unicode/render_spec.rb +6 -6
- data/spec/tty/table/renderer/unicode/separator_spec.rb +2 -2
- data/spec/tty/table/renderer_spec.rb +1 -1
- data/spec/tty/table/rotate_spec.rb +27 -27
- data/spec/tty/table/row/access_spec.rb +3 -3
- data/spec/tty/table/row/call_spec.rb +4 -4
- data/spec/tty/table/row/data_spec.rb +5 -5
- data/spec/tty/table/row/each_spec.rb +1 -1
- data/spec/tty/table/row/equality_spec.rb +16 -16
- data/spec/tty/table/row/height_spec.rb +1 -1
- data/spec/tty/table/row/new_spec.rb +11 -11
- data/spec/tty/table/row/to_ary_spec.rb +3 -3
- data/spec/tty/table/to_s_spec.rb +8 -8
- data/spec/tty/table/validatable/validate_options_spec.rb +5 -5
- data/spec/tty/table/validatable_spec.rb +14 -1
- data/spec/tty/terminal/color/code_spec.rb +3 -3
- data/spec/tty/terminal/color/remove_spec.rb +8 -9
- data/spec/tty/terminal/color/set_spec.rb +5 -6
- data/spec/tty/terminal/color_spec.rb +3 -5
- data/spec/tty/terminal/echo_spec.rb +3 -4
- data/spec/tty/terminal/home_spec.rb +12 -14
- data/spec/tty/terminal/page_spec.rb +5 -4
- data/spec/tty/terminal/pager/available_spec.rb +11 -11
- data/spec/tty/terminal/pager/basic/page_spec.rb +8 -8
- data/spec/tty/terminal/pager/command_spec.rb +2 -2
- data/spec/tty/terminal/pager/executables_spec.rb +3 -3
- data/spec/tty/terminal/pager/page_spec.rb +8 -10
- data/spec/tty/terminal/pager/system/page_spec.rb +8 -8
- data/spec/tty/terminal/size_spec.rb +39 -36
- data/spec/tty/text/distance/distance_spec.rb +13 -13
- data/spec/tty/text/distance/initialize_spec.rb +4 -4
- data/spec/tty/text/distance_spec.rb +2 -2
- data/spec/tty/text/truncate_spec.rb +2 -3
- data/spec/tty/text/truncation/initialize_spec.rb +9 -9
- data/spec/tty/text/truncation/truncate_spec.rb +14 -14
- data/spec/tty/text/wrap_spec.rb +2 -3
- data/spec/tty/text/wrapping/initialize_spec.rb +7 -7
- data/spec/tty/text/wrapping/wrap_spec.rb +11 -11
- data/spec/tty/tty_spec.rb +22 -0
- data/spec/tty/vector/new_spec.rb +7 -7
- data/tasks/console.rake +10 -0
- data/tasks/coverage.rake +11 -0
- data/tasks/metrics/cane.rake +2 -0
- data/tasks/metrics/flog.rake +2 -0
- data/tasks/metrics/heckle.rake +15 -0
- data/tasks/metrics/reek.rake +2 -0
- data/tasks/spec.rake +29 -0
- data/tty.gemspec +5 -12
- metadata +34 -91
- data/.rvmrc +0 -34
data/lib/tty/table/border.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
|
-
|
6
5
|
# Abstract base class that is responsible for building the table border.
|
7
6
|
class Border
|
8
7
|
include Unicode
|
@@ -43,7 +42,7 @@ module TTY
|
|
43
42
|
# @api private
|
44
43
|
def initialize(column_widths, options = nil)
|
45
44
|
if self.class == Border
|
46
|
-
|
45
|
+
fail NotImplementedError, "#{self} is an abstract class"
|
47
46
|
else
|
48
47
|
@widths = column_widths
|
49
48
|
@border = TTY::Table::BorderOptions.from options
|
@@ -215,7 +214,6 @@ module TTY
|
|
215
214
|
left + widths.map { |width| line * width }.join(intersection) + right
|
216
215
|
end
|
217
216
|
end
|
218
|
-
|
219
217
|
end # Border
|
220
218
|
end # Table
|
221
219
|
end # TTY
|
@@ -1,9 +1,8 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
5
|
class Border
|
6
|
-
|
7
6
|
# A class that represents no border.
|
8
7
|
class Null < Border
|
9
8
|
|
@@ -32,7 +31,6 @@ module TTY
|
|
32
31
|
def bottom_line
|
33
32
|
border ? super : nil
|
34
33
|
end
|
35
|
-
|
36
34
|
end # Null
|
37
35
|
end # Border
|
38
36
|
end # Table
|
@@ -1,12 +1,10 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
5
|
class Border
|
6
|
-
|
7
6
|
# A class for a table row line chars manipulation
|
8
7
|
class RowLine < Struct.new(:left, :center, :right)
|
9
|
-
|
10
8
|
# Colorize characters with a given style
|
11
9
|
#
|
12
10
|
# @api public
|
@@ -14,7 +12,6 @@ module TTY
|
|
14
12
|
colorized_chars = Border.set_color(style, right, center, left)
|
15
13
|
self.right, self.center, self.left = colorized_chars
|
16
14
|
end
|
17
|
-
|
18
15
|
end # RowLine
|
19
16
|
end # Border
|
20
17
|
end # Table
|
data/lib/tty/table/border_dsl.rb
CHANGED
@@ -1,15 +1,21 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
|
-
|
6
5
|
# A class responsible for bulding and modifying border
|
6
|
+
#
|
7
|
+
# Used internally by {Table::Border} to allow for building custom border
|
8
|
+
# through DSL
|
9
|
+
# @api private
|
7
10
|
class BorderDSL
|
8
11
|
extend TTY::Delegatable
|
9
12
|
|
13
|
+
# Border options
|
14
|
+
#
|
15
|
+
# @return [Table::BorderOptions]
|
10
16
|
attr_reader :options
|
11
17
|
|
12
|
-
delegatable_method :options, :characters, :style
|
18
|
+
delegatable_method :options, :characters, :style
|
13
19
|
|
14
20
|
# Initialize a BorderDSL
|
15
21
|
#
|
@@ -19,7 +25,7 @@ module TTY
|
|
19
25
|
# @return [undefined]
|
20
26
|
#
|
21
27
|
# @api private
|
22
|
-
def initialize(characters=nil, &block)
|
28
|
+
def initialize(characters = nil, &block)
|
23
29
|
@options = TTY::Table::BorderOptions.new
|
24
30
|
@options.characters = characters if characters
|
25
31
|
yield_or_eval(&block) if block_given?
|
@@ -46,7 +52,7 @@ module TTY
|
|
46
52
|
#
|
47
53
|
# @api public
|
48
54
|
def separator(value)
|
49
|
-
options.separator = value
|
55
|
+
value ? options.separator = value : options.separator
|
50
56
|
end
|
51
57
|
|
52
58
|
# Set top border character
|
@@ -229,19 +235,17 @@ module TTY
|
|
229
235
|
options.characters['right'] = value
|
230
236
|
end
|
231
237
|
|
232
|
-
|
233
|
-
|
234
|
-
# Evaluate block
|
235
|
-
#
|
236
|
-
# @return [Table]
|
237
|
-
#
|
238
|
-
# @api private
|
239
|
-
def yield_or_eval(&block)
|
240
|
-
return unless block
|
241
|
-
block.arity > 0 ? yield(self) : self.instance_eval(&block)
|
242
|
-
end
|
238
|
+
private
|
243
239
|
|
240
|
+
# Evaluate block
|
241
|
+
#
|
242
|
+
# @return [Table]
|
243
|
+
#
|
244
|
+
# @api private
|
245
|
+
def yield_or_eval(&block)
|
246
|
+
return unless block
|
247
|
+
block.arity > 0 ? yield(self) : instance_eval(&block)
|
248
|
+
end
|
244
249
|
end # BorderDSL
|
245
|
-
|
246
250
|
end # Table
|
247
251
|
end # TTY
|
@@ -1,11 +1,13 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
|
-
|
6
5
|
# A class that represents table border options
|
6
|
+
#
|
7
|
+
# Used internally by {Table::Border} to manage options such as style
|
8
|
+
#
|
9
|
+
# @api private
|
7
10
|
class BorderOptions < Struct.new(:characters, :separator, :style)
|
8
|
-
|
9
11
|
# Initialize a BorderOptions
|
10
12
|
#
|
11
13
|
# @api public
|
@@ -30,7 +32,7 @@ module TTY
|
|
30
32
|
# @api public
|
31
33
|
def update(obj)
|
32
34
|
obj.each_pair do |key, value|
|
33
|
-
|
35
|
+
send("#{key}=", value)
|
34
36
|
end
|
35
37
|
self
|
36
38
|
end
|
@@ -46,7 +48,6 @@ module TTY
|
|
46
48
|
end
|
47
49
|
hash
|
48
50
|
end
|
49
|
-
|
50
51
|
end # BorderOptions
|
51
52
|
end # Table
|
52
53
|
end # TTY
|
data/lib/tty/table/column_set.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
|
-
|
6
|
-
#
|
5
|
+
# A class that represents table column properties.
|
6
|
+
#
|
7
|
+
# Used by {Table} to manage column sizing.
|
8
|
+
#
|
9
|
+
# @api private
|
7
10
|
class ColumnSet
|
8
11
|
include Equatable
|
9
12
|
|
@@ -48,10 +51,12 @@ module TTY
|
|
48
51
|
# @api public
|
49
52
|
def self.assert_widths(column_widths, table_widths)
|
50
53
|
if column_widths.empty?
|
51
|
-
|
54
|
+
fail InvalidArgument, 'Value for :column_widths must be ' \
|
55
|
+
'a non-empty array'
|
52
56
|
end
|
53
57
|
if column_widths.size != table_widths
|
54
|
-
|
58
|
+
fail InvalidArgument, 'Value for :column_widths must match ' \
|
59
|
+
'table column count'
|
55
60
|
end
|
56
61
|
end
|
57
62
|
|
@@ -74,7 +79,7 @@ module TTY
|
|
74
79
|
when NilClass
|
75
80
|
new(table).extract_widths
|
76
81
|
else
|
77
|
-
|
82
|
+
fail TypeError, 'Invalid type for column widths'
|
78
83
|
end
|
79
84
|
end
|
80
85
|
|
@@ -111,7 +116,6 @@ module TTY
|
|
111
116
|
def self.find_maximum(data, index)
|
112
117
|
data.map { |row| (value = row.call(index)) ? value.length : 0 }.max
|
113
118
|
end
|
114
|
-
|
115
119
|
end # ColumnSet
|
116
120
|
end # Table
|
117
121
|
end # TTY
|
data/lib/tty/table/columns.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
|
-
|
6
|
-
#
|
5
|
+
# A class responsible for enforcing column constraints.
|
6
|
+
#
|
7
|
+
# Used internally by {Renderer::Basic} to enforce correct column widths.
|
8
|
+
#
|
9
|
+
# @api private
|
7
10
|
class Columns
|
8
11
|
|
9
12
|
attr_reader :table
|
@@ -16,7 +19,7 @@ module TTY
|
|
16
19
|
|
17
20
|
# Initialize a Columns
|
18
21
|
#
|
19
|
-
# @param [TTY::Table::Renderer]
|
22
|
+
# @param [TTY::Table::Renderer] renderer
|
20
23
|
#
|
21
24
|
# @api public
|
22
25
|
def initialize(renderer)
|
@@ -60,14 +63,16 @@ module TTY
|
|
60
63
|
renderer.column_widths.inject(0, &:+) + border_size
|
61
64
|
end
|
62
65
|
|
63
|
-
# Return the constrained column widths.
|
64
|
-
#
|
66
|
+
# Return the constrained column widths.
|
67
|
+
#
|
68
|
+
# Account for table field widths and any user defined
|
69
|
+
# constraints on the table width.
|
65
70
|
#
|
66
71
|
# @api public
|
67
72
|
def enforce
|
68
73
|
assert_minimum_width
|
69
74
|
|
70
|
-
|
75
|
+
unless renderer.padding.empty?
|
71
76
|
renderer.column_widths = adjust_padding
|
72
77
|
end
|
73
78
|
|
@@ -99,9 +104,10 @@ module TTY
|
|
99
104
|
#
|
100
105
|
# @api private
|
101
106
|
def rotate
|
102
|
-
TTY.shell.warn 'The table size exceeds the currently set width.'
|
103
|
-
|
104
|
-
|
107
|
+
TTY.shell.warn 'The table size exceeds the currently set width.' \
|
108
|
+
'To avoid error either. Defaulting to vertical ' \
|
109
|
+
'orientation.'
|
110
|
+
table.orientation = :vertical
|
105
111
|
table.rotate
|
106
112
|
end
|
107
113
|
|
@@ -138,11 +144,9 @@ module TTY
|
|
138
144
|
# @api private
|
139
145
|
def assert_minimum_width
|
140
146
|
width = renderer.width
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
"(min width #{minimum_width}, currently set #{width})"
|
145
|
-
end
|
147
|
+
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})"
|
146
150
|
end
|
147
151
|
|
148
152
|
# Distribute remaining width to meet the total width requirement.
|
@@ -161,7 +165,6 @@ module TTY
|
|
161
165
|
width + per_field_width + extra[index]
|
162
166
|
end
|
163
167
|
end
|
164
|
-
|
165
168
|
end # Columns
|
166
169
|
end # Table
|
167
170
|
end # TTY
|
data/lib/tty/table/error.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
|
-
|
6
5
|
# Raised when inserting into table with a mismatching row(s)
|
7
6
|
class DimensionMismatchError < ArgumentError; end
|
8
7
|
|
@@ -15,6 +14,5 @@ module TTY
|
|
15
14
|
super("element at(#{i},#{j}) not found")
|
16
15
|
end
|
17
16
|
end
|
18
|
-
|
19
17
|
end # Table
|
20
18
|
end # TTY
|
data/lib/tty/table/field.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
|
-
|
6
5
|
# A class that represents a unique element in a table.
|
6
|
+
#
|
7
|
+
# Used internally by {Table::Row} to define internal structure.
|
8
|
+
#
|
9
|
+
# @api private
|
7
10
|
class Field
|
8
11
|
include Equatable
|
9
12
|
|
@@ -12,6 +15,11 @@ module TTY
|
|
12
15
|
# @api public
|
13
16
|
attr_reader :value
|
14
17
|
|
18
|
+
# The name for the value
|
19
|
+
#
|
20
|
+
# @api public
|
21
|
+
attr_reader :name
|
22
|
+
|
15
23
|
# TODO: Change to :content to separate value from formatted string
|
16
24
|
attr_writer :value
|
17
25
|
|
@@ -33,18 +41,31 @@ module TTY
|
|
33
41
|
# Initialize a Field
|
34
42
|
#
|
35
43
|
# @example
|
36
|
-
# field =
|
44
|
+
# field = TTY::Table::Field.new 'a1'
|
37
45
|
# field.value # => a1
|
38
46
|
#
|
39
|
-
#
|
47
|
+
# @example
|
48
|
+
# field = TTY::Table::Field.new value: 'a1'
|
40
49
|
# field.value # => a1
|
41
50
|
#
|
42
|
-
#
|
51
|
+
# @example
|
52
|
+
# field = TTY::Table::Field.new value: 'a1', align: :center
|
43
53
|
# field.value # => a1
|
44
54
|
# field.align # => :center
|
45
55
|
#
|
46
56
|
# @api private
|
47
57
|
def initialize(value)
|
58
|
+
options = extract_options(value)
|
59
|
+
@width = options.fetch(:width) { @value.to_s.size }
|
60
|
+
@align = options.fetch(:align) { nil }
|
61
|
+
@colspan = options.fetch(:colspan) { 1 }
|
62
|
+
@rowspan = options.fetch(:rowspan) { 1 }
|
63
|
+
end
|
64
|
+
|
65
|
+
# Extract options and set value
|
66
|
+
#
|
67
|
+
# @api private
|
68
|
+
def extract_options(value)
|
48
69
|
if value.class <= Hash
|
49
70
|
options = value
|
50
71
|
@value = options.fetch(:value)
|
@@ -52,10 +73,7 @@ module TTY
|
|
52
73
|
@value = value
|
53
74
|
options = {}
|
54
75
|
end
|
55
|
-
|
56
|
-
@align = options.fetch(:align) { nil }
|
57
|
-
@colspan = options.fetch(:colspan) { 1 }
|
58
|
-
@rowspan = options.fetch(:rowspan) { 1 }
|
76
|
+
options
|
59
77
|
end
|
60
78
|
|
61
79
|
# Return the width this field would normally have bar other contraints
|
@@ -69,8 +87,9 @@ module TTY
|
|
69
87
|
@height
|
70
88
|
end
|
71
89
|
|
72
|
-
# Return number of lines this value spans.
|
73
|
-
#
|
90
|
+
# Return number of lines this value spans.
|
91
|
+
#
|
92
|
+
# A distinction is being made between escaped and non-escaped strings.
|
74
93
|
#
|
75
94
|
# @return [Array[String]]
|
76
95
|
#
|
@@ -115,7 +134,6 @@ module TTY
|
|
115
134
|
def to_s
|
116
135
|
value
|
117
136
|
end
|
118
|
-
|
119
137
|
end # Field
|
120
138
|
end # Table
|
121
139
|
end # TTY
|
data/lib/tty/table/header.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
require 'tty/vector'
|
4
4
|
require 'forwardable'
|
5
5
|
|
6
6
|
module TTY
|
7
7
|
class Table
|
8
|
-
|
9
8
|
# Convert an Array row into Header
|
10
9
|
#
|
11
10
|
# @return [TTY::Table::Header]
|
@@ -15,7 +14,7 @@ module TTY
|
|
15
14
|
Header.new(row)
|
16
15
|
end
|
17
16
|
|
18
|
-
# A set of header elements that correspond
|
17
|
+
# A set of header elements that correspond to values in each row
|
19
18
|
class Header < Vector
|
20
19
|
include Equatable
|
21
20
|
extend Forwardable
|
@@ -35,20 +34,26 @@ module TTY
|
|
35
34
|
# @return [undefined]
|
36
35
|
#
|
37
36
|
# @api public
|
38
|
-
def initialize(attributes=[])
|
37
|
+
def initialize(attributes = [])
|
39
38
|
@attributes = attributes.map { |attr| to_field(attr) }
|
40
39
|
@attribute_for = Hash[@attributes.each_with_index.map.to_a]
|
41
40
|
end
|
42
41
|
|
43
42
|
# Instantiates a new field
|
44
43
|
#
|
44
|
+
# @param [String,Hash] attribute
|
45
|
+
# the attribute value to convert to field object
|
46
|
+
#
|
45
47
|
# @api public
|
46
|
-
def to_field(
|
47
|
-
Field.new(
|
48
|
+
def to_field(attribute = nil)
|
49
|
+
Field.new(attribute)
|
48
50
|
end
|
49
51
|
|
50
52
|
# Lookup a column in the header given a name
|
51
53
|
#
|
54
|
+
# @param [Integer, String] attribute
|
55
|
+
# the attribute to look up by
|
56
|
+
#
|
52
57
|
# @api public
|
53
58
|
def [](attribute)
|
54
59
|
case attribute
|
@@ -56,7 +61,8 @@ module TTY
|
|
56
61
|
@attributes[attribute].value
|
57
62
|
else
|
58
63
|
@attribute_for.fetch(to_field(attribute)) do |header_name|
|
59
|
-
|
64
|
+
fail UnknownAttributeError,
|
65
|
+
"the header '#{header_name.value}' is unknown"
|
60
66
|
end
|
61
67
|
end
|
62
68
|
end
|
@@ -75,11 +81,13 @@ module TTY
|
|
75
81
|
#
|
76
82
|
# @api public
|
77
83
|
def []=(attribute, value)
|
78
|
-
|
84
|
+
attributes[attribute] = to_field(value)
|
79
85
|
end
|
80
86
|
|
81
87
|
# Size of the header
|
82
88
|
#
|
89
|
+
# @return [Integer]
|
90
|
+
#
|
83
91
|
# @api public
|
84
92
|
def size
|
85
93
|
to_ary.size
|
@@ -97,6 +105,8 @@ module TTY
|
|
97
105
|
|
98
106
|
# Convert the Header into an Array
|
99
107
|
#
|
108
|
+
# @return [Array]
|
109
|
+
#
|
100
110
|
# @api public
|
101
111
|
def to_ary
|
102
112
|
attributes.map { |attr| attr.value if attr }
|
@@ -118,7 +128,6 @@ module TTY
|
|
118
128
|
def to_hash
|
119
129
|
to_a.hash
|
120
130
|
end
|
121
|
-
|
122
131
|
end # Header
|
123
132
|
end # Table
|
124
133
|
end # TTY
|