I'd like to prevent "^C" from being outputted to the terminal when Ctrl+C is pressed.
I'm capturing the interrupt command like this:
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
<-c
// exit code here
}()
... however, when I press Ctrl+C, "^C" is outputted into the terminal. This isn't ideal.
If you print something afterwards, you can do
fmt.Print("\r")
log.Println("Shutting down")
\r is a carriage return character; it tells your terminal emulator to move the cursor at the start of the line. This way you can overwrite the ^C on the terminal.
If you put the terminal into raw mode, you'll get the keystrokes directly and the tty won't interpret them (and display ^C).
I'm not sure of the best way to set raw mode in Go, but golang.org/x/crypto/ssh/terminal's RawMode() does it. You would then have to enable INT and TERM handling otherwise you'll receive a ^C as input, instead having it be processed as an interruption.
An explanation of raw mode is here: https://unix.stackexchange.com/questions/21752/what-s-the-difference-between-a-raw-and-a-cooked-device-driver
A similar answer is here: https://superuser.com/questions/147013/how-to-disable-c-from-being-echoed-on-linux-on-ctrl-c
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With