lib/cli/ui/prompt.rb in cli-ui-1.2.1 vs lib/cli/ui/prompt.rb in cli-ui-1.2.2
- old
+ new
@@ -1,5 +1,6 @@
+# coding: utf-8
require 'cli/ui'
require 'readline'
module CLI
module UI
@@ -28,15 +29,19 @@
#
# * +:options+ - Options that the user may select from. Will use +InteractiveOptions+ to do so.
# * +:default+ - The default answer to the question (e.g. they just press enter and don't input anything)
# * +:is_file+ - Tells the input to use file auto-completion (tab completion)
# * +:allow_empty+ - Allows the answer to be empty
+ # * +:multiple+ - Allow multiple options to be selected
+ # * +:filter_ui+ - Enable option filtering (default: true)
+ # * +:select_ui+ - Enable long-form option selection (default: true)
#
# Note:
# * +:options+ or providing a +Block+ conflicts with +:default+ and +:is_file+, you cannot set options with either of these keywords
# * +:default+ conflicts with +:allow_empty:, you cannot set these together
# * +:options+ conflicts with providing a +Block+ , you may only set one
+ # * +:multiple+ can only be used with +:options+ or a +Block+; it is ignored, otherwise.
#
# ==== Block (optional)
#
# * A Proc that provides a +OptionsHandler+ and uses the public +:option+ method to add options and their
# respective handlers
@@ -69,17 +74,17 @@
# handler.option('go') { |selection| selection }
# handler.option('ruby') { |selection| selection }
# handler.option('python') { |selection| selection }
# end
#
- def ask(question, options: nil, default: nil, is_file: nil, allow_empty: true, multiple: false, &options_proc)
+ def ask(question, options: nil, default: nil, is_file: nil, allow_empty: true, multiple: false, filter_ui: true, select_ui: true, &options_proc)
if ((options || block_given?) && (default || is_file))
raise(ArgumentError, 'conflicting arguments: options provided with default or is_file')
end
if options || block_given?
- ask_interactive(question, options, multiple: multiple, &options_proc)
+ ask_interactive(question, options, multiple: multiple, filter_ui: filter_ui, select_ui: select_ui, &options_proc)
else
ask_free_form(question, default, is_file, allow_empty)
end
end
@@ -89,12 +94,14 @@
# ==== Example Usage:
#
# Confirmation question
# CLI::UI::Prompt.confirm('Is the sky blue?')
#
- def confirm(question)
- ask_interactive(question, %w(yes no)) == 'yes'
+ # CLI::UI::Prompt.confirm('Do a dangerous thing?', default: false)
+ #
+ def confirm(question, default: true)
+ ask_interactive(question, default ? %w(yes no) : %w(no yes), filter_ui: false) == 'yes'
end
private
def ask_free_form(question, default, is_file, allow_empty)
@@ -119,21 +126,23 @@
return line
end
end
end
- def ask_interactive(question, options = nil, multiple: false)
+ def ask_interactive(question, options = nil, multiple: false, filter_ui: true, select_ui: true)
raise(ArgumentError, 'conflicting arguments: options and block given') if options && block_given?
options ||= if block_given?
handler = OptionsHandler.new
yield handler
handler.options
end
raise(ArgumentError, 'insufficient options') if options.nil? || options.size < 2
instructions = (multiple ? "Toggle options. " : "") + "Choose with ↑ ↓ ⏎"
+ instructions += ", filter with 'f'" if filter_ui
+ instructions += ", enter option with 'e'" if select_ui and options.size > 9
puts_question("#{question} {{yellow:(#{instructions})}}")
resp = interactive_prompt(options, multiple: multiple)
# Clear the line
print ANSI.previous_line + ANSI.clear_to_end_of_line
@@ -193,13 +202,14 @@
# because Readline is a C library, CLI::UI's hooks into $stdout don't
# work. We could work around this by having CLI::UI use a pipe and a
# thread to manage output, but the current strategy feels like a
# better tradeoff.
prefix = CLI::UI.with_frame_color(:blue) { CLI::UI::Frame.prefix }
- prompt = prefix + CLI::UI.fmt('{{blue:> }}{{yellow:')
+ prompt = prefix + CLI::UI.fmt('{{blue:> }}') + CLI::UI::Color::YELLOW.code
begin
line = Readline.readline(prompt, true)
+ print CLI::UI::Color::RESET.code
line.to_s.chomp
rescue Interrupt
CLI::UI.raw { STDERR.puts('^C' + CLI::UI::Color::RESET.code) }
raise
end