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,12 +1,10 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Shell
|
5
5
|
class Question
|
6
|
-
|
7
6
|
# A class representing String modifications.
|
8
7
|
class Modifier
|
9
|
-
|
10
8
|
attr_reader :modifiers
|
11
9
|
private :modifiers
|
12
10
|
|
@@ -27,9 +25,9 @@ module TTY
|
|
27
25
|
#
|
28
26
|
# @api private
|
29
27
|
def apply_to(value)
|
30
|
-
modifiers.
|
28
|
+
modifiers.reduce(value) do |result, mod|
|
31
29
|
result = Modifier.letter_case mod, result
|
32
|
-
|
30
|
+
Modifier.whitespace mod, result
|
33
31
|
end
|
34
32
|
end
|
35
33
|
|
@@ -44,7 +42,8 @@ module TTY
|
|
44
42
|
# @option mod [Symbol] :uppercase change to upper case
|
45
43
|
# @option mod [Symbol] :down change to lower case
|
46
44
|
# @option mod [Symbol] :downcase change to lower case
|
47
|
-
# @option mod [Symbol] :capitalize change all words to start
|
45
|
+
# @option mod [Symbol] :capitalize change all words to start
|
46
|
+
# with uppercase case letter
|
48
47
|
#
|
49
48
|
# @return [String]
|
50
49
|
#
|
@@ -88,9 +87,7 @@ module TTY
|
|
88
87
|
value
|
89
88
|
end
|
90
89
|
end
|
91
|
-
|
92
90
|
end # Modifier
|
93
|
-
|
94
91
|
end # Question
|
95
92
|
end # Shell
|
96
93
|
end # TTY
|
@@ -1,12 +1,10 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Shell
|
5
5
|
class Question
|
6
|
-
|
7
6
|
# A class representing question validation.
|
8
7
|
class Validation
|
9
|
-
|
10
8
|
# @api private
|
11
9
|
attr_reader :validation
|
12
10
|
private :validation
|
@@ -18,7 +16,7 @@ module TTY
|
|
18
16
|
# @return [undefined]
|
19
17
|
#
|
20
18
|
# @api private
|
21
|
-
def initialize(validation=nil)
|
19
|
+
def initialize(validation = nil)
|
22
20
|
@validation = validation ? coerce(validation) : validation
|
23
21
|
end
|
24
22
|
|
@@ -36,7 +34,7 @@ module TTY
|
|
36
34
|
when Regexp, String
|
37
35
|
Regexp.new(validation.to_s)
|
38
36
|
else
|
39
|
-
|
37
|
+
fail TTY::ValidationCoercion, "Wrong type, got #{validation.class}"
|
40
38
|
end
|
41
39
|
end
|
42
40
|
|
@@ -80,11 +78,14 @@ module TTY
|
|
80
78
|
value = value.to_s
|
81
79
|
if validation.is_a?(Regexp) && validation =~ value
|
82
80
|
elsif validation.is_a?(Proc) && validation.call(value)
|
83
|
-
else
|
81
|
+
else
|
82
|
+
fail TTY::InvalidArgument, "Invalid input for #{value}"
|
84
83
|
end
|
84
|
+
true
|
85
|
+
else
|
86
|
+
false
|
85
87
|
end
|
86
88
|
end
|
87
|
-
|
88
89
|
end # Validation
|
89
90
|
end # Question
|
90
91
|
end # Shell
|
data/lib/tty/shell/reader.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
|
+
# A class responsible for shell prompt interactions.
|
4
5
|
class Shell
|
5
|
-
|
6
6
|
# A class responsible for reading character input from STDIN
|
7
7
|
class Reader
|
8
|
-
|
9
8
|
# @api private
|
10
9
|
attr_reader :shell
|
11
10
|
private :shell
|
@@ -19,14 +18,20 @@ module TTY
|
|
19
18
|
# Initialize a Reader
|
20
19
|
#
|
21
20
|
# @api public
|
22
|
-
def initialize(shell=
|
23
|
-
@shell = shell
|
21
|
+
def initialize(shell = Shell.new)
|
22
|
+
@shell = shell
|
24
23
|
end
|
25
24
|
|
26
25
|
# Get input in unbuffered mode.
|
27
26
|
#
|
27
|
+
# @example
|
28
|
+
# buffer do
|
29
|
+
# ...
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# @return [String]
|
28
33
|
#
|
29
|
-
# @api
|
34
|
+
# @api public
|
30
35
|
def buffer(&block)
|
31
36
|
bufferring = shell.output.sync
|
32
37
|
# Immediately flush output
|
@@ -48,26 +53,18 @@ module TTY
|
|
48
53
|
# @return [String]
|
49
54
|
#
|
50
55
|
# @api public
|
51
|
-
def getc(mask=(not_set=true))
|
52
|
-
value =
|
53
|
-
|
56
|
+
def getc(mask = (not_set = true))
|
57
|
+
value = ''
|
54
58
|
buffer do
|
55
59
|
begin
|
56
|
-
while (char = shell.input.getbyte)
|
60
|
+
while (char = shell.input.getbyte) &&
|
57
61
|
!(char == CARRIAGE_RETURN || char == NEWLINE)
|
58
|
-
|
59
|
-
if (char == BACKSPACE || char == DELETE)
|
60
|
-
value.slice!(-1, 1) unless value.empty?
|
61
|
-
else
|
62
|
-
print_char char, not_set, mask
|
63
|
-
value << char
|
64
|
-
end
|
62
|
+
value = handle_char value, char, not_set, mask
|
65
63
|
end
|
66
64
|
ensure
|
67
65
|
TTY.terminal.echo_on
|
68
66
|
end
|
69
67
|
end
|
70
|
-
|
71
68
|
value
|
72
69
|
end
|
73
70
|
|
@@ -80,13 +77,25 @@ module TTY
|
|
80
77
|
|
81
78
|
private
|
82
79
|
|
80
|
+
# Handle single character by appending to or removing from output
|
81
|
+
#
|
82
|
+
# @api private
|
83
|
+
def handle_char(value, char, not_set, mask)
|
84
|
+
if char == BACKSPACE || char == DELETE
|
85
|
+
value.slice!(-1, 1) unless value.empty?
|
86
|
+
else
|
87
|
+
print_char char, not_set, mask
|
88
|
+
value << char
|
89
|
+
end
|
90
|
+
value
|
91
|
+
end
|
92
|
+
|
83
93
|
# Print out character back to shell STDOUT
|
84
94
|
#
|
85
95
|
# @api private
|
86
96
|
def print_char(char, not_set, mask)
|
87
97
|
shell.output.putc((not_set || !mask) ? char : mask)
|
88
98
|
end
|
89
|
-
|
90
99
|
end # Reader
|
91
100
|
end # Shell
|
92
101
|
end # TTY
|
data/lib/tty/shell/response.rb
CHANGED
@@ -1,15 +1,22 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
require 'date'
|
4
4
|
|
5
5
|
module TTY
|
6
|
-
# A class responsible for shell prompt interactions
|
6
|
+
# A class responsible for shell prompt interactions
|
7
7
|
class Shell
|
8
|
-
|
9
|
-
# A class representing a question.
|
8
|
+
# A class representing a shell response
|
10
9
|
class Response
|
10
|
+
VALID_TYPES = [
|
11
|
+
:boolean,
|
12
|
+
:string,
|
13
|
+
:symbol,
|
14
|
+
:integer,
|
15
|
+
:float,
|
16
|
+
:date,
|
17
|
+
:datetime
|
18
|
+
]
|
11
19
|
|
12
|
-
VALID_TYPES = [:boolean, :string, :symbol, :integer, :float, :date, :datetime]
|
13
20
|
attr_reader :reader
|
14
21
|
private :reader
|
15
22
|
|
@@ -22,9 +29,9 @@ module TTY
|
|
22
29
|
# Initialize a Response
|
23
30
|
#
|
24
31
|
# @api public
|
25
|
-
def initialize(question, shell=
|
32
|
+
def initialize(question, shell = Shell.new)
|
26
33
|
@question = question
|
27
|
-
@shell = shell
|
34
|
+
@shell = shell
|
28
35
|
@reader = Reader.new(shell)
|
29
36
|
end
|
30
37
|
|
@@ -35,7 +42,7 @@ module TTY
|
|
35
42
|
# @return [undefined]
|
36
43
|
#
|
37
44
|
# @api public
|
38
|
-
def read(type=nil)
|
45
|
+
def read(type = nil)
|
39
46
|
question.evaluate_response read_input
|
40
47
|
end
|
41
48
|
|
@@ -58,7 +65,7 @@ module TTY
|
|
58
65
|
# error to display on failed conversion to string type
|
59
66
|
#
|
60
67
|
# @api public
|
61
|
-
def read_string(error=nil)
|
68
|
+
def read_string(error = nil)
|
62
69
|
question.evaluate_response(String(read_input).strip)
|
63
70
|
end
|
64
71
|
|
@@ -66,7 +73,7 @@ module TTY
|
|
66
73
|
#
|
67
74
|
# @api public
|
68
75
|
def read_char
|
69
|
-
question.
|
76
|
+
question.char(true)
|
70
77
|
question.evaluate_response String(read_input).chars.to_a[0]
|
71
78
|
end
|
72
79
|
|
@@ -80,14 +87,14 @@ module TTY
|
|
80
87
|
# Read ansewr and cast to Symbol type
|
81
88
|
#
|
82
89
|
# @api public
|
83
|
-
def read_symbol(error=nil)
|
84
|
-
|
90
|
+
def read_symbol(error = nil)
|
91
|
+
question.evaluate_response read_input.to_sym
|
85
92
|
end
|
86
93
|
|
87
94
|
# Read answer from predifined choicse
|
88
95
|
#
|
89
96
|
# @api public
|
90
|
-
def read_choice(type=nil)
|
97
|
+
def read_choice(type = nil)
|
91
98
|
question.argument(:required) unless question.default?
|
92
99
|
question.evaluate_response read_input
|
93
100
|
end
|
@@ -95,21 +102,21 @@ module TTY
|
|
95
102
|
# Read integer value
|
96
103
|
#
|
97
104
|
# @api public
|
98
|
-
def read_int(error=nil)
|
105
|
+
def read_int(error = nil)
|
99
106
|
question.evaluate_response TTY::Coercer::Integer.coerce(read_input)
|
100
107
|
end
|
101
108
|
|
102
109
|
# Read float value
|
103
110
|
#
|
104
111
|
# @api public
|
105
|
-
def read_float(error=nil)
|
112
|
+
def read_float(error = nil)
|
106
113
|
question.evaluate_response TTY::Coercer::Float.coerce(read_input)
|
107
114
|
end
|
108
115
|
|
109
116
|
# Read regular expression
|
110
117
|
#
|
111
118
|
# @api public
|
112
|
-
def read_regex(error=nil)
|
119
|
+
def read_regex(error = nil)
|
113
120
|
question.evaluate_response Kernel.send(:Regex, read_input)
|
114
121
|
end
|
115
122
|
|
@@ -137,14 +144,14 @@ module TTY
|
|
137
144
|
# Read boolean
|
138
145
|
#
|
139
146
|
# @api public
|
140
|
-
def read_bool(error=nil)
|
147
|
+
def read_bool(error = nil)
|
141
148
|
question.evaluate_response TTY::Coercer::Boolean.coerce read_input
|
142
149
|
end
|
143
150
|
|
144
151
|
# Read file contents
|
145
152
|
#
|
146
153
|
# @api public
|
147
|
-
def read_file(error=nil)
|
154
|
+
def read_file(error = nil)
|
148
155
|
question.evaluate_response File.open(File.join(directory, read_input))
|
149
156
|
end
|
150
157
|
|
@@ -155,9 +162,7 @@ module TTY
|
|
155
162
|
# @api public
|
156
163
|
def read_email
|
157
164
|
question.validate(/^[a-z0-9._%+-]+@([a-z0-9-]+\.)+[a-z]{2,6}$/i)
|
158
|
-
if question.error
|
159
|
-
question.prompt question.statement
|
160
|
-
end
|
165
|
+
question.prompt(question.statement) if question.error
|
161
166
|
with_exception { read_string }
|
162
167
|
end
|
163
168
|
|
@@ -213,7 +218,6 @@ module TTY
|
|
213
218
|
def valid_type?(type)
|
214
219
|
self.class::VALID_TYPES.include? type.to_sym
|
215
220
|
end
|
216
|
-
|
217
221
|
end # Response
|
218
222
|
end # Shell
|
219
223
|
end # TTY
|
data/lib/tty/shell/statement.rb
CHANGED
@@ -1,17 +1,22 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
|
+
# A class responsible for shell prompt interactions.
|
4
5
|
class Shell
|
5
|
-
|
6
6
|
# A class representing a statement output to shell.
|
7
7
|
class Statement
|
8
|
-
|
9
8
|
# @api private
|
10
9
|
attr_reader :shell
|
11
10
|
private :shell
|
12
11
|
|
12
|
+
# Flag to display newline
|
13
|
+
#
|
14
|
+
# @api public
|
13
15
|
attr_reader :newline
|
14
16
|
|
17
|
+
# Color used to display statement
|
18
|
+
#
|
19
|
+
# @api public
|
15
20
|
attr_reader :color
|
16
21
|
|
17
22
|
# Initialize a Statement
|
@@ -27,10 +32,10 @@ module TTY
|
|
27
32
|
# change the message display to color
|
28
33
|
#
|
29
34
|
# @api public
|
30
|
-
def initialize(shell=
|
31
|
-
@shell
|
32
|
-
@newline = options.fetch
|
33
|
-
@color = options.fetch
|
35
|
+
def initialize(shell = Shell.new, options = {})
|
36
|
+
@shell = shell
|
37
|
+
@newline = options.fetch(:newline, true)
|
38
|
+
@color = options.fetch(:color, nil)
|
34
39
|
end
|
35
40
|
|
36
41
|
# Output the message to the shell
|
@@ -40,7 +45,7 @@ module TTY
|
|
40
45
|
#
|
41
46
|
# @api public
|
42
47
|
def declare(message)
|
43
|
-
message = TTY
|
48
|
+
message = TTY.terminal.color.set message, *color if color
|
44
49
|
|
45
50
|
if newline && /( |\t)(\e\[\d+(;\d+)*m)?\Z/ !~ message
|
46
51
|
shell.output.puts message
|
@@ -49,7 +54,6 @@ module TTY
|
|
49
54
|
shell.output.flush
|
50
55
|
end
|
51
56
|
end
|
52
|
-
|
53
57
|
end # Statement
|
54
58
|
end # Shell
|
55
59
|
end # TTY
|
data/lib/tty/shell/suggestion.rb
CHANGED
@@ -1,12 +1,10 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
# A class responsible for shell prompt interactions.
|
5
5
|
class Shell
|
6
|
-
|
7
6
|
# A class representing a suggestion
|
8
7
|
class Suggestion
|
9
|
-
|
10
8
|
# @api private
|
11
9
|
attr_reader :shell
|
12
10
|
private :shell
|
@@ -26,6 +24,11 @@ module TTY
|
|
26
24
|
# @api public
|
27
25
|
attr_reader :plural_text
|
28
26
|
|
27
|
+
# Possible suggestions
|
28
|
+
#
|
29
|
+
# @api public
|
30
|
+
attr_reader :suggestions
|
31
|
+
|
29
32
|
DEFAULT_INDENT = 8
|
30
33
|
|
31
34
|
SINGLE_TEXT = 'Did you mean this?'
|
@@ -35,10 +38,11 @@ module TTY
|
|
35
38
|
# Initialize a Suggestion
|
36
39
|
#
|
37
40
|
# @api public
|
38
|
-
def initialize(options={})
|
41
|
+
def initialize(options = {})
|
39
42
|
@indent = options.fetch(:indent) { DEFAULT_INDENT }
|
40
43
|
@single_text = options.fetch(:single_text) { SINGLE_TEXT }
|
41
44
|
@plural_text = options.fetch(:plural_text) { PLURAL_TEXT }
|
45
|
+
@suggestions = []
|
42
46
|
end
|
43
47
|
|
44
48
|
# Suggest matches out of possibile strings
|
@@ -54,11 +58,9 @@ module TTY
|
|
54
58
|
max_distance = distances.keys.max
|
55
59
|
|
56
60
|
if minimum_distance < max_distance
|
57
|
-
suggestions = distances[minimum_distance].sort
|
58
|
-
evaluate(suggestions)
|
59
|
-
else
|
60
|
-
nil
|
61
|
+
@suggestions = distances[minimum_distance].sort
|
61
62
|
end
|
63
|
+
evaluate
|
62
64
|
end
|
63
65
|
|
64
66
|
# Measure distances between messag and possibilities
|
@@ -86,17 +88,19 @@ module TTY
|
|
86
88
|
# @return [String]
|
87
89
|
#
|
88
90
|
# @api private
|
89
|
-
def evaluate
|
90
|
-
|
91
|
+
def evaluate
|
92
|
+
return suggestions if suggestions.empty?
|
93
|
+
suggestion = ''
|
91
94
|
if suggestions.one?
|
92
95
|
suggestion << single_text + "\n"
|
93
|
-
suggestion << (
|
96
|
+
suggestion << (' ' * indent + @suggestions.first)
|
94
97
|
else
|
95
98
|
suggestion << plural_text + "\n"
|
96
|
-
suggestion << suggestions.map
|
99
|
+
suggestion << suggestions.map do |sugest|
|
100
|
+
' ' * indent + sugest
|
101
|
+
end.join("\n")
|
97
102
|
end
|
98
103
|
end
|
99
|
-
|
100
104
|
end # Suggestion
|
101
105
|
end # Shell
|
102
106
|
end # TTY
|