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

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(message, 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!(message, force: false, cancel_message: 'Operation cancelled')
  return if confirm(message, force: force)

  puts cancel_message
  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, options)
  loop do
    puts prompt
    options.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 options[selection - 1] if selection.between?(1, options.length)

    puts 'Invalid selection. Please try again.'
    puts
  end
end