Module: Kernel

Defined in:
lib/read-password.rb,
lib/read-password.rb,
lib/read-password.rb

Defined Under Namespace

Modules: API

Constant Summary collapse

ENABLE_ECHO_INPUT =
0x0004

Class Method Summary collapse

Class Method Details

.password(prompt = 'Password: ') ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/read-password.rb', line 38

def self.password(prompt='Password: ')
  handle = API.get_std_handle(0xFFFFFFF6)
  mode = API.get_console_mode(handle) rescue 9999

  res = if (mode & ENABLE_ECHO_INPUT) == ENABLE_ECHO_INPUT
          API.set_console_mode(handle, mode & ~ENABLE_ECHO_INPUT)
          STDIN.gets.chomp.tap {
            API.set_console_mode(handle, mode)
          }
        else
          STDIN.gets.chomp
        end
  STDOUT.puts

  res
end