#![allow(dead_code)]
pub fn format_bytes(n: u64) -> String {
const UNITS: &[&str] = &["B", "KiB", "MiB", "GiB", "TiB", "PiB"];
let mut value = n as f64;
let mut unit_idx = 0;
while value >= 1024.0 && unit_idx < UNITS.len() - 1 {
value /= 1024.0;
unit_idx += 1;
}
if unit_idx == 0 {
format!("{} {}", n, UNITS[0])
} else if value >= 100.0 {
format!("{:.0} {}", value, UNITS[unit_idx])
} else if value >= 10.0 {
format!("{:.1} {}", value, UNITS[unit_idx])
} else {
format!("{:.2} {}", value, UNITS[unit_idx])
}
}
pub fn format_secs(secs: u64) -> String {
if secs < 60 {
return format!("{}s", secs);
}
if secs < 3600 {
let m = secs / 60;
let s = secs % 60;
if s == 0 {
return format!("{}m", m);
}
return format!("{}m {}s", m, s);
}
if secs < 86_400 {
let h = secs / 3600;
let m = (secs % 3600) / 60;
if m == 0 {
return format!("{}h", h);
}
return format!("{}h {}m", h, m);
}
let d = secs / 86_400;
let h = (secs % 86_400) / 3600;
if h == 0 {
return format!("{}d", d);
}
format!("{}d {}h", d, h)
}
pub fn should_color() -> bool {
if std::env::var_os("NO_COLOR").is_some() {
return false;
}
if std::env::var_os("DRACON_FORCE_COLOR").is_some() {
return true;
}
use std::io::IsTerminal;
std::io::stdout().is_terminal()
}
pub fn onoff(b: bool) -> &'static str {
if b {
"on"
} else {
"off"
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn format_bytes_mib() {
assert_eq!(format_bytes(52_428_800), "50.0 MiB");
}
#[test]
fn format_secs_units() {
assert_eq!(format_secs(60), "1m");
assert_eq!(format_secs(3600), "1h");
}
}