OXYGENHUB
# 80642fde09cc oxygenhub / crates / oh / src / ui.rs
3.0 KiB · Rust · last changed Agent grants: agents act for their owner with no key (keyless step 5) 18 days ago source blame history raw
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
9
pub fn init(plain_flag: bool) {
10
    let plain = plain_flag || std::env::var_os("NO_COLOR").is_some_and(|v| !v.is_empty()) || !std::io::stdout().is_terminal();
11
    let _ = PLAIN.set(plain);
12
}
13
14
fn plain() -> bool {
15
    *PLAIN.get().unwrap_or(&true)
16
}
17
18
fn paint(code: &str, s: &str) -> String {
19
    if plain() {
20
        s.to_string()
21
    } else {
22
        format!("\x1b[{code}m{s}\x1b[0m")
23
    }
24
}
25
26
pub fn ok(s: &str) -> String {
27
    paint("32", s)
28
}
29
pub fn bad(s: &str) -> String {
30
    paint("31", s)
31
}
32
pub fn warn(s: &str) -> String {
33
    paint("35", s)
34
}
35
pub fn dim(s: &str) -> String {
36
    paint("2", s)
37
}
38
pub fn strong(s: &str) -> String {
39
    paint("1", s)
40
}
41
42
pub fn kv(key: &str, value: &str) -> String {
43
    format!("{} {value}", dim(&format!("{key:>16}")))
44
}
45
46
/// A run's or job's or check's status, as a glyph.
47
pub fn status(s: &str) -> String {
48
    match s {
49
        "passed" | "success" => ok("✔"),
50
        "failed" | "failure" | "error" => bad("✘"),
51
        "running" | "pending" => warn("●"),
52
        "queued" => dim("○"),
53
        "skipped" | "neutral" => dim("–"),
54
        _ => dim("?"),
55
    }
56
}
57
58
/// "3 minutes ago", as oxy says it.
59
pub fn ago(ms: i64) -> String {
60
    let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as i64).unwrap_or(0);
61
    let secs = (now - ms).max(0) / 1000;
62
    let unit = |n: i64, w: &str| format!("{n} {w}{} ago", if n == 1 { "" } else { "s" });
63
    match secs {
64
        0..=44 => "just now".into(),
65
        45..=89 => "a minute ago".into(),
66
        90..=2_699 => unit((secs + 59) / 60, "minute"),
67
        2_700..=5_399 => "an hour ago".into(),
68
        5_400..=79_199 => unit((secs + 3_599) / 3_600, "hour"),
69
        79_200..=129_599 => "yesterday".into(),
70
        _ => unit((secs + 86_399) / 86_400, "day"),
71
    }
72
}
73
74
/// A wall time as its UTC date and minute, `2026-11-16 09:30Z`.
75
pub fn date(ms: i64) -> String {
76
    let secs = ms.div_euclid(1000);
77
    let (days, rem) = (secs.div_euclid(86_400), secs.rem_euclid(86_400));
78
    // Days since 1970-01-01 to a civil date (Howard Hinnant's algorithm).
79
    let z = days + 719_468;
80
    let era = z.div_euclid(146_097);
81
    let doe = z - era * 146_097;
82
    let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365;
83
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
84
    let mp = (5 * doy + 2) / 153;
85
    let d = doy - (153 * mp + 2) / 5 + 1;
86
    let m = if mp < 10 { mp + 3 } else { mp - 9 };
87
    let y = yoe + era * 400 + i64::from(m <= 2);
88
    format!("{y:04}-{m:02}-{d:02} {:02}:{:02}Z", rem / 3_600, rem % 3_600 / 60)
89
}
90
91
#[cfg(test)]
92
mod date_tests {
93
    #[test]
94
    fn dates_are_utc_civil_dates() {
95
        assert_eq!(super::date(0), "1970-01-01 00:00Z");
96
        assert_eq!(super::date(1_792_229_400_000), "2026-10-17 09:30Z");
97
        assert_eq!(super::date(951_782_400_000), "2000-02-29 00:00Z");
98
    }
99
}