Repository navigation
grep: line-buffer terminal output - #147
Conversation
Merging this PR will not alter performance
|
| 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. |
There was a problem hiding this comment.
I'm not sure if the comment adds much nuance beyond what the code expresses. Line-buffering is the default for all terminal IO.
| 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(); |
There was a problem hiding this comment.
is_terminal is a comparatively very expensive call. Consider calling it once and using the variable both here and for --color flag handling.
| // 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> { |
There was a problem hiding this comment.
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).
|
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. |
|
Thanks for your PR |
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-bufferedcontinues 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:
cargo test --locked -j 2 -- --test-threads=2).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.