50 lines
1.7 KiB
Swift
50 lines
1.7 KiB
Swift
import Foundation
|
|
|
|
nonisolated enum MenuBarMetric: String, CaseIterable {
|
|
case cpu, gpu, memory, disk, network
|
|
|
|
var titleKey: String { self == .disk ? "menuBar.disk" : "metric." + rawValue }
|
|
}
|
|
|
|
nonisolated struct MenuBarReading: Equatable {
|
|
enum Content: Equatable {
|
|
case usage(Double?)
|
|
case network(upload: String, download: String)
|
|
}
|
|
|
|
let metric: MenuBarMetric
|
|
let title: String
|
|
let content: Content
|
|
|
|
static func readings(selection: Set<MenuBarMetric>, snapshot: MetricsSnapshot,
|
|
title: (MenuBarMetric) -> String) -> [Self] {
|
|
MenuBarMetric.allCases.filter(selection.contains).map { metric in
|
|
let content: Content = switch metric {
|
|
case .cpu: .usage(snapshot.cpu?.usage)
|
|
case .gpu: .usage(snapshot.gpuUsage)
|
|
case .memory: .usage(snapshot.memory?.usage)
|
|
case .disk: .usage(snapshot.disk?.usage)
|
|
case .network: .network(upload: compactRate(snapshot.uploadRate), download: compactRate(snapshot.downloadRate))
|
|
}
|
|
return Self(metric: metric, title: title(metric), content: content)
|
|
}
|
|
}
|
|
|
|
private static func compactRate(_ value: Double?) -> String {
|
|
MetricFormat.rate(value).replacingOccurrences(of: ".0 ", with: " ")
|
|
}
|
|
|
|
var accessibilityText: String {
|
|
switch content {
|
|
case let .usage(value): "\(title) \(MetricFormat.percent(value))"
|
|
case let .network(upload, download): "\(title) ↑ \(upload), ↓ \(download)"
|
|
}
|
|
}
|
|
}
|
|
|
|
nonisolated extension Array where Element == MenuBarReading {
|
|
var accessibilitySummary: String {
|
|
isEmpty ? "Kymo" : map(\.accessibilityText).joined(separator: ", ")
|
|
}
|
|
}
|