Class: Question::Confirm
- Inherits:
-
Object
- Object
- Question::Confirm
- Defined in:
- lib/question/confirm.rb
Instance Method Summary collapse
- #ask ⇒ Object
- #colorized_question ⇒ Object
-
#initialize(question, default: true) ⇒ Confirm
constructor
A new instance of Confirm.
- #render ⇒ Object
Constructor Details
#initialize(question, default: true) ⇒ Confirm
Returns a new instance of Confirm.
3 4 5 6 7 8 |
# File 'lib/question/confirm.rb', line 3 def initialize(question, default:true) @question = question @finished = false @default = default @answer = nil end |
Instance Method Details
#ask ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/question/confirm.rb', line 10 def ask print TTY::CODE::SAVE question = colorized_question if @default question += "(Y/n) ".light_white else question += "(y/N) ".light_white end # Use readline so keyboard shortcuts like alt-backspace work @answer = Readline.readline(question + TTY::CODE::NOOP, true) @answer = if @answer =~ /^y/i true elsif @answer =~ /^n/i false else @default end render @answer rescue Interrupt exit 1 end |
#colorized_question ⇒ Object
43 44 45 46 47 |
# File 'lib/question/confirm.rb', line 43 def colorized_question colorized_question = "? ".cyan colorized_question += @question colorized_question += ": " end |