Class | TermUtil |
In: |
lib/davclient/termutil.rb
lib/davclient/termutil.rb |
Parent: | Object |
Prompt for password Extracted from Ian Macdonald‘s Ruby/Password gem.
Example:
password = getc(message="Password: ", mask='*') puts "It's:" + password
# File lib/davclient/termutil.rb, line 14 14: def self.echo(on=true, masked=false) 15: term = Termios::getattr( $stdin ) 16: 17: if on 18: term.c_lflag |= ( Termios::ECHO | Termios::ICANON ) 19: else # off 20: term.c_lflag &= ~Termios::ECHO 21: term.c_lflag &= ~Termios::ICANON if masked 22: end 23: 24: Termios::setattr( $stdin, Termios::TCSANOW, term ) 25: end
# File lib/davclient/termutil.rb, line 14 14: def self.echo(on=true, masked=false) 15: term = Termios::getattr( $stdin ) 16: 17: if on 18: term.c_lflag |= ( Termios::ECHO | Termios::ICANON ) 19: else # off 20: term.c_lflag &= ~Termios::ECHO 21: term.c_lflag &= ~Termios::ICANON if masked 22: end 23: 24: Termios::setattr( $stdin, Termios::TCSANOW, term ) 25: end
# File lib/davclient/termutil.rb, line 27 27: def self.getc(message="Password: ", mask='*') 28: # Save current buffering mode 29: buffering = $stdout.sync 30: 31: # Turn off buffering 32: $stdout.sync = true 33: 34: begin 35: echo(false, true) 36: print message if message 37: pw = "" 38: 39: while ( char = $stdin.getc ) != 10 # break after [Enter] 40: putc mask 41: pw << char 42: end 43: 44: ensure 45: echo true 46: print "\n" 47: end 48: 49: # Restore original buffering mode 50: $stdout.sync = buffering 51: 52: return pw 53: end
# File lib/davclient/termutil.rb, line 27 27: def self.getc(message="Password: ", mask='*') 28: # Save current buffering mode 29: buffering = $stdout.sync 30: 31: # Turn off buffering 32: $stdout.sync = true 33: 34: begin 35: echo(false, true) 36: print message if message 37: pw = "" 38: 39: while ( char = $stdin.getc ) != 10 # break after [Enter] 40: putc mask 41: pw << char 42: end 43: 44: ensure 45: echo true 46: print "\n" 47: end 48: 49: # Restore original buffering mode 50: $stdout.sync = buffering 51: 52: return pw 53: end