v / vlib / os
Raw file | 27 loc (20 sloc) | 974 bytes | Latest commit hash 3b42f18de
1module os
2
3import term.termios
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.
7pub 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}