From 6d18839237d8dac2da303a9c12107820d24162e4 Mon Sep 17 00:00:00 2001 From: Chiahong Hong Date: Tue, 10 Sep 2024 00:49:31 +0800 Subject: [PATCH] refactor: terminal launch on macOS without temporary AppleScript --- src/Native/MacOS.cs | 50 ++++++--------------------------------------- 1 file changed, 6 insertions(+), 44 deletions(-) diff --git a/src/Native/MacOS.cs b/src/Native/MacOS.cs index cf7b5b4c..8c1c24c1 100644 --- a/src/Native/MacOS.cs +++ b/src/Native/MacOS.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Runtime.Versioning; -using System.Text; using Avalonia; @@ -12,12 +11,6 @@ namespace SourceGit.Native [SupportedOSPlatform("macOS")] internal class MacOS : OS.IBackend { - enum TerminalType - { - Default, - iTerm2, - } - public void SetupApp(AppBuilder builder) { builder.With(new MacOSPlatformOptions() @@ -58,41 +51,10 @@ namespace SourceGit.Native public void OpenTerminal(string workdir) { - var dir = string.IsNullOrEmpty(workdir) ? "~" : workdir; - dir = dir.Replace(" ", "\\ "); - + var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + var dir = string.IsNullOrEmpty(workdir) ? home : workdir; var terminal = DetectTerminal(); - var cmdBuilder = new StringBuilder(); - switch (terminal) - { - case TerminalType.iTerm2: - cmdBuilder.AppendLine("on run argv"); - cmdBuilder.AppendLine(" tell application \"iTerm2\""); - cmdBuilder.AppendLine(" create window with default profile"); - cmdBuilder.AppendLine(" tell the current session of the current window"); - cmdBuilder.AppendLine($" write text \"cd {dir}\""); - cmdBuilder.AppendLine(" end tell"); - cmdBuilder.AppendLine(" end tell"); - cmdBuilder.AppendLine("end run"); - break; - default: - cmdBuilder.AppendLine("on run argv"); - cmdBuilder.AppendLine(" tell application \"Terminal\""); - cmdBuilder.AppendLine($" do script \"cd {dir}\""); - cmdBuilder.AppendLine(" activate"); - cmdBuilder.AppendLine(" end tell"); - cmdBuilder.AppendLine("end run"); - break; - } - - var tmp = Path.GetTempFileName(); - File.WriteAllText(tmp, cmdBuilder.ToString()); - - var proc = Process.Start("osascript", $"\"{tmp}\""); - if (proc != null) - proc.Exited += (_, _) => File.Delete(tmp); - else - File.Delete(tmp); + Process.Start("open", $"-a {terminal} \"{dir}\""); } public void OpenWithDefaultEditor(string file) @@ -100,12 +62,12 @@ namespace SourceGit.Native Process.Start("open", $"\"{file}\""); } - private TerminalType DetectTerminal() + private static string DetectTerminal() { if (Directory.Exists("/Applications/iTerm.app")) - return TerminalType.iTerm2; + return "iTerm"; - return TerminalType.Default; + return "Terminal"; } } }