Add --sort-files flag.

When used, parallelism is disabled but the results are sorted by file
path.

Closes #263
This commit is contained in:
Andrew Gallant
2017-01-06 22:43:59 -05:00
parent 95cea77625
commit b65a8c353b
6 changed files with 73 additions and 2 deletions

View File

@@ -158,6 +158,7 @@ fn app<F>(next_line_help: bool, doc: F) -> App<'static, 'static>
.arg(flag("replace").short("r").value_name("ARG").takes_value(true))
.arg(flag("case-sensitive").short("s"))
.arg(flag("smart-case").short("S"))
.arg(flag("sort-files"))
.arg(flag("threads")
.short("j").value_name("ARG").takes_value(true)
.validator(validate_number))
@@ -426,6 +427,10 @@ lazy_static! {
"Searches case insensitively if the pattern is all lowercase. \
Search case sensitively otherwise. This is overridden by \
either -s/--case-sensitive or -i/--ignore-case.");
doc!(h, "sort-files",
"Sort results by file path. Implies --threads=1.",
"Sort results by file path. Note that this currently \
disables all parallelism and runs search in a single thread.");
doc!(h, "threads",
"The approximate number of threads to use.",
"The approximate number of threads to use. A value of 0 (which \

View File

@@ -64,6 +64,7 @@ pub struct Args {
quiet: bool,
quiet_matched: QuietMatched,
replace: Option<Vec<u8>>,
sort_files: bool,
text: bool,
threads: usize,
type_list: bool,
@@ -277,6 +278,9 @@ impl Args {
wd.ignore(!self.no_ignore);
wd.parents(!self.no_ignore_parent);
wd.threads(self.threads());
if self.sort_files {
wd.sort_by(|a, b| a.cmp(b));
}
wd
}
}
@@ -333,6 +337,7 @@ impl<'a> ArgMatches<'a> {
quiet: quiet,
quiet_matched: QuietMatched::new(quiet),
replace: self.replace(),
sort_files: self.is_present("sort-files"),
text: self.text(),
threads: try!(self.threads()),
type_list: self.is_present("type-list"),
@@ -654,6 +659,9 @@ impl<'a> ArgMatches<'a> {
/// Returns the approximate number of threads that ripgrep should use.
fn threads(&self) -> Result<usize> {
if self.is_present("sort-files") {
return Ok(1);
}
let threads = try!(self.usize_of("threads")).unwrap_or(0);
Ok(if threads == 0 {
cmp::min(12, num_cpus::get())