2017-01-15 16:32:30 -05:00
|
|
|
extern crate atty;
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-12 21:48:11 -05:00
|
|
|
extern crate bytecount;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate clap;
|
2016-08-28 01:37:12 -04:00
|
|
|
extern crate env_logger;
|
2016-06-20 16:53:48 -04:00
|
|
|
extern crate grep;
|
2016-10-11 19:57:09 -04:00
|
|
|
extern crate ignore;
|
2016-09-03 21:48:23 -04:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
2016-09-05 17:36:41 -04:00
|
|
|
extern crate libc;
|
2016-08-28 01:37:12 -04:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2016-03-10 20:48:44 -05:00
|
|
|
extern crate memchr;
|
|
|
|
extern crate memmap;
|
2016-08-25 21:44:37 -04:00
|
|
|
extern crate num_cpus;
|
2016-02-27 11:07:26 -05:00
|
|
|
extern crate regex;
|
Don't search stdout redirected file.
When running ripgrep like this:
rg foo > output
we must be careful not to search `output` since ripgrep is actively writing
to it. Searching it can cause massive blowups where the file grows without
bound.
While this is conceptually easy to fix (check the inode of the redirection
and the inode of the file you're about to search), there are a few problems
with it.
First, inodes are a Unix thing, so we need a Windows specific solution to
this as well. To resolve this concern, I created a new crate, `same-file`,
which provides a cross platform abstraction.
Second, stat'ing every file is costly. This is not avoidable on Windows,
but on Unix, we can get the inode number directly from directory traversal.
However, this information wasn't exposed, but now it is (through both the
ignore and walkdir crates).
Fixes #286
2017-01-08 10:27:30 -05:00
|
|
|
extern crate same_file;
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
extern crate termcolor;
|
2016-02-27 11:07:26 -05:00
|
|
|
|
|
|
|
use std::error::Error;
|
|
|
|
use std::process;
|
|
|
|
use std::result;
|
2016-12-24 10:06:37 -05:00
|
|
|
use std::sync::Arc;
|
2016-12-24 12:53:09 -05:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2016-11-05 21:44:15 -04:00
|
|
|
use std::sync::mpsc;
|
2016-08-28 01:37:12 -04:00
|
|
|
use std::thread;
|
2016-02-27 11:07:26 -05:00
|
|
|
|
2016-09-05 00:52:23 -04:00
|
|
|
use args::Args;
|
2016-11-05 21:44:15 -04:00
|
|
|
use worker::Work;
|
2016-08-27 01:01:06 -04:00
|
|
|
|
2016-08-25 21:44:37 -04:00
|
|
|
macro_rules! errored {
|
|
|
|
($($tt:tt)*) => {
|
|
|
|
return Err(From::from(format!($($tt)*)));
|
|
|
|
}
|
|
|
|
}
|
2016-02-27 11:07:26 -05:00
|
|
|
|
2016-08-25 21:44:37 -04:00
|
|
|
macro_rules! eprintln {
|
|
|
|
($($tt:tt)*) => {{
|
|
|
|
use std::io::Write;
|
|
|
|
let _ = writeln!(&mut ::std::io::stderr(), $($tt)*);
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-12 21:48:11 -05:00
|
|
|
mod app;
|
2016-09-05 00:52:23 -04:00
|
|
|
mod args;
|
2016-09-15 22:06:04 -04:00
|
|
|
mod pathutil;
|
2016-08-28 01:37:12 -04:00
|
|
|
mod printer;
|
2016-09-06 21:47:33 -04:00
|
|
|
mod search_buffer;
|
2016-09-10 00:08:42 -04:00
|
|
|
mod search_stream;
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-12 21:48:11 -05:00
|
|
|
mod unescape;
|
2016-11-05 21:44:15 -04:00
|
|
|
mod worker;
|
2016-03-10 20:48:44 -05:00
|
|
|
|
2016-08-28 01:37:12 -04:00
|
|
|
pub type Result<T> = result::Result<T, Box<Error + Send + Sync>>;
|
|
|
|
|
2016-02-27 11:07:26 -05:00
|
|
|
fn main() {
|
2016-11-05 21:44:15 -04:00
|
|
|
match Args::parse().map(Arc::new).and_then(run) {
|
2016-12-04 12:00:13 -05:00
|
|
|
Ok(0) => process::exit(1),
|
2016-09-13 21:11:46 -04:00
|
|
|
Ok(_) => process::exit(0),
|
2016-02-27 11:07:26 -05:00
|
|
|
Err(err) => {
|
2016-09-05 17:36:41 -04:00
|
|
|
eprintln!("{}", err);
|
2016-02-27 11:07:26 -05:00
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-05 21:44:15 -04:00
|
|
|
fn run(args: Arc<Args>) -> Result<u64> {
|
2016-11-06 13:09:53 -05:00
|
|
|
if args.never_match() {
|
|
|
|
return Ok(0);
|
|
|
|
}
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
let threads = args.threads();
|
2016-09-05 00:52:23 -04:00
|
|
|
if args.files() {
|
2016-11-05 21:44:15 -04:00
|
|
|
if threads == 1 || args.is_one_path() {
|
|
|
|
run_files_one_thread(args)
|
|
|
|
} else {
|
|
|
|
run_files_parallel(args)
|
|
|
|
}
|
|
|
|
} else if args.type_list() {
|
|
|
|
run_types(args)
|
|
|
|
} else if threads == 1 || args.is_one_path() {
|
|
|
|
run_one_thread(args)
|
|
|
|
} else {
|
|
|
|
run_parallel(args)
|
2016-09-25 21:27:17 -04:00
|
|
|
}
|
2016-11-05 21:44:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run_parallel(args: Arc<Args>) -> Result<u64> {
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
let bufwtr = Arc::new(args.buffer_writer());
|
2016-12-24 12:53:09 -05:00
|
|
|
let quiet_matched = args.quiet_matched();
|
2016-11-05 21:44:15 -04:00
|
|
|
let paths_searched = Arc::new(AtomicUsize::new(0));
|
|
|
|
let match_count = Arc::new(AtomicUsize::new(0));
|
|
|
|
|
|
|
|
args.walker_parallel().run(|| {
|
|
|
|
let args = args.clone();
|
|
|
|
let quiet_matched = quiet_matched.clone();
|
|
|
|
let paths_searched = paths_searched.clone();
|
|
|
|
let match_count = match_count.clone();
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
let bufwtr = bufwtr.clone();
|
|
|
|
let mut buf = bufwtr.buffer();
|
2016-11-05 21:44:15 -04:00
|
|
|
let mut worker = args.worker();
|
|
|
|
Box::new(move |result| {
|
|
|
|
use ignore::WalkState::*;
|
2016-08-28 20:18:34 -04:00
|
|
|
|
2016-11-05 21:44:15 -04:00
|
|
|
if quiet_matched.has_match() {
|
|
|
|
return Quit;
|
|
|
|
}
|
Don't search stdout redirected file.
When running ripgrep like this:
rg foo > output
we must be careful not to search `output` since ripgrep is actively writing
to it. Searching it can cause massive blowups where the file grows without
bound.
While this is conceptually easy to fix (check the inode of the redirection
and the inode of the file you're about to search), there are a few problems
with it.
First, inodes are a Unix thing, so we need a Windows specific solution to
this as well. To resolve this concern, I created a new crate, `same-file`,
which provides a cross platform abstraction.
Second, stat'ing every file is costly. This is not avoidable on Windows,
but on Unix, we can get the inode number directly from directory traversal.
However, this information wasn't exposed, but now it is (through both the
ignore and walkdir crates).
Fixes #286
2017-01-08 10:27:30 -05:00
|
|
|
let dent = match get_or_log_dir_entry(
|
|
|
|
result,
|
|
|
|
args.stdout_handle(),
|
|
|
|
args.no_messages(),
|
|
|
|
) {
|
2016-11-05 21:44:15 -04:00
|
|
|
None => return Continue,
|
|
|
|
Some(dent) => dent,
|
2016-08-28 20:18:34 -04:00
|
|
|
};
|
2016-11-05 21:44:15 -04:00
|
|
|
paths_searched.fetch_add(1, Ordering::SeqCst);
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
buf.clear();
|
2016-11-05 21:44:15 -04:00
|
|
|
{
|
|
|
|
// This block actually executes the search and prints the
|
|
|
|
// results into outbuf.
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
let mut printer = args.printer(&mut buf);
|
2016-11-05 21:44:15 -04:00
|
|
|
let count =
|
|
|
|
if dent.is_stdin() {
|
|
|
|
worker.run(&mut printer, Work::Stdin)
|
|
|
|
} else {
|
|
|
|
worker.run(&mut printer, Work::DirEntry(dent))
|
|
|
|
};
|
|
|
|
match_count.fetch_add(count as usize, Ordering::SeqCst);
|
|
|
|
if quiet_matched.set_match(count > 0) {
|
|
|
|
return Quit;
|
|
|
|
}
|
|
|
|
}
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
// BUG(burntsushi): We should handle this error instead of ignoring
|
|
|
|
// it. See: https://github.com/BurntSushi/ripgrep/issues/200
|
|
|
|
let _ = bufwtr.print(&buf);
|
2016-11-05 21:44:15 -04:00
|
|
|
Continue
|
|
|
|
})
|
|
|
|
});
|
|
|
|
if !args.paths().is_empty() && paths_searched.load(Ordering::SeqCst) == 0 {
|
2016-11-06 14:36:08 -05:00
|
|
|
if !args.no_messages() {
|
|
|
|
eprint_nothing_searched();
|
|
|
|
}
|
2016-08-28 20:18:34 -04:00
|
|
|
}
|
2016-11-05 21:44:15 -04:00
|
|
|
Ok(match_count.load(Ordering::SeqCst) as u64)
|
2016-08-28 20:18:34 -04:00
|
|
|
}
|
2016-08-25 21:44:37 -04:00
|
|
|
|
2016-09-25 21:27:17 -04:00
|
|
|
fn run_one_thread(args: Arc<Args>) -> Result<u64> {
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
let stdout = args.stdout();
|
|
|
|
let mut stdout = stdout.lock();
|
2016-11-05 21:44:15 -04:00
|
|
|
let mut worker = args.worker();
|
2016-09-25 21:27:17 -04:00
|
|
|
let mut paths_searched: u64 = 0;
|
2016-11-05 21:44:15 -04:00
|
|
|
let mut match_count = 0;
|
|
|
|
for result in args.walker() {
|
Don't search stdout redirected file.
When running ripgrep like this:
rg foo > output
we must be careful not to search `output` since ripgrep is actively writing
to it. Searching it can cause massive blowups where the file grows without
bound.
While this is conceptually easy to fix (check the inode of the redirection
and the inode of the file you're about to search), there are a few problems
with it.
First, inodes are a Unix thing, so we need a Windows specific solution to
this as well. To resolve this concern, I created a new crate, `same-file`,
which provides a cross platform abstraction.
Second, stat'ing every file is costly. This is not avoidable on Windows,
but on Unix, we can get the inode number directly from directory traversal.
However, this information wasn't exposed, but now it is (through both the
ignore and walkdir crates).
Fixes #286
2017-01-08 10:27:30 -05:00
|
|
|
let dent = match get_or_log_dir_entry(
|
|
|
|
result,
|
|
|
|
args.stdout_handle(),
|
|
|
|
args.no_messages(),
|
|
|
|
) {
|
2016-11-05 21:44:15 -04:00
|
|
|
None => continue,
|
|
|
|
Some(dent) => dent,
|
|
|
|
};
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
let mut printer = args.printer(&mut stdout);
|
2016-11-05 21:44:15 -04:00
|
|
|
if match_count > 0 {
|
2016-10-11 19:57:09 -04:00
|
|
|
if args.quiet() {
|
|
|
|
break;
|
2016-09-25 21:27:17 -04:00
|
|
|
}
|
2016-10-11 19:57:09 -04:00
|
|
|
if let Some(sep) = args.file_separator() {
|
|
|
|
printer = printer.file_separator(sep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
paths_searched += 1;
|
2016-11-05 21:44:15 -04:00
|
|
|
match_count +=
|
|
|
|
if dent.is_stdin() {
|
|
|
|
worker.run(&mut printer, Work::Stdin)
|
|
|
|
} else {
|
|
|
|
worker.run(&mut printer, Work::DirEntry(dent))
|
2016-10-11 19:57:09 -04:00
|
|
|
};
|
2016-09-25 21:27:17 -04:00
|
|
|
}
|
2016-10-11 19:57:09 -04:00
|
|
|
if !args.paths().is_empty() && paths_searched == 0 {
|
2016-11-06 14:36:08 -05:00
|
|
|
if !args.no_messages() {
|
|
|
|
eprint_nothing_searched();
|
|
|
|
}
|
2016-09-25 21:27:17 -04:00
|
|
|
}
|
2016-11-05 21:44:15 -04:00
|
|
|
Ok(match_count)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_files_parallel(args: Arc<Args>) -> Result<u64> {
|
|
|
|
let print_args = args.clone();
|
|
|
|
let (tx, rx) = mpsc::channel::<ignore::DirEntry>();
|
|
|
|
let print_thread = thread::spawn(move || {
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
let stdout = print_args.stdout();
|
|
|
|
let mut printer = print_args.printer(stdout.lock());
|
2016-11-05 21:44:15 -04:00
|
|
|
let mut file_count = 0;
|
|
|
|
for dent in rx.iter() {
|
|
|
|
printer.path(dent.path());
|
|
|
|
file_count += 1;
|
|
|
|
}
|
|
|
|
file_count
|
|
|
|
});
|
|
|
|
args.walker_parallel().run(move || {
|
Don't search stdout redirected file.
When running ripgrep like this:
rg foo > output
we must be careful not to search `output` since ripgrep is actively writing
to it. Searching it can cause massive blowups where the file grows without
bound.
While this is conceptually easy to fix (check the inode of the redirection
and the inode of the file you're about to search), there are a few problems
with it.
First, inodes are a Unix thing, so we need a Windows specific solution to
this as well. To resolve this concern, I created a new crate, `same-file`,
which provides a cross platform abstraction.
Second, stat'ing every file is costly. This is not avoidable on Windows,
but on Unix, we can get the inode number directly from directory traversal.
However, this information wasn't exposed, but now it is (through both the
ignore and walkdir crates).
Fixes #286
2017-01-08 10:27:30 -05:00
|
|
|
let args = args.clone();
|
2016-11-05 21:44:15 -04:00
|
|
|
let tx = tx.clone();
|
|
|
|
Box::new(move |result| {
|
Don't search stdout redirected file.
When running ripgrep like this:
rg foo > output
we must be careful not to search `output` since ripgrep is actively writing
to it. Searching it can cause massive blowups where the file grows without
bound.
While this is conceptually easy to fix (check the inode of the redirection
and the inode of the file you're about to search), there are a few problems
with it.
First, inodes are a Unix thing, so we need a Windows specific solution to
this as well. To resolve this concern, I created a new crate, `same-file`,
which provides a cross platform abstraction.
Second, stat'ing every file is costly. This is not avoidable on Windows,
but on Unix, we can get the inode number directly from directory traversal.
However, this information wasn't exposed, but now it is (through both the
ignore and walkdir crates).
Fixes #286
2017-01-08 10:27:30 -05:00
|
|
|
if let Some(dent) = get_or_log_dir_entry(
|
|
|
|
result,
|
|
|
|
args.stdout_handle(),
|
|
|
|
args.no_messages(),
|
|
|
|
) {
|
2016-11-05 21:44:15 -04:00
|
|
|
tx.send(dent).unwrap();
|
|
|
|
}
|
|
|
|
ignore::WalkState::Continue
|
|
|
|
})
|
|
|
|
});
|
|
|
|
Ok(print_thread.join().unwrap())
|
2016-09-25 21:27:17 -04:00
|
|
|
}
|
|
|
|
|
2016-11-05 21:44:15 -04:00
|
|
|
fn run_files_one_thread(args: Arc<Args>) -> Result<u64> {
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
let stdout = args.stdout();
|
|
|
|
let mut printer = args.printer(stdout.lock());
|
2016-09-05 00:52:23 -04:00
|
|
|
let mut file_count = 0;
|
2016-11-05 21:44:15 -04:00
|
|
|
for result in args.walker() {
|
Don't search stdout redirected file.
When running ripgrep like this:
rg foo > output
we must be careful not to search `output` since ripgrep is actively writing
to it. Searching it can cause massive blowups where the file grows without
bound.
While this is conceptually easy to fix (check the inode of the redirection
and the inode of the file you're about to search), there are a few problems
with it.
First, inodes are a Unix thing, so we need a Windows specific solution to
this as well. To resolve this concern, I created a new crate, `same-file`,
which provides a cross platform abstraction.
Second, stat'ing every file is costly. This is not avoidable on Windows,
but on Unix, we can get the inode number directly from directory traversal.
However, this information wasn't exposed, but now it is (through both the
ignore and walkdir crates).
Fixes #286
2017-01-08 10:27:30 -05:00
|
|
|
let dent = match get_or_log_dir_entry(
|
|
|
|
result,
|
|
|
|
args.stdout_handle(),
|
|
|
|
args.no_messages(),
|
|
|
|
) {
|
2016-11-05 21:44:15 -04:00
|
|
|
None => continue,
|
|
|
|
Some(dent) => dent,
|
|
|
|
};
|
2016-10-11 19:57:09 -04:00
|
|
|
printer.path(dent.path());
|
|
|
|
file_count += 1;
|
2016-09-03 21:48:23 -04:00
|
|
|
}
|
2016-09-05 00:52:23 -04:00
|
|
|
Ok(file_count)
|
|
|
|
}
|
2016-09-03 21:48:23 -04:00
|
|
|
|
2016-09-13 21:11:46 -04:00
|
|
|
fn run_types(args: Arc<Args>) -> Result<u64> {
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
let stdout = args.stdout();
|
|
|
|
let mut printer = args.printer(stdout.lock());
|
2016-09-05 00:52:23 -04:00
|
|
|
let mut ty_count = 0;
|
|
|
|
for def in args.type_defs() {
|
|
|
|
printer.type_def(def);
|
|
|
|
ty_count += 1;
|
2016-09-03 21:48:23 -04:00
|
|
|
}
|
2016-09-05 00:52:23 -04:00
|
|
|
Ok(ty_count)
|
2016-08-27 01:01:06 -04:00
|
|
|
}
|
|
|
|
|
2016-11-05 21:44:15 -04:00
|
|
|
fn get_or_log_dir_entry(
|
|
|
|
result: result::Result<ignore::DirEntry, ignore::Error>,
|
Don't search stdout redirected file.
When running ripgrep like this:
rg foo > output
we must be careful not to search `output` since ripgrep is actively writing
to it. Searching it can cause massive blowups where the file grows without
bound.
While this is conceptually easy to fix (check the inode of the redirection
and the inode of the file you're about to search), there are a few problems
with it.
First, inodes are a Unix thing, so we need a Windows specific solution to
this as well. To resolve this concern, I created a new crate, `same-file`,
which provides a cross platform abstraction.
Second, stat'ing every file is costly. This is not avoidable on Windows,
but on Unix, we can get the inode number directly from directory traversal.
However, this information wasn't exposed, but now it is (through both the
ignore and walkdir crates).
Fixes #286
2017-01-08 10:27:30 -05:00
|
|
|
stdout_handle: Option<&same_file::Handle>,
|
2016-11-06 14:36:08 -05:00
|
|
|
no_messages: bool,
|
2016-11-05 21:44:15 -04:00
|
|
|
) -> Option<ignore::DirEntry> {
|
|
|
|
match result {
|
|
|
|
Err(err) => {
|
2016-11-06 14:36:08 -05:00
|
|
|
if !no_messages {
|
|
|
|
eprintln!("{}", err);
|
|
|
|
}
|
2016-11-05 21:44:15 -04:00
|
|
|
None
|
2016-08-28 01:37:12 -04:00
|
|
|
}
|
2016-11-05 21:44:15 -04:00
|
|
|
Ok(dent) => {
|
|
|
|
if let Some(err) = dent.error() {
|
2016-11-06 14:36:08 -05:00
|
|
|
if !no_messages {
|
|
|
|
eprintln!("{}", err);
|
|
|
|
}
|
2016-09-05 10:15:13 -04:00
|
|
|
}
|
2016-11-06 18:23:50 -05:00
|
|
|
let ft = match dent.file_type() {
|
|
|
|
None => return Some(dent), // entry is stdin
|
|
|
|
Some(ft) => ft,
|
|
|
|
};
|
2016-11-06 19:02:14 -05:00
|
|
|
// A depth of 0 means the user gave the path explicitly, so we
|
|
|
|
// should always try to search it.
|
|
|
|
if dent.depth() == 0 && !ft.is_dir() {
|
Don't search stdout redirected file.
When running ripgrep like this:
rg foo > output
we must be careful not to search `output` since ripgrep is actively writing
to it. Searching it can cause massive blowups where the file grows without
bound.
While this is conceptually easy to fix (check the inode of the redirection
and the inode of the file you're about to search), there are a few problems
with it.
First, inodes are a Unix thing, so we need a Windows specific solution to
this as well. To resolve this concern, I created a new crate, `same-file`,
which provides a cross platform abstraction.
Second, stat'ing every file is costly. This is not avoidable on Windows,
but on Unix, we can get the inode number directly from directory traversal.
However, this information wasn't exposed, but now it is (through both the
ignore and walkdir crates).
Fixes #286
2017-01-08 10:27:30 -05:00
|
|
|
return Some(dent);
|
|
|
|
} else if !ft.is_file() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
// If we are redirecting stdout to a file, then don't search that
|
|
|
|
// file.
|
|
|
|
if is_stdout_file(&dent, stdout_handle, no_messages) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(dent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_stdout_file(
|
|
|
|
dent: &ignore::DirEntry,
|
|
|
|
stdout_handle: Option<&same_file::Handle>,
|
|
|
|
no_messages: bool,
|
|
|
|
) -> bool {
|
|
|
|
let stdout_handle = match stdout_handle {
|
|
|
|
None => return false,
|
|
|
|
Some(stdout_handle) => stdout_handle,
|
|
|
|
};
|
|
|
|
// If we know for sure that these two things aren't equal, then avoid
|
|
|
|
// the costly extra stat call to determine equality.
|
|
|
|
if !maybe_dent_eq_handle(dent, stdout_handle) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
match same_file::Handle::from_path(dent.path()) {
|
|
|
|
Ok(h) => stdout_handle == &h,
|
|
|
|
Err(err) => {
|
|
|
|
if !no_messages {
|
|
|
|
eprintln!("{}: {}", dent.path().display(), err);
|
2016-11-05 21:44:15 -04:00
|
|
|
}
|
Don't search stdout redirected file.
When running ripgrep like this:
rg foo > output
we must be careful not to search `output` since ripgrep is actively writing
to it. Searching it can cause massive blowups where the file grows without
bound.
While this is conceptually easy to fix (check the inode of the redirection
and the inode of the file you're about to search), there are a few problems
with it.
First, inodes are a Unix thing, so we need a Windows specific solution to
this as well. To resolve this concern, I created a new crate, `same-file`,
which provides a cross platform abstraction.
Second, stat'ing every file is costly. This is not avoidable on Windows,
but on Unix, we can get the inode number directly from directory traversal.
However, this information wasn't exposed, but now it is (through both the
ignore and walkdir crates).
Fixes #286
2017-01-08 10:27:30 -05:00
|
|
|
false
|
2016-09-05 10:15:13 -04:00
|
|
|
}
|
2016-09-05 00:52:23 -04:00
|
|
|
}
|
2016-11-05 21:44:15 -04:00
|
|
|
}
|
2016-09-05 00:52:23 -04:00
|
|
|
|
Don't search stdout redirected file.
When running ripgrep like this:
rg foo > output
we must be careful not to search `output` since ripgrep is actively writing
to it. Searching it can cause massive blowups where the file grows without
bound.
While this is conceptually easy to fix (check the inode of the redirection
and the inode of the file you're about to search), there are a few problems
with it.
First, inodes are a Unix thing, so we need a Windows specific solution to
this as well. To resolve this concern, I created a new crate, `same-file`,
which provides a cross platform abstraction.
Second, stat'ing every file is costly. This is not avoidable on Windows,
but on Unix, we can get the inode number directly from directory traversal.
However, this information wasn't exposed, but now it is (through both the
ignore and walkdir crates).
Fixes #286
2017-01-08 10:27:30 -05:00
|
|
|
#[cfg(unix)]
|
|
|
|
fn maybe_dent_eq_handle(
|
|
|
|
dent: &ignore::DirEntry,
|
|
|
|
handle: &same_file::Handle,
|
|
|
|
) -> bool {
|
|
|
|
dent.ino() == Some(handle.ino())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
fn maybe_dent_eq_handle(_: &ignore::DirEntry, _: &same_file::Handle) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2016-11-05 21:44:15 -04:00
|
|
|
fn eprint_nothing_searched() {
|
|
|
|
eprintln!("No files were searched, which means ripgrep probably \
|
|
|
|
applied a filter you didn't expect. \
|
|
|
|
Try running again with --debug.");
|
2016-09-03 21:48:23 -04:00
|
|
|
}
|