diff --git a/src/Native/Linux.cs b/src/Native/Linux.cs index 16fd1b78..9df41160 100644 --- a/src/Native/Linux.cs +++ b/src/Native/Linux.cs @@ -27,9 +27,7 @@ namespace SourceGit.Native public string FindTerminal(Models.ShellOrTerminal shell) { if (string.IsNullOrEmpty(shell.Exec)) - { return string.Empty; - } var pathVariable = Environment.GetEnvironmentVariable("PATH") ?? string.Empty; var pathes = pathVariable.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries); @@ -76,23 +74,18 @@ namespace SourceGit.Native public void OpenTerminal(string workdir) { - if (string.IsNullOrEmpty(OS.ShellOrTerminal)) - { - App.RaiseException(workdir, $"Terminal is not specified! Please confirm that the correct shell/terminal has been configured."); - return; - } - var startInfo = new ProcessStartInfo(); var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); startInfo.WorkingDirectory = string.IsNullOrEmpty(workdir) ? home : workdir; startInfo.FileName = OS.ShellOrTerminal; + try { Process.Start(startInfo); } catch (Exception e) { - App.RaiseException(workdir, e.Message); + App.RaiseException(workdir, $"Failed to start '{OS.ShellOrTerminal}'. Reason: {e.Message}"); } } diff --git a/src/Native/OS.cs b/src/Native/OS.cs index 7df0b3ae..bc9c5403 100644 --- a/src/Native/OS.cs +++ b/src/Native/OS.cs @@ -74,12 +74,17 @@ namespace SourceGit.Native return _backend.FindGitExecutable(); } - public static void SetShellOrTerminal(Models.ShellOrTerminal shellOrTerminal) + public static bool TestShellOrTerminal(Models.ShellOrTerminal shell) { - if (shellOrTerminal == null) + return !string.IsNullOrEmpty(_backend.FindTerminal(shell)); + } + + public static void SetShellOrTerminal(Models.ShellOrTerminal shell) + { + if (shell == null) ShellOrTerminal = string.Empty; else - ShellOrTerminal = _backend.FindTerminal(shellOrTerminal); + ShellOrTerminal = _backend.FindTerminal(shell); } public static void OpenInFileManager(string path, bool select = false) diff --git a/src/Native/Windows.cs b/src/Native/Windows.cs index 13438e02..ab91ffdb 100644 --- a/src/Native/Windows.cs +++ b/src/Native/Windows.cs @@ -158,7 +158,7 @@ namespace SourceGit.Native public void OpenTerminal(string workdir) { - if (string.IsNullOrEmpty(OS.ShellOrTerminal) || !File.Exists(OS.ShellOrTerminal)) + if (!File.Exists(OS.ShellOrTerminal)) { App.RaiseException(workdir, $"Terminal is not specified! Please confirm that the correct shell/terminal has been configured."); return; diff --git a/src/ViewModels/Preference.cs b/src/ViewModels/Preference.cs index b62e3720..2df40675 100644 --- a/src/ViewModels/Preference.cs +++ b/src/ViewModels/Preference.cs @@ -18,44 +18,15 @@ namespace SourceGit.ViewModels if (_instance == null) { _isLoading = true; - - var path = Path.Combine(Native.OS.DataDir, "preference.json"); - if (!File.Exists(path)) - { - _instance = new Preference(); - } - else - { - try - { - _instance = JsonSerializer.Deserialize(File.ReadAllText(path), JsonCodeGen.Default.Preference); - } - catch - { - _instance = new Preference(); - } - } + _instance = Load(); _isLoading = false; } if (!_instance.IsGitConfigured()) _instance.GitInstallPath = Native.OS.FindGitExecutable(); - if (!_instance.IsTerminalConfigured()) - { - for (var i = 0; i < Models.ShellOrTerminal.Supported.Count; i++) - { - _instance.ShellOrTerminal = i; - if (string.IsNullOrEmpty(_instance.ShellOrTerminalPath)) - { - _instance.ShellOrTerminal = -1; - } - else - { - break; - } - } - } + if (_instance._shellOrTerminal == -1) + _instance.AutoSelectShellOrTerminal(); if (_instance.Workspaces.Count == 0) _instance.Workspaces.Add(new Workspace() { Name = "Default", Color = 4278221015 }); @@ -396,11 +367,6 @@ namespace SourceGit.ViewModels return !string.IsNullOrEmpty(path) && File.Exists(path); } - public bool IsTerminalConfigured() - { - return ShellOrTerminal != -1; - } - public bool ShouldCheck4UpdateOnStartup() { if (!_check4UpdatesOnStartup) @@ -519,6 +485,35 @@ namespace SourceGit.ViewModels File.WriteAllText(file, data); } + private static Preference Load() + { + var path = Path.Combine(Native.OS.DataDir, "preference.json"); + if (!File.Exists(path)) + return new Preference(); + + try + { + return JsonSerializer.Deserialize(File.ReadAllText(path), JsonCodeGen.Default.Preference); + } + catch + { + return new Preference(); + } + } + + private void AutoSelectShellOrTerminal() + { + for (int i = 0; i < Models.ShellOrTerminal.Supported.Count; i++) + { + var shell = Models.ShellOrTerminal.Supported[i]; + if (Native.OS.TestShellOrTerminal(shell)) + { + ShellOrTerminal = i; + break; + } + } + } + private RepositoryNode FindNodeRecursive(string id, List collection) { foreach (var node in collection) @@ -557,7 +552,7 @@ namespace SourceGit.ViewModels return true; } - foreach (RepositoryNode one in collection) + foreach (var one in collection) { if (RemoveNodeRecursive(node, one.SubNodes)) return true; diff --git a/src/Views/ImageDiffView.axaml.cs b/src/Views/ImageDiffView.axaml.cs index bc516b5a..7e32c91a 100644 --- a/src/Views/ImageDiffView.axaml.cs +++ b/src/Views/ImageDiffView.axaml.cs @@ -1,17 +1,7 @@ -using System; - -using Avalonia; using Avalonia.Controls; -using Avalonia.Input; -using Avalonia.Media; -using Avalonia.Media.Imaging; namespace SourceGit.Views { - - - - public partial class ImageDiffView : UserControl { public ImageDiffView()