summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAndrew Gallant <jamslam@gmail.com>2016-12-24 12:53:09 -0500
committerAndrew Gallant <jamslam@gmail.com>2016-12-24 12:53:09 -0500
commitde5cb7d22e00c18dfce082a68b0cf45924b56e7f (patch)
tree74dcc51d61e2b71bea02265d365504f17bcd39e9 /src
parent7a682f465e74d31ade4cb96c8d32110d33deebd7 (diff)
Remove special ^C handling.
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
Diffstat (limited to 'src')
-rw-r--r--src/args.rs66
-rw-r--r--src/main.rs56
2 files changed, 64 insertions, 58 deletions
diff --git a/src/args.rs b/src/args.rs
index 2b87b32c..4048b119 100644
--- a/src/args.rs
+++ b/src/args.rs
@@ -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;
@@ -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,
@@ -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> {
@@ -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())
@@ -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> {
@@ -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,
@@ -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()),
@@ -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 }
+ }
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index a26812b1..236c093e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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;
@@ -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;
@@ -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() {
@@ -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));
@@ -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 }
- }
- }
-}