summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBrian Campbell <lambda@continuation.org>2016-10-18 23:37:49 -0400
committerAndrew Gallant <jamslam@gmail.com>2016-10-29 21:23:05 -0400
commit79a8d0ab3f75be2d30a8c55ae97dfd858e583be8 (patch)
treecc92c3ac9335fe55abfcb41f3ce4bbdaa064c9c5 /src
parentfbf8265cded97bf4743563249181641b1b17188e (diff)
Reset the terminal when Ctrl-C is pressed
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
Diffstat (limited to 'src')
-rw-r--r--src/main.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
index e64ddf9c..71c4da32 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,4 @@
+extern crate ctrlc;
extern crate deque;
extern crate docopt;
extern crate env_logger;
@@ -22,6 +23,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;
@@ -82,6 +84,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 =