This is a multi-part message in MIME format.
--Multipart_Thu__28_Aug_2003_16:25:45_-0400_00ab1d00
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
I should mention that I'm on a GNU/Linux but it'd be nice to have a
cross-platform solution to this.
If the termios module works on windows ( I don't know if it does ), the
attached should work. Either way it should work on Unix (and requires
termios, obviously).
It defines noecho() and getpass() for IO, and getpass() uses noecho().
Jim
--Multipart_Thu__28_Aug_2003_16:25:45_-0400_00ab1d00
Content-Type: text/plain;
name="termios-noecho.rb.txt"
Content-Disposition: attachment;
filename="termios-noecho.rb.txt"
Content-Transfer-Encoding: 7bit
require 'termios'
class IO
def noecho
if ! block_given?
raise "Block required for noecho()"
else
begin
term = Termios::getattr(self)
term.c_lflag &= ~Termios::ECHO
Termios::setattr(self, Termios::TCSANOW, term)
yield self
ensure
term.c_lflag |= Termios::ECHO
Termios::setattr(self, Termios::TCSANOW, term)
end
end
end
def getpass(prompt)
stderr_sync = STDERR.sync
STDERR.sync = 1
STDERR.print(prompt)
pw = nil
noecho { |s|
pw = s.gets.chomp
}
STDERR.puts
STDERR.sync = 0 unless stderr_sync
return pw
end
end
--Multipart_Thu__28_Aug_2003_16:25:45_-0400_00ab1d00--