Module: SnippetCli::WizardHelpers::PromptHelpers
- Included in:
- ReplacementTextCollector, ReplacementValidator, ReplacementWizard, TriggerResolver, VarBuilder, MatchFileSelector
- Defined in:
- lib/snippet_cli/wizard_helpers/prompt_helpers.rb
Overview
Gum prompt primitives with Ctrl+C detection via WizardInterrupted.
Instance Method Summary collapse
-
#collect_search_terms ⇒ Object
Prompts for search terms via a multiline write block.
-
#confirm!(text) ⇒ Object
Wraps Gum.confirm and checks $?.exitstatus for 130 (Ctrl+C).
-
#list_confirm!(label, rows, headers, question) ⇒ Object
Renders a table of collected items and asks a follow-up question, without a border.
-
#optional_prompt(question) ⇒ Object
Confirms a question then collects a value via the block, or returns nil if declined.
-
#prompt!(value) ⇒ Object
Returns the value if non-nil; raises WizardInterrupted otherwise.
Instance Method Details
#collect_search_terms ⇒ Object
Prompts for search terms via a multiline write block. Returns an empty array if the user declines.
49 50 51 52 53 54 |
# File 'lib/snippet_cli/wizard_helpers/prompt_helpers.rb', line 49 def collect_search_terms return [] unless confirm!('Add search terms?') raw = prompt!(Gum.write(header: 'Put one search term per line', prompt_style: UI::PROMPT_STYLE, header_style: UI::PROMPT_STYLE)) raw.to_s.lines.map(&:chomp).reject(&:empty?) end |
#confirm!(text) ⇒ Object
Wraps Gum.confirm and checks $?.exitstatus for 130 (Ctrl+C). Gum.confirm uses system() which swallows SIGINT and returns false, making it indistinguishable from the user answering “no” — except that $? records the child’s exit code 130. SIGINT can also raise Interrupt in Ruby before $? is read.
26 27 28 29 30 31 32 33 34 |
# File 'lib/snippet_cli/wizard_helpers/prompt_helpers.rb', line 26 def confirm!(text) result = Gum.confirm(text, prompt_style: UI::PROMPT_STYLE) raise WizardInterrupted if result.nil? raise WizardInterrupted if $CHILD_STATUS.respond_to?(:exitstatus) && $CHILD_STATUS.exitstatus == 130 result rescue Interrupt raise WizardInterrupted end |
#list_confirm!(label, rows, headers, question) ⇒ Object
Renders a table of collected items and asks a follow-up question, without a border.
37 38 39 40 |
# File 'lib/snippet_cli/wizard_helpers/prompt_helpers.rb', line 37 def list_confirm!(label, rows, headers, question) table = TableFormatter.render(rows, headers: headers) confirm!("Current #{label}s:\n\n#{table}\n\n#{question}") end |
#optional_prompt(question) ⇒ Object
Confirms a question then collects a value via the block, or returns nil if declined.
43 44 45 |
# File 'lib/snippet_cli/wizard_helpers/prompt_helpers.rb', line 43 def optional_prompt(question) yield if confirm!(question) end |
#prompt!(value) ⇒ Object
Returns the value if non-nil; raises WizardInterrupted otherwise. Gum.choose / .input / .filter / .write return nil on Ctrl+C.
13 14 15 16 17 18 19 |
# File 'lib/snippet_cli/wizard_helpers/prompt_helpers.rb', line 13 def prompt!(value) raise WizardInterrupted if value.nil? value rescue Interrupt raise WizardInterrupted end |