use chrono::Duration;
fn group(int: u64) -> String {
let s = int.to_string();
let mut out = String::new();
for (i, c) in s.chars().enumerate() {
if i > 0 && (s.len() - i).is_multiple_of(3) {
out.push(',');
}
out.push(c);
}
out
}
pub fn money(x: impl Into<f64>) -> String {
let x: f64 = x.into();
if !x.is_finite() {
return "-".into();
}
let neg = x < 0.0;
let a = x.abs();
let body = if a >= 1.0 || a == 0.0 {
let cents = (a * 100.0).round() as u64;
format!("{}.{:02}", group(cents / 100), cents % 100)
} else if a >= 0.01 {
format!("{a:.2}")
} else {
format!("{a:.4}")
};
format!("{}${body}", if neg { "-" } else { "" })
}
pub fn usd(x: impl Into<f64>) -> String {
let x: f64 = x.into();
if x.is_finite() && x.abs() >= 1000.0 {
let neg = x < 0.0;
format!(
"{}${}",
if neg { "-" } else { "" },
group(x.abs().round() as u64)
)
} else {
money(x)
}
}
pub fn tokens(n: u64) -> String {
let f = n as f64;
if n < 1_000 {
n.to_string()
} else if n < 1_000_000 {
format!("{:.1}k", f / 1e3)
} else if n < 1_000_000_000 {
format!("{:.2}M", f / 1e6)
} else {
format!("{:.2}B", f / 1e9)
}
}
pub fn count(n: u64) -> String {
group(n)
}
pub fn duration(d: Duration) -> String {
let s = d.num_seconds().max(0);
let (days, hours, mins) = (s / 86_400, (s % 86_400) / 3600, (s % 3600) / 60);
if days > 0 {
format!("{days}d {hours}h")
} else if hours > 0 {
format!("{hours}h {mins:02}m")
} else if mins > 0 {
format!("{mins}m")
} else {
format!("{s}s")
}
}
pub fn clock(d: Duration) -> String {
let s = d.num_seconds().max(0);
if s >= 3600 {
format!("{}:{:02}:{:02}", s / 3600, (s % 3600) / 60, s % 60)
} else {
format!("{}:{:02}", s / 60, s % 60)
}
}
pub fn pct(x: impl Into<f64>) -> String {
let x: f64 = x.into();
if x >= 9.95 || x == 0.0 {
format!("{x:.0}%")
} else {
format!("{x:.1}%")
}
}
const EIGHTHS: [char; 8] = ['▏', '▎', '▍', '▌', '▋', '▊', '▉', '█'];
pub fn bar(frac: f64, width: usize) -> String {
let frac = if frac.is_finite() {
frac.clamp(0.0, 1.0)
} else {
0.0
};
let eighths = (frac * width as f64 * 8.0).round() as usize;
let full = eighths / 8;
let rem = eighths % 8;
let mut s = "█".repeat(full);
if rem > 0 {
s.push(EIGHTHS[rem - 1]);
}
s
}
pub fn gauge(frac: f64, width: usize) -> String {
let b = bar(frac, width);
let used = unicode_width::UnicodeWidthStr::width(b.as_str());
format!("▕{b}{}▏", " ".repeat(width.saturating_sub(used)))
}
pub fn human_bytes(b: u64) -> String {
let f = b as f64;
if b >= 1 << 30 {
format!("{:.1} GB", f / (1u64 << 30) as f64)
} else if b >= 1 << 20 {
format!("{:.1} MB", f / (1u64 << 20) as f64)
} else if b >= 1 << 10 {
format!("{:.0} KB", f / 1024.0)
} else {
format!("{b} B")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn formats() {
assert_eq!(money(1234.567), "$1,234.57");
assert_eq!(money(0.0382254), "$0.04");
assert_eq!(money(0.004), "$0.0040");
assert_eq!(money(0.0), "$0.00");
assert_eq!(tokens(950), "950");
assert_eq!(tokens(12_345), "12.3k");
assert_eq!(tokens(4_560_000), "4.56M");
assert_eq!(
duration(Duration::seconds(3 * 86_400 + 15 * 3600 + 59)),
"3d 15h"
);
assert_eq!(duration(Duration::seconds(2 * 3600 + 5 * 60)), "2h 05m");
assert_eq!(clock(Duration::seconds(134)), "2:14");
assert_eq!(pct(77.0), "77%");
assert_eq!(pct(5.39), "5.4%");
}
#[test]
fn bars() {
assert_eq!(bar(0.5, 4), "██");
assert_eq!(bar(1.0 / 32.0, 4), "▏");
assert_eq!(bar(2.0, 2), "██");
assert_eq!(gauge(0.25, 4), "▕█ ▏");
}
}