Skip to content

Commit

Permalink
Remove special ^C handling.
Browse files Browse the repository at this point in the history
This means that ripgrep will no longer try to reset your colors in your
terminal if you kill it while searching. This could result in messing up
the colors in your terminal, and the fix is to simply run some other
command that resets them for you. For example:

    $ echo -ne "\033[0m"

The reason why the ^C handling was removed is because it is irrevocably
broken on Windows and is impossible to do correctly and efficiently in
ANSI terminals.

Fixes #281
  • Loading branch information
BurntSushi committed Dec 24, 2016
1 parent 7a682f4 commit de5cb7d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 71 deletions.
12 changes: 0 additions & 12 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ path = "tests/tests.rs"
[dependencies]
bytecount = "0.1.4"
clap = "2.19.0"
ctrlc = "2.0"
env_logger = "0.3"
grep = { version = "0.1.4", path = "grep" }
ignore = { version = "0.1.5", path = "ignore" }
Expand Down
66 changes: 62 additions & 4 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::io::{self, BufRead};
use std::ops;
use std::path::{Path, PathBuf};
use std::process;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

use clap;
use env_logger;
Expand Down Expand Up @@ -60,6 +62,7 @@ pub struct Args {
no_messages: bool,
null: bool,
quiet: bool,
quiet_matched: QuietMatched,
replace: Option<Vec<u8>>,
text: bool,
threads: usize,
Expand Down Expand Up @@ -125,6 +128,15 @@ impl Args {
self.quiet
}

/// Returns a thread safe boolean for determining whether to quit a search
/// early when quiet mode is enabled.
///
/// If quiet mode is disabled, then QuietMatched.has_match always returns
/// false.
pub fn quiet_matched(&self) -> QuietMatched {
self.quiet_matched.clone()
}

/// Create a new printer of individual search results that writes to the
/// writer given.
pub fn printer<W: termcolor::WriteColor>(&self, wtr: W) -> Printer<W> {
Expand All @@ -145,7 +157,12 @@ impl Args {

/// Retrieve the configured file separator.
pub fn file_separator(&self) -> Option<Vec<u8>> {
if self.heading && !self.count && !self.files_with_matches && !self.files_without_matches {
let use_heading_sep =
self.heading
&& !self.count
&& !self.files_with_matches
&& !self.files_without_matches;
if use_heading_sep {
Some(b"".to_vec())
} else if self.before_context > 0 || self.after_context > 0 {
Some(self.context_separator.clone())
Expand Down Expand Up @@ -264,8 +281,8 @@ impl Args {
}
}

/// `ArgMatches` wraps `clap::ArgMatches` and provides semantic meaning to several
/// options/flags.
/// `ArgMatches` wraps `clap::ArgMatches` and provides semantic meaning to
/// several options/flags.
struct ArgMatches<'a>(clap::ArgMatches<'a>);

impl<'a> ops::Deref for ArgMatches<'a> {
Expand All @@ -281,6 +298,7 @@ impl<'a> ArgMatches<'a> {
let mmap = try!(self.mmap(&paths));
let with_filename = self.with_filename(&paths);
let (before_context, after_context) = try!(self.contexts());
let quiet = self.is_present("quiet");
let args = Args {
paths: paths,
after_context: after_context,
Expand Down Expand Up @@ -312,7 +330,8 @@ impl<'a> ArgMatches<'a> {
no_ignore_vcs: self.no_ignore_vcs(),
no_messages: self.is_present("no-messages"),
null: self.is_present("null"),
quiet: self.is_present("quiet"),
quiet: quiet,
quiet_matched: QuietMatched::new(quiet),
replace: self.replace(),
text: self.text(),
threads: try!(self.threads()),
Expand Down Expand Up @@ -746,3 +765,42 @@ fn pattern_to_str(s: &OsStr) -> Result<&str> {
s.to_string_lossy()))),
}
}

/// A simple thread safe abstraction for determining whether a search should
/// stop if the user has requested quiet mode.
#[derive(Clone, Debug)]
pub struct QuietMatched(Arc<Option<AtomicBool>>);

impl QuietMatched {
/// Create a new QuietMatched value.
///
/// If quiet is true, then set_match and has_match will reflect whether
/// a search should quit or not because it found a match.
///
/// If quiet is false, then set_match is always a no-op and has_match
/// always returns false.
fn new(quiet: bool) -> QuietMatched {
let atomic = if quiet { Some(AtomicBool::new(false)) } else { None };
QuietMatched(Arc::new(atomic))
}

/// Returns true if and only if quiet mode is enabled and a match has
/// occurred.
pub fn has_match(&self) -> bool {
match *self.0 {
None => false,
Some(ref matched) => matched.load(Ordering::SeqCst),
}
}

/// Sets whether a match has occurred or not.
///
/// If quiet mode is disabled, then this is a no-op.
pub fn set_match(&self, yes: bool) -> bool {
match *self.0 {
None => false,
Some(_) if !yes => false,
Some(ref m) => { m.store(true, Ordering::SeqCst); true }
}
}
}
56 changes: 2 additions & 54 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
extern crate bytecount;
#[macro_use]
extern crate clap;
extern crate ctrlc;
extern crate env_logger;
extern crate grep;
extern crate ignore;
Expand All @@ -21,16 +20,13 @@ extern crate termcolor;
extern crate winapi;

use std::error::Error;
use std::io::Write;
use std::process;
use std::result;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc;
use std::thread;

use termcolor::WriteColor;

use args::Args;
use worker::Work;

Expand Down Expand Up @@ -74,15 +70,6 @@ fn run(args: Arc<Args>) -> Result<u64> {
if args.never_match() {
return Ok(0);
}
{
let args = args.clone();
ctrlc::set_handler(move || {
let mut writer = args.stdout();
let _ = writer.reset();
let _ = writer.flush();
process::exit(1);
});
}
let threads = args.threads();
if args.files() {
if threads == 1 || args.is_one_path() {
Expand All @@ -101,7 +88,7 @@ fn run(args: Arc<Args>) -> Result<u64> {

fn run_parallel(args: Arc<Args>) -> Result<u64> {
let bufwtr = Arc::new(args.buffer_writer());
let quiet_matched = QuietMatched::new(args.quiet());
let quiet_matched = args.quiet_matched();
let paths_searched = Arc::new(AtomicUsize::new(0));
let match_count = Arc::new(AtomicUsize::new(0));

Expand Down Expand Up @@ -281,42 +268,3 @@ fn eprint_nothing_searched() {
applied a filter you didn't expect. \
Try running again with --debug.");
}

/// A simple thread safe abstraction for determining whether a search should
/// stop if the user has requested quiet mode.
#[derive(Clone, Debug)]
pub struct QuietMatched(Arc<Option<AtomicBool>>);

impl QuietMatched {
/// Create a new QuietMatched value.
///
/// If quiet is true, then set_match and has_match will reflect whether
/// a search should quit or not because it found a match.
///
/// If quiet is false, then set_match is always a no-op and has_match
/// always returns false.
pub fn new(quiet: bool) -> QuietMatched {
let atomic = if quiet { Some(AtomicBool::new(false)) } else { None };
QuietMatched(Arc::new(atomic))
}

/// Returns true if and only if quiet mode is enabled and a match has
/// occurred.
pub fn has_match(&self) -> bool {
match *self.0 {
None => false,
Some(ref matched) => matched.load(Ordering::SeqCst),
}
}

/// Sets whether a match has occurred or not.
///
/// If quiet mode is disabled, then this is a no-op.
pub fn set_match(&self, yes: bool) -> bool {
match *self.0 {
None => false,
Some(_) if !yes => false,
Some(ref m) => { m.store(true, Ordering::SeqCst); true }
}
}
}

0 comments on commit de5cb7d

Please sign in to comment.