mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
580d9cedc7
* termio: new termio module move the tcgetattr and tcsetattr functions in a new termio module. The code needed refactoring as different OS have different fields size, position and number for the C.termios structure, which could not be correctly expressed consitently otherwise. It has the positive side effect to reduce the number of unsafe calls. New testing code was also added for the readline module as it is relying of the feature. * apply 2023 copyright to the new files too
30 lines
781 B
V
30 lines
781 B
V
module os
|
|
|
|
import term.termios
|
|
|
|
// input_password prompts the user for a password-like secret. It disables
|
|
// the terminal echo during user input and resets it back to normal when done.
|
|
pub fn input_password(prompt string) !string {
|
|
if is_atty(1) <= 0 || getenv('TERM') == 'dumb' {
|
|
return error('Could not obtain password discretely.')
|
|
}
|
|
|
|
mut old_state := termios.Termios{}
|
|
if termios.tcgetattr(0, mut old_state) != 0 {
|
|
return last_error()
|
|
}
|
|
defer {
|
|
termios.tcsetattr(0, C.TCSANOW, mut old_state)
|
|
println('')
|
|
}
|
|
|
|
mut new_state := old_state
|
|
|
|
new_state.c_lflag &= termios.invert(C.ECHO) // Disable echoing of characters
|
|
termios.tcsetattr(0, C.TCSANOW, mut new_state)
|
|
|
|
password := input_opt(prompt) or { return error('Failed to read password') }
|
|
|
|
return password
|
|
}
|