From d1eb4b93a68a655415ffd095a52db2693d6ade30 Mon Sep 17 00:00:00 2001 From: Eric Dahl Date: Mon, 16 Nov 2026 00:13:24 -0500 Subject: [PATCH 1/2] Diagnostics: a tab's lines as plain text, and a file name to save them Refs #162 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VCgWWsdAAQXJWYmYEXyTy7 --- .../DiagnosticsPresentation.swift | 11 +++++++ .../DiagnosticsPresentationTests.swift | 33 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/Packages/ControllerBridgeKit/Sources/ControllerBridgePresentation/DiagnosticsPresentation.swift b/Packages/ControllerBridgeKit/Sources/ControllerBridgePresentation/DiagnosticsPresentation.swift index 5563c80..e0a7431 100644 --- a/Packages/ControllerBridgeKit/Sources/ControllerBridgePresentation/DiagnosticsPresentation.swift +++ b/Packages/ControllerBridgeKit/Sources/ControllerBridgePresentation/DiagnosticsPresentation.swift @@ -5,6 +5,11 @@ public enum DiagnosticsTab: String, CaseIterable, Sendable { case osc = "OSC Log" /// The line shown in an empty list, so "nothing yet" never looks like "broken". + /// What Save… suggests (#162), such as "DeckBridge OSC Log.txt". The app passes its display name. + public func exportFileName(appName: String) -> String { + "\(appName) \(rawValue).txt" + } + public func emptyText(controllerConnected: Bool) -> String { switch self { case .monitor: controllerConnected ? "Press any button on the controller." : "No controller connected." @@ -91,6 +96,12 @@ public struct DiagnosticsLines: Equatable, Sendable { return Array((list + progressAndTime).sorted { $0.id < $1.id }.suffix(Self.limit)) } + /// What a tab shows, as plain text to copy or save (#162): one line each, ending in a newline. UNMAPPED keeps its + /// words; the dot is only how the window draws it. + public func plainText(_ tab: DiagnosticsTab) -> String { + shown(tab).map { $0.text + "\n" }.joined() + } + /// "OSC <- /Syndicate/Deck/1/Progress 0.25" or ".../Time 61": the address's last part, before the arguments. private static func isProgressOrTime(_ text: String) -> Bool { guard text.hasPrefix("OSC <- ") else { return false } diff --git a/Packages/ControllerBridgeKit/Tests/ControllerBridgePresentationTests/DiagnosticsPresentationTests.swift b/Packages/ControllerBridgeKit/Tests/ControllerBridgePresentationTests/DiagnosticsPresentationTests.swift index 649141e..55a620e 100644 --- a/Packages/ControllerBridgeKit/Tests/ControllerBridgePresentationTests/DiagnosticsPresentationTests.swift +++ b/Packages/ControllerBridgeKit/Tests/ControllerBridgePresentationTests/DiagnosticsPresentationTests.swift @@ -72,4 +72,37 @@ struct DiagnosticsPresentationTests { #expect(DiagnosticsTab.hiddenNotesHint([52, 53]) == "Notes 52 and 53, which the controller sends on its own, are hidden.") #expect(DiagnosticsTab.hiddenNotesHint([]) == nil) } + + // #162: the visible tab, as plain text to copy or save. + + @Test("A tab's plain text is its shown lines, oldest first, each ending in a newline; UNMAPPED keeps its words only") + func plainText() { + var lines = DiagnosticsLines() + lines.append(to: .monitor, message: "12:00:01.000 1 Note On 1 127 ", label: "play") + lines.append(to: .monitor, message: "12:00:02.000 1 Note On 99 127 ", label: "UNMAPPED", isUnmapped: true) + lines.append(to: .osc, message: "OSC -> /Syndicate/Deck/1/PlayPause 1") + #expect(lines.plainText(.monitor) == "12:00:01.000 1 Note On 1 127 play\n12:00:02.000 1 Note On 99 127 UNMAPPED\n") + #expect(lines.plainText(.sweep) == "") + } + + @Test("The OSC tab's plain text follows the Progress and Time filter, both ways") + func plainTextFollowsFilter() { + var lines = DiagnosticsLines() + lines.append(to: .osc, message: "OSC <- /Syndicate/Deck/1/Playing 1") + lines.append(to: .osc, message: "OSC <- /Syndicate/Deck/1/Progress 0.25") + lines.append(to: .osc, message: "OSC -> /Syndicate/Deck/1/PlayPause 1") + #expect(lines.plainText(.osc) == "OSC <- /Syndicate/Deck/1/Playing 1\nOSC -> /Syndicate/Deck/1/PlayPause 1\n") + lines.hideProgressAndTime = false + #expect(lines.plainText(.osc) + == "OSC <- /Syndicate/Deck/1/Playing 1\nOSC <- /Syndicate/Deck/1/Progress 0.25\nOSC -> /Syndicate/Deck/1/PlayPause 1\n") + } + + @Test("Save… suggests the app's name and the tab's, as a .txt file", arguments: [ + (DiagnosticsTab.monitor, "DeckBridge MIDI Monitor.txt"), + (.sweep, "DeckBridge LED Sweep.txt"), + (.osc, "DeckBridge OSC Log.txt"), + ]) + func exportFileName(tab: DiagnosticsTab, name: String) { + #expect(tab.exportFileName(appName: "DeckBridge") == name) + } } From 2edec4d35117ee142e8102a30faf9643e9fb4f10 Mon Sep 17 00:00:00 2001 From: Eric Dahl Date: Mon, 16 Nov 2026 00:14:17 -0500 Subject: [PATCH 2/2] =?UTF-8?q?Diagnostics:=20Copy=20log,=20Save=E2=80=A6,?= =?UTF-8?q?=20and=20selection=20across=20lines?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Each tab gets Copy log (⇧⌘C) and Save… (⌘S), writing the tab's plain text with its filter. The list is one selectable text, so a drag or ⌘A selects across lines and ⌘C copies them. It refreshes at most 10 times a second, so OSC floods during playback don't redraw it per message. Closes #162 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VCgWWsdAAQXJWYmYEXyTy7 --- App/DiagnosticsWindow.swift | 103 ++++++++++++++++++++++++++---------- 1 file changed, 74 insertions(+), 29 deletions(-) diff --git a/App/DiagnosticsWindow.swift b/App/DiagnosticsWindow.swift index 6efecaa..63c00ac 100644 --- a/App/DiagnosticsWindow.swift +++ b/App/DiagnosticsWindow.swift @@ -5,15 +5,16 @@ import ControllerBridgeCore import ControllerBridgePresentation import Observation import SwiftUI +import UniformTypeIdentifiers /// What the Diagnostics window shows. Main-actor; the engine's sink hops here once per event, only while it is open. @MainActor @Observable final class DiagnosticsLog { private(set) var lines = DiagnosticsLines() - var hideProgressAndTime: Bool { - get { lines.hideProgressAndTime } - set { lines.hideProgressAndTime = newValue } + /// Kept apart from `lines`, so the checkbox doesn't redraw the window on every new line (#162). + var hideProgressAndTime = true { + didSet { lines.hideProgressAndTime = hideProgressAndTime } } func append(_ event: DiagnosticEvent) { @@ -28,6 +29,9 @@ final class DiagnosticsLog { func shown(_ tab: DiagnosticsTab) -> [DiagnosticsLines.Line] { lines.shown(tab) } + /// The tab's lines as plain text, to copy or save (#162). + func plainText(_ tab: DiagnosticsTab) -> String { lines.plainText(tab) } + /// Clears the tab you're on; the other lists stay. func clear(_ tab: DiagnosticsTab) { lines.clear(tab) } } @@ -49,6 +53,9 @@ struct DiagnosticsWindow: View { /// Off only for snapshots: ImageRenderer draws no ScrollView content. var scrolls = true @State private var sweepDelay = 0.6 + /// The visible tab's lines, refreshed at most 10 times a second (#162): during playback OSC lines arrive far + /// faster, and redrawing the whole selectable list for each would stutter. Nil until the first refresh. + @State private var visible: [DiagnosticsLines.Line]? var body: some View { let presentation = status.presentation @@ -67,6 +74,14 @@ struct DiagnosticsWindow: View { // Only while this window is open does the bridge build anything for it. .onAppear { setOpen(true) } .onDisappear { setOpen(false) } + .task(id: tab) { + visible = nil + while !Task.isCancelled { + let lines = log.shown(tab) + if lines != visible { visible = lines } + try? await Task.sleep(for: .milliseconds(100)) + } + } } // MARK: Header and tabs @@ -96,6 +111,18 @@ struct DiagnosticsWindow: View { } .overlay(RoundedRectangle(cornerRadius: Theme.corner).strokeBorder(Theme.hairline, lineWidth: 1)) Spacer(minLength: 0) + // Copy and save what the tab shows, with its filter (#162), so it can be shared. + Button("Copy log") { + NSPasteboard.general.clearContents() + NSPasteboard.general.setString(log.plainText(tab), forType: .string) + } + .buttonStyle(LinkButtonStyle()) + .keyboardShortcut("c", modifiers: [.command, .shift]) + .accessibilityLabel("Copy the \(tab.rawValue)") + Button("Save…") { save(tab) } + .buttonStyle(LinkButtonStyle()) + .keyboardShortcut("s", modifiers: .command) + .accessibilityLabel("Save the \(tab.rawValue) to a file") Button("Clear log") { log.clear(tab) } .buttonStyle(LinkButtonStyle()) .keyboardShortcut("k", modifiers: .command) @@ -105,8 +132,23 @@ struct DiagnosticsWindow: View { // MARK: Tabs + /// Saves the tab's plain text through the save panel (#162); the user picks the place, so the sandbox allows it. + private func save(_ tab: Tab) { + let text = log.plainText(tab) + let panel = NSSavePanel() + panel.nameFieldStringValue = tab.exportFileName(appName: AppInfo.displayName) + panel.allowedContentTypes = [.plainText] + guard panel.runModal() == .OK, let url = panel.url else { return } + do { + try text.write(to: url, atomically: true, encoding: .utf8) + } catch { + NSAlert(error: error).runModal() + } + } + @ViewBuilder private var content: some View { - let lines = log.shown(tab) + // Snapshots draw once, with no task running, so they read the log directly. + let lines = scrolls ? (visible ?? log.shown(tab)) : log.shown(tab) let connected = status.controller?.connected == true switch tab { case .monitor: @@ -175,40 +217,43 @@ private struct LineList: View { .frame(maxWidth: .infinity, maxHeight: .infinity) } else if scrolls { ScrollViewReader { proxy in - ScrollView { rows(lazy: true) } - .onChange(of: lines.last?.id) { _, last in - if let last { proxy.scrollTo(last, anchor: .bottom) } - } + ScrollView { rows } + .onChange(of: lines.last?.id) { _, _ in proxy.scrollTo(Self.bottom, anchor: .bottom) } } } else { - rows(lazy: false).frame(maxHeight: .infinity, alignment: .top) + rows.frame(maxHeight: .infinity, alignment: .top) } } .background(Theme.panel) .clipShape(RoundedRectangle(cornerRadius: Theme.corner)) } - @ViewBuilder private func rows(lazy: Bool) -> some View { - let content = ForEach(lines) { line in - row(line) - .font(Theme.data(Theme.Size.label)) - .textSelection(.enabled) - .frame(maxWidth: .infinity, alignment: .leading) - .padding(.vertical, Theme.Space.xs) - .overlay(alignment: .bottom) { Theme.hairline.frame(height: 1) } - .id(line.id) - } - if lazy { - LazyVStack(alignment: .leading, spacing: 0) { content }.padding(.horizontal, Theme.Space.m) - } else { - VStack(alignment: .leading, spacing: 0) { content }.padding(.horizontal, Theme.Space.m) - } + /// Every line in one selectable text (#162), so a drag or ⌘A selects across lines and ⌘C copies them. + private var rows: some View { + Text(Self.attributed(lines)) + .font(Theme.data(Theme.Size.label)) + .lineSpacing(Theme.Space.xs) + .textSelection(.enabled) + .frame(maxWidth: .infinity, alignment: .leading) + .padding(.horizontal, Theme.Space.m) + .padding(.vertical, Theme.Space.xs) + .id(Self.bottom) } - /// The line's words are exactly the Python line. UNMAPPED also gets a dot and the attention color, never color alone. - private func row(_ line: DiagnosticsLines.Line) -> Text { - let message = Text(line.message).foregroundStyle(Theme.text) - guard line.isUnmapped else { return message + Text(line.label).foregroundStyle(Theme.text) } - return message + Text("● ").foregroundStyle(Theme.attention) + Text(line.label).foregroundStyle(Theme.attention) + static let bottom = "bottom" + + /// The lines, one per line, in the text color; UNMAPPED adds a dot and the attention color, never color alone. + static func attributed(_ lines: [DiagnosticsLines.Line]) -> AttributedString { + var out = AttributedString() + for (index, line) in lines.enumerated() { + var message = AttributedString(line.message) + message.foregroundColor = Theme.text + out += message + var label = AttributedString((line.isUnmapped ? "● " : "") + line.label) + label.foregroundColor = line.isUnmapped ? Theme.attention : Theme.text + out += label + if index < lines.count - 1 { out += AttributedString("\n") } + } + return out } }