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) -> Result { 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("-") { - if worker.match_count > 0 { - if let Some(ref sep) = filesep { - let _ = term.write_all(sep); - let _ = term.write_all(b"\n"); - } - } paths_searched += 1; let mut printer = args.printer(&mut term); + if worker.match_count > 0 { + if let Some(sep) = args.file_separator() { + printer = printer.file_separator(sep); + } + } worker.do_work(&mut printer, WorkReady::Stdin); } else { for ent in try!(args.walker(p)) { - if worker.match_count > 0 { - if let Some(ref sep) = filesep { - let _ = term.write_all(sep); - let _ = term.write_all(b"\n"); - } - } paths_searched += 1; let mut printer = args.printer(&mut term); + if worker.match_count > 0 { + if let Some(sep) = args.file_separator() { + printer = printer.file_separator(sep); + } + } let file = match File::open(ent.path()) { Ok(file) => file, Err(err) => { diff --git a/src/out.rs b/src/out.rs index 9ed0154d..389f5458 100644 --- a/src/out.rs +++ b/src/out.rs @@ -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) -> 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 { /// 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>, /// 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 Printer { 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 Printer { self } + /// If set, the separator is printed before any matches. By default, no + /// separator is printed. + pub fn file_separator(mut self, sep: Vec) -> Printer { + 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 Printer { column: Option, ) { 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 Printer { line_number: Option, ) { 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 Printer { 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 = + 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");