Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset the terminal when Ctrl-C is pressed #187

Closed
wants to merge 1 commit into from

Conversation

lambda
Copy link
Contributor

@lambda lambda commented Oct 19, 2016

If a user hits Ctrl-C to exit out of a search in the middle of printing
a line, we don't want to leave the terminal colors screwed up for them.
Catch Ctrl-C using the ctrlc crate, obtain a stdout lock to ensure that
other threads don't continue writing after we do so, reset the terminal,
and exit the program.

Closes #119

If a user hits Ctrl-C to exit out of a search in the middle of printing
a line, we don't want to leave the terminal colors screwed up for them.
Catch Ctrl-C using the ctrlc crate, obtain a stdout lock to ensure that
other threads don't continue writing after we do so, reset the terminal,
and exit the program.

Closes BurntSushi#119
@lambda
Copy link
Contributor Author

lambda commented Oct 19, 2016

This PR is a bit of a strawman; I figured I'd try the simplest possible thing that seemed to work. I first tried to see whether I should thread this into ColoredTerminal somehow, but that seemed complicated, and this approach seemed simpler.

I haven't tested this at all on Windows, so don't know if this will work properly on there.

On my system, for some reason, this suppresses the shell prompt printed after exiting. I haven't quite figure out why that is. But it does have the effect of resetting the colors appropriately, so if I just hit "return", I do get the prompt properly. Writing out extra newlines and flushing doesn't seem to help this.

I had expected that this would have the effect of losing the information about the process being killed by a signal, just returning 1 instead, since I didn't clear the signal handler and re-raise the signal, but echo $? gives me 130 as you'd expect from an interrupted process. So I guess that's good, if unexpected.

@BurntSushi
Copy link
Owner

@lambda Nice! Thanks so much for doing this. I'll pull this down and try to play with it soon before merging. (It may not happen until this weekend, sorry!)

@BurntSushi
Copy link
Owner

OK, I tried this out and it does indeed fix the issue for me. Interestingly, I'm not experiencing the shell prompt suppression. I get my shell prompt back, but I have no idea what the forces are at play here. (FWIW, I'm running bash in konsole as my terminal emulator on Linux.)

I merged this in 811fcc1 (after a rebase).

Thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants