Class: Question::Confirm

Inherits:
Object
  • Object
show all
Defined in:
lib/question/confirm.rb

Instance Method Summary collapse

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

#askObject



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_questionObject



43
44
45
46
47
# File 'lib/question/confirm.rb', line 43

def colorized_question
  colorized_question = "? ".cyan
  colorized_question += @question
  colorized_question += ": "
end

#renderObject



36
37
38
39
40
41
# File 'lib/question/confirm.rb', line 36

def render
  TTY.clear
  print colorized_question
  print (@answer ? "Yes" : "No").green
  print "\n"
end