refactor: detect terminal before it is needed

This commit is contained in:
leo 2024-08-20 21:05:20 +08:00
parent f4f5ae7430
commit 04a187203a
No known key found for this signature in database

View file

@ -14,15 +14,10 @@ namespace SourceGit.Native
{
enum TerminalType
{
Terminal,
Default,
iTerm2,
}
public MacOS()
{
_terminal = Directory.Exists("/Applications/iTerm.app") ? TerminalType.iTerm2 : TerminalType.Terminal;
}
public void SetupApp(AppBuilder builder)
{
builder.With(new MacOSPlatformOptions()
@ -33,7 +28,6 @@ namespace SourceGit.Native
public string FindGitExecutable()
{
// XCode built-in git
return File.Exists("/usr/bin/git") ? "/usr/bin/git" : string.Empty;
}
@ -57,22 +51,19 @@ namespace SourceGit.Native
public void OpenInFileManager(string path, bool select)
{
if (Directory.Exists(path))
{
Process.Start("open", $"\"{path}\"");
}
else if (File.Exists(path))
{
Process.Start("open", $"\"{path}\" -R");
}
}
public void OpenTerminal(string workdir)
{
var dir = string.IsNullOrEmpty(workdir) ? "~" : workdir;
dir = dir.Replace(" ", "\\ ");
var terminal = DetectTerminal();
var cmdBuilder = new StringBuilder();
switch (_terminal)
switch (terminal)
{
case TerminalType.iTerm2:
cmdBuilder.AppendLine("on run argv");
@ -109,6 +100,12 @@ namespace SourceGit.Native
Process.Start("open", $"\"{file}\"");
}
private readonly TerminalType _terminal;
private TerminalType DetectTerminal()
{
if (Directory.Exists("/Applications/iTerm.app"))
return TerminalType.iTerm2;
return TerminalType.Default;
}
}
}