tty 0.0.11 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- 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/renderer.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 rendering tabular data
|
6
|
+
#
|
7
|
+
# Used internally by {Table} to render table content out.
|
8
|
+
#
|
9
|
+
# @api private
|
7
10
|
class Renderer
|
8
11
|
autoload :ASCII, 'tty/table/renderer/ascii'
|
9
12
|
autoload :Basic, 'tty/table/renderer/basic'
|
@@ -11,22 +14,25 @@ module TTY
|
|
11
14
|
autoload :Unicode, 'tty/table/renderer/unicode'
|
12
15
|
|
13
16
|
RENDERER_MAPPER = {
|
14
|
-
:
|
15
|
-
:
|
16
|
-
:
|
17
|
-
:
|
17
|
+
ascii: TTY::Table::Renderer::ASCII,
|
18
|
+
basic: TTY::Table::Renderer::Basic,
|
19
|
+
color: TTY::Table::Renderer::Color,
|
20
|
+
unicode: TTY::Table::Renderer::Unicode
|
18
21
|
}
|
19
22
|
|
20
|
-
# Select renderer class based on string name
|
23
|
+
# Select renderer class based on string name.
|
24
|
+
#
|
25
|
+
# The possible values for renderer are
|
26
|
+
# [:basic, :ascii, :unicode, :color]
|
21
27
|
#
|
22
28
|
# @param [Symbol] renderer
|
23
|
-
# the renderer used for displaying table
|
29
|
+
# the renderer used for displaying table
|
24
30
|
#
|
25
31
|
# @return [TTY::Table::Renderer]
|
26
32
|
#
|
27
33
|
# @api private
|
28
34
|
def self.select(type)
|
29
|
-
|
35
|
+
RENDERER_MAPPER[type || :basic]
|
30
36
|
end
|
31
37
|
|
32
38
|
# Raises an error if provided border class is of wrong type or has invalid
|
@@ -42,10 +48,12 @@ module TTY
|
|
42
48
|
def self.assert_border_class(border_class)
|
43
49
|
return unless border_class
|
44
50
|
unless border_class <= TTY::Table::Border
|
45
|
-
|
51
|
+
fail TypeError,
|
52
|
+
"#{border_class} should inherit from TTY::Table::Border"
|
46
53
|
end
|
47
54
|
unless border_class.characters
|
48
|
-
|
55
|
+
fail NoImplementationError,
|
56
|
+
"#{border_class} should implement def_border"
|
49
57
|
end
|
50
58
|
end
|
51
59
|
|
@@ -64,7 +72,7 @@ module TTY
|
|
64
72
|
# raise if the klass does not implement def_border
|
65
73
|
#
|
66
74
|
# @api public
|
67
|
-
def self.render_with(border_class, table, options={}, &block)
|
75
|
+
def self.render_with(border_class, table, options = {}, &block)
|
68
76
|
assert_border_class(border_class)
|
69
77
|
options[:border_class] = border_class if border_class
|
70
78
|
render(table, options, &block)
|
@@ -83,12 +91,11 @@ module TTY
|
|
83
91
|
# @return [String]
|
84
92
|
#
|
85
93
|
# @api public
|
86
|
-
def self.render(table, options={}, &block)
|
94
|
+
def self.render(table, options = {}, &block)
|
87
95
|
renderer = select(options[:renderer]).new(table, options)
|
88
96
|
yield renderer if block_given?
|
89
97
|
renderer.render
|
90
98
|
end
|
91
99
|
end # Renderer
|
92
|
-
|
93
100
|
end # Table
|
94
101
|
end # TTY
|
@@ -1,14 +1,15 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
5
|
class Renderer
|
6
6
|
class ASCII < Basic
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
# Create ASCII renderer
|
8
|
+
#
|
9
|
+
# @api private
|
10
|
+
def initialize(table, options = {})
|
11
|
+
super(table, options.merge(border_class: TTY::Table::Border::ASCII))
|
10
12
|
end
|
11
|
-
|
12
13
|
end # ASCII
|
13
14
|
end # Renderer
|
14
15
|
end # Table
|
@@ -1,12 +1,13 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
require 'tty/table/validatable'
|
4
4
|
|
5
5
|
module TTY
|
6
6
|
class Table
|
7
7
|
class Renderer
|
8
|
-
|
9
8
|
# Renders table without any border styles.
|
9
|
+
#
|
10
|
+
# @api private
|
10
11
|
class Basic
|
11
12
|
include TTY::Table::Validatable
|
12
13
|
|
@@ -30,7 +31,7 @@ module TTY
|
|
30
31
|
# @return [Array]
|
31
32
|
#
|
32
33
|
# @api public
|
33
|
-
|
34
|
+
attr_writer :column_widths
|
34
35
|
|
35
36
|
# The table column alignments
|
36
37
|
#
|
@@ -71,9 +72,9 @@ module TTY
|
|
71
72
|
# @api public
|
72
73
|
attr_accessor :width
|
73
74
|
|
74
|
-
# The table resizing behaviour. If true the algorithm will
|
75
|
-
# expand or shrink table to fit the terminal
|
76
|
-
# By default its false.
|
75
|
+
# The table resizing behaviour. If true the algorithm will
|
76
|
+
# automatically expand or shrink table to fit the terminal
|
77
|
+
# width or specified width. By default its false.
|
77
78
|
#
|
78
79
|
# @return [Integer]
|
79
80
|
#
|
@@ -103,24 +104,23 @@ module TTY
|
|
103
104
|
#
|
104
105
|
# @api private
|
105
106
|
def initialize(table, options = {})
|
106
|
-
@table =
|
107
|
+
@table = assert_table_type(table)
|
107
108
|
@multiline = options.fetch(:multiline) { false }
|
108
109
|
@operations = TTY::Table::Operations.new(table)
|
109
|
-
|
110
|
-
@operations.add(:escape, Operation::Escape.new)
|
111
|
-
end
|
110
|
+
@operations.add(:escape, Operation::Escape.new)
|
112
111
|
@border = TTY::Table::BorderOptions.from(options.delete(:border))
|
113
112
|
@column_widths = options.fetch(:column_widths, nil)
|
114
113
|
@column_aligns = Array(options.delete(:column_aligns)).map(&:to_sym)
|
115
|
-
@filter = options.fetch(:filter) { proc { |val,
|
114
|
+
@filter = options.fetch(:filter) { proc { |val, _| val } }
|
116
115
|
@width = options.fetch(:width) { TTY.terminal.width }
|
117
116
|
@border_class = options.fetch(:border_class) { Border::Null }
|
118
117
|
@indent = options.fetch(:indent) { 0 }
|
119
118
|
@resize = options.fetch(:resize) { false }
|
120
|
-
@padding = TTY::Table::Padder.parse(options
|
119
|
+
@padding = TTY::Table::Padder.parse(options[:padding])
|
121
120
|
end
|
122
121
|
|
123
|
-
# Parses supplied column widths, if not present
|
122
|
+
# Parses supplied column widths, if not present
|
123
|
+
# calculates natural widths.
|
124
124
|
#
|
125
125
|
# @return [Array[Integer]]
|
126
126
|
#
|
@@ -131,9 +131,10 @@ module TTY
|
|
131
131
|
|
132
132
|
# Store border characters, style and separator for the table rendering
|
133
133
|
#
|
134
|
-
# @param [Hash, BorderOptions] options
|
134
|
+
# @param [Hash, Table::BorderOptions] options
|
135
135
|
#
|
136
|
-
# @yield []
|
136
|
+
# @yield [Table::BorderOptions]
|
137
|
+
# block representing border options
|
137
138
|
#
|
138
139
|
# @api public
|
139
140
|
def border(options=(not_set=true), &block)
|
@@ -222,10 +223,9 @@ module TTY
|
|
222
223
|
data_border = border_class.new(column_widths, border)
|
223
224
|
header = render_header(first_row, data_border)
|
224
225
|
rows_with_border = render_rows(data_border)
|
226
|
+
bottom_line = data_border.bottom_line
|
225
227
|
|
226
|
-
|
227
|
-
insert_indent(bottom_line)
|
228
|
-
end
|
228
|
+
insert_indent(bottom_line) if bottom_line
|
229
229
|
|
230
230
|
[header, rows_with_border, bottom_line].compact
|
231
231
|
end
|
@@ -288,7 +288,6 @@ module TTY
|
|
288
288
|
insert_indent(row_line)
|
289
289
|
end
|
290
290
|
end
|
291
|
-
|
292
291
|
end # Basic
|
293
292
|
end # Renderer
|
294
293
|
end # Table
|
@@ -1,14 +1,20 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
5
|
class Renderer
|
6
|
+
# Unicode representation of table renderer
|
7
|
+
#
|
8
|
+
# @api private
|
6
9
|
class Unicode < Basic
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
+
# Create Unicode renderer
|
11
|
+
#
|
12
|
+
# @param [Table] table
|
13
|
+
#
|
14
|
+
# @api private
|
15
|
+
def initialize(table, options = {})
|
16
|
+
super(table, options.merge(border_class: TTY::Table::Border::Unicode))
|
10
17
|
end
|
11
|
-
|
12
18
|
end # Unicode
|
13
19
|
end # Renderer
|
14
20
|
end # Table
|
data/lib/tty/table/row.rb
CHANGED
@@ -1,27 +1,29 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
require 'tty/vector'
|
4
4
|
|
5
5
|
module TTY
|
6
6
|
class Table
|
7
|
-
|
8
7
|
# Convert an Array row into Row
|
9
8
|
#
|
10
9
|
# @return [TTY::Table::Row]
|
11
10
|
#
|
12
11
|
# @api private
|
13
|
-
def to_row(row, header=nil)
|
12
|
+
def to_row(row, header = nil)
|
14
13
|
Row.new(row, header)
|
15
14
|
end
|
16
15
|
|
17
16
|
# A class that represents a row in a table.
|
17
|
+
#
|
18
|
+
# Used internally by {Table} to store row represenation by converting
|
19
|
+
# {Array} into {Row} instance.
|
20
|
+
#
|
21
|
+
# @api private
|
18
22
|
class Row < Vector
|
19
23
|
include Equatable
|
20
24
|
extend Forwardable
|
21
25
|
|
22
|
-
|
23
|
-
|
24
|
-
# The row attributes
|
26
|
+
# The row attributes that describe each element
|
25
27
|
#
|
26
28
|
# @return [Array]
|
27
29
|
#
|
@@ -35,8 +37,13 @@ module TTY
|
|
35
37
|
# @api private
|
36
38
|
attr_reader :data
|
37
39
|
|
40
|
+
# The row fields
|
41
|
+
#
|
42
|
+
# @api public
|
38
43
|
attr_reader :fields
|
39
44
|
|
45
|
+
def_delegators :to_ary, :join
|
46
|
+
|
40
47
|
# Initialize a Row
|
41
48
|
#
|
42
49
|
# @example
|
@@ -57,24 +64,34 @@ module TTY
|
|
57
64
|
# @return [undefined]
|
58
65
|
#
|
59
66
|
# @api public
|
60
|
-
def initialize(data, header=nil)
|
67
|
+
def initialize(data, header = nil)
|
61
68
|
case data
|
62
69
|
when Array
|
63
70
|
@attributes = (header || (0...data.length)).to_a
|
64
|
-
@fields
|
65
|
-
@data = Hash[@attributes.zip(fields)]
|
71
|
+
@fields = coerce_to_fields(data)
|
66
72
|
when Hash
|
67
|
-
@data
|
68
|
-
@fields
|
73
|
+
@data = data.dup
|
74
|
+
@fields = coerce_to_fields(@data.values)
|
69
75
|
@attributes = (header || data.keys).to_a
|
70
|
-
@data = Hash[@attributes.zip(fields)]
|
71
76
|
end
|
77
|
+
@data = Hash[@attributes.zip(fields)]
|
78
|
+
end
|
79
|
+
|
80
|
+
# Coerces values to field instances
|
81
|
+
#
|
82
|
+
# @param [Array[Object]] values
|
83
|
+
#
|
84
|
+
# @return [Array[TTY::Table::Field]]
|
85
|
+
#
|
86
|
+
# @api public
|
87
|
+
def coerce_to_fields(values)
|
88
|
+
values.reduce([]) { |acc, el| acc << to_field(el) }
|
72
89
|
end
|
73
90
|
|
74
91
|
# Instantiates a new field
|
75
92
|
#
|
76
93
|
# @api public
|
77
|
-
def to_field(options=nil)
|
94
|
+
def to_field(options = nil)
|
78
95
|
Field.new(options)
|
79
96
|
end
|
80
97
|
|
@@ -93,7 +110,7 @@ module TTY
|
|
93
110
|
data[attributes[attribute]].value
|
94
111
|
else
|
95
112
|
data.fetch(attribute) do |name|
|
96
|
-
|
113
|
+
fail UnknownAttributeError, "the attribute #{name} is unkown"
|
97
114
|
end.value
|
98
115
|
end
|
99
116
|
end
|
@@ -114,10 +131,10 @@ module TTY
|
|
114
131
|
def []=(attribute, value)
|
115
132
|
case attribute
|
116
133
|
when Integer
|
117
|
-
|
134
|
+
data[attributes[attribute]] = to_field(value)
|
118
135
|
else
|
119
|
-
|
120
|
-
|
136
|
+
data[attribute] = to_field(value)
|
137
|
+
attributes << attribute unless attributes.include?(attribute)
|
121
138
|
end
|
122
139
|
end
|
123
140
|
|
@@ -137,7 +154,7 @@ module TTY
|
|
137
154
|
#
|
138
155
|
# @api public
|
139
156
|
def height
|
140
|
-
fields.map
|
157
|
+
fields.map(&:height).max
|
141
158
|
end
|
142
159
|
|
143
160
|
# Convert the Row into Array
|
@@ -159,7 +176,7 @@ module TTY
|
|
159
176
|
# @api public
|
160
177
|
def to_hash
|
161
178
|
hash = data.dup
|
162
|
-
hash.update(hash) { |
|
179
|
+
hash.update(hash) { |_, val| val.value if val }
|
163
180
|
end
|
164
181
|
|
165
182
|
# Check if this row is equivalent to another row
|
@@ -196,6 +213,5 @@ module TTY
|
|
196
213
|
"#<#{self.class.name} fields=#{to_a}>"
|
197
214
|
end
|
198
215
|
end # Row
|
199
|
-
|
200
216
|
end # Table
|
201
217
|
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 for transforming table values
|
6
|
+
#
|
7
|
+
# Used internally by {Table}
|
8
|
+
#
|
9
|
+
# @api private
|
7
10
|
class Transformation
|
8
|
-
|
9
11
|
# Extract the header and row tuples from the value
|
10
12
|
#
|
11
13
|
# @param [Array] args
|
@@ -29,10 +31,9 @@ module TTY
|
|
29
31
|
# @api public
|
30
32
|
def self.group_header_and_rows(value)
|
31
33
|
header = value.map(&:keys).flatten.uniq
|
32
|
-
rows = value.
|
34
|
+
rows = value.reduce([]) { |arr, el| arr + el.values }
|
33
35
|
[header, rows]
|
34
36
|
end
|
35
|
-
|
36
37
|
end # Transformation
|
37
38
|
end # Table
|
38
39
|
end # TTY
|
@@ -1,11 +1,13 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
|
+
# Mixin to provide validation for {Table}.
|
6
|
+
#
|
7
|
+
# Include this mixin to add validation for options.
|
8
|
+
#
|
9
|
+
# @api private
|
5
10
|
module Validatable
|
6
|
-
|
7
|
-
MIN_CELL_WIDTH = 3.freeze
|
8
|
-
|
9
11
|
# Check if table rows are the equal size
|
10
12
|
#
|
11
13
|
# @raise [DimensionMismatchError]
|
@@ -17,33 +19,46 @@ module TTY
|
|
17
19
|
def assert_row_sizes(rows)
|
18
20
|
size = (rows[0] || []).size
|
19
21
|
rows.each do |row|
|
20
|
-
if
|
21
|
-
|
22
|
-
|
22
|
+
next if row.size == size
|
23
|
+
fail TTY::Table::DimensionMismatchError,
|
24
|
+
"row size differs (#{row.size} should be #{size})"
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
26
|
-
|
28
|
+
# Check if table type is provided
|
29
|
+
#
|
30
|
+
# @raise [ArgumentRequired]
|
31
|
+
#
|
32
|
+
# @return [Table]
|
33
|
+
#
|
34
|
+
# @api private
|
35
|
+
def assert_table_type(value)
|
36
|
+
return value if value.is_a?(TTY::Table)
|
37
|
+
fail ArgumentRequired,
|
38
|
+
"Expected TTY::Table instance, got #{value.inspect}"
|
27
39
|
end
|
28
40
|
|
29
|
-
def
|
30
|
-
end
|
41
|
+
# def assert_matching_widths(rows)
|
42
|
+
# end
|
43
|
+
#
|
44
|
+
# def assert_string_values(rows)
|
45
|
+
# end
|
31
46
|
|
32
47
|
# Check if options are of required type
|
33
48
|
#
|
34
49
|
# @api private
|
35
50
|
def validate_options!(options)
|
36
|
-
|
37
|
-
|
38
|
-
|
51
|
+
header = options[:header]
|
52
|
+
rows = options[:rows]
|
53
|
+
|
54
|
+
if header && (!header.is_a?(Array) || header.empty?)
|
55
|
+
fail InvalidArgument, ':header must be a non-empty array'
|
39
56
|
end
|
40
57
|
|
41
|
-
if (rows
|
42
|
-
|
43
|
-
raise InvalidArgument, ":rows must be a non-empty array or hash"
|
58
|
+
if rows && !(rows.is_a?(Array) || rows.is_a?(Hash))
|
59
|
+
fail InvalidArgument, ':rows must be a non-empty array or hash'
|
44
60
|
end
|
45
61
|
end
|
46
|
-
|
47
62
|
end # Validatable
|
48
63
|
end # Table
|
49
64
|
end # TTY
|