Files
Kymo/kymo/Features/MenuBar/ProcessTableProjection.swift

67 lines
3.3 KiB
Swift

import Foundation
/// Compute once per data/filter/sort event, never from row rendering or scroll position.
nonisolated enum ProcessTableProjection {
/// Filtering hides rows without terminating processes. A failed read proves no departure.
static func exitedSelection(_ selected: ProcessIdentity?, in snapshot: ProcessSnapshot?) -> ProcessIdentity? {
guard let selected, let snapshot, !snapshot.failed,
!snapshot.rows.contains(where: { $0.id == selected }) else { return nil }
return selected
}
static func rows(_ input: [ProcessMetrics], query: String, sorting: [ProcessSort], order: [ProcessIdentity]) -> [ProcessMetrics] {
let matches = query.isEmpty ? input : input.filter {
$0.name.localizedCaseInsensitiveContains(query) || String($0.id.pid).contains(query)
|| ($0.path?.localizedCaseInsensitiveContains(query) ?? false)
}
func precedes(_ lhs: ProcessMetrics, _ rhs: ProcessMetrics) -> Bool {
for comparator in sorting {
let result = comparator.compare(lhs, rhs)
if result != .orderedSame { return result == .orderedAscending }
}
return lhs.name == rhs.name ? lhs.id.pid < rhs.id.pid : lhs.name < rhs.name
}
guard !order.isEmpty else { return matches.sorted(by: precedes) }
// A snapshot has one row per process identity. Refresh retained rows in their
// displayed order, then apply the active sort only to newly visible processes.
var remaining = Dictionary(uniqueKeysWithValues: matches.lazy.map { ($0.id, $0) })
var result = order.compactMap { remaining.removeValue(forKey: $0) }
result.append(contentsOf: remaining.values.sorted(by: precedes))
return result
}
}
/// Missing metrics stay last in either direction. Every newly selected column starts descending.
nonisolated struct ProcessSort: SortComparator, Equatable {
enum Key: String { case pid, name, path, cpu, memory, threads, power }
let key: Key
var order: SortOrder = .reverse
func compare(_ lhs: ProcessMetrics, _ rhs: ProcessMetrics) -> ComparisonResult {
func metric<T: Comparable>(_ a: T?, _ b: T?) -> ComparisonResult {
switch (a, b) {
case (nil, nil): return .orderedSame
case (nil, _): return .orderedDescending
case (_, nil): return .orderedAscending
case let (a?, b?): return directed(a < b ? .orderedAscending : a > b ? .orderedDescending : .orderedSame)
}
}
switch key {
case .pid: return metric(lhs.id.pid, rhs.id.pid)
case .name: return directed(lhs.name.localizedStandardCompare(rhs.name))
case .path: return directed((lhs.path ?? "").localizedStandardCompare(rhs.path ?? ""))
case .cpu: return metric(lhs.cpu, rhs.cpu)
case .memory: return metric(lhs.footprintBytes, rhs.footprintBytes)
case .power: return metric(lhs.cpuPower, rhs.cpuPower)
case .threads: return metric(lhs.threads, rhs.threads)
}
}
private func directed(_ value: ComparisonResult) -> ComparisonResult {
guard order == .reverse else { return value }
return value == .orderedAscending ? .orderedDescending : value == .orderedDescending ? .orderedAscending : .orderedSame
}
}