use std::time::Duration;
pub fn rel_time(d: Duration) -> String {
let s = d.as_secs();
if s < 60 {
format!("{s}s")
} else if s < 3600 {
format!("{}m", s / 60)
} else if s < 86_400 {
format!("{}h", s / 3600)
} else {
format!("{}d", s / 86_400)
}
}
pub fn bytes(n: usize) -> String {
if n < 1024 {
format!("{n} B")
} else if n < 1024 * 1024 {
format!("{} KiB", n / 1024)
} else {
format!("{} MiB", n / (1024 * 1024))
}
}
pub fn truncate(s: &str, max: usize) -> String {
if max == 0 {
return String::new();
}
let clean = s.chars().map(|c| if c.is_control() { ' ' } else { c });
let count = s.chars().count();
if count <= max {
return clean.collect();
}
let mut out: String = clean.take(max - 1).collect();
out.push('…');
out
}
pub fn pad(s: &str, width: usize) -> String {
let t = truncate(s, width);
let w = t.chars().count();
if w < width {
let mut t = t;
t.extend(std::iter::repeat_n(' ', width - w));
t
} else {
t
}
}