mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
os: add input_password(prompt) and unit tests (#15507)
This commit is contained in:
42
vlib/os/password_nix.c.v
Normal file
42
vlib/os/password_nix.c.v
Normal file
@ -0,0 +1,42 @@
|
||||
module os
|
||||
|
||||
#include <termios.h>
|
||||
|
||||
pub struct C.termios {
|
||||
mut:
|
||||
c_iflag int
|
||||
c_oflag int
|
||||
c_cflag int
|
||||
c_lflag int
|
||||
c_cc [20]u8
|
||||
}
|
||||
|
||||
fn C.tcgetattr(fd int, ptr &C.termios) int
|
||||
|
||||
fn C.tcsetattr(fd int, action int, const_ptr &C.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.')
|
||||
}
|
||||
|
||||
old_state := C.termios{}
|
||||
if unsafe { C.tcgetattr(0, &old_state) } != 0 {
|
||||
return last_error()
|
||||
}
|
||||
defer {
|
||||
unsafe { C.tcsetattr(0, C.TCSANOW, &old_state) }
|
||||
println('')
|
||||
}
|
||||
|
||||
mut new_state := old_state
|
||||
|
||||
new_state.c_lflag &= int(~u32(C.ECHO)) // Disable echoing of characters
|
||||
unsafe { C.tcsetattr(0, C.TCSANOW, &new_state) }
|
||||
|
||||
password := input_opt(prompt) or { return error('Failed to read password') }
|
||||
|
||||
return password
|
||||
}
|
26
vlib/os/password_windows.c.v
Normal file
26
vlib/os/password_windows.c.v
Normal file
@ -0,0 +1,26 @@
|
||||
module os
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
// 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.')
|
||||
}
|
||||
|
||||
std_handle := C.GetStdHandle(C.STD_INPUT_HANDLE)
|
||||
mut mode := u32(0)
|
||||
|
||||
unsafe { C.GetConsoleMode(std_handle, &mode) }
|
||||
unsafe { C.SetConsoleMode(std_handle, mode & (~u32(C.ENABLE_ECHO_INPUT))) }
|
||||
|
||||
defer {
|
||||
unsafe { C.SetConsoleMode(std_handle, &mode) }
|
||||
println('')
|
||||
}
|
||||
|
||||
password := input_opt(prompt) or { return error('Failed to read password') }
|
||||
|
||||
return password
|
||||
}
|
Reference in New Issue
Block a user