Class: Question::Password

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

Instance Method Summary collapse

Constructor Details

#initialize(question) ⇒ Password

Returns a new instance of Password.



3
4
5
6
7
8
# File 'lib/question/password.rb', line 3

def initialize(question)
  @question = question
  @finished = false
  @finished = false
  @answer = ""
end

Instance Method Details

#askObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/question/password.rb', line 10

def ask
  TTY.interactive do
    while !@finished
      render
      handle_input
    end
  end
  render

  @answer
end

#handle_inputObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/question/password.rb', line 22

def handle_input
  case input = TTY.input
  when TTY::CODE::SIGINT
    exit 130
  when TTY::CODE::RETURN
    @finished = true
  when TTY::CODE::BACKSPACE, TTY::CODE::DELETE
    @answer.chop! unless @answer.length == 0
  when /^[^\e]/
    @answer += input
  end
end

#renderObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/question/password.rb', line 35

def render
  obscured_password = TTY::UI::SECURE * @answer.length
  TTY.clear
  print "? ".cyan
  print @question
  print ": "
  print @finished ? obscured_password.green : obscured_password
  print "\n"
  print TTY::CODE::NOOP unless @finished
end