Created
October 30, 2012 13:37
-
-
Save mamantoha/3980231 to your computer and use it in GitHub Desktop.
Ask for a email and password in Linux terminal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
class AskForCredentials | |
attr_reader :email, :password | |
def initialize | |
ask_for_credentials | |
end | |
private | |
def ask_for_credentials | |
puts "Enter your credentials." | |
print "Email: " | |
@email = ask | |
print "Password (typing will be hidden): " | |
@password = ask_for_password | |
nil | |
end | |
def ask | |
$stdin.gets.to_s.strip | |
end | |
def ask_for_password | |
echo_off | |
password = ask | |
puts | |
echo_on | |
return password | |
end | |
def echo_off | |
with_tty do | |
system "stty -echo" | |
end | |
end | |
def echo_on | |
with_tty do | |
system "stty echo" | |
end | |
end | |
def with_tty(&block) | |
return unless $stdin.isatty | |
yield | |
end | |
end | |
c = AskForCredentials.new | |
puts c | |
puts "Email: #{c.email}, Password: #{c.password}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment