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 a973c36
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 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)'")
}
}
18 changes: 15 additions & 3 deletions MiniSim/Service/Terminal/TerminalApps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import Foundation

protocol TerminalApp {
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"
}

struct AppleTerminal: TerminalApp {
var name: String = "Terminal"
func getLaunchScript(deviceId: String, logcatCommand: String) -> String {
func getLaunchScript(logcatCommand: String) -> String {
"""
tell app \"Terminal\"
activate
Expand All @@ -32,7 +33,7 @@ struct AppleTerminal: TerminalApp {
struct ITermTerminal: TerminalApp {
var name: String = "iTerm"

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 +44,14 @@ struct ITermTerminal: TerminalApp {
"""
}
}

struct WezTermTerminal: TerminalApp {
var name: String = "WezTerm"

func getLaunchScript(logcatCommand: String) -> String {
"""
tell application \"wezterm\" to activate
do shell script \"/Applications/WezTerm.app/Contents/MacOS/wezterm cli spawn \(logcatCommand)\"
"""
}
}

0 comments on commit a973c36

Please sign in to comment.