1use crate::core::ProcessStatus;
6use colored::*;
7
8pub fn format_duration(secs: u64) -> String {
12 if secs < 60 {
13 format!("{}s", secs)
14 } else if secs < 3600 {
15 format!("{}m {}s", secs / 60, secs % 60)
16 } else if secs < 86400 {
17 format!("{}h {}m", secs / 3600, (secs % 3600) / 60)
18 } else {
19 format!("{}d {}h", secs / 86400, (secs % 86400) / 3600)
20 }
21}
22
23pub fn truncate_string(s: &str, max_len: usize) -> String {
25 if s.len() <= max_len {
26 s.to_string()
27 } else {
28 format!("{}...", &s[..max_len.saturating_sub(3)])
29 }
30}
31
32pub fn truncate_path(path: &str, max_len: usize) -> String {
34 if path.len() <= max_len {
35 path.to_string()
36 } else {
37 let start = path.len().saturating_sub(max_len.saturating_sub(3));
38 format!("...{}", &path[start..])
39 }
40}
41
42pub fn colorize_status(status: &ProcessStatus, status_str: &str) -> ColoredString {
44 match status {
45 ProcessStatus::Running => status_str.green(),
46 ProcessStatus::Sleeping => status_str.blue(),
47 ProcessStatus::Stopped => status_str.yellow(),
48 ProcessStatus::Zombie => status_str.red(),
49 _ => status_str.white(),
50 }
51}