Phase 9, MA1: crates/ghostty-macos (AppKit via objc2; empty stub on non-Apple targets) ports the foundation of Ghostty 1.3.1's Swift macOS app (ghostty/macos): main, AppDelegate (+Ghostty) with lifecycle, dock menu, open-file, notifications, shortcut sync and quit/reopen paths; MainMenu.xib as generated menu tables built into NSMenus; the Ghostty.App/Config/Surface wrappers driving libghostty in-process via ghostty-lib; the generic helpers; scripts/macos-bundle.sh to assemble Ghostty.app (dry-run on Linux). Feature areas are stubbed for MA2-MA5. Design and type mapping: docs/rewrite/notes/macos-app-design.md (App Intents is a Swift-only framework and cannot be ported). Compile-checked only (no macOS here): just check-macos passes for aarch64/x86_64-apple-darwin with -D warnings; 125 platform-independent tests run on Linux. Test Plan: - just check-macos - cargo test -p ghostty-macos
65 lines
2.1 KiB
Rust
65 lines
2.1 KiB
Rust
//! Duration formatting used by `Ghostty.App.commandFinished` notifications.
|
|
//! Portable (and tested) so the formatting rules are checked on every target.
|
|
|
|
use std::time::Duration;
|
|
|
|
/// `Duration.formatted(.units(allowed: [.hours, .minutes, .seconds,
|
|
/// .milliseconds], width: .abbreviated, fractionalPart: .hide))` in the en_US
|
|
/// locale: zero-valued units are hidden, units are joined with ", " and use
|
|
/// the abbreviations `hr`, `min`, `sec`, `ms` (e.g. "1 hr, 2 min, 3 sec").
|
|
/// A zero duration formats as "0 ms" (the smallest allowed unit). Sub
|
|
/// millisecond remainders are hidden (truncated).
|
|
///
|
|
/// Note: the exact rounding of `fractionalPart: .hide` and non-English locales
|
|
/// are Foundation behaviour that cannot be verified off macOS.
|
|
#[must_use]
|
|
pub fn format_duration_abbreviated(duration: Duration) -> String {
|
|
let total_ms = duration.as_millis();
|
|
let hours = total_ms / 3_600_000;
|
|
let minutes = (total_ms / 60_000) % 60;
|
|
let seconds = (total_ms / 1000) % 60;
|
|
let millis = total_ms % 1000;
|
|
let mut parts = Vec::new();
|
|
if hours > 0 {
|
|
parts.push(format!("{hours} hr"));
|
|
}
|
|
if minutes > 0 {
|
|
parts.push(format!("{minutes} min"));
|
|
}
|
|
if seconds > 0 {
|
|
parts.push(format!("{seconds} sec"));
|
|
}
|
|
if millis > 0 || parts.is_empty() {
|
|
parts.push(format!("{millis} ms"));
|
|
}
|
|
parts.join(", ")
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn duration_formatting() {
|
|
assert_eq!(format_duration_abbreviated(Duration::from_secs(5)), "5 sec");
|
|
assert_eq!(
|
|
format_duration_abbreviated(Duration::from_secs(3723)),
|
|
"1 hr, 2 min, 3 sec"
|
|
);
|
|
assert_eq!(
|
|
format_duration_abbreviated(Duration::from_millis(1500)),
|
|
"1 sec, 500 ms"
|
|
);
|
|
assert_eq!(
|
|
format_duration_abbreviated(Duration::from_secs(60)),
|
|
"1 min"
|
|
);
|
|
assert_eq!(format_duration_abbreviated(Duration::ZERO), "0 ms");
|
|
// Sub-millisecond remainders are hidden.
|
|
assert_eq!(
|
|
format_duration_abbreviated(Duration::from_nanos(2_000_999_999)),
|
|
"2 sec"
|
|
);
|
|
}
|
|
}
|