Skip to content

Commit

Permalink
Add --no-messages flag.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
BurntSushi committed Nov 6, 2016
1 parent 58aca2e commit 77ad758
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 18 deletions.
5 changes: 5 additions & 0 deletions doc/rg.1
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ context related options.)
.RS
.RE
.TP
.B \-\-no\-messages
Suppress all error messages.
.RS
.RE
.TP
.B \-\-no\-mmap
Never use memory maps, even when they might be faster.
.RS
Expand Down
3 changes: 3 additions & 0 deletions doc/rg.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ Project home page: https://github.com/BurntSushi/ripgrep
when ripgrep thinks it will be faster. (Note that mmap searching
doesn't currently support the various context related options.)

--no-messages
: Suppress all error messages.

--no-mmap
: Never use memory maps, even when they might be faster.

Expand Down
7 changes: 0 additions & 7 deletions globset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,6 @@ pub use glob::{Glob, GlobBuilder, GlobMatcher};
mod glob;
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.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Error {
Expand Down
15 changes: 14 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ Less common options:
when ripgrep thinks it will be faster. (Note that mmap searching
doesn't currently support the various context related options.)
--no-messages
Suppress all error messages.
--no-mmap
Never use memory maps, even when they might be faster.
Expand Down Expand Up @@ -256,6 +259,7 @@ pub struct RawArgs {
flag_no_ignore_parent: bool,
flag_no_ignore_vcs: bool,
flag_no_line_number: bool,
flag_no_messages: bool,
flag_no_mmap: bool,
flag_no_filename: bool,
flag_null: bool,
Expand Down Expand Up @@ -306,6 +310,7 @@ pub struct Args {
no_ignore: bool,
no_ignore_parent: bool,
no_ignore_vcs: bool,
no_messages: bool,
null: bool,
quiet: bool,
replace: Option<Vec<u8>>,
Expand Down Expand Up @@ -429,6 +434,7 @@ impl RawArgs {
no_ignore_vcs:
// --no-ignore implies --no-ignore-vcs
self.flag_no_ignore_vcs || no_ignore,
no_messages: self.flag_no_messages,
null: self.flag_null,
quiet: self.flag_quiet,
replace: self.flag_replace.clone().map(|s| s.into_bytes()),
Expand Down Expand Up @@ -711,6 +717,11 @@ impl Args {
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.
pub fn walker(&self) -> ignore::Walk {
self.walker_builder().build()
Expand All @@ -730,7 +741,9 @@ impl Args {
}
for path in &self.ignore_files {
if let Some(err) = wd.add_ignore(path) {
eprintln!("{}", err);
if !self.no_messages {
eprintln!("{}", err);
}
}
}

Expand Down
26 changes: 18 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn run_parallel(args: Arc<Args>) -> Result<u64> {
if quiet_matched.has_match() {
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,
Some(dent) => dent,
};
Expand Down Expand Up @@ -160,7 +160,9 @@ fn run_parallel(args: Arc<Args>) -> Result<u64> {
})
});
if !args.paths().is_empty() && paths_searched.load(Ordering::SeqCst) == 0 {
eprint_nothing_searched();
if !args.no_messages() {
eprint_nothing_searched();
}
}
Ok(match_count.load(Ordering::SeqCst) as u64)
}
Expand All @@ -171,7 +173,7 @@ fn run_one_thread(args: Arc<Args>) -> Result<u64> {
let mut paths_searched: u64 = 0;
let mut match_count = 0;
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,
Some(dent) => dent,
};
Expand All @@ -193,7 +195,9 @@ fn run_one_thread(args: Arc<Args>) -> Result<u64> {
};
}
if !args.paths().is_empty() && paths_searched == 0 {
eprint_nothing_searched();
if !args.no_messages() {
eprint_nothing_searched();
}
}
Ok(match_count)
}
Expand All @@ -211,10 +215,11 @@ fn run_files_parallel(args: Arc<Args>) -> Result<u64> {
}
file_count
});
let no_messages = args.no_messages();
args.walker_parallel().run(move || {
let tx = tx.clone();
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();
}
ignore::WalkState::Continue
Expand All @@ -228,7 +233,7 @@ fn run_files_one_thread(args: Arc<Args>) -> Result<u64> {
let mut printer = args.printer(term);
let mut file_count = 0;
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,
Some(dent) => dent,
};
Expand All @@ -251,15 +256,20 @@ fn run_types(args: Arc<Args>) -> Result<u64> {

fn get_or_log_dir_entry(
result: result::Result<ignore::DirEntry, ignore::Error>,
no_messages: bool,
) -> Option<ignore::DirEntry> {
match result {
Err(err) => {
eprintln!("{}", err);
if !no_messages {
eprintln!("{}", err);
}
None
}
Ok(dent) => {
if let Some(err) = dent.error() {
eprintln!("{}", err);
if !no_messages {
eprintln!("{}", err);
}
}
if !dent.file_type().map_or(true, |x| x.is_file()) {
None
Expand Down
10 changes: 8 additions & 2 deletions src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct Options {
invert_match: bool,
line_number: bool,
max_count: Option<u64>,
no_messages: bool,
quiet: bool,
text: bool,
}
Expand All @@ -51,6 +52,7 @@ impl Default for Options {
invert_match: false,
line_number: false,
max_count: None,
no_messages: false,
quiet: false,
text: false,
}
Expand Down Expand Up @@ -186,7 +188,9 @@ impl Worker {
let file = match File::open(path) {
Ok(file) => file,
Err(err) => {
eprintln!("{}: {}", path.display(), err);
if !self.opts.no_messages {
eprintln!("{}: {}", path.display(), err);
}
return 0;
}
};
Expand All @@ -205,7 +209,9 @@ impl Worker {
count
}
Err(err) => {
eprintln!("{}", err);
if !self.opts.no_messages {
eprintln!("{}", err);
}
0
}
}
Expand Down

0 comments on commit 77ad758

Please sign in to comment.