vtcode_core/tools/pty/
command_utils.rs1use std::path::Path;
2
3pub fn is_cargo_command(program: &str) -> bool {
5 let name = Path::new(program)
6 .file_name()
7 .and_then(|value| value.to_str())
8 .unwrap_or(program)
9 .to_ascii_lowercase();
10 name == "cargo"
11}
12
13pub fn is_cargo_command_string(command: &str) -> bool {
15 let trimmed = command.trim();
16 trimmed.starts_with("cargo ") || trimmed == "cargo"
17}
18
19const LOCKFILE_COMMANDS: &[&str] = &[
23 "cargo", "npm", "pnpm", "yarn", "bun", "bunx", "go", "gradle", "mvn", "pip", "pip3", "poetry", "composer", "bundler", "bundle", ];
39
40fn normalize_program_name(program: &str) -> String {
41 Path::new(program)
42 .file_name()
43 .and_then(|value| value.to_str())
44 .unwrap_or(program)
45 .to_ascii_lowercase()
46}
47
48pub fn is_lockfile_command(program: &str) -> bool {
52 let name = normalize_program_name(program);
53 LOCKFILE_COMMANDS.contains(&name.as_str())
54}
55
56pub fn is_long_running_command(program: &str) -> bool {
58 let name = normalize_program_name(program);
59 is_development_toolchain_command(&name) || is_lockfile_command(&name)
60}
61
62pub fn is_lockfile_command_string(command: &str) -> bool {
64 let trimmed = command.trim();
65 LOCKFILE_COMMANDS
66 .iter()
67 .any(|&cmd| trimmed.starts_with(&format!("{cmd} ")) || trimmed == cmd)
68}
69
70pub fn is_long_running_command_string(command: &str) -> bool {
72 let trimmed = command.trim();
73 if trimmed.is_empty() {
74 return false;
75 }
76 if is_lockfile_command_string(trimmed) {
77 return true;
78 }
79 let first = trimmed.split_whitespace().next().unwrap_or_default();
80 is_development_toolchain_command(first)
81}
82
83pub(super) fn is_shell_program(program: &str) -> bool {
84 let name = Path::new(program)
85 .file_name()
86 .and_then(|value| value.to_str())
87 .unwrap_or(program)
88 .to_ascii_lowercase();
89 matches!(
90 name.as_str(),
91 "bash"
92 | "sh"
93 | "zsh"
94 | "fish"
95 | "dash"
96 | "ash"
97 | "busybox"
98 | "ksh"
99 | "ksh93"
100 | "mksh"
101 | "tcsh"
102 | "csh"
103 | "posh"
104 | "yash"
105 | "oksh"
106 | "loksh"
107 | "oil"
108 | "osh"
109 | "nu"
110 | "nushell"
111 | "elvish"
112 | "pwsh"
113 )
114}
115
116pub(super) fn is_sandbox_wrapper_program(program: &str, args: &[String]) -> bool {
117 let name = Path::new(program)
118 .file_name()
119 .and_then(|value| value.to_str())
120 .unwrap_or(program)
121 .to_ascii_lowercase();
122 if name == "sandbox-exec" {
123 return true;
124 }
125
126 args.iter().any(|arg| {
127 matches!(
128 arg.as_str(),
129 "--sandbox-policy" | "--sandbox-policy-cwd" | "--seccomp-profile" | "--resource-limits"
130 )
131 })
132}
133
134pub fn is_development_toolchain_command(program: &str) -> bool {
139 let name = Path::new(program)
140 .file_name()
141 .and_then(|value| value.to_str())
142 .unwrap_or(program)
143 .to_ascii_lowercase();
144 matches!(
145 name.as_str(),
146 "cargo"
147 | "rustc"
148 | "rustup"
149 | "rustfmt"
150 | "clippy"
151 | "npm"
152 | "node"
153 | "yarn"
154 | "pnpm"
155 | "bun"
156 | "bunx"
157 | "go"
158 | "python"
159 | "python3"
160 | "pip"
161 | "pip3"
162 | "java"
163 | "javac"
164 | "mvn"
165 | "gradle"
166 | "make"
167 | "cmake"
168 | "gcc"
169 | "g++"
170 | "clang"
171 | "clang++"
172 | "which"
173 )
174}
175
176#[cfg(test)]
177mod tests {
178 use super::*;
179
180 #[test]
181 fn test_is_cargo_command() {
182 assert!(is_cargo_command("cargo"));
183 assert!(is_cargo_command("Cargo"));
184 assert!(is_cargo_command("/usr/bin/cargo"));
185 assert!(is_cargo_command("~/.cargo/bin/cargo"));
186 assert!(!is_cargo_command("rustc"));
187 assert!(!is_cargo_command("npm"));
188 assert!(!is_cargo_command("cargo-watch"));
189 }
190
191 #[test]
192 fn test_is_cargo_command_string() {
193 assert!(is_cargo_command_string("cargo check"));
194 assert!(is_cargo_command_string("cargo test --lib"));
195 assert!(is_cargo_command_string("cargo build --release"));
196 assert!(is_cargo_command_string(" cargo clippy "));
197 assert!(is_cargo_command_string("cargo"));
198 assert!(!is_cargo_command_string("rustc --version"));
199 assert!(!is_cargo_command_string("npm install"));
200 assert!(!is_cargo_command_string("echo cargo"));
201 }
202
203 #[test]
204 fn test_is_long_running_command() {
205 assert!(is_long_running_command("cargo"));
206 assert!(is_long_running_command("cmake"));
207 assert!(is_long_running_command("composer"));
208 assert!(is_long_running_command("bundle"));
209 assert!(is_long_running_command("poetry"));
210 assert!(!is_long_running_command("rg"));
211 }
212
213 #[test]
214 fn test_is_long_running_command_string() {
215 assert!(is_long_running_command_string("cargo test"));
216 assert!(is_long_running_command_string(" cmake --build ."));
217 assert!(is_long_running_command_string("composer install"));
218 assert!(is_long_running_command_string("bundle exec rake"));
219 assert!(is_long_running_command_string("poetry install"));
220 assert!(!is_long_running_command_string("rg \"fn main\" ."));
221 }
222
223 #[test]
224 fn test_is_sandbox_wrapper_program() {
225 assert!(is_sandbox_wrapper_program("sandbox-exec", &[]));
226 assert!(is_sandbox_wrapper_program("vtcode-linux-sandbox", &["--sandbox-policy".to_string()]));
227 assert!(!is_sandbox_wrapper_program("bash", &["-lc".to_string(), "ls".to_string()]));
228 }
229}