use crate::core::ProcessStatus;
use colored::*;
pub fn format_duration(secs: u64) -> String {
if secs < 60 {
format!("{}s", secs)
} else if secs < 3600 {
format!("{}m {}s", secs / 60, secs % 60)
} else if secs < 86400 {
format!("{}h {}m", secs / 3600, (secs % 3600) / 60)
} else {
format!("{}d {}h", secs / 86400, (secs % 86400) / 3600)
}
}
pub fn plural(n: usize) -> &'static str {
if n == 1 {
""
} else {
"es"
}
}
pub fn truncate_string(s: &str, max_len: usize) -> String {
if s.len() <= max_len {
s.to_string()
} else {
format!("{}...", &s[..max_len.saturating_sub(3)])
}
}
pub fn truncate_path(path: &str, max_len: usize) -> String {
if path.len() <= max_len {
path.to_string()
} else {
let start = path.len().saturating_sub(max_len.saturating_sub(3));
format!("...{}", &path[start..])
}
}
pub fn format_memory(memory_mb: f64) -> String {
if memory_mb < 1.0 {
format!("{:.0}KB", memory_mb * 1000.0)
} else if memory_mb < 1000.0 {
format!("{:.1}MB", memory_mb)
} else {
format!("{:.1}GB", memory_mb / 1000.0)
}
}
pub fn colorize_status(status: &ProcessStatus, status_str: &str) -> ColoredString {
match status {
ProcessStatus::Running => status_str.green(),
ProcessStatus::Sleeping => status_str.blue(),
ProcessStatus::Stopped => status_str.yellow(),
ProcessStatus::Zombie => status_str.red(),
_ => status_str.white(),
}
}