Skip to content

fix(grep): support piped stdin input - #1542

Closed
CtriXin wants to merge 1 commit into
rtk-ai:masterfrom
CtriXin:fix/issue-1476-grep-pipe
Closed

CtriXin wants to merge 1 commit into
rtk-ai:masterfrom
CtriXin:fix/issue-1476-grep-pipe

Conversation

@CtriXin

@CtriXin CtriXin commented Apr 26, 2026

Copy link
Copy Markdown

Summary

rtk grep <pattern> now reads from stdin when input is piped (e.g. ps aux | rtk grep bash), matching plain grep behavior.

Root cause: rtk grep always invoked rg/grep on the filesystem path, ignoring stdin even when the shell had piped data available.

Fix: detect non-tty stdin via std::io::IsTerminal, read and filter lines directly, and output matching results in the same grouped format.

Also handles:

  • grep -c via stdin (returns bare integer)
  • -i case-insensitive flag via stdin

Fixes: #1476

Test Plan

  • cargo test grep_cmd — 9 tests pass
  • Manual: ps aux | rtk grep bash returns matching lines
  • Manual: echo -e "foo\nbar\nfoo" | rtk grep foo returns 2 matches

`rtk grep pattern` now reads from stdin when output is piped
(e.g. `ps aux | rtk grep bash`), matching grep behavior.

Also supports `grep -c` via stdin and basic extra_args like `-i`.

Fixes: rtk-ai#1476
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@pszymkowiak pszymkowiak added bug Something isn't working effort-small Quelques heures, 1 fichier enhancement New feature or request labels Apr 26, 2026
@pszymkowiak

Copy link
Copy Markdown
Collaborator

[w] wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Adds stdin pipe support to rtk grep so it can filter piped input (e.g. ps aux | rtk grep bash), matching standard grep behavior. The fix detects non-tty stdin via IsTerminal, reads lines directly, and outputs matches in the existing grouped format, with support for -c (count) and -i (case-insensitive) flags.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #1476


Analyzed automatically by wshm · This is an automated analysis, not a human review.

@gene1wood

Copy link
Copy Markdown

This PR seeks to achieve what #881 was trying to achieve before it was closed. This should address #838 and my duplicate #1476

@KuSh

KuSh commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

Thanks for this. Piped stdin for rtk grep landed on develop in #3128 (merged 23 July 2026), which moved grep into src/cmds/system/search.rs and streams piped input instead of searching the filesystem; the -c and -i cases you added are covered there too, and the grep_cmd.rs module this PR edits no longer exists. Verified against a current develop build: piping into rtk grep returns the matching lines, rtk grep -c returns the count, and rtk grep -i matches case-insensitively. Closing as covered, but if some part of your case is still broken please comment here or reopen.

@KuSh KuSh closed this Sep 6, 2026
@gene1wood

Copy link
Copy Markdown

It looks like the underlying stdin-reading fix landed in #2641 (commit 37ee6cf, "fix(grep): match real grep output and read piped stdin"), which reads piped stdin instead of injecting . into the search. Just leaving this hear as a breadcrumb for the future.

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

Labels

bug Something isn't working effort-small Quelques heures, 1 fichier enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rtk grep doesn't work with piped input

6 participants