//! Shared presentation helpers. use super::ui; use anyhow::Result; use oxy_core::hash::ChangeHash; use oxy_core::objects::Change; use oxy_core::repo::{Repo, SignatureStatus}; use oxy_core::state::ChangeMeta; pub fn short_id(c: &Change) -> String { c.change_id.short(8) } pub fn short_meta_id(c: &ChangeMeta) -> String { c.change_id.short(8) } /// `n head` / `n heads`. pub fn plural(n: usize, word: &str) -> String { format!("{n} {word}{}", if n == 1 { "" } else { "s" }) } /// Milliseconds since the Unix epoch as `YYYY-MM-DD HH:MM:SSZ`. pub fn when(ms: u64) -> String { let secs = (ms / 1000) as i64; let days = secs.div_euclid(86_400); let rem = secs.rem_euclid(86_400); // Civil-from-days (Howard Hinnant), UTC. let z = days + 719_468; let era = z.div_euclid(146_097); let doe = z - era * 146_097; let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; let y = yoe + era * 400; let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); let mp = (5 * doy + 2) / 153; let d = doy - (153 * mp + 2) / 5 + 1; let m = if mp < 10 { mp + 3 } else { mp - 9 }; let y = if m <= 2 { y + 1 } else { y }; format!("{y:04}-{m:02}-{d:02} {:02}:{:02}:{:02}Z", rem / 3600, (rem % 3600) / 60, rem % 60) } /// One line describing how a change is authenticated. pub fn signature_line(repo: &Repo, h: &ChangeHash) -> Result { Ok(match repo.authenticate_change(h)? { SignatureStatus::Unsigned => "unsigned".to_string(), SignatureStatus::Valid { key, name, email, sealed, revoked } => { format!("{} by {name} <{email}> key {}{}", if sealed { "sealed" } else { "signed" }, key.short(12), if revoked { " (KEY REVOKED)" } else { "" }) } SignatureStatus::Invalid(why) => format!("INVALID signature: {why}"), }) } /// A compact, styled authentication marker for log lines. pub fn signature_mark(repo: &Repo, h: &ChangeHash) -> Result { Ok(match repo.authenticate_change(h)? { SignatureStatus::Unsigned => ui::dim(format!("{} unsigned", ui::glyph("·", "-"))), SignatureStatus::Valid { sealed, revoked: true, .. } => ui::warn(format!("{} {} (key revoked)", ui::glyph("⚠", "!"), if sealed { "sealed" } else { "signed" })), SignatureStatus::Valid { sealed, .. } => ui::ok(format!("{} {}", ui::glyph("✔", "v"), if sealed { "sealed" } else { "signed" })), SignatureStatus::Invalid(why) => ui::err(format!("{} INVALID: {why}", ui::glyph("✖", "x"))), }) } /// The author's display name: the identity's name, or a Git-Author trailer /// for imported changes, or a shortened key. pub fn author_name(repo: &Repo, c: &ChangeMeta) -> String { if !c.author.is_zero() { if let Ok(id) = repo.identity(&c.author) { return id.name; } return c.author.short(12); } c.message .lines() .rev() .find_map(|l| l.strip_prefix("Git-Author: ")) .map(|a| a.split('<').next().unwrap_or(a).trim().to_string()) .unwrap_or_else(|| "unknown".into()) } /// The first line of a message, or a placeholder. pub fn subject_of(message: &str) -> &str { match message.lines().next() { Some(l) if !l.trim().is_empty() => l, _ => "(no description)", } } pub fn deps_line(c: &Change) -> String { deps_of(&c.deps) } pub fn deps_of(deps: &[oxy_core::hash::ChangeHash]) -> String { deps.iter().map(|d| format!("#{}", d.short(12))).collect::>().join(" ") } #[cfg(test)] mod tests { use super::*; #[test] fn dates() { assert_eq!(when(0), "1970-01-01 00:00:00Z"); assert_eq!(when(1_700_000_000_000), "2023-11-14 22:13:20Z"); assert_eq!(plural(1, "head"), "1 head"); assert_eq!(plural(2, "head"), "2 heads"); } }