@@ -2160,6 +2160,114 @@ if [[ -o interactive ]] || [[ -n $ZSH_VERSION ]]; then
21602160 "$__ORIGINAL_GREP_PATH" "$@"
21612161 }
21622162
2163+ # Direct argument version of g2rg for stdin input
2164+ g2rg-direct-stdin() {
2165+ local output="rg"
2166+ local pattern=""
2167+ local glob_excludes=()
2168+ local pattern_found=false
2169+
2170+ local i=1
2171+ while [[ $i -le $# ]]; do
2172+ local word="${!i}"
2173+
2174+ # Handle combined flags like -ri, -rn, etc.
2175+ if [[ $word =~ ^-[a-zA-Z]{2,}$ ]] && [[ $word != --* ]]; then
2176+ # Split combined flags (e.g., -ri becomes -r -i)
2177+ local flags=${word#-}
2178+ for (( j=1; j<=${#flags}; j++ )); do
2179+ local flag=${flags:$((j-1)):1}
2180+ case $flag in
2181+ r|R)
2182+ # ripgrep reads stdin when no files specified
2183+ ;;
2184+ i) output="$output -i" ;;
2185+ v) output="$output -v" ;;
2186+ n) output="$output -n" ;;
2187+ H) output="$output -H" ;;
2188+ h) output="$output -h" ;;
2189+ l) output="$output -l" ;;
2190+ L) output="$output -L" ;;
2191+ c) output="$output -c" ;;
2192+ w) output="$output -w" ;;
2193+ x) output="$output -x" ;;
2194+ *) output="$output -$flag" ;;
2195+ esac
2196+ done
2197+ else
2198+ case $word in
2199+ -r|-R|--recursive)
2200+ # not relevant for stdin
2201+ ;;
2202+ -i|--ignore-case) output="$output -i" ;;
2203+ -v|--invert-match) output="$output -v" ;;
2204+ -n|--line-number) output="$output -n" ;;
2205+ -H|--with-filename) output="$output -H" ;;
2206+ -h|--no-filename) output="$output -h" ;;
2207+ -l|--files-with-matches) output="$output -l" ;;
2208+ -L|--files-without-match) output="$output --files-without-match" ;;
2209+ -c|--count) output="$output -c" ;;
2210+ -w|--word-regexp) output="$output -w" ;;
2211+ -x|--line-regexp) output="$output -x" ;;
2212+ -A*)
2213+ if [[ $word == "-A" ]]; then
2214+ ((i++))
2215+ local num="${!i}"
2216+ output="$output -A $num"
2217+ else
2218+ local num=${word#-A}
2219+ output="$output -A $num"
2220+ fi
2221+ ;;
2222+ -B*)
2223+ if [[ $word == "-B" ]]; then
2224+ ((i++))
2225+ local num="${!i}"
2226+ output="$output -B $num"
2227+ else
2228+ local num=${word#-B}
2229+ output="$output -B $num"
2230+ fi
2231+ ;;
2232+ -C*)
2233+ if [[ $word == "-C" ]]; then
2234+ ((i++))
2235+ local num="${!i}"
2236+ output="$output -C $num"
2237+ else
2238+ local num=${word#-C}
2239+ output="$output -C $num"
2240+ fi
2241+ ;;
2242+ --exclude-dir|--exclude)
2243+ ((i++))
2244+ # Skip exclude patterns for stdin - not relevant
2245+ ;;
2246+ -*)
2247+ output="$output $word"
2248+ ;;
2249+ *)
2250+ if [[ $pattern_found == false ]]; then
2251+ pattern=$word
2252+ pattern_found=true
2253+ fi
2254+ # Skip additional arguments (would be file paths) for stdin
2255+ ;;
2256+ esac
2257+ fi
2258+ ((i++))
2259+ done
2260+
2261+ # Build the final ripgrep command for stdin
2262+ if [[ -n $pattern ]]; then
2263+ output="$output $(printf %q "$pattern")"
2264+ fi
2265+
2266+ # Don't add any file paths - ripgrep will read from stdin
2267+
2268+ echo "$output"
2269+ }
2270+
21632271 # Direct argument version of g2rg for the grep function
21642272 g2rg-direct() {
21652273 local output="rg"
@@ -2170,7 +2278,7 @@ if [[ -o interactive ]] || [[ -n $ZSH_VERSION ]]; then
21702278
21712279 local i=1
21722280 while [[ $i -le $# ]]; do
2173- local word=${@:$i:1}
2281+ local word="${!i}"
21742282
21752283 # Handle combined flags like -ri, -rn, etc.
21762284 if [[ $word =~ ^-[a-zA-Z]{2,}$ ]] && [[ $word != --* ]]; then
@@ -2179,7 +2287,9 @@ if [[ -o interactive ]] || [[ -n $ZSH_VERSION ]]; then
21792287 for (( j=1; j<=${#flags}; j++ )); do
21802288 local flag=${flags:$((j-1)):1}
21812289 case $flag in
2182- r|R) ;; # ripgrep is recursive by default
2290+ r|R)
2291+ # ripgrep is recursive by default
2292+ ;;
21832293 i) output="$output -i" ;;
21842294 v) output="$output -v" ;;
21852295 n) output="$output -n" ;;
@@ -2195,7 +2305,9 @@ if [[ -o interactive ]] || [[ -n $ZSH_VERSION ]]; then
21952305 done
21962306 else
21972307 case $word in
2198- -r|-R|--recursive) ;; # ripgrep is recursive by default
2308+ -r|-R|--recursive)
2309+ # ripgrep is recursive by default
2310+ ;;
21992311 -i|--ignore-case) output="$output -i" ;;
22002312 -v|--invert-match) output="$output -v" ;;
22012313 -n|--line-number) output="$output -n" ;;
@@ -2209,7 +2321,7 @@ if [[ -o interactive ]] || [[ -n $ZSH_VERSION ]]; then
22092321 -A*)
22102322 if [[ $word == "-A" ]]; then
22112323 ((i++))
2212- local num=${@:$i:1}
2324+ local num="${!i}"
22132325 output="$output -A $num"
22142326 else
22152327 local num=${word#-A}
@@ -2219,7 +2331,7 @@ if [[ -o interactive ]] || [[ -n $ZSH_VERSION ]]; then
22192331 -B*)
22202332 if [[ $word == "-B" ]]; then
22212333 ((i++))
2222- local num=${@:$i:1}
2334+ local num="${!i}"
22232335 output="$output -B $num"
22242336 else
22252337 local num=${word#-B}
@@ -2229,7 +2341,7 @@ if [[ -o interactive ]] || [[ -n $ZSH_VERSION ]]; then
22292341 -C*)
22302342 if [[ $word == "-C" ]]; then
22312343 ((i++))
2232- local num=${@:$i:1}
2344+ local num="${!i}"
22332345 output="$output -C $num"
22342346 else
22352347 local num=${word#-C}
@@ -2238,7 +2350,7 @@ if [[ -o interactive ]] || [[ -n $ZSH_VERSION ]]; then
22382350 ;;
22392351 --exclude-dir)
22402352 ((i++))
2241- local dir_pattern=${@:$i:1}
2353+ local dir_pattern="${!i}"
22422354 if [[ $dir_pattern == */* ]]; then
22432355 glob_excludes+=("!$dir_pattern")
22442356 else
@@ -2247,7 +2359,7 @@ if [[ -o interactive ]] || [[ -n $ZSH_VERSION ]]; then
22472359 ;;
22482360 --exclude)
22492361 ((i++))
2250- local file_pattern=${@:$i:1}
2362+ local file_pattern="${!i}"
22512363 glob_excludes+=("!$file_pattern")
22522364 ;;
22532365 -*)
@@ -2296,13 +2408,24 @@ if [[ -o interactive ]] || [[ -n $ZSH_VERSION ]]; then
22962408 return $?
22972409 fi
22982410
2411+ # Check if stdin is being piped
2412+ local use_stdin=false
2413+ if [[ ! -t 0 ]]; then
2414+ use_stdin=true
2415+ fi
2416+
22992417 # Pass arguments directly instead of building a string
23002418 local rg_cmd
2301- rg_cmd=$(g2rg-direct "$@")
2419+ if [[ $use_stdin == true ]]; then
2420+ rg_cmd=$(g2rg-direct-stdin "$@")
2421+ else
2422+ rg_cmd=$(g2rg-direct "$@")
2423+ fi
23022424
23032425 # Show conversion if verbose mode is enabled
23042426 if [[ -n $GREP_TO_RG_VERBOSE ]]; then
23052427 echo "# Converting grep with ${#@} arguments" >&2
2428+ echo "# Using stdin: $use_stdin" >&2
23062429 echo "# Running: $rg_cmd" >&2
23072430 fi
23082431
0 commit comments