OXYGENHUB
# 2b872d074ddb oxygen / src / cli / format.rs
3.7 KiB · Rust · last changed CLI: modern output across all commands: rail-and-bullet log with -n/-v, relative times, authors, signature marks; glyphs on UTF-8 terminals with ASCII fallback 1 month ago source blame history raw
1
//! Shared presentation helpers.
2
3
use super::ui;
4
use anyhow::Result;
5
use oxy_core::hash::ChangeHash;
6
use oxy_core::objects::Change;
7
use oxy_core::repo::{Repo, SignatureStatus};
8
use oxy_core::state::ChangeMeta;
9
10
pub fn short_id(c: &Change) -> String {
11
    c.change_id.short(8)
12
}
13
14
pub fn short_meta_id(c: &ChangeMeta) -> String {
15
    c.change_id.short(8)
16
}
17
18
/// `n head` / `n heads`.
19
pub fn plural(n: usize, word: &str) -> String {
20
    format!("{n} {word}{}", if n == 1 { "" } else { "s" })
21
}
22
23
/// Milliseconds since the Unix epoch as `YYYY-MM-DD HH:MM:SSZ`.
24
pub fn when(ms: u64) -> String {
25
    let secs = (ms / 1000) as i64;
26
    let days = secs.div_euclid(86_400);
27
    let rem = secs.rem_euclid(86_400);
28
    // Civil-from-days (Howard Hinnant), UTC.
29
    let z = days + 719_468;
30
    let era = z.div_euclid(146_097);
31
    let doe = z - era * 146_097;
32
    let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
33
    let y = yoe + era * 400;
34
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
35
    let mp = (5 * doy + 2) / 153;
36
    let d = doy - (153 * mp + 2) / 5 + 1;
37
    let m = if mp < 10 { mp + 3 } else { mp - 9 };
38
    let y = if m <= 2 { y + 1 } else { y };
39
    format!("{y:04}-{m:02}-{d:02} {:02}:{:02}:{:02}Z", rem / 3600, (rem % 3600) / 60, rem % 60)
40
}
41
42
/// One line describing how a change is authenticated.
43
pub fn signature_line(repo: &Repo, h: &ChangeHash) -> Result<String> {
44
    Ok(match repo.authenticate_change(h)? {
45
        SignatureStatus::Unsigned => "unsigned".to_string(),
46
        SignatureStatus::Valid { key, name, email, sealed, revoked } => {
47
            format!("{} by {name} <{email}> key {}{}", if sealed { "sealed" } else { "signed" }, key.short(12), if revoked { " (KEY REVOKED)" } else { "" })
48
        }
49
        SignatureStatus::Invalid(why) => format!("INVALID signature: {why}"),
50
    })
51
}
52
53
/// A compact, styled authentication marker for log lines.
54
pub fn signature_mark(repo: &Repo, h: &ChangeHash) -> Result<String> {
55
    Ok(match repo.authenticate_change(h)? {
56
        SignatureStatus::Unsigned => ui::dim(format!("{} unsigned", ui::glyph("·", "-"))),
57
        SignatureStatus::Valid { sealed, revoked: true, .. } => ui::warn(format!("{} {} (key revoked)", ui::glyph("⚠", "!"), if sealed { "sealed" } else { "signed" })),
58
        SignatureStatus::Valid { sealed, .. } => ui::ok(format!("{} {}", ui::glyph("✔", "v"), if sealed { "sealed" } else { "signed" })),
59
        SignatureStatus::Invalid(why) => ui::err(format!("{} INVALID: {why}", ui::glyph("✖", "x"))),
60
    })
61
}
62
63
/// The author's display name: the identity's name, or a Git-Author trailer
64
/// for imported changes, or a shortened key.
65
pub fn author_name(repo: &Repo, c: &ChangeMeta) -> String {
66
    if !c.author.is_zero() {
67
        if let Ok(id) = repo.identity(&c.author) {
68
            return id.name;
69
        }
70
        return c.author.short(12);
71
    }
72
    c.message
73
        .lines()
74
        .rev()
75
        .find_map(|l| l.strip_prefix("Git-Author: "))
76
        .map(|a| a.split('<').next().unwrap_or(a).trim().to_string())
77
        .unwrap_or_else(|| "unknown".into())
78
}
79
80
/// The first line of a message, or a placeholder.
81
pub fn subject_of(message: &str) -> &str {
82
    match message.lines().next() {
83
        Some(l) if !l.trim().is_empty() => l,
84
        _ => "(no description)",
85
    }
86
}
87
88
pub fn deps_line(c: &Change) -> String {
89
    deps_of(&c.deps)
90
}
91
92
pub fn deps_of(deps: &[oxy_core::hash::ChangeHash]) -> String {
93
    deps.iter().map(|d| format!("#{}", d.short(12))).collect::<Vec<_>>().join(" ")
94
}
95
96
#[cfg(test)]
97
mod tests {
98
    use super::*;
99
100
    #[test]
101
    fn dates() {
102
        assert_eq!(when(0), "1970-01-01 00:00:00Z");
103
        assert_eq!(when(1_700_000_000_000), "2023-11-14 22:13:20Z");
104
        assert_eq!(plural(1, "head"), "1 head");
105
        assert_eq!(plural(2, "head"), "2 heads");
106
    }
107
}