Skip to content

grep: line-buffer terminal output - #147

Merged
sylvestre merged 2 commits into
uutils:mainfrom
zengfanfan:codex/145-terminal-line-buffering
Oct 21, 2026
Merged

sylvestre merged 2 commits into
uutils:mainfrom
zengfanfan:codex/145-terminal-line-buffering

Conversation

@zengfanfan

@zengfanfan zengfanfan commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

When stdout is a terminal, matching lines currently remain buffered while grep waits for more input. Enable the existing line-flushing path for terminal stdout so interactive output becomes visible before EOF.

Redirected output keeps its default buffering, and explicit --line-buffered continues to flush redirected output. A single terminal check is shared with automatic color selection; color behavior and dependencies are unchanged.

Following review, the added buffering test module has been removed to keep this change small.

Validation on Ubuntu 24.04.4 / WSL2:

  • Existing suite: 13 unit tests and 102 integration tests passed (cargo test --locked -j 2 -- --test-threads=2).
  • Strict Clippy, formatting, and diff whitespace checks passed.
  • External terminal and pipe checks confirmed output before EOF where line buffering applies, expected color behavior, and no early output during a bounded observation of default pipe buffering.

Windows/macOS have not been rerun locally for this revision. Hosted CI results are tracked below. AI assistance was used for the implementation, validation, and review.

Fixes #145.

@codspeed

codspeed Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ 3 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 10 untouched benchmarks
⏩ 17 skipped benchmarks1


Comparing zengfanfan:codex/145-terminal-line-buffering (af1697f) with main (01fb6c3)

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. ↩

Comment thread src/lib.rs Outdated
let byte_offset = matches.get_flag("byte_offset");
let line_number = matches.get_flag("line_number");
let line_buffered = matches.get_flag("line_buffered");
// Interactive output must be visible while grep waits for more input.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if the comment adds much nuance beyond what the code expresses. Line-buffering is the default for all terminal IO.

Comment thread src/lib.rs Outdated
let line_number = matches.get_flag("line_number");
let line_buffered = matches.get_flag("line_buffered");
// Interactive output must be visible while grep waits for more input.
let line_buffered = matches.get_flag("line_buffered") || std::io::stdout().is_terminal();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_terminal is a comparatively very expensive call. Consider calling it once and using the variable both here and for --color flag handling.

Comment thread tests/buffering/mod.rs Outdated
Comment on lines +7 to +9
// Captured output is read by uutests in the background. Poll with a deadline
// while keeping stdin open, so EOF cannot accidentally satisfy the test.
fn wait_for_output(child: &mut UChild, stderr: bool) -> Vec<u8> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this unit test is very overkill. If we cannot express this in a meaningfully compact way, I'd favor no tests at all, as the backing logic for --line-buffered is and will always remain a primitive if condition (= easy to review and verify).

@zengfanfan

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Updated in af1697f: removed the redundant comment, reused a single stdout terminal check for line buffering and automatic color, and removed the added buffering test module as suggested.

The existing test suite (13 unit tests and 102 integration tests), formatting, and Clippy pass on Linux. I also checked terminal and pipe output behavior locally.

@sylvestre
sylvestre merged commit 9323666 into uutils:main Oct 21, 2026
22 checks passed
@sylvestre

Copy link
Copy Markdown
Contributor

Thanks for your PR

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.

Default terminal output delays matching lines while stdin remains open

3 participants