const FULL: u64 = 1000;
const ALARM: u64 = 900;
const GREEN_HUE: u64 = 120;
const SECTOR: u64 = 60;
const MAX_CHANNEL: u64 = 255;
const GREEN: &str = "\x1b[32m";
const AMBER: &str = "\x1b[33m";
const RED: &str = "\x1b[31m";
pub(crate) fn fill(used: u64, limit: u64) -> Option<u64> {
if used == 0 || limit == 0 {
return None;
}
let scaled = used.saturating_mul(FULL).checked_div(limit)?;
Some(scaled.min(FULL))
}
pub(crate) fn alarm(fill: u64) -> &'static str {
if fill >= ALARM { "!" } else { "" }
}
pub(crate) fn ansi(fill: u64, truecolor: bool) -> String {
if !truecolor {
return band(fill).to_owned();
}
let (r, g, b) = rgb(hue(fill));
format!("\x1b[38;2;{r};{g};{b}m")
}
fn band(fill: u64) -> &'static str {
if fill >= ALARM {
RED
} else if hue(fill) > SECTOR {
GREEN
} else {
AMBER
}
}
fn hue(fill: u64) -> u64 {
let room = ALARM.saturating_sub(fill);
let scaled = room.saturating_mul(FULL).checked_div(ALARM).unwrap_or(0);
let root = scaled.saturating_mul(FULL).isqrt().min(FULL);
GREEN_HUE
.saturating_mul(root)
.checked_div(FULL)
.unwrap_or(0)
}
fn rgb(hue: u64) -> (u64, u64, u64) {
if hue <= SECTOR {
(MAX_CHANNEL, sector(hue), 0)
} else {
(sector(GREEN_HUE.saturating_sub(hue)), MAX_CHANNEL, 0)
}
}
fn sector(degrees: u64) -> u64 {
degrees
.saturating_mul(MAX_CHANNEL)
.checked_div(SECTOR)
.unwrap_or(MAX_CHANNEL)
.min(MAX_CHANNEL)
}
pub(crate) fn truecolor() -> bool {
std::env::var("COLORTERM")
.is_ok_and(|v| v.contains("truecolor") || v.contains("24bit"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fill_is_the_level_over_the_limit_in_per_mille() {
assert_eq!(fill(500, 1000), Some(500));
assert_eq!(fill(963_000, 1_000_000), Some(963));
}
#[test]
fn a_zero_level_has_no_fill_at_all() {
assert_eq!(fill(0, 1_000_000), None);
}
#[test]
fn a_zero_limit_has_no_fill_either() {
assert_eq!(fill(963_000, 0), None);
}
#[test]
fn a_level_over_the_limit_pins_at_full() {
assert_eq!(fill(2_000_000, 1_000_000), Some(FULL));
}
#[test]
fn an_empty_gauge_is_green_and_a_full_one_is_red() {
assert_eq!(rgb(hue(0)), (0, MAX_CHANNEL, 0));
assert_eq!(rgb(hue(FULL)), (MAX_CHANNEL, 0, 0));
}
#[test]
fn the_ramp_holds_green_through_the_flat_half() {
assert!(hue(500) > SECTOR, "hue at 50% was {}", hue(500));
assert!(hue(ALARM) < SECTOR, "hue at the alarm was {}", hue(ALARM));
}
#[test]
fn the_hue_never_leaves_the_green_to_red_arc() {
for f in 0..=FULL {
assert!(hue(f) <= GREEN_HUE, "hue({f}) = {}", hue(f));
}
}
#[test]
fn the_ramp_never_turns_back_toward_green() {
let mut last = GREEN_HUE;
for f in 0..=FULL {
let h = hue(f);
assert!(h <= last, "hue rose at {f}: {last} then {h}");
last = h;
}
}
#[test]
fn the_alarm_mark_appears_only_in_the_last_tenth() {
assert_eq!(alarm(899), "");
assert_eq!(alarm(ALARM), "!");
assert_eq!(alarm(FULL), "!");
}
#[test]
fn without_truecolor_the_same_fill_falls_to_three_bands() {
assert_eq!(ansi(100, false), GREEN);
assert_eq!(ansi(800, false), AMBER);
assert_eq!(ansi(950, false), RED);
}
#[test]
fn the_alarm_zone_is_red_in_both_renderings() {
for f in ALARM..=FULL {
assert_eq!(rgb(hue(f)), (MAX_CHANNEL, 0, 0), "ramp at {f}");
assert_eq!(band(f), RED, "band at {f}");
assert_eq!(alarm(f), "!", "mark at {f}");
}
}
#[test]
fn the_ramp_reaches_red_no_earlier_than_the_alarm() {
assert!(hue(ALARM - 1) > 0, "hue just under alarm was 0");
}
#[test]
fn with_truecolor_the_fill_becomes_a_24_bit_escape() {
assert_eq!(ansi(0, true), "\x1b[38;2;0;255;0m");
assert_eq!(ansi(FULL, true), "\x1b[38;2;255;0;0m");
}
}