Skip to content

grep: apply GREP_COLORS sl=, cx= and rv whole-line coloring - #257

Open
yunusarda-sudo wants to merge 2 commits into
uutils:mainfrom
yunusarda-sudo:grep-colors-sl-cx-rv
Open

yunusarda-sudo wants to merge 2 commits into
uutils:mainfrom
yunusarda-sudo:grep-colors-sl-cx-rv

Conversation

@yunusarda-sudo

Copy link
Copy Markdown
Contributor

GNU grep's GREP_COLORS supports whole-line colors: sl= for selected lines, cx= for context lines, and rv, which swaps the two when -v (config.invert_match) is active.

While ColorConfig::from_env parsed and stored these values, OutputWriter::write_line_with_matches in src/output.rs previously did not emit them.

Solution

  1. In OutputWriter::write_line_with_matches:
    • Determine line_color based on view.is_match, invert_match, and reverse_video:
      let swap = self.config.invert_match && self.config.color_config.reverse_video;
      let line_color = if view.is_match ^ swap {
          self.config.color_config.selected_line
      } else {
          self.config.color_config.context_line
      };
    • Emit write_colored_prefix(line_color) at the start of the line content if has_line_color.
    • After each match position where write_colored_bytes resets styling, re-emit write_colored_prefix(line_color) so the remaining line segments stay colored.
    • Emit write_colored_suffix() before the line terminator if has_line_color.
    • Preserve uncolored match-only output in -o mode.
  2. In tests/test_grep.rs:
    • Added integration test grep_colors_selected_and_context_line covering sl=, cx=, rv with -v, sl= without rv, and -o.

Fixes #169

Support whole-line colors from GREP_COLORS: sl= for selected lines,
cx= for context lines, and rv to swap sl and cx when -v is specified.
Wrap line content in the resolved line color, restore line color after
match segments, and preserve uncolored output in -o mode.

Fixes uutils#169

Signed-off-by: Yunus Arda Güney <[email protected]>
@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.37%. Comparing base (bf6f25e) to head (8592c62).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #257      +/-   ##
==========================================
+ Coverage   97.22%   97.37%   +0.14%     
==========================================
  Files           6        6              
  Lines        2270     2284      +14     
  Branches      245      251       +6     
==========================================
+ Hits         2207     2224      +17     
+ Misses         63       60       -3     
Flag Coverage Δ
macOS_latest 98.27% <100.00%> (+0.14%) ⬆️
ubuntu_latest 98.27% <100.00%> (+0.14%) ⬆️
windows_latest 0.00% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed

codspeed Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 3 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 10 untouched benchmarks
⏩ 17 skipped benchmarks1


Comparing yunusarda-sudo:grep-colors-sl-cx-rv (8592c62) with main (bf6f25e)

Open in CodSpeed

Footnotes

  1. 17 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

grep - GREP_COLORS sl=, cx= and rv are parsed but never applied

1 participant