1 | module os |
2 | |
3 | #include <windows.h> |
4 | |
5 | // input_password prompts the user for a password-like secret. It disables |
6 | // the terminal echo during user input and resets it back to normal when done. |
7 | pub fn input_password(prompt string) !string { |
8 | if is_atty(1) <= 0 || getenv('TERM') == 'dumb' { |
9 | return error('Could not obtain password discretely.') |
10 | } |
11 | |
12 | std_handle := C.GetStdHandle(C.STD_INPUT_HANDLE) |
13 | mut mode := u32(0) |
14 | |
15 | unsafe { C.GetConsoleMode(std_handle, &mode) } |
16 | unsafe { C.SetConsoleMode(std_handle, mode & (~u32(C.ENABLE_ECHO_INPUT))) } |
17 | |
18 | defer { |
19 | unsafe { C.SetConsoleMode(std_handle, &mode) } |
20 | println('') |
21 | } |
22 | |
23 | password := input_opt(prompt) or { return error('Failed to read password') } |
24 | |
25 | return password |
26 | } |