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
@@ -1,8 +1,7 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
|
-
|
6
5
|
# A class responsible for indenting table representation
|
7
6
|
class Indentation
|
8
7
|
|
@@ -48,7 +47,6 @@ module TTY
|
|
48
47
|
line = line.is_a?(Array) ? line[0] : line
|
49
48
|
line.insert(0, indentation) if line
|
50
49
|
end
|
51
|
-
|
52
50
|
end # Indentation
|
53
51
|
end # Table
|
54
52
|
end # TTY
|
@@ -1,10 +1,11 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
5
|
module Operation
|
6
|
-
|
7
6
|
# A class representing an alignment of cell data.
|
7
|
+
#
|
8
|
+
# @api private
|
8
9
|
class Alignment
|
9
10
|
include TTY::Coercion
|
10
11
|
|
@@ -27,7 +28,7 @@ module TTY
|
|
27
28
|
# raise if the alignment is not supported type
|
28
29
|
#
|
29
30
|
# @api private
|
30
|
-
def initialize(type=nil)
|
31
|
+
def initialize(type = nil)
|
31
32
|
@type = coerce_to((type || LEFT), Symbol, :to_sym)
|
32
33
|
assert_valid_type
|
33
34
|
end
|
@@ -41,9 +42,8 @@ module TTY
|
|
41
42
|
#
|
42
43
|
# @api private
|
43
44
|
def assert_valid_type
|
44
|
-
|
45
|
-
|
46
|
-
end
|
45
|
+
return if supported.include?(type)
|
46
|
+
fail TypeError, "Alignment must be one of: #{supported.join(' ')}"
|
47
47
|
end
|
48
48
|
|
49
49
|
# List supported alignment types
|
@@ -67,7 +67,7 @@ module TTY
|
|
67
67
|
# @return [String] aligned
|
68
68
|
#
|
69
69
|
# @api public
|
70
|
-
def format(field, column_width, space='')
|
70
|
+
def format(field, column_width, space = '')
|
71
71
|
case type
|
72
72
|
when :left
|
73
73
|
"%-#{column_width}s#{space}" % field.to_s
|
@@ -78,7 +78,7 @@ module TTY
|
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
81
|
-
|
81
|
+
private
|
82
82
|
|
83
83
|
# Center aligns field
|
84
84
|
#
|
@@ -101,7 +101,6 @@ module TTY
|
|
101
101
|
"#{field}#{space}"
|
102
102
|
end
|
103
103
|
end
|
104
|
-
|
105
104
|
end # Alignment
|
106
105
|
end # Operation
|
107
106
|
end # Table
|
@@ -1,15 +1,17 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
5
|
module Operation
|
6
|
-
|
7
6
|
# A class which responsiblity is to align table rows and header.
|
8
7
|
class AlignmentSet < Vector
|
9
8
|
|
10
9
|
attr_reader :widths
|
11
10
|
|
12
|
-
|
11
|
+
# Initialize an AlignmentSet
|
12
|
+
#
|
13
|
+
# @api private
|
14
|
+
def initialize(aligns, widths = nil)
|
13
15
|
@elements = convert_to_array(aligns)
|
14
16
|
@widths = widths
|
15
17
|
end
|
@@ -48,7 +50,7 @@ module TTY
|
|
48
50
|
align_field(field, col)
|
49
51
|
end
|
50
52
|
|
51
|
-
|
53
|
+
private
|
52
54
|
|
53
55
|
# Align each field in a row
|
54
56
|
#
|
@@ -68,7 +70,6 @@ module TTY
|
|
68
70
|
alignment = Alignment.new(field.align || self[col])
|
69
71
|
field.value = alignment.format(field, column_width)
|
70
72
|
end
|
71
|
-
|
72
73
|
end # AlignmentSet
|
73
74
|
end # Operation
|
74
75
|
end # Table
|
@@ -1,10 +1,11 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
5
|
module Operation
|
6
|
-
|
7
6
|
# A class responsible for escaping special chars in a table field
|
7
|
+
#
|
8
|
+
# @api private
|
8
9
|
class Escape
|
9
10
|
|
10
11
|
# Escape special characters in a table field
|
@@ -23,7 +24,6 @@ module TTY
|
|
23
24
|
val.dump.gsub('"', '')
|
24
25
|
end
|
25
26
|
end
|
26
|
-
|
27
27
|
end # Escape
|
28
28
|
end # Operation
|
29
29
|
end # Table
|
@@ -1,12 +1,12 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
5
|
module Operation
|
6
|
-
|
7
6
|
# A class responsible for transforming table field
|
7
|
+
#
|
8
|
+
# @api private
|
8
9
|
class Filter
|
9
|
-
|
10
10
|
# Initialize a Filter
|
11
11
|
#
|
12
12
|
# @api public
|
@@ -28,7 +28,6 @@ module TTY
|
|
28
28
|
def call(field, row, col)
|
29
29
|
field.value = @filter.call(field.value, row, col)
|
30
30
|
end
|
31
|
-
|
32
31
|
end # Filter
|
33
32
|
end # Operation
|
34
33
|
end # Table
|
@@ -1,10 +1,11 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
5
|
module Operation
|
6
|
-
|
7
6
|
# A class responsible for shortening text.
|
7
|
+
#
|
8
|
+
# @api private
|
8
9
|
class Truncation
|
9
10
|
|
10
11
|
attr_reader :widths
|
@@ -48,7 +49,6 @@ module TTY
|
|
48
49
|
def truncate(string, width)
|
49
50
|
TTY::Text.truncate(string, width)
|
50
51
|
end
|
51
|
-
|
52
52
|
end # Truncation
|
53
53
|
end # Operation
|
54
54
|
end # Table
|
@@ -1,10 +1,11 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
5
|
module Operation
|
6
|
-
|
7
6
|
# A class responsible for wrapping text.
|
7
|
+
#
|
8
|
+
# @api private
|
8
9
|
class Wrapped
|
9
10
|
|
10
11
|
attr_reader :widths
|
@@ -51,9 +52,7 @@ module TTY
|
|
51
52
|
def wrap(string, width)
|
52
53
|
TTY::Text.wrap(string, width, padding: padding)
|
53
54
|
end
|
54
|
-
|
55
55
|
end # Wrapped
|
56
56
|
end # Operation
|
57
57
|
end # Table
|
58
58
|
end # TTY
|
59
|
-
|
data/lib/tty/table/operations.rb
CHANGED
@@ -1,17 +1,24 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
|
-
|
6
|
-
#
|
5
|
+
# A class holding table field operations.
|
6
|
+
#
|
7
|
+
# @api private
|
7
8
|
class Operations
|
8
|
-
|
9
9
|
# The table
|
10
10
|
#
|
11
11
|
# @api private
|
12
12
|
attr_reader :table
|
13
13
|
private :table
|
14
14
|
|
15
|
+
# Available operations
|
16
|
+
#
|
17
|
+
# @return [Hash]
|
18
|
+
#
|
19
|
+
# @api public
|
20
|
+
attr_reader :operations
|
21
|
+
|
15
22
|
# Initialize Operations
|
16
23
|
#
|
17
24
|
# @param [TTY::Table] table
|
@@ -20,23 +27,14 @@ module TTY
|
|
20
27
|
# @return [Object]
|
21
28
|
#
|
22
29
|
# @api public
|
23
|
-
def initialize(table
|
24
|
-
@table
|
25
|
-
@
|
26
|
-
end
|
27
|
-
|
28
|
-
# Available operations
|
29
|
-
#
|
30
|
-
# @return [Hash]
|
31
|
-
#
|
32
|
-
# @api public
|
33
|
-
def operations
|
34
|
-
@operations ||= Hash.new { |hash, key| hash[key] = [] }
|
30
|
+
def initialize(table)
|
31
|
+
@table = table
|
32
|
+
@operations = Hash.new { |hash, key| hash[key] = [] }
|
35
33
|
end
|
36
34
|
|
37
35
|
# Add operation
|
38
36
|
#
|
39
|
-
# @param [Symbol]
|
37
|
+
# @param [Symbol] operation_type
|
40
38
|
# the operation type
|
41
39
|
# @param [Object] object
|
42
40
|
# the callable object
|
@@ -44,8 +42,8 @@ module TTY
|
|
44
42
|
# @return [Hash]
|
45
43
|
#
|
46
44
|
# @api public
|
47
|
-
def add(
|
48
|
-
operations[
|
45
|
+
def add(operation_type, object)
|
46
|
+
operations[operation_type] << object
|
49
47
|
end
|
50
48
|
|
51
49
|
# Apply operations to a table row
|
@@ -59,14 +57,13 @@ module TTY
|
|
59
57
|
#
|
60
58
|
# @api public
|
61
59
|
def run_operations(*args)
|
62
|
-
|
60
|
+
operation_types = args
|
63
61
|
table.each_with_index do |val, row, col|
|
64
|
-
|
62
|
+
operation_types.each do |type|
|
65
63
|
operations[type].each { |op| op.call(val, row, col) }
|
66
64
|
end
|
67
65
|
end
|
68
66
|
end
|
69
|
-
|
70
67
|
end # Operations
|
71
68
|
end # Table
|
72
69
|
end # TTY
|
@@ -1,11 +1,11 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
|
-
|
6
5
|
# A class representing table orientation
|
6
|
+
#
|
7
|
+
# @api private
|
7
8
|
class Orientation
|
8
|
-
|
9
9
|
# The name for the orientation
|
10
10
|
#
|
11
11
|
# @api public
|
@@ -30,7 +30,8 @@ module TTY
|
|
30
30
|
when /v|ert(ical)?/i
|
31
31
|
Vertical.new :vertical
|
32
32
|
else
|
33
|
-
|
33
|
+
fail InvalidOrientationError,
|
34
|
+
'orientation must be one of :horizontal, :vertical'
|
34
35
|
end
|
35
36
|
end
|
36
37
|
|
@@ -51,7 +52,6 @@ module TTY
|
|
51
52
|
def horizontal?
|
52
53
|
name == :horizontal
|
53
54
|
end
|
54
|
-
|
55
55
|
end # Orientation
|
56
56
|
end # Table
|
57
57
|
end # TTY
|
@@ -1,21 +1,25 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
|
-
|
6
5
|
# A class representing table orientation
|
7
6
|
class Orientation
|
8
|
-
|
9
7
|
# A class responsible for horizontal table transformation
|
10
8
|
class Horizontal < Orientation
|
11
|
-
|
9
|
+
# Rotate table horizontally
|
10
|
+
#
|
11
|
+
# @param [Table] table
|
12
|
+
#
|
13
|
+
# @return [nil]
|
14
|
+
#
|
15
|
+
# @api public
|
12
16
|
def transform(table)
|
13
17
|
table.rotate_horizontal
|
14
18
|
end
|
15
19
|
|
16
20
|
# Slice vertical table data into horizontal
|
17
21
|
#
|
18
|
-
# @param [
|
22
|
+
# @param [Table] table
|
19
23
|
#
|
20
24
|
# @api public
|
21
25
|
def slice(table)
|
@@ -38,7 +42,6 @@ module TTY
|
|
38
42
|
end
|
39
43
|
[head, body]
|
40
44
|
end
|
41
|
-
|
42
45
|
end # Horizontal
|
43
46
|
end # Orientation
|
44
47
|
end # Table
|
@@ -1,21 +1,25 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
|
-
|
6
5
|
# A class representing table orientation
|
7
6
|
class Orientation
|
8
|
-
|
9
7
|
# A class responsible for vertical table transformation
|
10
8
|
class Vertical < Orientation
|
11
|
-
|
9
|
+
# Rotate table vertically
|
10
|
+
#
|
11
|
+
# @param [Table] table
|
12
|
+
#
|
13
|
+
# @return [nil]
|
14
|
+
#
|
15
|
+
# @api public
|
12
16
|
def transform(table)
|
13
17
|
table.rotate_vertical
|
14
18
|
end
|
15
19
|
|
16
20
|
# Slice horizontal table data into vertical
|
17
21
|
#
|
18
|
-
# @param [
|
22
|
+
# @param [Table] table
|
19
23
|
#
|
20
24
|
# @api public
|
21
25
|
def slice(table)
|
@@ -24,11 +28,10 @@ module TTY
|
|
24
28
|
|
25
29
|
head = header ? header : (0..row_size).map { |n| (n + 1).to_s }
|
26
30
|
|
27
|
-
(0...row_size).
|
31
|
+
(0...row_size).reduce([]) do |array, index|
|
28
32
|
array + head.zip(table.rows[index]).map { |row| table.to_row(row) }
|
29
33
|
end
|
30
34
|
end
|
31
|
-
|
32
35
|
end # Vertical
|
33
36
|
end # Orientation
|
34
37
|
end # Table
|
data/lib/tty/table/padder.rb
CHANGED
@@ -1,12 +1,18 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Table
|
5
|
-
|
6
5
|
# A class responsible for processing table field padding
|
6
|
+
#
|
7
|
+
# Used internally by {Table::Renderer}
|
8
|
+
#
|
9
|
+
# @api private
|
7
10
|
class Padder
|
8
11
|
include TTY::Equatable
|
9
12
|
|
13
|
+
# Padding for the table cells
|
14
|
+
#
|
15
|
+
# @return [Array[Integer]]
|
10
16
|
attr_reader :padding
|
11
17
|
|
12
18
|
# Initialize a Padder
|
@@ -18,15 +24,32 @@ module TTY
|
|
18
24
|
|
19
25
|
# Parse padding options
|
20
26
|
#
|
27
|
+
# Turn possible values into 4 element array
|
28
|
+
#
|
29
|
+
# @example
|
30
|
+
# padder = TTY::Table::Padder.parse(5)
|
31
|
+
# padder.padding # => [5, 5, 5, 5]
|
32
|
+
#
|
21
33
|
# @param [Object] value
|
22
34
|
#
|
23
35
|
# @return [TTY::Padder]
|
36
|
+
# the new padder with padding values
|
24
37
|
#
|
25
38
|
# @api public
|
26
39
|
def self.parse(value = nil)
|
27
|
-
return value if value.
|
40
|
+
return value if value.is_a?(self)
|
28
41
|
|
29
|
-
|
42
|
+
new(convert_to_ary(value))
|
43
|
+
end
|
44
|
+
|
45
|
+
# Convert value to 4 element array
|
46
|
+
#
|
47
|
+
# @return [Array[Integer]]
|
48
|
+
# the 4 element padding array
|
49
|
+
#
|
50
|
+
# @api private
|
51
|
+
def self.convert_to_ary(value)
|
52
|
+
if value.class <= Numeric
|
30
53
|
[value, value, value, value]
|
31
54
|
elsif value.nil?
|
32
55
|
[]
|
@@ -35,9 +58,8 @@ module TTY
|
|
35
58
|
elsif value.size == 4
|
36
59
|
value
|
37
60
|
else
|
38
|
-
|
61
|
+
fail ArgumentError, 'Wrong :padding parameter, must be an array'
|
39
62
|
end
|
40
|
-
new(padding)
|
41
63
|
end
|
42
64
|
|
43
65
|
# Top padding
|
@@ -53,6 +75,8 @@ module TTY
|
|
53
75
|
#
|
54
76
|
# @param [Integer] val
|
55
77
|
#
|
78
|
+
# @return [nil]
|
79
|
+
#
|
56
80
|
# @api public
|
57
81
|
def top=(value)
|
58
82
|
@padding[0] = value
|
@@ -89,6 +113,8 @@ module TTY
|
|
89
113
|
#
|
90
114
|
# @param [Integer] value
|
91
115
|
#
|
116
|
+
# @return [nil]
|
117
|
+
#
|
92
118
|
# @api public
|
93
119
|
def bottom=(value)
|
94
120
|
@padding[2] = value
|
@@ -107,6 +133,8 @@ module TTY
|
|
107
133
|
#
|
108
134
|
# @param [Integer] value
|
109
135
|
#
|
136
|
+
# @return [nil]
|
137
|
+
#
|
110
138
|
# @api public
|
111
139
|
def left=(value)
|
112
140
|
@padding[3] = value
|
@@ -114,6 +142,8 @@ module TTY
|
|
114
142
|
|
115
143
|
# Check if padding is set
|
116
144
|
#
|
145
|
+
# @return [Boolean]
|
146
|
+
#
|
117
147
|
# @api public
|
118
148
|
def empty?
|
119
149
|
padding.empty?
|
@@ -125,7 +155,7 @@ module TTY
|
|
125
155
|
#
|
126
156
|
# @api public
|
127
157
|
def vertical?
|
128
|
-
top.nonzero?
|
158
|
+
top.nonzero? || bottom.nonzero?
|
129
159
|
end
|
130
160
|
|
131
161
|
# Check if horizontal padding is applied
|
@@ -134,9 +164,17 @@ module TTY
|
|
134
164
|
#
|
135
165
|
# @api public
|
136
166
|
def horizontal?
|
137
|
-
left.nonzero?
|
167
|
+
left.nonzero? || right.nonzero?
|
138
168
|
end
|
139
169
|
|
170
|
+
# String represenation of this padder with padding values
|
171
|
+
#
|
172
|
+
# @return [String]
|
173
|
+
#
|
174
|
+
# @api public
|
175
|
+
def to_s
|
176
|
+
inspect
|
177
|
+
end
|
140
178
|
end # Padder
|
141
179
|
end # Table
|
142
180
|
end # TTY
|