Skip to content

Commit

Permalink
feat: add wezterm support
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski committed Sep 1, 2024
1 parent 736d6d8 commit 9d9aeb8
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 35 deletions.
10 changes: 4 additions & 6 deletions MiniSim/Service/DeviceService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Check warning on line 421 in MiniSim/Service/DeviceService.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Cyclomatic Complexity Violation: Function should have complexity 10 or less; currently complexity is 15 (cyclomatic_complexity)

Check warning on line 421 in MiniSim/Service/DeviceService.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Function Body Length Violation: Function body should span 50 lines or less excluding comments and whitespace: currently spans 51 lines (function_body_length)
Expand Down
18 changes: 14 additions & 4 deletions MiniSim/Service/Terminal/Terminal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)'")
}
}
49 changes: 46 additions & 3 deletions MiniSim/Service/Terminal/TerminalApps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)\"
"""
}
}
36 changes: 14 additions & 22 deletions MiniSim/Views/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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?",
Expand Down

0 comments on commit 9d9aeb8

Please sign in to comment.