Method: Password.getc

Defined in:
lib/password.rb

.getc(message = "Password: ", mask = '*') ⇒ Object

Get a password from STDIN in unbuffered mode, i.e. one key at a time. message will be displayed as the prompt and each key press with echo mask to the terminal. There is no need to hit [Enter] at the end.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/password.rb', line 208

def Password.getc(message="Password: ", mask='*')
  # Save current buffering mode
  buffering = $stdout.sync

  # Turn off buffering
  $stdout.sync = true

  begin
    Password.echo(false, true)
    print message if message
    pw = ""

    while ( char = $stdin.getc ) != 10 # break after [Enter]
	putc mask
	pw << char
    end

  ensure
    Password.echo true
    print "\n"
  end

  # Restore original buffering mode
  $stdout.sync = buffering

  Password.new( pw )
end