From 1812619ede1df816d6d9acb832b4aa81eaa80e91 Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Mon, 9 Nov 2026 22:21:22 +0530 Subject: [PATCH] grep: print the group separator with -o and context options GNU grep does not print context lines under -o, but it still tracks the context groups and prints the group separator between groups that are not adjacent. Keep the context settings with -o and skip only the context lines themselves when writing. Fixes #175 --- src/lib.rs | 18 ++++++++---------- src/output.rs | 9 +++++++-- tests/test_grep.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7fd4174..ac23060 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -326,18 +326,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // Color output requires an explicit --color option. _ => ColorMode::Never, }; + // With "-o" the context lines themselves are not printed (see + // `Writer::write_line`), but GNU grep still tracks the context groups and + // prints the group separator between groups that are not adjacent. let (before_context, after_context, has_context) = { - // "-o" overrides any context arguments - if only_matching { - (0, 0, false) - } else { - let fallback = context.unwrap_or(0); - let before = before_context.unwrap_or(fallback); - let after = after_context.unwrap_or(fallback); - let has = context.is_some() || before_context.is_some() || after_context.is_some(); + let fallback = context.unwrap_or(0); + let before = before_context.unwrap_or(fallback); + let after = after_context.unwrap_or(fallback); + let has = context.is_some() || before_context.is_some() || after_context.is_some(); - (before, after, has) - } + (before, after, has) }; // --include, --exclude and --exclude-from in command-line order. let mut globs: Vec<(usize, bool, String)> = Vec::new(); diff --git a/src/output.rs b/src/output.rs index dc90127..a8c8312 100644 --- a/src/output.rs +++ b/src/output.rs @@ -52,8 +52,13 @@ impl<'a> OutputWriter<'a> { /// Write a matching or context line. pub fn write_line(&mut self, view: &LineView<'_>, filename: &Path) -> io::Result<()> { - if self.config.only_matching && view.is_match { - self.write_only_matching(view, filename) + if self.config.only_matching { + if view.is_match { + self.write_only_matching(view, filename) + } else { + // `-o` never prints context lines. + Ok(()) + } } else { self.write_line_with_matches(view, filename) } diff --git a/tests/test_grep.rs b/tests/test_grep.rs index 75961f7..8ab56f6 100644 --- a/tests/test_grep.rs +++ b/tests/test_grep.rs @@ -2153,3 +2153,47 @@ fn only_matching_with_context() { .succeeds() .stdout_only("4:xx\n"); } + +#[test] +fn only_matching_with_context_keeps_group_separator() { + // `-o` does not print context lines, but like GNU grep it still prints the + // separator between context groups that are not adjacent. + let input = "foo 1\nline 2\nline 3\nfoo 4\nline 5\n"; + + let (_s, mut c) = ucmd(); + c.args(&["-o", "-A1", "foo"]) + .pipe_in(input) + .succeeds() + .stdout_only("foo\n--\nfoo\n"); + + let (_s, mut c) = ucmd(); + c.args(&["-o", "-B1", "foo"]) + .pipe_in(input) + .succeeds() + .stdout_only("foo\n--\nfoo\n"); + + let (_s, mut c) = ucmd(); + c.args(&["-o", "-A0", "foo"]) + .pipe_in(input) + .succeeds() + .stdout_only("foo\n--\nfoo\n"); + + let (_s, mut c) = ucmd(); + c.args(&["-o", "--group-separator=XX", "-A1", "foo"]) + .pipe_in(input) + .succeeds() + .stdout_only("foo\nXX\nfoo\n"); + + let (_s, mut c) = ucmd(); + c.args(&["-o", "--no-group-separator", "-A1", "foo"]) + .pipe_in(input) + .succeeds() + .stdout_only("foo\nfoo\n"); + + // Adjacent groups (context 1-2 and 3-5 touch) get no separator. + let (_s, mut c) = ucmd(); + c.args(&["-o", "-n", "-C1", "foo"]) + .pipe_in(input) + .succeeds() + .stdout_only("1:foo\n4:foo\n"); +}