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/coercer/range.rb
CHANGED
@@ -1,17 +1,18 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Coercer
|
5
|
-
|
6
5
|
# A class responsible for range type coercion
|
7
6
|
class Range
|
8
|
-
|
9
7
|
# Coerce value to Range type with possible ranges
|
10
8
|
#
|
11
9
|
# @param [Object] value
|
12
10
|
#
|
13
11
|
# @example
|
14
|
-
# coerce('0
|
12
|
+
# coerce('0,9') # => (0..9)
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
# coerce('0-9') # => (0..9)
|
15
16
|
#
|
16
17
|
# @api public
|
17
18
|
def self.coerce(value)
|
@@ -23,11 +24,9 @@ module TTY
|
|
23
24
|
when /\A(\w)(\.{2}\.?|-|,)(\w)\Z/
|
24
25
|
::Range.new($1.to_s, $3.to_s, $2 == '...')
|
25
26
|
else
|
26
|
-
|
27
|
+
fail InvalidArgument, "#{value} could not be coerced into Range type"
|
27
28
|
end
|
28
29
|
end
|
29
|
-
|
30
30
|
end # Range
|
31
|
-
|
32
31
|
end # Coercer
|
33
32
|
end # TTY
|
data/lib/tty/logger.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
|
-
|
5
4
|
# A class providing logging system
|
6
5
|
class Logger
|
7
6
|
include TTY::Equatable
|
@@ -33,8 +32,10 @@ module TTY
|
|
33
32
|
# @option options [String] :output
|
34
33
|
#
|
35
34
|
# @api public
|
36
|
-
def initialize(options={})
|
37
|
-
@namespace = options.fetch(:namespace)
|
35
|
+
def initialize(options = {})
|
36
|
+
@namespace = options.fetch(:namespace) do
|
37
|
+
fail ArgumentError, 'Logger must have namespace', caller
|
38
|
+
end
|
38
39
|
@output = options.fetch(:output) { $stderr }
|
39
40
|
@level = options.fetch(:level) { ALL }
|
40
41
|
@timestamp_format = options.fetch(:timestamp_format) { '%Y-%m-%d %T' }
|
@@ -45,27 +46,40 @@ module TTY
|
|
45
46
|
@level = level
|
46
47
|
end
|
47
48
|
|
49
|
+
# Create a timestamp
|
50
|
+
#
|
51
|
+
# @return [Time]
|
52
|
+
#
|
48
53
|
# @api public
|
49
54
|
def timestamp
|
50
55
|
Time.now.strftime(timestamp_format)
|
51
56
|
end
|
52
57
|
|
53
58
|
def validate_level(level)
|
54
|
-
|
55
|
-
|
56
|
-
end
|
59
|
+
return if valid_level?(level)
|
60
|
+
fail ArgumentError, "Log level must be 0..#{MAX_LEVELS}", caller
|
57
61
|
end
|
58
62
|
|
63
|
+
# Verify valid level
|
64
|
+
#
|
65
|
+
# @param [Number] level
|
66
|
+
# the level of this logger
|
67
|
+
#
|
68
|
+
# @return [Boolean]
|
69
|
+
#
|
70
|
+
# @api public
|
59
71
|
def self.valid_level?(level)
|
60
|
-
!level.nil? && level.
|
72
|
+
!level.nil? && level.is_a?(Numeric) && level >= ALL && level <= OFF
|
61
73
|
end
|
62
74
|
|
63
75
|
# Print formatted log to output
|
64
76
|
#
|
77
|
+
# @param [String] message
|
78
|
+
# the message to print to output
|
79
|
+
#
|
65
80
|
# @api public
|
66
81
|
def log(message)
|
67
82
|
output.print timestamp + ' - ' + message
|
68
83
|
end
|
69
|
-
|
70
84
|
end # Logger
|
71
85
|
end # TTY
|
data/lib/tty/plugins.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
|
-
|
5
4
|
# A class responsible for managing plugins installation
|
6
5
|
class Plugins
|
7
|
-
|
8
6
|
PLUGIN_PREFIX = 'tty'
|
9
7
|
|
10
8
|
attr_accessor :plugins
|
@@ -33,7 +31,7 @@ module TTY
|
|
33
31
|
# @param [TTY::Plugin] plugin
|
34
32
|
#
|
35
33
|
# @api public
|
36
|
-
def register(name, plugin=false)
|
34
|
+
def register(name, plugin = false)
|
37
35
|
if plugin && !loaded?(name)
|
38
36
|
plugins << plugin
|
39
37
|
end
|
@@ -56,7 +54,7 @@ module TTY
|
|
56
54
|
#
|
57
55
|
# @api public
|
58
56
|
def names
|
59
|
-
plugins.
|
57
|
+
plugins.reduce({}) do |hash, plugin|
|
60
58
|
hash[plugin.name] = plugin
|
61
59
|
hash
|
62
60
|
end
|
@@ -70,6 +68,5 @@ module TTY
|
|
70
68
|
def loaded?(name)
|
71
69
|
plugins.any? { |plugin| plugin.gem_name == name }
|
72
70
|
end
|
73
|
-
|
74
71
|
end # PluginManager
|
75
72
|
end # TTY
|
data/lib/tty/plugins/plugin.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
|
-
|
5
4
|
# A class responsible for plugin loading
|
6
5
|
class Plugin
|
7
|
-
|
8
6
|
attr_reader :name
|
9
7
|
|
10
8
|
attr_reader :gem
|
@@ -35,7 +33,7 @@ module TTY
|
|
35
33
|
#
|
36
34
|
# @api public
|
37
35
|
def enabled?
|
38
|
-
|
36
|
+
enabled
|
39
37
|
end
|
40
38
|
|
41
39
|
# Load the plugin (require the gem)
|
@@ -45,12 +43,11 @@ module TTY
|
|
45
43
|
begin
|
46
44
|
require gem_name unless enabled?
|
47
45
|
rescue LoadError => error
|
48
|
-
TTY.shell.error("Unable to load plugin #{gem_name}.")
|
46
|
+
TTY.shell.error("Unable to load plugin #{gem_name} due to #{error}.")
|
49
47
|
rescue => error
|
50
48
|
TTY.shell.error("require '#{gem_name}' failed with #{error}")
|
51
49
|
end
|
52
|
-
|
50
|
+
@enabled = true
|
53
51
|
end
|
54
|
-
|
55
52
|
end # Plugin
|
56
53
|
end # TTY
|
data/lib/tty/shell.rb
CHANGED
@@ -1,23 +1,23 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
|
-
|
5
4
|
# A class responsible for shell prompt interactions.
|
6
5
|
class Shell
|
7
|
-
|
8
6
|
# @api private
|
9
7
|
attr_reader :input
|
10
8
|
|
11
9
|
# @api private
|
12
10
|
attr_reader :output
|
13
11
|
|
12
|
+
# Shell prompt prefix
|
13
|
+
#
|
14
14
|
# @api private
|
15
15
|
attr_reader :prefix
|
16
16
|
|
17
17
|
# Initialize a Shell
|
18
18
|
#
|
19
19
|
# @api public
|
20
|
-
def initialize(input=stdin, output=stdout, options={})
|
20
|
+
def initialize(input = stdin, output = stdout, options = {})
|
21
21
|
@input = input
|
22
22
|
@output = output
|
23
23
|
@prefix = options.fetch(:prefix) { '' }
|
@@ -48,7 +48,7 @@ module TTY
|
|
48
48
|
question.prompt(statement)
|
49
49
|
end
|
50
50
|
|
51
|
-
# A shortcut method to ask the user positive question and return
|
51
|
+
# A shortcut method to ask the user positive question and return
|
52
52
|
# true for 'yes' reply.
|
53
53
|
#
|
54
54
|
# @return [Boolean]
|
@@ -79,7 +79,7 @@ module TTY
|
|
79
79
|
# @return [String]
|
80
80
|
#
|
81
81
|
# @api public
|
82
|
-
def say(message=
|
82
|
+
def say(message = '', options = {})
|
83
83
|
message = message.to_str
|
84
84
|
return unless message.length > 0
|
85
85
|
|
@@ -100,14 +100,14 @@ module TTY
|
|
100
100
|
# @api public
|
101
101
|
def confirm(*args)
|
102
102
|
options = Utils.extract_options!(args)
|
103
|
-
args.each { |message| say message, options.merge(:
|
103
|
+
args.each { |message| say message, options.merge(color: :green) }
|
104
104
|
end
|
105
105
|
|
106
106
|
# Print statement(s) out in yellow color.
|
107
107
|
#
|
108
108
|
# @example
|
109
109
|
# shell.warn "This action can have dire consequences"
|
110
|
-
# shell.warn "Carefull young apprentice", "This is potentially dangerous
|
110
|
+
# shell.warn "Carefull young apprentice", "This is potentially dangerous"
|
111
111
|
#
|
112
112
|
# @param [Array] messages
|
113
113
|
#
|
@@ -116,7 +116,7 @@ module TTY
|
|
116
116
|
# @api public
|
117
117
|
def warn(*args)
|
118
118
|
options = Utils.extract_options!(args)
|
119
|
-
args.each { |message| say message, options.merge(:
|
119
|
+
args.each { |message| say message, options.merge(color: :yellow) }
|
120
120
|
end
|
121
121
|
|
122
122
|
# Print statement(s) out in red color.
|
@@ -132,7 +132,7 @@ module TTY
|
|
132
132
|
# @api public
|
133
133
|
def error(*args)
|
134
134
|
options = Utils.extract_options!(args)
|
135
|
-
args.each { |message| say message, options.merge(:
|
135
|
+
args.each { |message| say message, options.merge(color: :red) }
|
136
136
|
end
|
137
137
|
|
138
138
|
# Takes the string provided by the user and compare it with other possible
|
@@ -157,7 +157,7 @@ module TTY
|
|
157
157
|
# @return [String]
|
158
158
|
#
|
159
159
|
# @api public
|
160
|
-
def suggest(message, possibilities, options={})
|
160
|
+
def suggest(message, possibilities, options = {})
|
161
161
|
suggestion = Suggestion.new(options)
|
162
162
|
say(suggestion.suggest(message, possibilities))
|
163
163
|
end
|
@@ -187,17 +187,25 @@ module TTY
|
|
187
187
|
stdout.tty?
|
188
188
|
end
|
189
189
|
|
190
|
+
# Return standard in
|
191
|
+
#
|
192
|
+
# @api private
|
190
193
|
def stdin
|
191
194
|
$stdin
|
192
195
|
end
|
193
196
|
|
197
|
+
# Return standard out
|
198
|
+
#
|
199
|
+
# @api private
|
194
200
|
def stdout
|
195
201
|
$stdout
|
196
202
|
end
|
197
203
|
|
204
|
+
# Return standard error
|
205
|
+
#
|
206
|
+
# @api private
|
198
207
|
def stderr
|
199
208
|
$stderr
|
200
209
|
end
|
201
|
-
|
202
210
|
end # Shell
|
203
211
|
end # TTY
|
data/lib/tty/shell/question.rb
CHANGED
@@ -1,18 +1,17 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
require 'date'
|
4
4
|
|
5
5
|
module TTY
|
6
6
|
# A class responsible for shell prompt interactions.
|
7
7
|
class Shell
|
8
|
-
|
9
|
-
# A class representing a question.
|
8
|
+
# A class representing a command line question
|
10
9
|
class Question
|
11
10
|
include TTY::Shell::ResponseDelegation
|
12
11
|
|
13
|
-
PREFIX =
|
14
|
-
MULTIPLE_PREFIX =
|
15
|
-
ERROR_PREFIX =
|
12
|
+
PREFIX = ' + '
|
13
|
+
MULTIPLE_PREFIX = ' * '
|
14
|
+
ERROR_PREFIX = ' ERROR:'
|
16
15
|
|
17
16
|
# Store statement.
|
18
17
|
#
|
@@ -41,16 +40,6 @@ module TTY
|
|
41
40
|
|
42
41
|
attr_reader :error
|
43
42
|
|
44
|
-
# Returns echo mode
|
45
|
-
#
|
46
|
-
# @api public
|
47
|
-
attr_reader :echo
|
48
|
-
|
49
|
-
# Returns character mask
|
50
|
-
#
|
51
|
-
# @api public
|
52
|
-
attr_reader :mask
|
53
|
-
|
54
43
|
# Returns character mode
|
55
44
|
#
|
56
45
|
# @api public
|
@@ -63,16 +52,18 @@ module TTY
|
|
63
52
|
# Initialize a Question
|
64
53
|
#
|
65
54
|
# @api public
|
66
|
-
def initialize(shell, options={})
|
67
|
-
@shell
|
68
|
-
@required
|
69
|
-
@echo
|
70
|
-
@mask
|
71
|
-
@character
|
72
|
-
@in
|
73
|
-
@modifier
|
74
|
-
@valid_values
|
75
|
-
@validation
|
55
|
+
def initialize(shell, options = {})
|
56
|
+
@shell = shell || Shell.new
|
57
|
+
@required = options.fetch(:required) { false }
|
58
|
+
@echo = options.fetch(:echo) { true }
|
59
|
+
@mask = options.fetch(:mask) { false }
|
60
|
+
@character = options.fetch(:character) { false }
|
61
|
+
@in = options.fetch(:in) { false }
|
62
|
+
@modifier = Modifier.new options.fetch(:modifier) { [] }
|
63
|
+
@valid_values = options.fetch(:valid) { [] }
|
64
|
+
@validation = Validation.new options.fetch(:validation) { nil }
|
65
|
+
@default_value = nil
|
66
|
+
@error = false
|
76
67
|
end
|
77
68
|
|
78
69
|
# Set a new prompt
|
@@ -81,6 +72,7 @@ module TTY
|
|
81
72
|
#
|
82
73
|
# @return [self]
|
83
74
|
#
|
75
|
+
# @api public
|
84
76
|
def prompt(message)
|
85
77
|
self.statement = message
|
86
78
|
shell.say shell.prefix + statement
|
@@ -91,7 +83,7 @@ module TTY
|
|
91
83
|
#
|
92
84
|
# @api public
|
93
85
|
def default(value)
|
94
|
-
return self if value ==
|
86
|
+
return self if value == ''
|
95
87
|
@default_value = value
|
96
88
|
self
|
97
89
|
end
|
@@ -136,7 +128,7 @@ module TTY
|
|
136
128
|
# @return [Question]
|
137
129
|
#
|
138
130
|
# @api public
|
139
|
-
def validate(value=nil, &block)
|
131
|
+
def validate(value = nil, &block)
|
140
132
|
@validation = Validation.new(value || block)
|
141
133
|
self
|
142
134
|
end
|
@@ -176,7 +168,7 @@ module TTY
|
|
176
168
|
# Setup behaviour when error(s) occur
|
177
169
|
#
|
178
170
|
# @api public
|
179
|
-
def on_error(action=nil)
|
171
|
+
def on_error(action = nil)
|
180
172
|
@error = action
|
181
173
|
self
|
182
174
|
end
|
@@ -192,8 +184,8 @@ module TTY
|
|
192
184
|
# that the entered characters are not echoed back to the screen.
|
193
185
|
#
|
194
186
|
# @api public
|
195
|
-
def echo(value=
|
196
|
-
return @echo if
|
187
|
+
def echo(value = nil)
|
188
|
+
return @echo if value.nil?
|
197
189
|
@echo = value
|
198
190
|
self
|
199
191
|
end
|
@@ -212,9 +204,9 @@ module TTY
|
|
212
204
|
# @return [self]
|
213
205
|
#
|
214
206
|
# @api public
|
215
|
-
def mask(
|
216
|
-
return @mask if
|
217
|
-
@mask =
|
207
|
+
def mask(char = nil)
|
208
|
+
return @mask if char.nil?
|
209
|
+
@mask = char
|
218
210
|
self
|
219
211
|
end
|
220
212
|
|
@@ -234,8 +226,8 @@ module TTY
|
|
234
226
|
# @return [self]
|
235
227
|
#
|
236
228
|
# @api public
|
237
|
-
def
|
238
|
-
return @character if
|
229
|
+
def char(value = nil)
|
230
|
+
return @character if value.nil?
|
239
231
|
@character = value
|
240
232
|
self
|
241
233
|
end
|
@@ -254,8 +246,8 @@ module TTY
|
|
254
246
|
# @param [String] value
|
255
247
|
#
|
256
248
|
# @api public
|
257
|
-
def in(value=
|
258
|
-
return @in if
|
249
|
+
def in(value = nil)
|
250
|
+
return @in if value.nil?
|
259
251
|
@in = TTY::Coercer::Range.coerce value
|
260
252
|
self
|
261
253
|
end
|
@@ -278,13 +270,13 @@ module TTY
|
|
278
270
|
# @api private
|
279
271
|
def evaluate_response(value)
|
280
272
|
return default_value if !value && default?
|
273
|
+
check_required(value)
|
274
|
+
return if value.nil?
|
281
275
|
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
validation.valid_value? value
|
287
|
-
modifier.apply_to value
|
276
|
+
check_valid(value) unless valid_values.empty?
|
277
|
+
within?(value)
|
278
|
+
validation.valid_value?(value)
|
279
|
+
modifier.apply_to(value)
|
288
280
|
end
|
289
281
|
|
290
282
|
private
|
@@ -294,7 +286,7 @@ module TTY
|
|
294
286
|
# @api private
|
295
287
|
def check_required(value)
|
296
288
|
if required? && !default? && value.nil?
|
297
|
-
|
289
|
+
fail ArgumentRequired, 'No value provided for required'
|
298
290
|
end
|
299
291
|
end
|
300
292
|
|
@@ -304,7 +296,8 @@ module TTY
|
|
304
296
|
def check_valid(value)
|
305
297
|
if Array(value).all? { |val| valid_values.include? val }
|
306
298
|
return value
|
307
|
-
else
|
299
|
+
else
|
300
|
+
fail InvalidArgument, "Valid values are: #{valid_values.join(', ')}"
|
308
301
|
end
|
309
302
|
end
|
310
303
|
|
@@ -314,11 +307,11 @@ module TTY
|
|
314
307
|
def within?(value)
|
315
308
|
if in? && value
|
316
309
|
if @in.include?(value)
|
317
|
-
else
|
310
|
+
else
|
311
|
+
fail InvalidArgument, "Value #{value} is not included in the range #{@in}"
|
318
312
|
end
|
319
313
|
end
|
320
314
|
end
|
321
|
-
|
322
315
|
end # Question
|
323
316
|
end # Shell
|
324
317
|
end # TTY
|