Module: Dev::UI::Prompt
- Defined in:
- lib/dev/ui/prompt.rb
Class Method Summary collapse
-
.ask(question, options: nil, default: nil, is_file: nil, allow_empty: true) ⇒ Object
Ask a user a question with either free form answer or a set of answers Do not use this method for yes/no questions.
-
.confirm(question) ⇒ Object
Asks the user a yes/no question.
Class Method Details
.ask(question, options: nil, default: nil, is_file: nil, allow_empty: true) ⇒ Object
Ask a user a question with either free form answer or a set of answers Do not use this method for yes/no questions. Use confirm Can use arrows, y/n, numbers, and vim bindings to control
-
Handles free form answers (options are nil)
-
Handles default answers for free form text
-
Handles file auto completion for file input
-
Handles interactively choosing answers using
InteractivePrompt

Attributes
-
question- (required) The question to ask the user
Options
-
:options- Options to ask the user. Will useInteractivePromptto 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
Note:
-
:optionsconflicts with:defaultand:is_file, you cannot set options with either of these keywords -
:defaultconflicts with +:allow_empty:, you cannot set these together
Example Usage:
Free form question
Dev::UI::Prompt.ask('What color is the sky?')
Free form question with a file answer
Dev::UI::Prompt.ask('Where is your Gemfile located?', is_file: true)
Free form question with a default answer
Dev::UI::Prompt.ask('What color is the sky?', default: 'blue')
Free form question when the answer can be empty
Dev::UI::Prompt.ask('What is your opinion on this question?', allow_empty: true)
Question with answers
Dev::UI::Prompt.ask('What kind of project is this?', options: %w(rails go ruby python))
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/dev/ui/prompt.rb', line 52 def ask(question, options: nil, default: nil, is_file: nil, allow_empty: true) if (default && !allow_empty) || ( && (default || is_file)) raise(ArgumentError, 'conflicting arguments') end if default puts_question("#{question} (empty = #{default})") elsif puts_question("#{question} {{yellow:(choose with ↑ ↓ ⏎)}}") else puts_question(question) end return InteractivePrompt.call() if loop do line = readline(is_file: is_file) if line.empty? && default write_default_over_empty_input(default) return default end if !line.empty? || allow_empty return line end end end |
.confirm(question) ⇒ Object
89 90 91 92 |
# File 'lib/dev/ui/prompt.rb', line 89 def confirm(question) puts_question("#{question} {{yellow:(choose with ↑ ↓ ⏎)}}") InteractivePrompt.call(%w(yes no)) == 'yes' end |