Skip to content

Commit

Permalink
printer: always use \n in the summary printer
Browse files Browse the repository at this point in the history
Previously, the \x00 byte would be printed when --null-data was being used.
  • Loading branch information
ltrzesniewski committed May 12, 2024
1 parent bb6f0f5 commit 4b692bb
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions crates/printer/src/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 => {
Expand All @@ -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 => {}
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 4b692bb

Please sign in to comment.