mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-03-17 20:28:03 +02:00
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:
parent
58aca2efb2
commit
77ad7588ae
5
doc/rg.1
5
doc/rg.1
@ -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
|
||||
|
@ -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.
|
||||
|
||||
|
@ -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 {
|
||||
|
15
src/args.rs
15
src/args.rs
@ -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.
|
||||
|
||||
@ -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,
|
||||
@ -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>>,
|
||||
@ -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()),
|
||||
@ -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()
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
26
src/main.rs
26
src/main.rs
@ -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,
|
||||
};
|
||||
@ -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)
|
||||
}
|
||||
@ -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,
|
||||
};
|
||||
@ -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)
|
||||
}
|
||||
@ -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
|
||||
@ -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,
|
||||
};
|
||||
@ -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
|
||||
|
@ -35,6 +35,7 @@ struct Options {
|
||||
invert_match: bool,
|
||||
line_number: bool,
|
||||
max_count: Option<u64>,
|
||||
no_messages: bool,
|
||||
quiet: bool,
|
||||
text: bool,
|
||||
}
|
||||
@ -51,6 +52,7 @@ impl Default for Options {
|
||||
invert_match: false,
|
||||
line_number: false,
|
||||
max_count: None,
|
||||
no_messages: false,
|
||||
quiet: false,
|
||||
text: false,
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
};
|
||||
@ -205,7 +209,9 @@ impl Worker {
|
||||
count
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("{}", err);
|
||||
if !self.opts.no_messages {
|
||||
eprintln!("{}", err);
|
||||
}
|
||||
0
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user