| 1 | module os |
| 2 | |
| 3 | import term.termios |
| 4 | |
| 5 | // input_password prompts the user for a password-like secret. |
| 6 | // It disables 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 | mut old_state := termios.Termios{} |
| 13 | if termios.tcgetattr(0, mut old_state) != 0 { |
| 14 | return last_error() |
| 15 | } |
| 16 | |
| 17 | mut new_state := old_state |
| 18 | new_state.disable_echo() |
| 19 | termios.set_state(0, new_state) |
| 20 | |
| 21 | password := input_opt(prompt) or { return error('Failed to read password') } |
| 22 | |
| 23 | termios.set_state(0, old_state) |
| 24 | |
| 25 | println('') |
| 26 | return password |
| 27 | } |
| 28 | |