OXYGENHUB

Coverage / crates/oh/src/ui.rs 84.0%

63 of 75 lines run, 14 of 31 functions · run #480 on api-unknown-fields · run missed

1
//! Output styling in the manner of `oxy`: colour and glyphs on a terminal,
2
//! plain text with `--plain`, `NO_COLOR` or when piped.
3
4
use std::io::IsTerminal;
5
use std::sync::OnceLock;
6
7
static PLAIN: OnceLock<bool> = OnceLock::new();
8
947×
pub fn init(plain_flag: bool) {
1047×
    let plain = plain_flag || std::env::var_os("NO_COLOR").is_some_and(|v| !v.is_empty()) || !std::io::stdout().is_terminal();
1147×
    let _ = PLAIN.set(plain);
1247×
}
13
14111×
fn plain() -> bool {
15111×
    *PLAIN.get().unwrap_or(&true)
16111×
}
17
18111×
fn paint(code: &str, s: &str) -> String {
19111×
    if plain() {
20111×
        s.to_string()
21
    } else {
220×
        format!("\x1b[{code}m{s}\x1b[0m")
23
    }
24111×
}
25
2628×
pub fn ok(s: &str) -> String {
2728×
    paint("32", s)
2828×
}
297×
pub fn bad(s: &str) -> String {
307×
    paint("31", s)
317×
}
323×
pub fn warn(s: &str) -> String {
333×
    paint("35", s)
343×
}
3561×
pub fn dim(s: &str) -> String {
3661×
    paint("2", s)
3761×
}
3812×
pub fn strong(s: &str) -> String {
3912×
    paint("1", s)
4012×
}
41
426×
pub fn kv(key: &str, value: &str) -> String {
436×
    format!("{} {value}", dim(&format!("{key:>16}")))
446×
}
45
46
/// A run's or job's or check's status, as a glyph.
472×
pub fn status(s: &str) -> String {
482×
    match s {
492×
        "passed" | "success" => ok("✔"),
502×
        "failed" | "failure" | "error" => bad("✘"),
512×
        "running" | "pending" => warn("●"),
522×
        "queued" => dim("○"),
530×
        "skipped" | "neutral" => dim("–"),
540×
        "cancelled" => dim("⊘"),
550×
        _ => dim("?"),
56
    }
572×
}
58
59
/// "3 minutes ago", as oxy says it.
6010×
pub fn ago(ms: i64) -> String {
6110×
    let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as i64).unwrap_or(0);
6210×
    let secs = (now - ms).max(0) / 1000;
6310×
    let unit = |n: i64, w: &str| format!("{n} {w}{} ago", if n == 1 { "" } else { "s" });
6410×
    match secs {
6510×
        0..=44 => "just now".into(),
660×
        45..=89 => "a minute ago".into(),
670×
        90..=2_699 => unit((secs + 59) / 60, "minute"),
680×
        2_700..=5_399 => "an hour ago".into(),
690×
        5_400..=79_199 => unit((secs + 3_599) / 3_600, "hour"),
700×
        79_200..=129_599 => "yesterday".into(),
710×
        _ => unit((secs + 86_399) / 86_400, "day"),
72
    }
7310×
}
74
75
/// A wall time as its UTC date and minute, `2026-11-16 09:30Z`.
763×
pub fn date(ms: i64) -> String {
773×
    let secs = ms.div_euclid(1000);
783×
    let (days, rem) = (secs.div_euclid(86_400), secs.rem_euclid(86_400));
79
    // Days since 1970-01-01 to a civil date (Howard Hinnant's algorithm).
803×
    let z = days + 719_468;
813×
    let era = z.div_euclid(146_097);
823×
    let doe = z - era * 146_097;
833×
    let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365;
843×
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
853×
    let mp = (5 * doy + 2) / 153;
863×
    let d = doy - (153 * mp + 2) / 5 + 1;
873×
    let m = if mp < 10 { mp + 3 } else { mp - 9 };
883×
    let y = yoe + era * 400 + i64::from(m <= 2);
893×
    format!("{y:04}-{m:02}-{d:02} {:02}:{:02}Z", rem / 3_600, rem % 3_600 / 60)
903×
}
91
92
#[cfg(test)]
93
mod date_tests {
94
    #[test]
951×
    fn dates_are_utc_civil_dates() {
961×
        assert_eq!(super::date(0), "1970-01-01 00:00Z");
971×
        assert_eq!(super::date(1_792_229_400_000), "2026-10-17 09:30Z");
981×
        assert_eq!(super::date(951_782_400_000), "2000-02-29 00:00Z");
991×
    }
100
}