Skip to content

Reset the terminal when Ctrl-C is pressed - #187

Closed
lambda wants to merge 1 commit into
BurntSushi:masterfrom
lambda:ctrlc-reset-terminal
Closed

lambda wants to merge 1 commit into
BurntSushi:masterfrom
lambda:ctrlc-reset-terminal

Conversation

@lambda

@lambda lambda commented Oct 19, 2016

Copy link
Copy Markdown
Contributor

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

lambda commented Oct 19, 2016

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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
Copy Markdown
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