Module: LanguageOperator::CLI::Helpers::UserPrompts
- Defined in:
- lib/language_operator/cli/helpers/user_prompts.rb
Overview
Helper module for user confirmation prompts and interactive input. Consolidates the repeated confirmation pattern used throughout commands.
Class Method Summary collapse
-
.ask(prompt, default: nil) ⇒ String
Ask user for text input.
-
.confirm(message, force: false) ⇒ Boolean
Ask user for confirmation rubocop:disable Naming/PredicateMethod.
-
.confirm!(message, force: false, cancel_message: 'Operation cancelled') ⇒ void
Ask user for confirmation and exit if not confirmed.
-
.select(prompt, options) ⇒ String
Ask user to select from options.
Class Method Details
.ask(prompt, default: nil) ⇒ String
Ask user for text input
40 41 42 43 44 45 |
# File 'lib/language_operator/cli/helpers/user_prompts.rb', line 40 def self.ask(prompt, default: nil) prompt_text = default ? "#{prompt} [#{default}]" : prompt print "#{prompt_text}: " response = $stdin.gets&.chomp || '' response.empty? && default ? default : response end |
.confirm(message, force: false) ⇒ Boolean
Ask user for confirmation rubocop:disable Naming/PredicateMethod
14 15 16 17 18 19 20 21 |
# File 'lib/language_operator/cli/helpers/user_prompts.rb', line 14 def self.confirm(, force: false) return true if force print "#{message} (y/N): " response = $stdin.gets&.chomp || '' puts response.downcase == 'y' end |
.confirm!(message, force: false, cancel_message: 'Operation cancelled') ⇒ void
This method returns an undefined value.
Ask user for confirmation and exit if not confirmed
29 30 31 32 33 34 |
# File 'lib/language_operator/cli/helpers/user_prompts.rb', line 29 def self.confirm!(, force: false, cancel_message: 'Operation cancelled') return if confirm(, force: force) puts exit 0 end |
.select(prompt, options) ⇒ String
Ask user to select from options
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/language_operator/cli/helpers/user_prompts.rb', line 51 def self.select(prompt, ) loop do puts prompt .each_with_index do |option, index| puts " #{index + 1}. #{option}" end print "\nSelect (1-#{options.length}): " input = $stdin.gets&.chomp || '' # Allow user to quit/cancel if input.downcase.match?(/^q(uit)?$/) puts 'Selection cancelled' exit 0 end selection = input.to_i return [selection - 1] if selection.between?(1, .length) puts 'Invalid selection. Please try again.' puts end end |