Help with term.ReadPassword
Hello, I'm trying to use term.ReadPassword for entering a password from the cli.
I have a problem with the ctrl+c hotkey. If the user presses ctrl+c, the terminal freezes
I use this auxiliary function:
func InputPassword() (string, error) {
fd := int(os.Stdin.Fd())
oldState, err := term.GetState(fd)
if err != nil {
return "", err
}
defer term.Restore(fd, oldState)
// check for ctrl-c signal
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-signalChan
term.Restore(fd, oldState)
}()
fmt.Print("Create a password: ")
bytePassword, err := term.ReadPassword(fd)
if err != nil {
return "", err
}
password := string(bytePassword)
fmt.Println()
fmt.Print("Confirm the password: ")
byteConfirmPassword, err := term.ReadPassword(fd)
if err != nil {
return "", err
}
confirmPassword := string(byteConfirmPassword)
fmt.Println()
if password != confirmPassword {
return "", errors.New("The passwords don't match. Try again.")
}
// stop watching signals
signal.Stop(signalChan)
return password, nil
}
Hello,
I've not had time to try your code and find a fix, but I would suggest removing the following snippets from the code above and see if it works:
fd := int(os.Stdin.Fd())
oldState, err := term.GetState(fd)
if err != nil {
return "", err
}
defer term.Restore(fd, oldState)
// check for ctrl-c signal
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-signalChan
term.Restore(fd, oldState)
}()
And removing also this:
// stop watching signals
signal.Stop(signalChan)
Hi, I've been struggling with the same issue. I have a minimal reproducer in https://github.com/YangchenYe323/console-terminal-freeze where a command asks for user input by reading stdin, and hitting ctrl-c freezes the terminal. Maybe there is a more supported way of prompting for user input with console?
Привет,
У меня не было времени опробовать ваш код и найти решение, но я бы предложил удалить следующие фрагменты из кода выше и посмотреть, сработает ли это:
fd := int(os.Stdin.Fd()) oldState, err := term.GetState(fd) if err != nil { return "", err } defer term.Restore(fd, oldState)
// check for ctrl-c signal signalChan := make(chan os.Signal, 1) signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM) go func() { <-signalChan term.Restore(fd, oldState) }() И удалив также это:
// stop watching signals signal.Stop(signalChan)
I tried this, it didn't work. Sorry for the long reply. I'll send a minimal reproducible example over the weekend. For now I have solved the problem using the library pterm (https://github.com/pterm/pterm/tree/master/_examples/interactive_textinput#interactive_textinputpassword)
Hi, I've been struggling with the same issue. I have a minimal reproducer in https://github.com/YangchenYe323/console-terminal-freeze where a command asks for user input by reading stdin, and hitting ctrl-c freezes the terminal. Maybe there is a more supported way of prompting for user input with console?
Hi, try pterm library, it helped me solve the problem (this is not an advertisement 😄)
I just did. Works like a charm, thanks so much!
On Thu, Apr 3, 2025 at 5:25 PM Игорь @.***> wrote:
Hi, I've been struggling with the same issue. I have a minimal reproducer in https://github.com/YangchenYe323/console-terminal-freeze where a command asks for user input by reading stdin, and hitting ctrl-c freezes the terminal. Maybe there is a more supported way of prompting for user input with console?
Hi, try pterm https://github.com/pterm/pterm/tree/master/_examples/interactive_textinput#interactive_textinputdemo library, it helped me solve the problem (this is not an advertisement 😄)
— Reply to this email directly, view it on GitHub https://github.com/reeflective/console/issues/67#issuecomment-2776951717, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKHNXTWNCD3N66UU6FTTMCD2XWRNZAVCNFSM6AAAAABYG73XR2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDONZWHE2TCNZRG4 . You are receiving this because you commented.Message ID: @.***> [image: igor2104]igor2104 left a comment (reeflective/console#67) https://github.com/reeflective/console/issues/67#issuecomment-2776951717
Hi, I've been struggling with the same issue. I have a minimal reproducer in https://github.com/YangchenYe323/console-terminal-freeze where a command asks for user input by reading stdin, and hitting ctrl-c freezes the terminal. Maybe there is a more supported way of prompting for user input with console?
Hi, try pterm https://github.com/pterm/pterm/tree/master/_examples/interactive_textinput#interactive_textinputdemo library, it helped me solve the problem (this is not an advertisement 😄)
— Reply to this email directly, view it on GitHub https://github.com/reeflective/console/issues/67#issuecomment-2776951717, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKHNXTWNCD3N66UU6FTTMCD2XWRNZAVCNFSM6AAAAABYG73XR2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDONZWHE2TCNZRG4 . You are receiving this because you commented.Message ID: @.***>
Hello,
I will take some time soon to merge the functionality in this library.
After taking a look at this, and given that using the pterm library works along with the console library, I'm going to close this issue. Merging pterm's interactive code for password reading is not really useful, and increases the code to maintain.
Solution to password input using the console library is to use pterm functionality. See here for password input demos: https://github.com/pterm/pterm/tree/master/_examples/interactive_textinput#interactive_textinputpassword