amphetamine 0.1.0

Reclaim memory and win scheduler contention on Apple Silicon, safely.
//! Terminal output.

use owo_colors::OwoColorize;

const KB: f64 = 1024.0;
const MB: f64 = KB * 1024.0;
const GB: f64 = MB * 1024.0;

pub fn bytes(n: u64) -> String {
    let f = n as f64;
    match f {
        _ if f >= GB => format!("{:.1} GB", f / GB),
        _ if f >= MB => format!("{:.0} MB", f / MB),
        _ if f >= KB => format!("{:.0} KB", f / KB),
        _ => format!("{n} B"),
    }
}

/// A ten-cell meter, coloured by how alarming the value is.
pub fn bar(pct: f64) -> String {
    let filled = ((pct / 10.0).round() as usize).min(10);
    let meter: String = "".repeat(filled) + &"".repeat(10 - filled);
    match pct {
        p if p >= 90.0 => meter.red().to_string(),
        p if p >= 70.0 => meter.yellow().to_string(),
        _ => meter.green().to_string(),
    }
}

pub fn title(s: &str) {
    println!("\n{}", s.bold());
}

pub fn row(label: &str, value: &str) {
    println!("  {:<12} {}", label.dimmed(), value);
}

pub fn note(s: &str) {
    println!("  {} {}", "".dimmed(), s.dimmed());
}

pub fn warn(s: &str) {
    println!("  {} {}", "!".yellow().bold(), s);
}

pub fn good(s: &str) {
    println!("  {} {}", "".green().bold(), s);
}

pub fn skipped(s: &str) {
    println!("  {} {}", "·".dimmed(), s.dimmed());
}