pub fn set_process_title(title: &str) {
#[cfg(target_os = "linux")]
{
use std::io::Write;
let truncated = if title.len() > 15 {
&title[..15]
} else {
title
};
if let Ok(mut f) = std::fs::File::create("/proc/self/comm") {
let _ = f.write_all(truncated.as_bytes());
}
unsafe {
let cmdline = match std::fs::read("/proc/self/cmdline") {
Ok(c) => c,
Err(_) => return,
};
let total_size = cmdline.len();
if total_size == 0 {
return;
}
unsafe extern "C" {
static __progname_full: *mut std::os::raw::c_char;
}
let p = &__progname_full;
if p.is_null() || (*p).is_null() {
return;
}
let mut start = *p;
loop {
if start.is_null() {
break;
}
let prev = start.sub(1);
if *prev == 0 {
break;
}
start = prev;
}
let dst = start as *mut u8;
let new_bytes = title.as_bytes();
let copy_len = new_bytes.len().min(total_size);
std::ptr::copy_nonoverlapping(new_bytes.as_ptr(), dst, copy_len);
if total_size > copy_len {
std::ptr::write_bytes(dst.add(copy_len), 0u8, total_size - copy_len);
}
}
}
#[cfg(not(target_os = "linux"))]
{
let _ = title;
}
}
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(""));
}
#[test]
fn test_set_process_title() {
#[cfg(target_os = "linux")]
{
set_process_title("test_cmd");
let comm = std::fs::read_to_string("/proc/self/comm")
.expect("should be able to read /proc/self/comm");
assert!(
comm.trim() == "test_cmd",
"expected 'test_cmd', got '{}'",
comm.trim()
);
let long_name = "this_is_a_very_long_command_name";
set_process_title(long_name);
let comm = std::fs::read_to_string("/proc/self/comm")
.expect("should be able to read /proc/self/comm");
assert_eq!(comm.trim().len(), 15);
assert!(comm.trim().starts_with("this_is_a_very_"));
set_process_title("test_set_proces");
}
#[cfg(not(target_os = "linux"))]
{
set_process_title("test_cmd");
}
}
}