Skip to content

Commit b9d6646

Browse files
fix(rewrite): skip cat rewrite when incompatible flags are present
cat flags like -v, -A, -e, -t, -s have different semantics than rtk read flags (-v means verbose, -n means line numbers). Blindly forwarding flags caused incorrect behavior (e.g. cat -A file → rtk read -A file which fails). Only skip rewrite for incompatible flags. cat -n (line numbers) maps correctly to rtk read -n, so it is still rewritten for token savings. Plain `cat file` continues to rewrite to `rtk read file` as before.
1 parent 5e8626d commit b9d6646

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

src/discover/registry.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,16 @@ fn rewrite_segment(seg: &str, excluded: &[String]) -> Option<String> {
613613
return rewrite_tail_lines(cmd_part).map(|r| format!("{}{}", r, redirect_suffix));
614614
}
615615

616+
// Most cat flags (-v, -A, -e, -t, -s, -b, --show-all, etc.) have different
617+
// semantics than rtk read or no equivalent at all. Only `-n` (line numbers)
618+
// maps correctly to `rtk read -n`. Skip rewrite for any other flag.
619+
if cmd_part.starts_with("cat ") {
620+
let args = cmd_part["cat ".len()..].trim_start();
621+
if args.starts_with('-') && !args.starts_with("-n ") && !args.starts_with("-n\t") {
622+
return None;
623+
}
624+
}
625+
616626
// Use classify_command for correct ignore/prefix handling
617627
let rtk_equivalent = match classify_command(cmd_part) {
618628
Classification::Supported { rtk_equivalent, .. } => {
@@ -1163,6 +1173,26 @@ mod tests {
11631173
);
11641174
}
11651175

1176+
#[test]
1177+
fn test_rewrite_cat_with_incompatible_flags_skipped() {
1178+
// cat flags with different semantics than rtk read — skip rewrite
1179+
assert_eq!(rewrite_command("cat -A file.cpp", &[]), None);
1180+
assert_eq!(rewrite_command("cat -v file.txt", &[]), None);
1181+
assert_eq!(rewrite_command("cat -e file.txt", &[]), None);
1182+
assert_eq!(rewrite_command("cat -t file.txt", &[]), None);
1183+
assert_eq!(rewrite_command("cat -s file.txt", &[]), None);
1184+
assert_eq!(rewrite_command("cat --show-all file.txt", &[]), None);
1185+
}
1186+
1187+
#[test]
1188+
fn test_rewrite_cat_with_compatible_flags() {
1189+
// cat -n (line numbers) maps to rtk read -n — allow rewrite
1190+
assert_eq!(
1191+
rewrite_command("cat -n file.txt", &[]),
1192+
Some("rtk read -n file.txt".into())
1193+
);
1194+
}
1195+
11661196
#[test]
11671197
fn test_rewrite_rg_pattern() {
11681198
assert_eq!(

0 commit comments

Comments
 (0)