Class: CreateRailsApp::UI::Prompter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(out: $stdout) ⇒ Prompter

Returns a new instance of Prompter.

Parameters:

  • (defaults to: $stdout)

    output stream



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.

Parameters:

  • (defaults to: nil)

Returns:



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: options, default: default, filter_ui: false)
rescue BackKeyPressed
  Wizard::BACK
end

#confirm(question, default: true) ⇒ Boolean

Prompts for yes/no confirmation.

Parameters:

  • (defaults to: true)

Returns:



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.

Parameters:

Yields:

  • block executed inside the frame



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.

Parameters:

  • message with optional cli-ui formatting tags



99
100
101
# File 'lib/create_rails_app/ui/prompter.rb', line 99

def say(message)
  @out.puts(::CLI::UI.fmt(message))
end

#text(question, default: nil, allow_empty: true) ⇒ String

Prompts for free-text input.

Parameters:

  • (defaults to: nil)
  • (defaults to: true)

Returns:



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