Class: Pvectl::Config::SimplePrompt

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/config/wizard.rb

Overview

Simple fallback prompt when TTY::Prompt is not available.

Provides basic input methods without fancy formatting.

Instance Method Summary collapse

Instance Method Details

#ask(question, default: nil, required: false, convert: nil) ⇒ Object

Asks a question and returns the answer.

Parameters:

  • question (String)

    question to ask

  • default (Object, nil) (defaults to: nil)

    default value

  • required (Boolean) (defaults to: false)

    whether a non-empty answer is required

  • convert (Symbol, nil) (defaults to: nil)

    type conversion (:int, :float, :bool)

Returns:

  • (Object)

    user input (converted if requested)



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/pvectl/config/wizard.rb', line 223

def ask(question, default: nil, required: false, convert: nil, **)
  loop do
    print "#{question} "
    print "[#{default}] " if default
    input = $stdin.gets&.chomp
    value = input.nil? || input.empty? ? default : input

    if required && (value.nil? || value.to_s.empty?)
      puts "Value is required."
      next
    end

    return convert_value(value, convert)
  end
end

#error(_message) ⇒ Object



286
# File 'lib/pvectl/config/wizard.rb', line 286

def error(_message); end

#mask(question) ⇒ String

Asks for masked input (password).

Parameters:

  • question (String)

    question to ask

Returns:

  • (String)

    user input



270
271
272
273
274
275
276
277
278
279
280
# File 'lib/pvectl/config/wizard.rb', line 270

def mask(question, **)
  print "#{question} "
  begin
    $stdin.noecho { $stdin.gets&.chomp }
  rescue NoMethodError
    # Fallback if noecho not available
    $stdin.gets&.chomp
  ensure
    puts
  end
end

#ok(_message) ⇒ Object



282
# File 'lib/pvectl/config/wizard.rb', line 282

def ok(_message); end

#select(question, choices) ⇒ Object

Prompts for selection from choices.

Parameters:

  • question (String)

    question to ask

  • choices (Array)

    list of choices

Returns:

  • (Object)

    selected value



254
255
256
257
258
259
260
261
262
263
264
# File 'lib/pvectl/config/wizard.rb', line 254

def select(question, choices, **)
  puts question
  choices.each_with_index do |choice, index|
    name = choice.is_a?(Hash) ? choice[:name] : choice
    puts "  #{index + 1}. #{name}"
  end
  print "Enter number: "
  input = $stdin.gets&.chomp&.to_i || 1
  choice = choices[input - 1] || choices.first
  choice.is_a?(Hash) ? choice[:value] : choice
end

#warn(_message) ⇒ Object



284
# File 'lib/pvectl/config/wizard.rb', line 284

def warn(_message); end

#yes?(question) ⇒ Boolean

Asks a yes/no question.

Parameters:

  • question (String)

    question to ask

Returns:

  • (Boolean)

    true for yes



243
244
245
246
247
# File 'lib/pvectl/config/wizard.rb', line 243

def yes?(question, **)
  print "#{question} (y/n) "
  input = $stdin.gets&.chomp&.downcase
  %w[y yes].include?(input)
end