Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 7 additions & 2 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
44 changes: 44 additions & 0 deletions tests/test_grep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Loading