Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add wezterm support #127

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ DerivedData/
!default.mode2v3
*.perspectivev3
!default.perspectivev3
buildServer.json

## Obj-C/Swift specific
*.hmap
Expand Down
12 changes: 4 additions & 8 deletions MiniSim/Service/DeviceService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
attributes: .concurrent
)
private static let deviceBootedError = "Unable to boot device in current state: Booted"
private static let crashDataError = "Storing crashdata"

private static let derivedDataLocation = "~/Library/Developer/Xcode/DerivedData"

private enum ProcessPaths: String {
Expand Down Expand Up @@ -280,7 +278,7 @@
try shellOut(to: ProcessPaths.xcrun.rawValue, arguments: ["simctl", "delete", uuid])
}

static func handleiOSAction(device: Device, commandTag: SubMenuItems.Tags, itemName: String) {

Check warning on line 281 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 11 (cyclomatic_complexity)
queue.async {
switch commandTag {
case .copyName:
Expand Down Expand Up @@ -412,15 +410,13 @@
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 419 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 419 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)
queue.async {
do {
switch commandTag {
Expand Down Expand Up @@ -480,4 +476,4 @@
}
}
}
}

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

View workflow job for this annotation

GitHub Actions / SwiftLint

File Length Violation: File should contain 400 lines or less: currently contains 479 (file_length)

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

View workflow job for this annotation

GitHub Actions / build

File Length Violation: File should contain 400 lines or less: currently contains 479 (file_length)

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

View workflow job for this annotation

GitHub Actions / build

File Length Violation: File should contain 400 lines or less: currently contains 479 (file_length)
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(command: command)
try shellOut(to: "osascript -e '\(terminalScript)'")
}
}
53 changes: 48 additions & 5 deletions MiniSim/Service/Terminal/TerminalApps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,84 @@
// 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(command: 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(command: String) -> String {
"""
tell app \"Terminal\"
activate
do script \"\(logcatCommand)\"
do script \"\(command)\"
end tell
"""
}
}

struct ITermTerminal: TerminalApp {
var name: String = "iTerm"
var bundleIdentifier: String = "com.googlecode.iterm2"

func getLaunchScript(deviceId: String, logcatCommand: String) -> String {
func getLaunchScript(command: String) -> String {
"""
tell app \"iTerm\"
set newWindow to (create window with default profile)
tell current session of newWindow
write text \"\(logcatCommand)\"
write text \"\(command)\"
end tell
end tell
"""
}
}

struct WezTermTerminal: TerminalApp {
var name: String = "WezTerm"
var bundleIdentifier: String = "com.github.wez.wezterm"

func getLaunchScript(command: String) -> String {
"""
tell application \"wezterm\" to activate
do shell script \"/Applications/WezTerm.app/Contents/MacOS/wezterm cli spawn \(command)\"
"""
}
}
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
Loading