Method: Blufin::Terminal.prompt_yes_no

Defined in:
lib/core/terminal.rb

.prompt_yes_no(title = nil, message = nil, confirmation_message = nil, preceding_blank_line = true) ⇒ Object

Gives a prompt where ‘y/Y’ will return TRUE, ‘n/N’ will return false, and ANY other key will do nothing.

Returns:

  • void



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/core/terminal.rb', line 335

def self.prompt_yes_no(title = nil, message = nil, confirmation_message = nil, preceding_blank_line = true)
    title                = 'Please confirm YES or NO.' if title.nil?
    confirmation_message = 'Would you like to continue?' if confirmation_message.nil?
    puts if preceding_blank_line
    puts "  \x1B[38;5;231m\x1B[48;5;55m Confirm \x1B[0m #{title.nil? ? '' : "\xe2\x86\x92 "}#{title}\n"
    parse_messages(message)
    response = ''
    until %w[y Y n N x X a A].include? response
        response = Readline::readline("     \x1B[38;5;161m#{confirmation_message} \x1B[0m[y/n]\x1B[90m => \x1B[0m", true)
    end
    case response.downcase
        when 'y'
            puts "\n"
            return true
        when 'n'
            puts "\n"
            return false
        when 'a'
            Blufin::Terminal::error('Abandon ship!', ["You have chosen to \x1B[38;5;196mABORT\x1B[38;5;240m the script.", nil, 'Please note that whenever you do this, any scripted tasks which were running', 'will have been interrupted mid-script. This may (or may not) cause problems.'], true)
        when 'x'
            Blufin::Terminal::error('Abandon ship!', ["You have chosen to \x1B[38;5;196mABORT\x1B[38;5;240m the script.", nil, 'Please note that whenever you do this, any scripted tasks which were running', 'will have been interrupted mid-script. This may (or may not) cause problems.'], true)
        else
            raise RuntimeError, "Un-handled response: #{response.downcase}"
    end
end