Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133import ArgumentParserimport Foundationimport ParcelIPC
struct DiagnosticsCommand: CLICommand { static let configuration = CommandConfiguration( commandName: "diagnostics", abstract: String(localized: "Inspect AX latency, app health, permissions, and private APIs.", bundle: .module) )
@OptionGroup var output: CLIOptions
var invocation: CLIInvocation { .request(command: "diagnostics", args: [], query: nil, json: output.json) }}
struct DiagnosticsOutput { static func format(_ value: JSONValue, json: Bool) throws -> String { let report = try IPCCodec.decode(IPCDiagnostics.self, from: IPCCodec.encode(value)) guard report.v == IPCDiagnostics.schemaVersion else { throw IPCTransportError.protocolViolation( String(localized: "Unsupported diagnostics version: \(report.v)", bundle: .module)) } if json { return try CLIOutput.jsonLine(value) } var sections = [ String(localized: "Parcel diagnostics", bundle: .module) + "\n" + (report.running ? String(localized: "Engine is running.", bundle: .module) : String(localized: "Engine is stopped.", bundle: .module)) + "\n", String(localized: "AX batch write latency (milliseconds)", bundle: .module) + "\n" + applications(report.applications), ] sections.append( String(localized: "Suspended displays", table: "Suspension", bundle: .module) + "\n" + suspendedDisplays(report.suspendedDisplays)) sections.append( String(localized: "Permissions", bundle: .module) + "\n" + CLIOutput.table( headers: [ String(localized: "Permission", bundle: .module), String(localized: "Status", bundle: .module), ], rows: [ [ String(localized: "Accessibility", bundle: .module), permission(report.permissions.accessibility), ], [ String(localized: "Input Monitoring", bundle: .module), permission(report.permissions.inputMonitoring), ], [ String(localized: "Screen Recording", bundle: .module), permission(report.permissions.screenRecording), ], ])) sections.append( String(localized: "Private APIs", bundle: .module) + "\n" + CLIOutput.table( headers: [ String(localized: "Symbol", bundle: .module), String(localized: "Available", bundle: .module), String(localized: "Verification", bundle: .module), String(localized: "Detail", bundle: .module), ], rows: report.capabilities.sorted { $0.symbol < $1.symbol }.map { [$0.symbol, flag($0.available), verification($0.verification), $0.detail ?? ""] })) return sections.joined(separator: "\n") }
private static func suspendedDisplays(_ displays: [IPCDiagnostics.SuspendedDisplay]) -> String { guard !displays.isEmpty else { return String(localized: "No displays are suspended.", table: "Suspension", bundle: .module) + "\n" } return CLIOutput.table( headers: [ String(localized: "Display", table: "Suspension", bundle: .module), String(localized: "Reason", table: "Suspension", bundle: .module), String(localized: "Since", table: "Suspension", bundle: .module), ], rows: displays.sorted { ($0.display, $0.reason) < ($1.display, $1.reason) }.map { [ $0.display, $0.detail, Date(timeIntervalSince1970: $0.sinceUnixSeconds).formatted(date: .numeric, time: .standard), ] }) }
private static func applications(_ applications: [IPCDiagnostics.Application]) -> String { guard !applications.isEmpty else { return String(localized: "No application measurements.", bundle: .module) + "\n" } return CLIOutput.table( headers: [ String(localized: "PID", bundle: .module), String(localized: "Application", bundle: .module), String(localized: "Slow", bundle: .module), String(localized: "Wedged", bundle: .module), String(localized: "Samples", bundle: .module), String(localized: "Retained", bundle: .module), String(localized: "p50 (ms)", bundle: .module), String(localized: "p95 (ms)", bundle: .module), ], rows: applications.sorted { $0.pid < $1.pid }.map { [ String($0.pid), $0.bundleID ?? String(localized: "—", bundle: .module), flag($0.slow), flag($0.wedged), String($0.sampleCount), String($0.retainedSampleCount), milliseconds($0.p50Milliseconds), milliseconds($0.p95Milliseconds), ] }) }
private static func milliseconds(_ value: Double?) -> String { guard let value else { return String(localized: "—", bundle: .module) } return value.formatted(.number.precision(.fractionLength(3))) }
private static func flag(_ value: Bool) -> String { value ? String(localized: "Yes", bundle: .module) : String(localized: "No", bundle: .module) }
private static func permission(_ state: IPCDiagnostics.PermissionState) -> String { switch state { case .granted: String(localized: "Granted", bundle: .module) case .notGranted: String(localized: "Not granted", bundle: .module) case .staleGrant: String(localized: "Stale grant", bundle: .module) } }
private static func verification(_ state: IPCDiagnostics.Verification) -> String { switch state { case .unverified: String(localized: "Unverified", bundle: .module) case .verified: String(localized: "Verified", bundle: .module) case .failed: String(localized: "Failed", bundle: .module) } }}