Using Go to command line, how do I hide the command line input from the user terminal?

I am writing a simple command line utility that prompts the user for a password. I've decided to use bufio to read user input and can't figure out how to hide the password on the user terminal. Is this functionality the Go standard library, or do I need to use OS level functionality to do this? (This will probably only work on Linux, but would prefer something that works on Windows too.)

+3


source to share


2 answers


You want to turn off echo. This is usually accomplished by configuring the terminal driver, termios. Your best bet is to read the Linux documentation of this driver ( termios (3) ). As far as I know, it needs to be disabled before reading the password ECHO

. After reading the password, turn it on again ECHO

. Don't use bufio, as you don't want buffering to get in the way of low-level terminals. You can access system calls from the syscall package .



termios is available on every UNIX-like platform. This includes Linux, BSD, OS X and Solaris, but not Microsoft Windows.

+3


source


You can use termbox which uses keyboard input without output. So far, I can say it works on Windows, but I'm not sure if it works on Linux (I think it does). After installation, there is a _demos folder, inside this folder there is an example named keyboard.go. Once launched, it will display the keyboard and highlight the keys pressed. However, it works on my computer, but when I press the spacebar / enter / function keys or the CTRL- (any) key, the program freezes. Perhaps this is a bug in the code or inside Termbox. I haven't figured it out yet. Also, you need to use CGO to use termbox to run.

Screenshot:



enter image description here

+1


source







All Articles