diff --git a/src/discover/registry.rs b/src/discover/registry.rs index cc975b3b..f8d37c31 100644 --- a/src/discover/registry.rs +++ b/src/discover/registry.rs @@ -615,6 +615,16 @@ fn rewrite_segment(seg: &str, excluded: &[String]) -> Option { return rewrite_tail_lines(cmd_part).map(|r| format!("{}{}", r, redirect_suffix)); } + // Most cat flags (-v, -A, -e, -t, -s, -b, --show-all, etc.) have different + // semantics than rtk read or no equivalent at all. Only `-n` (line numbers) + // maps correctly to `rtk read -n`. Skip rewrite for any other flag. + if cmd_part.starts_with("cat ") { + let args = cmd_part["cat ".len()..].trim_start(); + if args.starts_with('-') && !args.starts_with("-n ") && !args.starts_with("-n\t") { + return None; + } + } + // Use classify_command for correct ignore/prefix handling let rtk_equivalent = match classify_command(cmd_part) { Classification::Supported { rtk_equivalent, .. } => { @@ -1165,6 +1175,26 @@ mod tests { ); } + #[test] + fn test_rewrite_cat_with_incompatible_flags_skipped() { + // cat flags with different semantics than rtk read — skip rewrite + assert_eq!(rewrite_command("cat -A file.cpp", &[]), None); + assert_eq!(rewrite_command("cat -v file.txt", &[]), None); + assert_eq!(rewrite_command("cat -e file.txt", &[]), None); + assert_eq!(rewrite_command("cat -t file.txt", &[]), None); + assert_eq!(rewrite_command("cat -s file.txt", &[]), None); + assert_eq!(rewrite_command("cat --show-all file.txt", &[]), None); + } + + #[test] + fn test_rewrite_cat_with_compatible_flags() { + // cat -n (line numbers) maps to rtk read -n — allow rewrite + assert_eq!( + rewrite_command("cat -n file.txt", &[]), + Some("rtk read -n file.txt".into()) + ); + } + #[test] fn test_rewrite_rg_pattern() { assert_eq!(