diff options
Diffstat (limited to 'menubar/model.swift')
| -rw-r--r-- | menubar/model.swift | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/menubar/model.swift b/menubar/model.swift index e60cb84..67a693a 100644 --- a/menubar/model.swift +++ b/menubar/model.swift @@ -126,12 +126,38 @@ func run(_ path: String, _ args: [String]) -> String? { return String(data: data, encoding: .utf8) } +/// Like run(), but reports why a command failed: exit status and stderr. +func runReporting(_ path: String, _ args: [String]) -> (ok: Bool, output: String) { + let process = Process() + process.executableURL = URL(fileURLWithPath: path) + process.arguments = args + let out = Pipe(), err = Pipe() + process.standardOutput = out + process.standardError = err + do { + try process.run() + } catch { + return (false, "cannot run \(path): \(error.localizedDescription)") + } + let stdout = String(data: out.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? "" + let stderr = String(data: err.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? "" + process.waitUntilExit() + let text = (stdout + stderr).trimmingCharacters(in: .whitespacesAndNewlines) + return (process.terminationStatus == 0, text) +} + +/// The tmux binary: launchd gives apps a PATH without Homebrew, so look in +/// the usual places. +let tmuxBinary: String = environment["DINGGER_TMUX"] + ?? ["/opt/homebrew/bin/tmux", "/usr/local/bin/tmux", "/usr/bin/tmux"] + .first { FileManager.default.isExecutableFile(atPath: $0) } ?? "/opt/homebrew/bin/tmux" + 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) + return run(tmuxBinary, all + args) } let dinggerBinary = environment["DINGGER_BIN"] ?? (NSHomeDirectory() + "/.local/bin/dingger") @@ -162,7 +188,8 @@ func fetchRows() -> [Row] { /// 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]) + let result = runReporting(dinggerBinary, ["focus", "--pane", paneId]) + if !result.ok { log("jump: dingger focus --pane \(paneId) failed: \(result.output)") } } // MARK: rendering |
