Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,10 +675,7 @@ fn is_likely_command(text: &str) -> bool {
return false;
}

let first_word = text.split_whitespace().next().unwrap_or("");
crate::providers::COMMAND_STARTERS
.iter()
.any(|cmd| first_word.starts_with(cmd))
crate::providers::is_command_starter(text)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve common versioned command detection

This now routes CLI command detection through the provider matcher, which only accepts exact or hyphenated first tokens. Because maybe_execute_command only injects or executes when is_likely_command returns true, valid replies such as python3.11 -m venv .venv, javac Main.java, or gofmt -w . now fall through as plain text even in command-oriented flows; these were previously accepted via the python, java, and go prefixes. Consider adding the common full executable names or allowing version/companion-command boundaries without reintroducing ordinary-word false positives.

Useful? React with 👍 / 👎.

}

fn list_profiles(config: &Config) -> Result<()> {
Expand Down Expand Up @@ -767,3 +764,17 @@ fn list_profiles(config: &Config) -> Result<()> {

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_is_likely_command_false_positives() {
assert!(!is_likely_command("catapult is a word"));
assert!(!is_likely_command("finding a solution"));
assert!(!is_likely_command("timeoutish behavior"));
assert!(is_likely_command("cat file.txt"));
assert!(is_likely_command("find . -name \"*.rs\""));
}
}
24 changes: 12 additions & 12 deletions src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub const COMMAND_STARTERS: &[&str] = &[
];

/// Checks if a line starts with a known command.
fn line_starts_with_command(line: &str) -> bool {
pub fn is_command_starter(line: &str) -> bool {
let first_word = line.split_whitespace().next().unwrap_or("");
COMMAND_STARTERS.iter().any(|&cmd| {
if cmd == "./" || cmd == "/" || cmd == "~" {
Expand Down Expand Up @@ -203,7 +203,7 @@ pub fn flatten_command_if_safe(text: &str) -> Option<String> {
return None;
}
// Must look like a command
if !line_starts_with_command(line) {
if !is_command_starter(line) {
return None;
}
}
Expand Down Expand Up @@ -294,19 +294,19 @@ mod tests {
}

#[test]
fn test_line_starts_with_command_exact_matching() {
fn test_is_command_starter_exact_matching() {
// False positives should not be detected
assert!(!line_starts_with_command("catapult is a word"));
assert!(!line_starts_with_command(".gitignore is a file"));
assert!(!line_starts_with_command("timeoutish"));
assert!(!is_command_starter("catapult is a word"));
assert!(!is_command_starter(".gitignore is a file"));
assert!(!is_command_starter("timeoutish"));

// Valid commands should be detected
assert!(line_starts_with_command("cat file.txt"));
assert!(line_starts_with_command(". ./script.sh"));
assert!(line_starts_with_command("./my_script"));
assert!(line_starts_with_command("/usr/bin/ls"));
assert!(line_starts_with_command("~/bin/run"));
assert!(line_starts_with_command("apt-get install htop"));
assert!(is_command_starter("cat file.txt"));
assert!(is_command_starter(". ./script.sh"));
assert!(is_command_starter("./my_script"));
assert!(is_command_starter("/usr/bin/ls"));
assert!(is_command_starter("~/bin/run"));
assert!(is_command_starter("apt-get install htop"));
}

#[test]
Expand Down
Loading