use std::time::Duration;
pub fn format_bytes(bytes: u64) -> String {
const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB"];
if bytes == 0 {
return "0 B".to_string();
}
let mut size = bytes as f64;
let mut unit_index = 0;
while size >= 1024.0 && unit_index < UNITS.len() - 1 {
size /= 1024.0;
unit_index += 1;
}
if unit_index == 0 {
format!("{bytes} {}", UNITS[unit_index])
} else {
format!("{size:.1} {}", UNITS[unit_index])
}
}
pub fn format_duration(duration: Duration) -> String {
let total_seconds = duration.as_secs();
if total_seconds < 60 {
format!("{total_seconds}s")
} else if total_seconds < 3600 {
let minutes = total_seconds / 60;
let seconds = total_seconds % 60;
if seconds == 0 {
format!("{minutes}m")
} else {
format!("{minutes}m{seconds}s")
}
} else if total_seconds < 86400 {
let hours = total_seconds / 3600;
let minutes = (total_seconds % 3600) / 60;
if minutes == 0 {
format!("{hours}h")
} else {
format!("{hours}h{minutes}m")
}
} else {
let days = total_seconds / 86400;
let hours = (total_seconds % 86400) / 3600;
if hours == 0 {
format!("{days}d")
} else {
format!("{days}d{hours}h")
}
}
}