Class: CreateRubyGem::UI::Prompter

Inherits:
Object
  • Object
show all
Defined in:
lib/create_ruby_gem/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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out: $stdout) ⇒ Prompter

Returns a new instance of Prompter.

Parameters:

  • out (IO) (defaults to: $stdout)

    output stream



24
25
26
# File 'lib/create_ruby_gem/ui/prompter.rb', line 24

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.



18
19
20
21
# File 'lib/create_ruby_gem/ui/prompter.rb', line 18

def self.setup!
  ::CLI::UI::StdoutRouter.enable
  BackNavigationPatch.apply!
end

Instance Method Details

#choose(question, options:, default: nil) ⇒ String

Presents a single-choice list.

Parameters:

  • question (String)
  • options (Array<String>)
  • default (String, nil) (defaults to: nil)

Returns:



43
44
45
46
47
# File 'lib/create_ruby_gem/ui/prompter.rb', line 43

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:

  • question (String)
  • default (Boolean) (defaults to: true)

Returns:

  • (Boolean)


64
65
66
# File 'lib/create_ruby_gem/ui/prompter.rb', line 64

def confirm(question, default: true)
  ::CLI::UI.confirm(question, default: default)
end

#frame(title) { ... } ⇒ void

This method returns an undefined value.

Opens a visual frame with a title.

Parameters:

  • title (String)

Yields:

  • block executed inside the frame



33
34
35
# File 'lib/create_ruby_gem/ui/prompter.rb', line 33

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 (String)

    message with optional cli-ui formatting tags



72
73
74
# File 'lib/create_ruby_gem/ui/prompter.rb', line 72

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

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

Prompts for free-text input.

Parameters:

  • question (String)
  • default (String, nil) (defaults to: nil)
  • allow_empty (Boolean) (defaults to: true)

Returns:

  • (String)


55
56
57
# File 'lib/create_ruby_gem/ui/prompter.rb', line 55

def text(question, default: nil, allow_empty: true)
  ::CLI::UI.ask(question, default: default, allow_empty: allow_empty)
end