Class: Pvectl::Config::SimplePrompt
- Inherits:
-
Object
- Object
- Pvectl::Config::SimplePrompt
- Defined in:
- lib/pvectl/config/wizard.rb,
sig/pvectl/config/wizard.rbs
Overview
Simple fallback prompt when TTY::Prompt is not available.
Provides basic input methods without fancy formatting.
Instance Method Summary collapse
-
#ask(question, default: nil, required: false, convert: nil) ⇒ Object
Asks a question and returns the answer.
-
#convert_value(value, type) ⇒ Object
Converts a string value to the requested type.
- #error(_message) ⇒ void
-
#mask(question) ⇒ String
Asks for masked input (password).
- #ok(_message) ⇒ void
-
#select(question, choices) ⇒ Object
Prompts for selection from choices.
- #warn(_message) ⇒ void
-
#yes?(question) ⇒ Boolean
Asks a yes/no question.
Instance Method Details
#ask(question, default: nil, required: false, convert: nil) ⇒ Object
Asks a question and returns the answer.
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 |
#convert_value(value, type) ⇒ Object
Converts a string value to the requested type.
295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/pvectl/config/wizard.rb', line 295 def convert_value(value, type) return value if type.nil? || value.nil? case type when :int, :integer then value.to_s.empty? ? value : Integer(value) when :float then value.to_s.empty? ? value : Float(value) when :bool, :boolean %w[y yes true 1].include?(value.to_s.downcase) else value end end |
#error(_message) ⇒ void
This method returns an undefined value.
286 |
# File 'lib/pvectl/config/wizard.rb', line 286 def error(); end |
#mask(question) ⇒ String
Asks for masked input (password).
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) ⇒ void
This method returns an undefined value.
282 |
# File 'lib/pvectl/config/wizard.rb', line 282 def ok(); end |
#select(question, choices) ⇒ Object
Prompts for selection from choices.
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) ⇒ void
This method returns an undefined value.
284 |
# File 'lib/pvectl/config/wizard.rb', line 284 def warn(); end |
#yes?(question) ⇒ Boolean
Asks a yes/no question.
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 |