class Question::Password

Public Class Methods

new(question) click to toggle source
# File lib/question/password.rb, line 3
def initialize(question)
  @question = question
  @finished = false
  @finished = false
  @answer = ""
end

Public Instance Methods

ask() click to toggle source
# File lib/question/password.rb, line 10
def ask
  TTY.interactive do
    while !@finished
      render
      handle_input
    end
  end
  render

  @answer
end
handle_input() click to toggle source
# 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
render() click to toggle source
# 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