Skip to content
Merged
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
50 changes: 6 additions & 44 deletions src/Native/MacOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Diagnostics;
using System.IO;
using System.Runtime.Versioning;
using System.Text;

using Avalonia;

Expand All @@ -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()
Expand Down Expand Up @@ -58,54 +51,23 @@ public void OpenInFileManager(string path, bool select)

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)
{
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";
}
}
}