SPB Git

Repository Public

Format.swift 7.0 KB 166 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : Claude Remote / Codex Remote9// File             : Packages/ClaudeRemoteKit/Sources/ClaudeRemoteDesign/Format.swift10// Module           : ClaudeRemoteDesign11// Repository       : https://git.spboucher.ai/claude-remote-apple12// Project Website  : https://claude.spboucher.ai13// Created          : 2026-11-2914// Last Modified    : 2026-11-2915//16// =============================================================================17// AUTHOR & PROFESSIONAL AFFILIATION18// =============================================================================19//20// Author           : Simon-Pierre Boucher21// Position         : Professor of Finance22// Institution      : Université du Québec en Outaouais (UQO)23// Department       : Département des sciences administratives24// Organization     : SP Boucher25//26// Website          : https://spboucher.ai27// Git              : https://git.spboucher.ai28//29// =============================================================================30// OFFICIAL CONTACT DIRECTORY31// =============================================================================32//33// General          : info@spboucher.ai34// Professional     : contact@spboucher.ai35// Direct           : simon@spboucher.ai36// Research         : research@spboucher.ai37// Development      : dev@spboucher.ai38// Support          : support@spboucher.ai39// Security/Admin   : admin@spboucher.ai40// Notifications    : notifications@spboucher.ai41//42// Preferred Contact: support@spboucher.ai43//44// =============================================================================45// COPYRIGHT & INTELLECTUAL PROPERTY46// =============================================================================47//48// Copyright (c) 202649// Simon-Pierre Boucher50//51// All rights reserved where applicable.52//53// This file is governed by the applicable project license,54// copyright notices, institutional agreements and relevant law.55//56// Unless expressly authorized by the applicable license or57// rights holder, copying, redistribution, sublicensing,58// publication and commercial exploitation are not permitted.59//60// Existing third-party rights and notices remain unaffected.61//62// =============================================================================63// CONFIDENTIALITY & SECURITY64// =============================================================================65//66// Classification   : Public67// Distribution     : Public repository; use is governed by the project license68// Security Contact : admin@spboucher.ai69//70// Access to this source does not independently grant permission71// to redistribute or disclose confidential materials.72//73// Do not embed credentials, private keys, access tokens,74// certificates or confidential personal information.75//76// =============================================================================77// LICENSE & MAINTENANCE78// =============================================================================79//80// License          : None specified (no license file in this repository)81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import Foundation8990/// Deterministic, locale-independent formatters for dashboards.91public enum CRFormat {92    /// Placeholder for a value that is not known (never show 0 for "unknown").93    public static let missing = "—"9495    /// 1536 → "1.5 KB" (binary multiples).96    public static func bytes(_ count: Int64?) -> String {97        guard let count else { return missing }98        let units = ["B", "KB", "MB", "GB", "TB", "PB"]99        var value = Double(abs(count))100        var unit = 0101        while value >= 1024, unit < units.count - 1 {102            value /= 1024103            unit += 1104        }105        let sign = count < 0 ? "-" : ""106        if unit == 0 { return "\(sign)\(Int(value)) B" }107        return sign + String(format: value >= 100 ? "%.0f %@" : "%.1f %@", locale: Locale(identifier: "en_US_POSIX"), value, units[unit])108    }109110    /// Megabytes → "512 MB" / "1.5 GB".111    public static func megabytes(_ mb: Double?) -> String {112        guard let mb else { return missing }113        return bytes(Int64(mb * 1024 * 1024))114    }115116    /// Seconds → "45s", "3m 05s", "1h 02m", "2d 4h".117    public static func duration(_ seconds: Double?) -> String {118        guard let seconds, seconds.isFinite else { return missing }119        let s = Int(max(0, seconds.rounded()))120        if s < 60 { return "\(s)s" }121        if s < 3600 { return "\(s / 60)m " + String(format: "%02ds", s % 60) }122        if s < 86400 { return "\(s / 3600)h " + String(format: "%02dm", (s % 3600) / 60) }123        return "\(s / 86400)d \((s % 86400) / 3600)h"124    }125126    /// 42.4 → "42%"; nil → "—". `value` is already a percentage (0…100).127    public static func percent(_ value: Double?, decimals: Int = 0) -> String {128        guard let value, value.isFinite else { return missing }129        return String(format: "%.\(max(0, decimals))f%%", locale: Locale(identifier: "en_US_POSIX"), value)130    }131132    /// Milliseconds → "23 ms" / "1.2 s".133    public static func latency(ms: Double?) -> String {134        guard let ms, ms.isFinite else { return missing }135        if ms < 1000 { return "\(Int(ms.rounded())) ms" }136        return String(format: "%.1f s", locale: Locale(identifier: "en_US_POSIX"), ms / 1000)137    }138139    /// Compact count: 1234 → "1.2k".140    public static func count(_ value: Int?) -> String {141        guard let value else { return missing }142        if abs(value) < 1000 { return "\(value)" }143        let posix = Locale(identifier: "en_US_POSIX")144        if abs(value) < 1_000_000 { return String(format: "%.1fk", locale: posix, Double(value) / 1000) }145        return String(format: "%.1fM", locale: posix, Double(value) / 1_000_000)146    }147148    /// Short relative time: "now", "5m ago", "2h ago", "3d ago", "in 5m"; older than 30 days → a date.149    public static func relative(_ date: Date, now: Date = Date()) -> String {150        let delta = now.timeIntervalSince(date)151        let s = Int(abs(delta))152        let text: String153        if s < 45 { return "now" }154        else if s < 3600 { text = "\(max(1, s / 60))m" }155        else if s < 86400 { text = "\(s / 3600)h" }156        else if s < 30 * 86400 { text = "\(s / 86400)d" }157        else { return date.formatted(date: .abbreviated, time: .omitted) }158        return delta >= 0 ? "\(text) ago" : "in \(text)"159    }160161    /// Full date and time for accessibility labels and tooltips.162    public static func absolute(_ date: Date) -> String {163        date.formatted(date: .abbreviated, time: .shortened)164    }165}166
Files main
Menu

Theme