Skip to content

Commit

Permalink
Reset the terminal when Ctrl-C is pressed
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lambda committed Oct 19, 2016
1 parent 31fbae5 commit 6132170
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ name = "integration"
path = "tests/tests.rs"

[dependencies]
ctrlc = "2.0"
deque = "0.3"
docopt = "0.6"
env_logger = "0.3"
Expand Down
14 changes: 14 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
extern crate ctrlc;
extern crate deque;
extern crate docopt;
extern crate env_logger;
Expand All @@ -24,6 +25,7 @@ extern crate winapi;
use std::error::Error;
use std::fs::File;
use std::io;
use std::io::Write;
use std::path::Path;
use std::process;
use std::result;
Expand Down Expand Up @@ -88,6 +90,18 @@ fn main() {

fn run(args: Args) -> Result<u64> {
let args = Arc::new(args);

let handler_args = args.clone();
ctrlc::set_handler(move || {
let stdout = io::stdout();
let mut stdout = stdout.lock();

let _ = handler_args.stdout().reset();
let _ = stdout.flush();

process::exit(1);
});

let paths = args.paths();
let threads = cmp::max(1, args.threads() - 1);
let isone =
Expand Down

0 comments on commit 6132170

Please sign in to comment.