Class: IO

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/io_get_password.rb

Instance Method Summary collapse

Instance Method Details

#get_password(out: $stderr) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/core_ext/io_get_password.rb', line 2

def get_password(out: $stderr)
  result = ''
  noecho do
    while char = getch
      case char
      when /[\r\n]/
        break
      when /[\e\b\x7f]/
        result.replace result.chop
        out.write "\b \b"
      when /[\x3\x1A]/ # interrupt, background
        out.write "\nOk, aborting\n"
        abort
      else
        result << char
        out.write '*'
      end
    end
  end
  out.write "\n"
  result
end