Skip to content

Commit 7111ffb

Browse files
committed
refactor: allows to specify startup options for external shell/terminal (#1856)
Signed-off-by: leo <[email protected]>
1 parent 96fbe38 commit 7111ffb

File tree

10 files changed

+70
-43
lines changed

10 files changed

+70
-43
lines changed

src/Models/ShellOrTerminal.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class ShellOrTerminal
1111
public string Type { get; set; }
1212
public string Name { get; set; }
1313
public string Exec { get; set; }
14+
public string Args { get; set; }
1415

1516
public Bitmap Icon
1617
{
@@ -32,18 +33,18 @@ static ShellOrTerminal()
3233
new ShellOrTerminal("git-bash", "Git Bash", "bash.exe"),
3334
new ShellOrTerminal("pwsh", "PowerShell", "pwsh.exe|powershell.exe"),
3435
new ShellOrTerminal("cmd", "Command Prompt", "cmd.exe"),
35-
new ShellOrTerminal("wt", "Windows Terminal", "wt.exe")
36+
new ShellOrTerminal("wt", "Windows Terminal", "wt.exe", "-d .")
3637
};
3738
}
3839
else if (OperatingSystem.IsMacOS())
3940
{
4041
Supported = new List<ShellOrTerminal>()
4142
{
42-
new ShellOrTerminal("mac-terminal", "Terminal", ""),
43-
new ShellOrTerminal("iterm2", "iTerm", ""),
44-
new ShellOrTerminal("warp", "Warp", ""),
45-
new ShellOrTerminal("ghostty", "Ghostty", ""),
46-
new ShellOrTerminal("kitty", "kitty", "")
43+
new ShellOrTerminal("mac-terminal", "Terminal", "Terminal"),
44+
new ShellOrTerminal("iterm2", "iTerm", "iTerm"),
45+
new ShellOrTerminal("warp", "Warp", "Warp"),
46+
new ShellOrTerminal("ghostty", "Ghostty", "Ghostty"),
47+
new ShellOrTerminal("kitty", "kitty", "kitty")
4748
};
4849
}
4950
else
@@ -57,19 +58,20 @@ static ShellOrTerminal()
5758
new ShellOrTerminal("deepin-terminal", "Deepin Terminal", "deepin-terminal"),
5859
new ShellOrTerminal("mate-terminal", "MATE Terminal", "mate-terminal"),
5960
new ShellOrTerminal("foot", "Foot", "foot"),
60-
new ShellOrTerminal("wezterm", "WezTerm", "wezterm"),
61-
new ShellOrTerminal("ptyxis", "Ptyxis", "ptyxis"),
61+
new ShellOrTerminal("wezterm", "WezTerm", "wezterm", "start --cwd ."),
62+
new ShellOrTerminal("ptyxis", "Ptyxis", "ptyxis", "--new-window --working-directory=."),
6263
new ShellOrTerminal("kitty", "kitty", "kitty"),
6364
new ShellOrTerminal("custom", "Custom", ""),
6465
};
6566
}
6667
}
6768

68-
public ShellOrTerminal(string type, string name, string exec)
69+
public ShellOrTerminal(string type, string name, string exec, string args = null)
6970
{
7071
Type = type;
7172
Name = name;
7273
Exec = exec;
74+
Args = args;
7375
}
7476
}
7577
}

src/Native/Linux.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,15 @@ public void OpenInFileManager(string path, bool select)
8787
}
8888
}
8989

90-
public void OpenTerminal(string workdir)
90+
public void OpenTerminal(string workdir, string args)
9191
{
9292
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
9393
var cwd = string.IsNullOrEmpty(workdir) ? home : workdir;
94-
var terminal = OS.ShellOrTerminal;
9594

9695
var startInfo = new ProcessStartInfo();
9796
startInfo.WorkingDirectory = cwd;
98-
startInfo.FileName = terminal;
99-
100-
if (terminal.EndsWith("wezterm", StringComparison.OrdinalIgnoreCase))
101-
startInfo.Arguments = $"start --cwd {cwd.Quoted()}";
102-
else if (terminal.EndsWith("ptyxis", StringComparison.OrdinalIgnoreCase))
103-
startInfo.Arguments = $"--new-window --working-directory={cwd.Quoted()}";
97+
startInfo.FileName = OS.ShellOrTerminal;
98+
startInfo.Arguments = args;
10499

105100
try
106101
{

src/Native/MacOS.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,7 @@ public string FindGitExecutable()
6262

6363
public string FindTerminal(Models.ShellOrTerminal shell)
6464
{
65-
return shell.Type switch
66-
{
67-
"mac-terminal" => "Terminal",
68-
"iterm2" => "iTerm",
69-
"warp" => "Warp",
70-
"ghostty" => "Ghostty",
71-
"kitty" => "kitty",
72-
_ => string.Empty,
73-
};
65+
return shell.Exec;
7466
}
7567

7668
public List<Models.ExternalTool> FindExternalTools()
@@ -101,7 +93,7 @@ public void OpenInFileManager(string path, bool select)
10193
Process.Start("open", $"{path.Quoted()} -R");
10294
}
10395

104-
public void OpenTerminal(string workdir)
96+
public void OpenTerminal(string workdir, string _)
10597
{
10698
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
10799
var dir = string.IsNullOrEmpty(workdir) ? home : workdir;

src/Native/OS.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public interface IBackend
2121
string FindTerminal(Models.ShellOrTerminal shell);
2222
List<Models.ExternalTool> FindExternalTools();
2323

24-
void OpenTerminal(string workdir);
24+
void OpenTerminal(string workdir, string args);
2525
void OpenInFileManager(string path, bool select);
2626
void OpenBrowser(string url);
2727
void OpenWithDefaultEditor(string file);
@@ -70,6 +70,12 @@ public static string ShellOrTerminal
7070
set;
7171
} = string.Empty;
7272

73+
public static string ShellOrTerminalArgs
74+
{
75+
get;
76+
set;
77+
} = string.Empty;
78+
7379
public static List<Models.ExternalTool> ExternalTools
7480
{
7581
get;
@@ -168,6 +174,8 @@ public static void SetShellOrTerminal(Models.ShellOrTerminal shell)
168174
ShellOrTerminal = string.Empty;
169175
else
170176
ShellOrTerminal = _backend.FindTerminal(shell);
177+
178+
ShellOrTerminalArgs = shell.Args;
171179
}
172180

173181
public static Models.DiffMergeTool GetDiffMergeTool(bool onlyDiff)
@@ -212,7 +220,7 @@ public static void OpenTerminal(string workdir)
212220
if (string.IsNullOrEmpty(ShellOrTerminal))
213221
App.RaiseException(workdir, "Terminal is not specified! Please confirm that the correct shell/terminal has been configured.");
214222
else
215-
_backend.OpenTerminal(workdir);
223+
_backend.OpenTerminal(workdir, ShellOrTerminalArgs);
216224
}
217225

218226
public static void OpenWithDefaultEditor(string file)

src/Native/Windows.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public void OpenBrowser(string url)
203203
Process.Start(info);
204204
}
205205

206-
public void OpenTerminal(string workdir)
206+
public void OpenTerminal(string workdir, string args)
207207
{
208208
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
209209
var cwd = string.IsNullOrEmpty(workdir) ? home : workdir;
@@ -218,11 +218,7 @@ public void OpenTerminal(string workdir)
218218
var startInfo = new ProcessStartInfo();
219219
startInfo.WorkingDirectory = cwd;
220220
startInfo.FileName = terminal;
221-
222-
// Directly launching `Windows Terminal` need to specify the `-d` parameter
223-
if (terminal.EndsWith("wt.exe", StringComparison.OrdinalIgnoreCase))
224-
startInfo.Arguments = $"-d {cwd.Quoted()}";
225-
221+
startInfo.Arguments = args;
226222
Process.Start(startInfo);
227223
}
228224

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@
629629
<x:String x:Key="Text.Preferences.GPG.UserKey.Placeholder" xml:space="preserve">User's gpg signing key</x:String>
630630
<x:String x:Key="Text.Preferences.Integration" xml:space="preserve">INTEGRATION</x:String>
631631
<x:String x:Key="Text.Preferences.Shell" xml:space="preserve">SHELL/TERMINAL</x:String>
632+
<x:String x:Key="Text.Preferences.Shell.Args" xml:space="preserve">Arguments</x:String>
632633
<x:String x:Key="Text.Preferences.Shell.Path" xml:space="preserve">Path</x:String>
633634
<x:String x:Key="Text.Preferences.Shell.Type" xml:space="preserve">Shell/Terminal</x:String>
634635
<x:String x:Key="Text.PruneRemote" xml:space="preserve">Prune Remote</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@
633633
<x:String x:Key="Text.Preferences.GPG.UserKey.Placeholder" xml:space="preserve">输入签名提交所使用的KEY</x:String>
634634
<x:String x:Key="Text.Preferences.Integration" xml:space="preserve">第三方工具集成</x:String>
635635
<x:String x:Key="Text.Preferences.Shell" xml:space="preserve">终端/SHELL</x:String>
636+
<x:String x:Key="Text.Preferences.Shell.Args" xml:space="preserve">启动参数</x:String>
636637
<x:String x:Key="Text.Preferences.Shell.Path" xml:space="preserve">安装路径</x:String>
637638
<x:String x:Key="Text.Preferences.Shell.Type" xml:space="preserve">终端/SHELL</x:String>
638639
<x:String x:Key="Text.PruneRemote" xml:space="preserve">清理远程已删除分支</x:String>

src/Resources/Locales/zh_TW.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@
633633
<x:String x:Key="Text.Preferences.GPG.UserKey.Placeholder" xml:space="preserve">填寫簽章提交所使用的金鑰</x:String>
634634
<x:String x:Key="Text.Preferences.Integration" xml:space="preserve">第三方工具整合</x:String>
635635
<x:String x:Key="Text.Preferences.Shell" xml:space="preserve">終端機/Shell</x:String>
636+
<x:String x:Key="Text.Preferences.Shell.Args" xml:space="preserve">啟動參數</x:String>
636637
<x:String x:Key="Text.Preferences.Shell.Path" xml:space="preserve">安裝路徑</x:String>
637638
<x:String x:Key="Text.Preferences.Shell.Type" xml:space="preserve">終端機/Shell</x:String>
638639
<x:String x:Key="Text.PruneRemote" xml:space="preserve">清理遠端已刪除分支</x:String>

src/ViewModels/Preferences.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,15 @@ public int ShellOrTerminal
349349
get => _shellOrTerminal;
350350
set
351351
{
352-
if (SetProperty(ref _shellOrTerminal, value))
352+
if (SetProperty(ref _shellOrTerminal, value) && !_isLoading)
353353
{
354354
if (value >= 0 && value < Models.ShellOrTerminal.Supported.Count)
355355
Native.OS.SetShellOrTerminal(Models.ShellOrTerminal.Supported[value]);
356356
else
357357
Native.OS.SetShellOrTerminal(null);
358358

359359
OnPropertyChanged(nameof(ShellOrTerminalPath));
360+
OnPropertyChanged(nameof(ShellOrTerminalArgs));
360361
}
361362
}
362363
}
@@ -374,6 +375,19 @@ public string ShellOrTerminalPath
374375
}
375376
}
376377

378+
public string ShellOrTerminalArgs
379+
{
380+
get => Native.OS.ShellOrTerminalArgs;
381+
set
382+
{
383+
if (value != Native.OS.ShellOrTerminalArgs)
384+
{
385+
Native.OS.ShellOrTerminalArgs = value;
386+
OnPropertyChanged();
387+
}
388+
}
389+
}
390+
377391
public int ExternalMergeToolType
378392
{
379393
get => Native.OS.ExternalMergerType;

src/Views/Preferences.axaml

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -471,15 +471,15 @@
471471
<TextBlock Classes="bold" Margin="4,0,0,0" Text="{DynamicResource Text.Preferences.Shell}"/>
472472
</StackPanel>
473473
<Rectangle Margin="0,8" Fill="{DynamicResource Brush.Border2}" Height=".6" HorizontalAlignment="Stretch"/>
474-
<Grid Margin="8,0,0,0" RowDefinitions="32,Auto">
474+
<Grid Margin="8,0,0,0" RowDefinitions="32,Auto,Auto">
475475
<Grid.ColumnDefinitions>
476476
<ColumnDefinition Width="Auto" SharedSizeGroup="IntegrationLabel"/>
477477
<ColumnDefinition Width="*"/>
478478
</Grid.ColumnDefinitions>
479479

480480
<TextBlock Grid.Row="0" Grid.Column="0"
481481
Text="{DynamicResource Text.Preferences.Shell.Type}"
482-
HorizontalAlignment="Right"
482+
HorizontalAlignment="Right" VerticalAlignment="Center"
483483
Margin="0,0,16,0"/>
484484
<ComboBox Grid.Row="0" Grid.Column="1"
485485
MinHeight="28"
@@ -497,11 +497,14 @@
497497
</ComboBox.ItemTemplate>
498498
</ComboBox>
499499

500-
<TextBlock Grid.Row="1" Grid.Column="0"
501-
Text="{DynamicResource Text.Preferences.Shell.Path}"
502-
HorizontalAlignment="Right"
503-
Margin="0,0,16,0"
504-
IsVisible="{OnPlatform True, macOS=False}"/>
500+
<Border Grid.Row="1" Grid.Column="0"
501+
Height="32"
502+
Margin="0,0,16,0"
503+
IsVisible="{OnPlatform True, macOS=False}">
504+
<TextBlock Grid.Row="1" Grid.Column="0"
505+
Text="{DynamicResource Text.Preferences.Shell.Path}"
506+
HorizontalAlignment="Right" />
507+
</Border>
505508
<TextBox Grid.Row="1" Grid.Column="1"
506509
Height="28"
507510
CornerRadius="3"
@@ -513,6 +516,20 @@
513516
</Button>
514517
</TextBox.InnerRightContent>
515518
</TextBox>
519+
520+
<Border Grid.Row="2" Grid.Column="0"
521+
Height="32"
522+
Margin="0,0,16,0"
523+
IsVisible="{OnPlatform True, macOS=False}">
524+
<TextBlock Grid.Row="1" Grid.Column="0"
525+
Text="{DynamicResource Text.Preferences.Shell.Args}"
526+
HorizontalAlignment="Right" />
527+
</Border>
528+
<TextBox Grid.Row="2" Grid.Column="1"
529+
Height="28"
530+
CornerRadius="3"
531+
Text="{Binding ShellOrTerminalArgs, Mode=TwoWay}"
532+
IsVisible="{OnPlatform True, macOS=False}"/>
516533
</Grid>
517534

518535
<StackPanel Orientation="Horizontal" Margin="0,24,0,0">

0 commit comments

Comments
 (0)