1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2025-03-03 14:32:22 +02:00

printer: always use \n in the summary printer

Previously, the \x00 byte would be printed when --null-data was being used.
This commit is contained in:
Lucas Trzesniewski 2024-05-12 16:22:26 +02:00
parent bb6f0f5519
commit 4b692bb7b0

View File

@ -561,13 +561,13 @@ impl<'p, 's, M: Matcher, W: WriteColor> SummarySink<'p, 's, M, W> {
/// write that path to the underlying writer followed by a line terminator.
/// (If a path terminator is set, then that is used instead of the line
/// terminator.)
fn write_path_line(&mut self, searcher: &Searcher) -> io::Result<()> {
fn write_path_line(&mut self) -> io::Result<()> {
if self.path.is_some() {
self.write_path()?;
if let Some(term) = self.summary.config.path_terminator {
self.write(&[term])?;
} else {
self.write_line_term(searcher)?;
self.write_line_term()?;
}
}
Ok(())
@ -624,9 +624,9 @@ impl<'p, 's, M: Matcher, W: WriteColor> SummarySink<'p, 's, M, W> {
self.interpolator.finish(status, &mut *self.summary.wtr.borrow_mut())
}
/// Write the line terminator configured on the given searcher.
fn write_line_term(&self, searcher: &Searcher) -> io::Result<()> {
self.write(searcher.line_terminator().as_bytes())
/// Write the `\n` line terminator.
fn write_line_term(&self) -> io::Result<()> {
self.write(b"\n")
}
/// Write the given bytes using the give style.
@ -774,7 +774,7 @@ impl<'p, 's, M: Matcher, W: WriteColor> Sink for SummarySink<'p, 's, M, W> {
if show_count {
self.write_path_field()?;
self.write(self.match_count.to_string().as_bytes())?;
self.write_line_term(searcher)?;
self.write_line_term()?;
}
}
SummaryKind::CountMatches => {
@ -785,17 +785,17 @@ impl<'p, 's, M: Matcher, W: WriteColor> Sink for SummarySink<'p, 's, M, W> {
.as_ref()
.expect("CountMatches should enable stats tracking");
self.write(stats.matches().to_string().as_bytes())?;
self.write_line_term(searcher)?;
self.write_line_term()?;
}
}
SummaryKind::PathWithMatch => {
if self.match_count > 0 {
self.write_path_line(searcher)?;
self.write_path_line()?;
}
}
SummaryKind::PathWithoutMatch => {
if self.match_count == 0 {
self.write_path_line(searcher)?;
self.write_path_line()?;
}
}
SummaryKind::Quiet => {}
@ -806,6 +806,7 @@ impl<'p, 's, M: Matcher, W: WriteColor> Sink for SummarySink<'p, 's, M, W> {
#[cfg(test)]
mod tests {
use grep_matcher::LineTerminator;
use grep_regex::RegexMatcher;
use grep_searcher::SearcherBuilder;
use termcolor::NoColor;
@ -1163,4 +1164,24 @@ and exhibited clearly, with a label attached.
// mush on to find all matches.
assert_eq!(3, match_count);
}
#[test]
fn use_standard_line_terminator() {
let matcher = RegexMatcher::new(r"Watson|Sherlock").unwrap();
let mut printer = SummaryBuilder::new()
.kind(SummaryKind::CountMatches)
.build_no_color(vec![]);
SearcherBuilder::new()
.line_terminator(LineTerminator::byte(0))
.build()
.search_reader(
&matcher,
SHERLOCK,
printer.sink_with_path(&matcher, "sherlock"),
)
.unwrap();
let got = printer_contents(&mut printer);
assert_eq_printed!("sherlock:4\n", got);
}
}