Class: CreateRailsApp::UI::Prompter
- Inherits:
-
Object
- Object
- CreateRailsApp::UI::Prompter
- Defined in:
- lib/create_rails_app/ui/prompter.rb
Overview
Thin wrapper around cli-ui for all user interaction.
Every prompt the wizard issues goes through this class, making it easy to inject a test double.
Defined Under Namespace
Modules: ReadCharPatch
Constant Summary collapse
- CTRL_B =
"\u0002"
Class Method Summary collapse
-
.setup! ⇒ void
Enables the
cli-uistdout router and applies the Ctrl+B patch.
Instance Method Summary collapse
-
#choose(question, options:, default: nil) ⇒ String
Presents a single-choice list.
-
#confirm(question, default: true) ⇒ Boolean
Prompts for yes/no confirmation.
-
#frame(title) { ... } ⇒ void
Opens a visual frame with a title.
-
#initialize(out: $stdout) ⇒ Prompter
constructor
A new instance of Prompter.
-
#say(message) ⇒ void
Prints a formatted message to the output stream.
-
#text(question, default: nil, allow_empty: true) ⇒ String
Prompts for free-text input.
Constructor Details
#initialize(out: $stdout) ⇒ Prompter
Returns a new instance of Prompter.
45 46 47 |
# File 'lib/create_rails_app/ui/prompter.rb', line 45 def initialize(out: $stdout) @out = out end |
Class Method Details
.setup! ⇒ void
This method returns an undefined value.
Enables the cli-ui stdout router and applies the Ctrl+B patch.
Call once before creating a Prompter instance. Idempotent.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/create_rails_app/ui/prompter.rb', line 31 def self.setup! ::CLI::UI::StdoutRouter.enable unless ::CLI::UI::Prompt.respond_to?(:read_char) raise Error, 'CLI::UI::Prompt does not respond to read_char; back-navigation patch cannot be applied' end singleton = ::CLI::UI::Prompt.singleton_class return if singleton.ancestors.include?(ReadCharPatch) singleton.prepend(ReadCharPatch) end |
Instance Method Details
#choose(question, options:, default: nil) ⇒ String
Presents a single-choice list.
64 65 66 67 68 |
# File 'lib/create_rails_app/ui/prompter.rb', line 64 def choose(question, options:, default: nil) ::CLI::UI.ask(question, options: , default: default, filter_ui: false) rescue BackKeyPressed Wizard::BACK end |
#confirm(question, default: true) ⇒ Boolean
Prompts for yes/no confirmation.
87 88 89 90 91 92 93 |
# File 'lib/create_rails_app/ui/prompter.rb', line 87 def confirm(question, default: true) ::CLI::UI.confirm(question, default: default) rescue BackKeyPressed # Confirm callers (preset save, overwrite prompts) don't handle BACK; # swallowing the key press and returning the default is intentional. default end |
#frame(title) { ... } ⇒ void
This method returns an undefined value.
Opens a visual frame with a title.
54 55 56 |
# File 'lib/create_rails_app/ui/prompter.rb', line 54 def frame(title, &) ::CLI::UI::Frame.open(title, &) end |
#say(message) ⇒ void
This method returns an undefined value.
Prints a formatted message to the output stream.
99 100 101 |
# File 'lib/create_rails_app/ui/prompter.rb', line 99 def say() @out.puts(::CLI::UI.fmt()) end |
#text(question, default: nil, allow_empty: true) ⇒ String
Prompts for free-text input.
76 77 78 79 80 |
# File 'lib/create_rails_app/ui/prompter.rb', line 76 def text(question, default: nil, allow_empty: true) ::CLI::UI.ask(question, default: default, allow_empty: allow_empty) rescue BackKeyPressed Wizard::BACK end |