diff options
author | Andrew Gallant <jamslam@gmail.com> | 2016-09-26 19:57:23 -0400 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2016-09-26 19:57:23 -0400 |
commit | 3e78fce3a3ca226dde4e332a30bf24fad03f8f35 (patch) | |
tree | 04a927a7a706238b10158ef8dae0fe455eb54f58 | |
parent | 7a3fd1f23fe7a4d679fb1fb7d13884a653e5d4f5 (diff) |
Don't print empty lines in single threaded mode.
Fixes #99.
-rw-r--r-- | src/main.rs | 21 | ||||
-rw-r--r-- | src/out.rs | 2 | ||||
-rw-r--r-- | src/printer.rs | 23 | ||||
-rw-r--r-- | tests/tests.rs | 19 |
4 files changed, 51 insertions, 14 deletions
diff --git a/src/main.rs b/src/main.rs index 74326e1f..b095f009 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,7 @@ extern crate winapi; use std::error::Error; use std::fs::File; -use std::io::{self, Write}; +use std::io; use std::path::Path; use std::process; use std::result; @@ -157,31 +157,28 @@ fn run_one_thread(args: Arc<Args>) -> Result<u64> { match_count: 0, }; let paths = args.paths(); - let filesep = args.file_separator(); let mut term = args.stdout(); let mut paths_searched: u64 = 0; for p in paths { if p == Path::new("-") { + paths_searched += 1; + let mut printer = args.printer(&mut term); if worker.match_count > 0 { - if let Some(ref sep) = filesep { - let _ = term.write_all(sep); - let _ = term.write_all(b"\n"); + if let Some(sep) = args.file_separator() { + printer = printer.file_separator(sep); } } - paths_searched += 1; - let mut printer = args.printer(&mut term); worker.do_work(&mut printer, WorkReady::Stdin); } else { for ent in try!(args.walker(p)) { + paths_searched += 1; + let mut printer = args.printer(&mut term); if worker.match_count > 0 { - if let Some(ref sep) = filesep { - let _ = term.write_all(sep); - let _ = term.write_all(b"\n"); + if let Some(sep) = args.file_separator() { + printer = printer.file_separator(sep); } } - paths_searched += 1; - let mut printer = args.printer(&mut term); let file = match File::open(ent.path()) { Ok(file) => file, Err(err) => { @@ -48,8 +48,6 @@ impl Out { /// If set, the separator is printed between matches from different files. /// By default, no separator is printed. - /// - /// If sep is empty, then no file separator is printed. pub fn file_separator(mut self, sep: Vec<u8>) -> Out { self.file_separator = Some(sep); self diff --git a/src/printer.rs b/src/printer.rs index 07c96754..c9f844bc 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -25,6 +25,8 @@ pub struct Printer<W> { /// printed via the match directly, but occasionally we need to insert them /// ourselves (for example, to print a context separator). eol: u8, + /// A file separator to show before any matches are printed. + file_separator: Option<Vec<u8>>, /// Whether to show file name as a heading or not. /// /// N.B. If with_filename is false, then this setting has no effect. @@ -51,6 +53,7 @@ impl<W: Terminal + Send> Printer<W> { column: false, context_separator: "--".to_string().into_bytes(), eol: b'\n', + file_separator: None, heading: false, line_per_match: false, quiet: false, @@ -79,6 +82,13 @@ impl<W: Terminal + Send> Printer<W> { self } + /// If set, the separator is printed before any matches. By default, no + /// separator is printed. + pub fn file_separator(mut self, sep: Vec<u8>) -> Printer<W> { + self.file_separator = Some(sep); + self + } + /// Whether to show file name as a heading or not. /// /// N.B. If with_filename is false, then this setting has no effect. @@ -230,6 +240,7 @@ impl<W: Terminal + Send> Printer<W> { column: Option<u64>, ) { if self.heading && self.with_filename && !self.has_printed { + self.write_file_sep(); self.write_heading(path.as_ref()); } else if !self.heading && self.with_filename { self.write_path(path.as_ref()); @@ -284,6 +295,7 @@ impl<W: Terminal + Send> Printer<W> { line_number: Option<u64>, ) { if self.heading && self.with_filename && !self.has_printed { + self.write_file_sep(); self.write_heading(path.as_ref()); } else if !self.heading && self.with_filename { self.write_path(path.as_ref()); @@ -355,4 +367,15 @@ impl<W: Terminal + Send> Printer<W> { let eol = self.eol; self.write(&[eol]); } + + fn write_file_sep(&mut self) { + if self.quiet { + return; + } + if let Some(ref sep) = self.file_separator { + self.has_printed = true; + let _ = self.wtr.write_all(sep); + let _ = self.wtr.write_all(b"\n"); + } + } } diff --git a/tests/tests.rs b/tests/tests.rs index d6767065..11513aa2 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -54,6 +54,13 @@ fn path(unix: &str) -> String { } } +fn sort_lines(lines: &str) -> String { + let mut lines: Vec<String> = + lines.trim().lines().map(|s| s.to_owned()).collect(); + lines.sort(); + format!("{}\n", lines.join("\n")) +} + sherlock!(single_file, |wd: WorkDir, mut cmd| { let lines: String = wd.stdout(&mut cmd); let expected = "\ @@ -716,6 +723,18 @@ clean!(regression_93, r"(\d{1,3}\.){3}\d{1,3}", ".", assert_eq!(lines, "foo:192.168.1.1\n"); }); +// See: https://github.com/BurntSushi/ripgrep/issues/99 +clean!(regression_99, "test", ".", +|wd: WorkDir, mut cmd: Command| { + wd.create("foo1", "test"); + wd.create("foo2", "zzz"); + wd.create("bar", "test"); + cmd.arg("-j1").arg("--heading"); + + let lines: String = wd.stdout(&mut cmd); + assert_eq!(sort_lines(&lines), sort_lines("bar\ntest\n\nfoo1\ntest\n")); +}); + // See: https://github.com/BurntSushi/ripgrep/issues/105 clean!(regression_105_part1, "test", ".", |wd: WorkDir, mut cmd: Command| { wd.create("foo", "zztest"); |