Add --no-messages flag.

This flag is similar to what's found in grep: it will suppress all error
messages, such as those shown when a particular file couldn't be read.

Closes #149
This commit is contained in:
Andrew Gallant 2016-11-06 14:36:08 -05:00
parent 58aca2efb2
commit 77ad7588ae
6 changed files with 48 additions and 18 deletions

View File

@ -226,6 +226,11 @@ context related options.)
.RS .RS
.RE .RE
.TP .TP
.B \-\-no\-messages
Suppress all error messages.
.RS
.RE
.TP
.B \-\-no\-mmap .B \-\-no\-mmap
Never use memory maps, even when they might be faster. Never use memory maps, even when they might be faster.
.RS .RS

View File

@ -147,6 +147,9 @@ Project home page: https://github.com/BurntSushi/ripgrep
when ripgrep thinks it will be faster. (Note that mmap searching when ripgrep thinks it will be faster. (Note that mmap searching
doesn't currently support the various context related options.) doesn't currently support the various context related options.)
--no-messages
: Suppress all error messages.
--no-mmap --no-mmap
: Never use memory maps, even when they might be faster. : Never use memory maps, even when they might be faster.

View File

@ -130,13 +130,6 @@ pub use glob::{Glob, GlobBuilder, GlobMatcher};
mod glob; mod glob;
mod pathutil; mod pathutil;
macro_rules! eprintln {
($($tt:tt)*) => {{
use std::io::Write;
let _ = writeln!(&mut ::std::io::stderr(), $($tt)*);
}}
}
/// Represents an error that can occur when parsing a glob pattern. /// Represents an error that can occur when parsing a glob pattern.
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum Error { pub enum Error {

View File

@ -153,6 +153,9 @@ Less common options:
when ripgrep thinks it will be faster. (Note that mmap searching when ripgrep thinks it will be faster. (Note that mmap searching
doesn't currently support the various context related options.) doesn't currently support the various context related options.)
--no-messages
Suppress all error messages.
--no-mmap --no-mmap
Never use memory maps, even when they might be faster. Never use memory maps, even when they might be faster.
@ -256,6 +259,7 @@ pub struct RawArgs {
flag_no_ignore_parent: bool, flag_no_ignore_parent: bool,
flag_no_ignore_vcs: bool, flag_no_ignore_vcs: bool,
flag_no_line_number: bool, flag_no_line_number: bool,
flag_no_messages: bool,
flag_no_mmap: bool, flag_no_mmap: bool,
flag_no_filename: bool, flag_no_filename: bool,
flag_null: bool, flag_null: bool,
@ -306,6 +310,7 @@ pub struct Args {
no_ignore: bool, no_ignore: bool,
no_ignore_parent: bool, no_ignore_parent: bool,
no_ignore_vcs: bool, no_ignore_vcs: bool,
no_messages: bool,
null: bool, null: bool,
quiet: bool, quiet: bool,
replace: Option<Vec<u8>>, replace: Option<Vec<u8>>,
@ -429,6 +434,7 @@ impl RawArgs {
no_ignore_vcs: no_ignore_vcs:
// --no-ignore implies --no-ignore-vcs // --no-ignore implies --no-ignore-vcs
self.flag_no_ignore_vcs || no_ignore, self.flag_no_ignore_vcs || no_ignore,
no_messages: self.flag_no_messages,
null: self.flag_null, null: self.flag_null,
quiet: self.flag_quiet, quiet: self.flag_quiet,
replace: self.flag_replace.clone().map(|s| s.into_bytes()), replace: self.flag_replace.clone().map(|s| s.into_bytes()),
@ -711,6 +717,11 @@ impl Args {
self.type_list self.type_list
} }
/// Returns true if error messages should be suppressed.
pub fn no_messages(&self) -> bool {
self.no_messages
}
/// Create a new recursive directory iterator over the paths in argv. /// Create a new recursive directory iterator over the paths in argv.
pub fn walker(&self) -> ignore::Walk { pub fn walker(&self) -> ignore::Walk {
self.walker_builder().build() self.walker_builder().build()
@ -730,9 +741,11 @@ impl Args {
} }
for path in &self.ignore_files { for path in &self.ignore_files {
if let Some(err) = wd.add_ignore(path) { if let Some(err) = wd.add_ignore(path) {
if !self.no_messages {
eprintln!("{}", err); eprintln!("{}", err);
} }
} }
}
wd.follow_links(self.follow); wd.follow_links(self.follow);
wd.hidden(!self.hidden); wd.hidden(!self.hidden);

View File

@ -124,7 +124,7 @@ fn run_parallel(args: Arc<Args>) -> Result<u64> {
if quiet_matched.has_match() { if quiet_matched.has_match() {
return Quit; return Quit;
} }
let dent = match get_or_log_dir_entry(result) { let dent = match get_or_log_dir_entry(result, args.no_messages()) {
None => return Continue, None => return Continue,
Some(dent) => dent, Some(dent) => dent,
}; };
@ -160,8 +160,10 @@ fn run_parallel(args: Arc<Args>) -> Result<u64> {
}) })
}); });
if !args.paths().is_empty() && paths_searched.load(Ordering::SeqCst) == 0 { if !args.paths().is_empty() && paths_searched.load(Ordering::SeqCst) == 0 {
if !args.no_messages() {
eprint_nothing_searched(); eprint_nothing_searched();
} }
}
Ok(match_count.load(Ordering::SeqCst) as u64) Ok(match_count.load(Ordering::SeqCst) as u64)
} }
@ -171,7 +173,7 @@ fn run_one_thread(args: Arc<Args>) -> Result<u64> {
let mut paths_searched: u64 = 0; let mut paths_searched: u64 = 0;
let mut match_count = 0; let mut match_count = 0;
for result in args.walker() { for result in args.walker() {
let dent = match get_or_log_dir_entry(result) { let dent = match get_or_log_dir_entry(result, args.no_messages()) {
None => continue, None => continue,
Some(dent) => dent, Some(dent) => dent,
}; };
@ -193,8 +195,10 @@ fn run_one_thread(args: Arc<Args>) -> Result<u64> {
}; };
} }
if !args.paths().is_empty() && paths_searched == 0 { if !args.paths().is_empty() && paths_searched == 0 {
if !args.no_messages() {
eprint_nothing_searched(); eprint_nothing_searched();
} }
}
Ok(match_count) Ok(match_count)
} }
@ -211,10 +215,11 @@ fn run_files_parallel(args: Arc<Args>) -> Result<u64> {
} }
file_count file_count
}); });
let no_messages = args.no_messages();
args.walker_parallel().run(move || { args.walker_parallel().run(move || {
let tx = tx.clone(); let tx = tx.clone();
Box::new(move |result| { Box::new(move |result| {
if let Some(dent) = get_or_log_dir_entry(result) { if let Some(dent) = get_or_log_dir_entry(result, no_messages) {
tx.send(dent).unwrap(); tx.send(dent).unwrap();
} }
ignore::WalkState::Continue ignore::WalkState::Continue
@ -228,7 +233,7 @@ fn run_files_one_thread(args: Arc<Args>) -> Result<u64> {
let mut printer = args.printer(term); let mut printer = args.printer(term);
let mut file_count = 0; let mut file_count = 0;
for result in args.walker() { for result in args.walker() {
let dent = match get_or_log_dir_entry(result) { let dent = match get_or_log_dir_entry(result, args.no_messages()) {
None => continue, None => continue,
Some(dent) => dent, Some(dent) => dent,
}; };
@ -251,16 +256,21 @@ fn run_types(args: Arc<Args>) -> Result<u64> {
fn get_or_log_dir_entry( fn get_or_log_dir_entry(
result: result::Result<ignore::DirEntry, ignore::Error>, result: result::Result<ignore::DirEntry, ignore::Error>,
no_messages: bool,
) -> Option<ignore::DirEntry> { ) -> Option<ignore::DirEntry> {
match result { match result {
Err(err) => { Err(err) => {
if !no_messages {
eprintln!("{}", err); eprintln!("{}", err);
}
None None
} }
Ok(dent) => { Ok(dent) => {
if let Some(err) = dent.error() { if let Some(err) = dent.error() {
if !no_messages {
eprintln!("{}", err); eprintln!("{}", err);
} }
}
if !dent.file_type().map_or(true, |x| x.is_file()) { if !dent.file_type().map_or(true, |x| x.is_file()) {
None None
} else { } else {

View File

@ -35,6 +35,7 @@ struct Options {
invert_match: bool, invert_match: bool,
line_number: bool, line_number: bool,
max_count: Option<u64>, max_count: Option<u64>,
no_messages: bool,
quiet: bool, quiet: bool,
text: bool, text: bool,
} }
@ -51,6 +52,7 @@ impl Default for Options {
invert_match: false, invert_match: false,
line_number: false, line_number: false,
max_count: None, max_count: None,
no_messages: false,
quiet: false, quiet: false,
text: false, text: false,
} }
@ -186,7 +188,9 @@ impl Worker {
let file = match File::open(path) { let file = match File::open(path) {
Ok(file) => file, Ok(file) => file,
Err(err) => { Err(err) => {
if !self.opts.no_messages {
eprintln!("{}: {}", path.display(), err); eprintln!("{}: {}", path.display(), err);
}
return 0; return 0;
} }
}; };
@ -205,7 +209,9 @@ impl Worker {
count count
} }
Err(err) => { Err(err) => {
if !self.opts.no_messages {
eprintln!("{}", err); eprintln!("{}", err);
}
0 0
} }
} }