pub fn command_exists(cmd: &str) -> bool {
if cmd.is_empty() {
return false;
}
if let Ok(path) = std::env::var("PATH") {
for dir in std::env::split_paths(&path) {
let full_path = dir.join(cmd);
if full_path.exists() {
return true;
}
#[cfg(target_os = "windows")]
{
for ext in &[".exe", ".cmd", ".bat", ".com"] {
let full_path_with_ext = dir.join(format!("{}{}", cmd, ext));
if full_path_with_ext.exists() {
return true;
}
}
}
}
}
false
}
pub const SUPPORTED_COMMANDS: &[&str] = &[
"ant",
"blkid",
"common",
"curl",
"cvs",
"df",
"diff",
"dig",
"diskutil",
"dnf",
"docker",
"du",
"kdig",
"dummy",
"env",
"esperanto",
"fdisk",
"findmnt",
"free",
"gcc",
"getfacl",
"getsebool",
"id",
"ifconfig",
"ip",
"iptables",
"irclog",
"iwconfig",
"jobs",
"kubectl",
"last",
"ldap",
"log",
"lolcat",
"lsattr",
"lsblk",
"lsmod",
"lsof",
"lspci",
"ls",
"lsusb",
"mount",
"mvn",
"netstat",
"nmap",
"ntpdate",
"php",
"ping",
"ping2",
"podman",
"proftpd",
"ps",
"pv",
"semanage",
"sensors",
"showmount",
"sockstat",
"sql",
"ss",
"stat",
"sysctl",
"systemctl",
"journalctl",
"tail",
"tcpdump",
"traceroute",
"tune2fs",
"ulimit",
"uptime",
"vmstat",
"wdiff",
"whois",
"yaml",
"go",
"iostat",
];
pub fn should_use_colorization_for_command_supported(command: &str) -> bool {
SUPPORTED_COMMANDS.contains(&command)
}
pub const PSEUDO_NO_COLOR: &[&str] = &["ls"];
pub fn pseudo_command_excluded(pseudo_command: &str) -> bool {
if pseudo_command.is_empty() {
return false;
}
let parts: Vec<&str> = pseudo_command.split_whitespace().collect();
if parts.is_empty() {
return false;
}
if !PSEUDO_NO_COLOR.contains(&parts[0]) {
return false;
}
if parts.len() == 1 {
return true;
}
!parts[1].starts_with('-')
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_command_exists() {
let candidates_unix = ["sh", "bash", "ls", "true", "false", "echo"];
let candidates_windows = ["cmd.exe", "powershell.exe", "where.exe"];
let found_on_unix = candidates_unix.iter().any(|c| command_exists(c));
let found_on_windows = candidates_windows.iter().any(|c| command_exists(c));
assert!(
found_on_unix || found_on_windows,
"expected at least one standard command to be present on PATH (checked: sh,bash,ls,true,false,echo,cmd.exe,powershell.exe,where.exe)"
);
assert!(
!command_exists("nonexistent_command_xyz123"),
"nonexistent command should not exist"
);
if cfg!(unix) {
assert!(
command_exists("/bin/echo") || command_exists("/usr/bin/echo"),
"echo should exist in standard locations on Unix hosts"
);
}
assert!(
!command_exists(""),
"empty string should not be a valid command"
);
assert!(
!command_exists("command with spaces"),
"commands with spaces should not exist"
);
}
#[test]
fn test_should_use_colorization_for_command_supported() {
assert!(should_use_colorization_for_command_supported("ping"));
assert!(should_use_colorization_for_command_supported("ls"));
assert!(should_use_colorization_for_command_supported("df"));
assert!(should_use_colorization_for_command_supported("journalctl"));
assert!(!should_use_colorization_for_command_supported(
"unknown_command"
));
assert!(!should_use_colorization_for_command_supported(""));
}
#[test]
fn test_pseudo_command_excluded() {
assert!(pseudo_command_excluded("ls"));
assert!(pseudo_command_excluded("ls ~"));
assert!(pseudo_command_excluded("ls ~/"));
assert!(pseudo_command_excluded("ls /home"));
assert!(pseudo_command_excluded("ls ."));
assert!(pseudo_command_excluded("ls ./"));
assert!(pseudo_command_excluded("ls .."));
assert!(pseudo_command_excluded("ls /"));
assert!(pseudo_command_excluded("ls somefile"));
assert!(pseudo_command_excluded("ls file.txt"));
assert!(!pseudo_command_excluded("ls -l"));
assert!(!pseudo_command_excluded("ls -l /home"));
assert!(!pseudo_command_excluded("ls -la"));
assert!(!pseudo_command_excluded("ls --long"));
assert!(!pseudo_command_excluded("ls --long /home"));
assert!(!pseudo_command_excluded("df"));
assert!(!pseudo_command_excluded("df /home"));
assert!(!pseudo_command_excluded(""));
}
}