Class: Prompts::Prompt

Inherits:
Object
  • Object
show all
Defined in:
lib/prompts/prompt.rb

Direct Known Subclasses

ConfirmPrompt, PausePrompt, SelectPrompt, TextPrompt

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label: nil, prompt: "> ", hint: nil, default: nil, required: false, validate: nil) ⇒ Prompt

Returns a new instance of Prompt.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/prompts/prompt.rb', line 13

def initialize(label: nil, prompt: "> ", hint: nil, default: nil, required: false, validate: nil)
  @label = label
  @prompt = prompt
  @hint = hint
  @default = default
  @required = required
  @validate = validate

  @content = nil
  @error = nil
  @attempts = 0
  @instructions = nil
  @validations = []
  @choice = nil
  @content_prepared = false
end

Class Method Details

.ask(label: nil, prompt: "> ", hint: nil, default: nil, required: false, validate: nil) {|instance| ... } ⇒ Object

Yields:

  • (instance)


7
8
9
10
11
# File 'lib/prompts/prompt.rb', line 7

def self.ask(label: nil, prompt: "> ", hint: nil, default: nil, required: false, validate: nil)
  instance = new(label: label, prompt: prompt, hint: hint, default: default, required: required, validate: validate)
  yield instance if block_given?
  instance.ask
end

Instance Method Details

#askObject

standard:enable Style/TrivialAccessors



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/prompts/prompt.rb', line 50

def ask
  prepare_content if !@content_prepared
  prepare_default if @default
  prepare_validations

  loop do
    @content.render
    *initial_prompt_lines, last_prompt_line = formatted_prompt
    puts initial_prompt_lines.join("\n") if initial_prompt_lines.any?
    response = Reline.readline(last_prompt_line, _history = false).chomp
    @choice = resolve_choice_from(response)

    if (@error = ensure_validity(response))
      @content.reset!
      @content.paragraph Fmt("%{error}|>red|>bold", error: @error + " Try again (×#{@attempts})...")
      @attempts += 1
      next
    else
      break @choice
    end
  end

  @choice
rescue Interrupt
  exit 0
end

#content {|@content| ... } ⇒ Object

Yields:



30
31
32
33
34
# File 'lib/prompts/prompt.rb', line 30

def content(&block)
  @content ||= Prompts::Content.new
  yield @content
  @content
end

#default(default) ⇒ Object



45
46
47
# File 'lib/prompts/prompt.rb', line 45

def default(default)
  @default = default
end

#hint(hint) ⇒ Object



41
42
43
# File 'lib/prompts/prompt.rb', line 41

def hint(hint)
  @hint = hint
end

#label(label) ⇒ Object

standard:disable Style/TrivialAccessors



37
38
39
# File 'lib/prompts/prompt.rb', line 37

def label(label)
  @label = label
end

#prepare_contentObject



81
82
83
84
85
86
87
88
# File 'lib/prompts/prompt.rb', line 81

def prepare_content
  @content ||= Prompts::Content.new
  @content.paragraph formatted_label if @label
  @content.paragraph formatted_hint if @hint
  @content.paragraph formatted_error if @error
  @content_prepared = true
  @content
end

#prepend_content(*lines) ⇒ Object



77
78
79
# File 'lib/prompts/prompt.rb', line 77

def prepend_content(*lines)
  @content.prepend(*lines)
end