From 9d9aeb87a97c931d10121b6dcaf1c1947e4fd032 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Kwas=CC=81niewski?= Date: Sun, 1 Sep 2024 17:15:36 +0200 Subject: [PATCH] feat: add wezterm support --- MiniSim/Service/DeviceService.swift | 10 ++--- MiniSim/Service/Terminal/Terminal.swift | 18 ++++++-- MiniSim/Service/Terminal/TerminalApps.swift | 49 +++++++++++++++++++-- MiniSim/Views/Preferences.swift | 36 ++++++--------- 4 files changed, 78 insertions(+), 35 deletions(-) diff --git a/MiniSim/Service/DeviceService.swift b/MiniSim/Service/DeviceService.swift index 8b97e15..be4945f 100644 --- a/MiniSim/Service/DeviceService.swift +++ b/MiniSim/Service/DeviceService.swift @@ -412,12 +412,10 @@ extension DeviceService { guard let deviceId = device.identifier else { throw DeviceError.deviceNotFound } - guard let preferedTerminal = Terminal( - rawValue: UserDefaults.standard.preferedTerminal ?? Terminal.terminal.rawValue - ) - else { return } - let terminal = TerminalService.getTerminal(type: preferedTerminal) - try TerminalService.launchTerminal(terminal: terminal, deviceId: deviceId) + + guard let adbPath = try? ADB.getAdbPath() else { return } + let logcatCommand = "\(adbPath) -s \(deviceId) logcat -v color" + try TerminalService.launchTerminal(command: logcatCommand) } static func handleAndroidAction(device: Device, commandTag: SubMenuItems.Tags, itemName: String) { diff --git a/MiniSim/Service/Terminal/Terminal.swift b/MiniSim/Service/Terminal/Terminal.swift index d464dd6..34318ad 100644 --- a/MiniSim/Service/Terminal/Terminal.swift +++ b/MiniSim/Service/Terminal/Terminal.swift @@ -10,7 +10,7 @@ import ShellOut protocol TerminalServiceProtocol { static func getTerminal(type: Terminal) -> TerminalApp - static func launchTerminal(terminal: TerminalApp, deviceId: String) throws + static func launchTerminal(command: String, terminal: TerminalApp) throws } class TerminalService: TerminalServiceProtocol { @@ -20,12 +20,22 @@ class TerminalService: TerminalServiceProtocol { return AppleTerminal() case .iterm: return ITermTerminal() + case .wezterm: + return WezTermTerminal() } } - static func launchTerminal(terminal: TerminalApp, deviceId: String) throws { - let logcatCommand = "adb -s \(deviceId) logcat -v color" - let terminalScript = terminal.getLaunchScript(deviceId: deviceId, logcatCommand: logcatCommand) + private static func getPrefferedTerminal() -> TerminalApp { + guard let preferedTerminal = Terminal( + rawValue: UserDefaults.standard.preferedTerminal ?? Terminal.terminal.rawValue + ) + else { return getTerminal(type: Terminal.terminal) } + + return getTerminal(type: preferedTerminal) + } + + static func launchTerminal(command: String, terminal: TerminalApp = getPrefferedTerminal()) throws { + let terminalScript = terminal.getLaunchScript(logcatCommand: command) try shellOut(to: "osascript -e '\(terminalScript)'") } } diff --git a/MiniSim/Service/Terminal/TerminalApps.swift b/MiniSim/Service/Terminal/TerminalApps.swift index c15d692..a5eb6fe 100644 --- a/MiniSim/Service/Terminal/TerminalApps.swift +++ b/MiniSim/Service/Terminal/TerminalApps.swift @@ -5,21 +5,51 @@ // Created by Gokulakrishnan Subramaniyan on 02/12/23. // +import AppKit import Foundation protocol TerminalApp { + var bundleIdentifier: String { get } var name: String { get } - func getLaunchScript(deviceId: String, logcatCommand: String) -> String + func getLaunchScript(logcatCommand: String) -> String } enum Terminal: String, CaseIterable { case terminal = "Terminal" case iterm = "iTerm" + case wezterm = "WezTerm" + + var bundleIdentifier: String { + switch self { + case .terminal: + return "com.apple.Terminal" + case .iterm: + return "com.googlecode.iterm2" + case .wezterm: + return "com.github.wez.wezterm" + } + } + + func getApplicationURL() -> URL? { + NSWorkspace.shared.urlForApplication(withBundleIdentifier: bundleIdentifier) + } + + func isAvailable() -> Bool { + getApplicationURL() != nil + } + + func getAppIcon() -> NSImage? { + let appIcon = NSWorkspace.shared.icon(forFile: getApplicationURL()?.path ?? "") + appIcon.size = NSSize(width: 18, height: 18) + return appIcon + } } struct AppleTerminal: TerminalApp { var name: String = "Terminal" - func getLaunchScript(deviceId: String, logcatCommand: String) -> String { + var bundleIdentifier: String = "com.apple.Terminal" + + func getLaunchScript(logcatCommand: String) -> String { """ tell app \"Terminal\" activate @@ -31,8 +61,9 @@ struct AppleTerminal: TerminalApp { struct ITermTerminal: TerminalApp { var name: String = "iTerm" + var bundleIdentifier: String = "com.googlecode.iterm2" - func getLaunchScript(deviceId: String, logcatCommand: String) -> String { + func getLaunchScript(logcatCommand: String) -> String { """ tell app \"iTerm\" set newWindow to (create window with default profile) @@ -43,3 +74,15 @@ struct ITermTerminal: TerminalApp { """ } } + +struct WezTermTerminal: TerminalApp { + var name: String = "WezTerm" + var bundleIdentifier: String = "com.github.wez.wezterm" + + func getLaunchScript(logcatCommand: String) -> String { + """ + tell application \"wezterm\" to activate + do shell script \"/Applications/WezTerm.app/Contents/MacOS/wezterm cli spawn \(logcatCommand)\" + """ + } +} diff --git a/MiniSim/Views/Preferences.swift b/MiniSim/Views/Preferences.swift index 2dfe37a..0e275ce 100644 --- a/MiniSim/Views/Preferences.swift +++ b/MiniSim/Views/Preferences.swift @@ -41,19 +41,23 @@ struct Preferences: View { .descriptionText() } Settings.Section(title: "Preferred Terminal:") { - Picker("", selection: $preferedTerminal) { - let availableTerminal = Terminal.allCases.filter { checkAppIsInstalled(appName: $0) } + Picker("", selection: $preferedTerminal) { + let availableTerminal = Terminal.allCases.filter { $0.isAvailable() } ForEach(availableTerminal, id: \.self) { terminal in + HStack { + Image(nsImage: terminal.getAppIcon() ?? NSImage()) Text(terminal.rawValue) + } + .tag(terminal.rawValue) } - } - .onChange(of: preferedTerminal) { _ in - UserDefaults.standard.setValue( - preferedTerminal.rawValue, forKey: UserDefaults.Keys.preferedTerminal - ) - } - Text("Users can choose their preferred terminal from the above supported terminal list") - .descriptionText() + } + .onChange(of: preferedTerminal) { _ in + UserDefaults.standard.setValue( + preferedTerminal.rawValue, forKey: UserDefaults.Keys.preferedTerminal + ) + } + Text("Users can choose their preferred terminal from the above supported terminal list") + .descriptionText() } Settings.Section(title: "Hotkey:") { KeyboardShortcuts.Recorder("", name: .toggleMiniSim) @@ -82,18 +86,6 @@ struct Preferences: View { } } - func checkAppIsInstalled(appName: Terminal) -> Bool { - if appName.rawValue == Terminal.terminal.rawValue { - return true - } - let command = "ls /Applications/ | grep -i \(appName.rawValue)" - do { - var _ = try shellOut(to: "\(command)") - return true - } catch { - return false - } - } func resetDefaults() { let shouldReset = NSAlert.showQuestionDialog( title: "Are you sure?",