OXYGENHUB
# 94084087a876 oxygen / src / cli / format.rs
3.5 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 } => {
47
            format!("{} by {name} <{email}> key {}", if sealed { "sealed" } else { "signed" }, key.short(12))
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, .. } => ui::ok(format!("{} {}", ui::glyph("✔", "v"), if sealed { "sealed" } else { "signed" })),
58
        SignatureStatus::Invalid(why) => ui::err(format!("{} INVALID: {why}", ui::glyph("✖", "x"))),
59
    })
60
}
61
62
/// The author's display name: the identity's name, or a Git-Author trailer
63
/// for imported changes, or a shortened key.
64
pub fn author_name(repo: &Repo, c: &ChangeMeta) -> String {
65
    if !c.author.is_zero() {
66
        if let Ok(id) = repo.identity(&c.author) {
67
            return id.name;
68
        }
69
        return c.author.short(12);
70
    }
71
    c.message
72
        .lines()
73
        .rev()
74
        .find_map(|l| l.strip_prefix("Git-Author: "))
75
        .map(|a| a.split('<').next().unwrap_or(a).trim().to_string())
76
        .unwrap_or_else(|| "unknown".into())
77
}
78
79
/// The first line of a message, or a placeholder.
80
pub fn subject_of(message: &str) -> &str {
81
    match message.lines().next() {
82
        Some(l) if !l.trim().is_empty() => l,
83
        _ => "(no description)",
84
    }
85
}
86
87
pub fn deps_line(c: &Change) -> String {
88
    deps_of(&c.deps)
89
}
90
91
pub fn deps_of(deps: &[oxy_core::hash::ChangeHash]) -> String {
92
    deps.iter().map(|d| format!("#{}", d.short(12))).collect::<Vec<_>>().join(" ")
93
}
94
95
#[cfg(test)]
96
mod tests {
97
    use super::*;
98
99
    #[test]
100
    fn dates() {
101
        assert_eq!(when(0), "1970-01-01 00:00:00Z");
102
        assert_eq!(when(1_700_000_000_000), "2023-11-14 22:13:20Z");
103
        assert_eq!(plural(1, "head"), "1 head");
104
        assert_eq!(plural(2, "head"), "2 heads");
105
    }
106
}