// dingger menu bar: herdr's agent panel as a macOS status item. // // Reads the pane options the dingger daemon writes (@agent, @agent_state, // @dingger_since) and renders herdr's Symbols style with herdr's palette: // // blocked × red done ✓ teal working ◐ yellow idle ○ green // // in herdr's priority order (blocked > done > working > idle), oldest // waiting first. Clicking a row runs `dingger focus --pane`, the same tested // code path as the tmux status-line click. // // Debug flags (used by tests/e2e.sh): // --print print the title and rows the menu would show, then exit // --click perform a row click for , then exit import AppKit // MARK: herdr's palette (catppuccin mocha on dark, latte on light) func herdrColor(dark: (CGFloat, CGFloat, CGFloat), light: (CGFloat, CGFloat, CGFloat)) -> NSColor { NSColor(name: nil) { appearance in let isDark = appearance.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua let (r, g, b) = isDark ? dark : light return NSColor(srgbRed: r / 255, green: g / 255, blue: b / 255, alpha: 1) } } let herdrRed = herdrColor(dark: (243, 139, 168), light: (210, 15, 57)) let herdrTeal = herdrColor(dark: (148, 226, 213), light: (23, 146, 153)) let herdrYellow = herdrColor(dark: (249, 226, 175), light: (223, 142, 29)) let herdrGreen = herdrColor(dark: (166, 227, 161), light: (64, 160, 43)) let herdrGray = herdrColor(dark: (108, 112, 134), light: (156, 160, 176)) // MARK: herdr's status vocabulary enum Status: String { case blocked, done, working, idle, unknown var symbol: String { switch self { case .blocked: return "×" case .done: return "✓" case .working: return "◐" case .idle: return "○" case .unknown: return "·" } } var color: NSColor { switch self { case .blocked: return herdrRed case .done: return herdrTeal case .working: return herdrYellow case .idle: return herdrGreen case .unknown: return herdrGray } } var priority: Int { switch self { case .blocked: return 4 case .done: return 3 case .working: return 2 case .idle: return 1 case .unknown: return 0 } } } struct Row { let paneId: String let agent: String let status: Status let target: String let since: Int } // MARK: tmux and dingger access let environment = ProcessInfo.processInfo.environment func run(_ path: String, _ args: [String]) -> String? { let process = Process() process.executableURL = URL(fileURLWithPath: path) 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) } func tmux(_ args: [String]) -> String? { var all = ["-u"] if let socket = environment["DINGGER_TMUX_SOCKET"] { all += ["-L", socket] } return run(environment["DINGGER_TMUX"] ?? "/opt/homebrew/bin/tmux", all + args) } let dinggerBinary = environment["DINGGER_BIN"] ?? (NSHomeDirectory() + "/.local/bin/dingger") /// Every agent pane, in herdr's priority order, oldest waiting first. func fetchRows() -> [Row] { let format = [ "#{pane_id}", "#{@agent}", "#{@agent_state}", "#{@dingger_since}", "#{session_name}:#{window_name}", "#{window_panes}", "#{pane_index}", ].joined(separator: "\t") guard let out = tmux(["list-panes", "-a", "-F", format]) else { return [] } let rows: [Row] = out.split(separator: "\n").compactMap { line in let f = line.split(separator: "\t", omittingEmptySubsequences: false).map(String.init) guard f.count >= 7, !f[1].isEmpty else { return nil } let status = Status(rawValue: f[2]) ?? .unknown guard status != .unknown else { return nil } let target = (Int(f[5]) ?? 1) > 1 ? "\(f[4]).\(f[6])" : f[4] return Row(paneId: f[0], agent: f[1], status: status, target: target, since: Int(f[3]) ?? Int.max) } return rows.sorted { if $0.status.priority != $1.status.priority { return $0.status.priority > $1.status.priority } if $0.since != $1.since { return $0.since < $1.since } return $0.target < $1.target } } /// Show the pane and mark it attended; the same code path as the tmux /// status-line click. func focus(_ paneId: String) { _ = run(dinggerBinary, ["focus", "--pane", paneId]) } // MARK: rendering /// herdr-style summary: `×1 ✓3 ◐2` in priority order; `○` when every agent /// is idle; `·` when there are none. func title(for rows: [Row]) -> NSAttributedString { let font = NSFont.monospacedDigitSystemFont(ofSize: 13, weight: .medium) let text = NSMutableAttributedString() for status in [Status.blocked, .done, .working] { let count = rows.filter { $0.status == status }.count guard count > 0 else { continue } if text.length > 0 { text.append(NSAttributedString(string: " ", attributes: [.font: font])) } text.append(NSAttributedString( string: "\(status.symbol)\(count)", attributes: [.font: font, .foregroundColor: status.color])) } if text.length == 0 { let status: Status = rows.isEmpty ? .unknown : .idle text.append(NSAttributedString( string: status.symbol, attributes: [.font: font, .foregroundColor: status.color])) } return text } func plainTitle(for rows: [Row]) -> String { title(for: rows).string } func rowTitle(_ row: Row, tabStop: CGFloat) -> NSAttributedString { let font = NSFont.menuFont(ofSize: 0) let mono = NSFont.monospacedSystemFont(ofSize: font.pointSize - 1, weight: .regular) let paragraph = NSMutableParagraphStyle() paragraph.tabStops = [NSTextTab(textAlignment: .right, location: tabStop)] let text = NSMutableAttributedString() text.append(NSAttributedString( string: "\(row.status.symbol) ", attributes: [.font: mono, .foregroundColor: row.status.color, .paragraphStyle: paragraph])) text.append(NSAttributedString( string: row.agent, attributes: [.font: font, .paragraphStyle: paragraph])) text.append(NSAttributedString( string: "\t\(row.target)", attributes: [.font: mono, .foregroundColor: NSColor.secondaryLabelColor, .paragraphStyle: paragraph])) return text } // MARK: app final class Menubar: NSObject, NSApplicationDelegate, NSMenuDelegate { private let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) private let menu = NSMenu() func applicationDidFinishLaunching(_: Notification) { menu.delegate = self item.menu = menu refreshTitle() let timer = Timer(timeInterval: 1.0, repeats: true) { [weak self] _ in self?.refreshTitle() } RunLoop.main.add(timer, forMode: .common) } private func refreshTitle() { let rows = fetchRows() item.button?.attributedTitle = title(for: rows) var tip: [String] = [] for status in [Status.blocked, .done, .working, .idle] { let count = rows.filter { $0.status == status }.count if count > 0 { tip.append("\(count) \(status.rawValue)") } } let tipText = tip.isEmpty ? "No agents" : tip.joined(separator: " · ") item.button?.toolTip = tipText item.button?.setAccessibilityLabel("dingger, " + tipText) } // Rebuilt from fresh data every time the menu opens. func menuNeedsUpdate(_ menu: NSMenu) { menu.removeAllItems() let rows = fetchRows() if rows.isEmpty { let none = NSMenuItem(title: "No agents", action: nil, keyEquivalent: "") none.isEnabled = false menu.addItem(none) } let font = NSFont.menuFont(ofSize: 0) let mono = NSFont.monospacedSystemFont(ofSize: font.pointSize - 1, weight: .regular) let widest = rows.map { row -> CGFloat in ("\(row.status.symbol) " as NSString).size(withAttributes: [.font: mono]).width + (row.agent as NSString).size(withAttributes: [.font: font]).width + (row.target as NSString).size(withAttributes: [.font: mono]).width }.max() ?? 0 let tabStop = ceil(widest + 40) for row in rows { let entry = NSMenuItem(title: row.agent, action: #selector(rowClicked(_:)), keyEquivalent: "") entry.target = self entry.attributedTitle = rowTitle(row, tabStop: tabStop) entry.toolTip = "\(row.status.rawValue) · \(row.target)" entry.representedObject = row.paneId menu.addItem(entry) } menu.addItem(.separator()) menu.addItem(NSMenuItem(title: "Quit dingger", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")) } @objc private func rowClicked(_ sender: NSMenuItem) { guard let paneId = sender.representedObject as? String else { return } DispatchQueue.global().async { focus(paneId) DispatchQueue.main.async { self.refreshTitle() } } } } let arguments = CommandLine.arguments if arguments.contains("--print") { let rows = fetchRows() print("title: \(plainTitle(for: rows))") for row in rows { print("\(row.status.symbol) \(row.status.rawValue) \(row.agent) \(row.target)") } exit(0) } if let index = arguments.firstIndex(of: "--click"), index + 1 < arguments.count { focus(arguments[index + 1]) exit(0) } let app = NSApplication.shared let delegate = Menubar() app.delegate = delegate app.setActivationPolicy(.accessory) app.run()