Module: SnippetCli::WizardHelpers::ValidationLoop

Included in:
ReplacementTextCollector, ReplacementWizard, TriggerResolver
Defined in:
lib/snippet_cli/wizard_helpers/validation_loop.rb

Overview

Loop-until-valid prompt abstractions.

Instance Method Summary collapse

Instance Method Details

#prompt_non_empty(warning_message, &prompt_block) ⇒ Object

Loops until the block yields a non-empty string. Shows warning_message as a transient warning on empty input.



25
26
27
28
29
30
# File 'lib/snippet_cli/wizard_helpers/validation_loop.rb', line 25

def prompt_non_empty(warning_message, &prompt_block)
  prompt_until_valid do
    value = prompt_block.call
    [value, value.strip.empty? ? warning_message : nil]
  end
end

#prompt_until_validObject

General loop-until-valid primitive. The block must yield [value, error_or_nil]. When error is a String, shows it as a transient warning. When error is a Callable (e.g. a lambda), uses it directly as the clear function. Loops until the block yields a nil error.



12
13
14
15
16
17
18
19
20
21
# File 'lib/snippet_cli/wizard_helpers/validation_loop.rb', line 12

def prompt_until_valid
  clear = nil
  loop do
    value, error = yield
    clear&.call
    return value if error.nil?

    clear = error.respond_to?(:call) ? error : UI.transient_warning(error)
  end
end