// dingger menu bar: live agent states from tmux pane user options // (@agent / @agent_state), written by the dingger daemon. // // Visual language is herdr's agent panel: green = ready (finished, waiting // for you), yellow = working, red = blocked; filled glyph = in the ready // queue (unseen), outline = already seen. Priority order = queue order. import AppKit struct PaneRow { let paneId: String let target: String let agent: String let state: String } let red = NSColor.systemRed let yellow = NSColor.systemYellow let green = NSColor.systemGreen let dim = NSColor.systemGray /// Tint a symbol image (non-template, so it keeps its color in menus). func tinted(_ image: NSImage?, _ color: NSColor) -> NSImage? { guard let image = image else { return nil } let img = NSImage(size: image.size, flipped: false) { rect in image.draw(in: rect) color.set() rect.fill(using: .sourceAtop) return true } img.isTemplate = false return img } func symbol(_ name: String, _ color: NSColor? = nil) -> NSImage? { let image = NSImage(systemSymbolName: name, accessibilityDescription: nil)? .withSymbolConfiguration(.init(pointSize: 13, weight: .regular)) return color.map { tinted(image, $0) } ?? image } func rowSymbol(_ state: String, queued: Bool) -> NSImage? { switch (state, queued) { case ("blocked", true): return symbol("exclamationmark.circle.fill", red) case ("idle", true): return symbol("checkmark.circle.fill", green) case ("working", _): return symbol("circle.dotted", yellow) case ("blocked", _): return symbol("exclamationmark.circle", dim) default: return symbol("checkmark.circle", dim) } } final class Menubar: NSObject, NSApplicationDelegate, NSMenuDelegate { private let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) private let menu = NSMenu() private var panes: [PaneRow] = [] private var queue: [String] = [] // pane ids, oldest waiting first func applicationDidFinishLaunching(_: Notification) { NSApp.setActivationPolicy(.accessory) // no Dock icon item.button?.image = symbol("bell", dim) item.button?.font = NSFont.monospacedDigitSystemFont(ofSize: 13, weight: .regular) item.menu = menu menu.delegate = self let timer = Timer(timeInterval: 2.0, repeats: true) { [weak self] _ in self?.refreshTitle() } RunLoop.main.add(timer, forMode: .common) refreshTitle() } // MARK: data private func runTmux(_ args: [String]) -> String? { let process = Process() process.executableURL = URL(fileURLWithPath: "/opt/homebrew/bin/tmux") process.arguments = args let pipe = Pipe() process.standardOutput = pipe process.standardError = FileHandle.nullDevice do { try process.run() } catch { return nil } let data = pipe.fileHandleForReading.readDataToEndOfFile() process.waitUntilExit() guard process.terminationStatus == 0 else { return nil } return String(data: data, encoding: .utf8) } private func fetchPanes() -> [PaneRow] { let fmt = "#{pane_id}\t#{@agent}\t#{@agent_state}\t#{@dingger_ack}\t#{session_name}\t#{window_name}\t#{window_panes}\t#{pane_index}" guard let out = runTmux(["-u", "list-panes", "-a", "-F", fmt]) else { return [] } return out.split(separator: "\n").compactMap { line in let fields = line.split(separator: "\t", omittingEmptySubsequences: false) guard fields.count >= 8 else { return nil } let agent = String(fields[1]) let state = fields[2].isEmpty ? "unknown" : String(fields[2]) // Only tracked panes with a live state belong in the menu. guard !agent.isEmpty, ["idle", "working", "blocked"].contains(state) else { return nil } // Display target: session:window-name, with the pane index only // when the window is split. (Switching uses the pane id.) var target = "\(fields[4]):\(fields[5])" if Int(fields[6]) ?? 1 > 1 { target += ".\(fields[7])" } return PaneRow(paneId: String(fields[0]), target: target, agent: agent, state: state) } } private func fetchQueue() -> [String] { guard let out = runTmux(["-u", "show-option", "-gv", "@dingger_queue"]) else { return [] } return out.trimmingCharacters(in: .whitespacesAndNewlines) .split(separator: ",").map { $0.split(separator: ":").first.map(String.init) ?? "" } } // MARK: title (timer-driven; never touches the open menu) private func refreshTitle() { panes = fetchPanes() queue = fetchQueue() FileHandle.standardError.write(Data( "\(Date()) menubar: panes=\(panes.count) queue=\(queue.count)\n".utf8)) let queued = panes.filter { row in queue.contains(row.paneId) } let blocked = queued.filter { $0.state == "blocked" }.count let working = panes.filter { $0.state == "working" }.count let (name, color, count): (String, NSColor, String) if blocked > 0 { (name, color, count) = ("bell.fill", red, String(queued.count)) } else if !queued.isEmpty { (name, color, count) = ("bell.fill", green, String(queued.count)) } else if working > 0 { (name, color, count) = ("bell", yellow, "") } else { (name, color, count) = ("bell", dim, "") } item.button?.image = symbol(name, color) item.button?.title = count var tip: [String] = [] if blocked > 0 { tip.append("\(blocked) blocked") } if queued.count - blocked > 0 { tip.append("\(queued.count - blocked) waiting") } if working > 0 { tip.append("\(working) working") } let tipText = tip.isEmpty ? "Nothing waiting" : tip.joined(separator: " · ") item.button?.toolTip = tipText item.button?.setAccessibilityLabel("dingger, " + tipText) } // MARK: menu (rebuilt when opened, so rows don't reshuffle under the cursor) func menuNeedsUpdate(_ menu: NSMenu) { rebuildMenu() } private func rebuildMenu() { menu.removeAllItems() // Queue order (oldest waiting first), then the rest by target. var ordered: [PaneRow] = [] for paneId in queue { if let row = panes.first(where: { $0.paneId == paneId && $0.state != "working" }) { ordered.append(row) } } let orderedIds = ordered.map { $0.paneId } let rest = panes .filter { !orderedIds.contains($0.paneId) } .sorted { $0.target < $1.target } ordered.append(contentsOf: rest) if panes.isEmpty { let none = NSMenuItem(title: "No agents", action: nil, keyEquivalent: "") none.isEnabled = false menu.addItem(none) } var previousWaiting: Bool? for pane in ordered { let queued = queue.contains(pane.paneId) let waiting = pane.state == "idle" || pane.state == "blocked" // One separator between waiting agents and busy ones. if let previous = previousWaiting, previous && !waiting { menu.addItem(.separator()) } previousWaiting = waiting let row = NSMenuItem(title: "\(pane.agent)\t\(pane.target)", action: #selector(paneClicked(_:)), keyEquivalent: "") row.target = self row.image = rowSymbol(pane.state, queued: queued) row.attributedTitle = attributedRow(agent: pane.agent, target: pane.target) row.toolTip = "\(pane.state) — \(pane.target)" row.representedObject = pane.paneId menu.addItem(row) } menu.addItem(.separator()) let quit = NSMenuItem(title: "Quit dingger", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q") menu.addItem(quit) } /// Agent left, tmux target right-aligned at a computed tab stop — like a /// native menu's key-equivalent column. private func attributedRow(agent: String, target: String) -> NSAttributedString { let menuFont = NSFont.menuFont(ofSize: 0) let monoFont = NSFont.monospacedSystemFont( ofSize: menuFont.pointSize - 1, weight: .regular) let agentWidth = (agent as NSString).size(withAttributes: [.font: menuFont]).width let targetWidth = (target as NSString).size(withAttributes: [.font: monoFont]).width let tabStop = ceil(agentWidth + 40 + targetWidth) let paragraph = NSMutableParagraphStyle() paragraph.tabStops = [NSTextTab(textAlignment: .right, location: tabStop)] let text = NSMutableAttributedString() text.append(NSAttributedString( string: agent, attributes: [.font: menuFont, .paragraphStyle: paragraph])) text.append(NSAttributedString(string: "\t")) text.append(NSAttributedString( string: target, attributes: [.font: monoFont, .paragraphStyle: paragraph])) return text } // MARK: actions func printPanesForDebug() { let rows = fetchPanes() let queue = fetchQueue() if rows.isEmpty { print("(no agents)") } for pane in rows { let queued = queue.contains(pane.paneId) print("\(emojilessState(pane.state, queued: queued)) \(pane.agent) \(pane.target)") } } private func emojilessState(_ state: String, queued: Bool) -> String { switch (state, queued) { case ("blocked", true): return "[blocked!]" case ("idle", true): return "[waiting]" case ("working", _): return "[working]" case ("blocked", _): return "[blocked]" default: return "[idle]" } } @objc private func paneClicked(_ sender: NSMenuItem) { guard let paneId = sender.representedObject as? String else { return } // Ack like `dingger focus` does, so both counts drop together. _ = runTmux(["-u", "set-option", "-p", "-t", paneId, "@dingger_ack", "1"]) let target = panes.first { $0.paneId == paneId }?.target ?? "" guard !target.isEmpty else { return } _ = runTmux(["-u", "switch-client", "-t", target]) for bundle in ["com.mitchellh.ghostty", "org.alacritty", "com.apple.Terminal"] { if let app = NSRunningApplication.runningApplications(withBundleIdentifier: bundle).first { app.activate() break } } } } let app = NSApplication.shared let delegate = Menubar() app.delegate = delegate app.setActivationPolicy(.accessory) // Debug: print the rows the menu would show and exit. if CommandLine.arguments.contains("--print") { delegate.printPanesForDebug() exit(0) } app.run()