Skip to content

Commit 9735686

Browse files
Refactor preset initialization and improve wt.exe support
--- - 🛠️ Refactored default preset initialization to use lazy-loading with a private backing field and InitializePresets method. - 🗂️ Moved preset setup logic out of LoadSettingsFromStorage for better organization and reliability. - 🧩 Improved detection and handling of the GitHub Console preset using safer environment variable access. - 🪟 Updated process launch logic to set UseShellExecute for Windows Terminal (wt.exe/wt) and only modify PATH when appropriate. - 📝 Updated "Windows Terminal" preset to use "wt.exe" and proper directory argument for improved compatibility. Fixes #91 Fixes #100
1 parent 1349174 commit 9735686

2 files changed

Lines changed: 54 additions & 30 deletions

File tree

‎src/OpenCommandLine/CommandLineLauncher.cs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,22 @@ public static void StartProcess(string workingDirectory, string command, string
1919
command = Environment.ExpandEnvironmentVariables(command ?? string.Empty);
2020
arguments = Environment.ExpandEnvironmentVariables(arguments ?? string.Empty);
2121

22+
// Windows Terminal (wt.exe) is a modern Windows app that requires shell execution
23+
bool useShellExecute = command.IndexOf("wt.exe", StringComparison.OrdinalIgnoreCase) >= 0 ||
24+
command.IndexOf("wt", StringComparison.OrdinalIgnoreCase) >= 0 &&
25+
!command.EndsWith(".exe", StringComparison.OrdinalIgnoreCase);
26+
2227
var start = new ProcessStartInfo(command, arguments)
2328
{
2429
WorkingDirectory = workingDirectory,
2530
LoadUserProfile = true,
26-
UseShellExecute = false
31+
UseShellExecute = useShellExecute
2732
};
2833

29-
ModifyPathVariable(start);
34+
if (!useShellExecute)
35+
{
36+
ModifyPathVariable(start);
37+
}
3038

3139
using (Process.Start(start))
3240
{

‎src/OpenCommandLine/Options.cs‎

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.VisualStudio.Shell;
1+
using Microsoft.VisualStudio.Shell;
22
using System;
33
using System.Collections.Generic;
44
using System.ComponentModel;
@@ -8,10 +8,52 @@ namespace MadsKristensen.OpenCommandLine
88
{
99
internal class Options : DialogPage
1010
{
11-
public static Dictionary<string, Command> DefaultPresets = new Dictionary<string, Command>();
11+
private static Dictionary<string, Command> _defaultPresets;
1212
private bool _isLoading, _isChanging;
1313
private string _preset;
1414

15+
public static Dictionary<string, Command> DefaultPresets
16+
{
17+
get
18+
{
19+
if (_defaultPresets == null)
20+
{
21+
InitializePresets();
22+
}
23+
return _defaultPresets;
24+
}
25+
}
26+
27+
private static void InitializePresets()
28+
{
29+
_defaultPresets = new Dictionary<string, Command>();
30+
31+
string installDir = VsHelpers.GetInstallDirectory();
32+
string devPromptFile = installDir != null
33+
? Path.Combine(installDir, @"..\Tools\VsDevCmd.bat")
34+
: "";
35+
36+
_defaultPresets["cmd"] = new Command("cmd.exe");
37+
_defaultPresets["Dev Cmd Prompt"] = new Command("cmd.exe", "/k \"" + devPromptFile + "\"");
38+
_defaultPresets["PowerShellCore"] = new Command("pwsh.exe", "-ExecutionPolicy Bypass -NoExit");
39+
_defaultPresets["PowerShell"] = new Command("powershell.exe", "-ExecutionPolicy Bypass -NoExit");
40+
_defaultPresets["PowerShell ISE"] = new Command("powershell_ise.exe");
41+
_defaultPresets["posh-git"] = new Command("powershell.exe", @"-ExecutionPolicy Bypass -NoExit -Command .(Resolve-Path ""$env:LOCALAPPDATA\GitHub\shell.ps1""); .(Resolve-Path ""$env:github_posh_git\profile.example.ps1"")");
42+
_defaultPresets["Git Bash"] = new Command(@"C:\Program Files\Git\git-bash.exe");
43+
_defaultPresets["Babun"] = new Command(@"%UserProfile%\.babun\cygwin\bin\mintty.exe", "/bin/env CHERE_INVOKING=1 /bin/zsh.exe");
44+
45+
string GitHubForWindowsPath = Path.Combine(Environment.GetEnvironmentVariable("LocalAppData") ?? "", "GitHub", "GitHub.appref-ms");
46+
if (File.Exists(GitHubForWindowsPath))
47+
{
48+
_defaultPresets["GitHub Console"] = new Command(@"%LOCALAPPDATA%\GitHub\GitHub.appref-ms", "-open-shell");
49+
}
50+
51+
_defaultPresets["cmder"] = new Command("cmder.exe", "/START \"%folder%\"");
52+
_defaultPresets["ConEmu"] = new Command("ConEmu64.exe", "/cmd PowerShell.exe");
53+
_defaultPresets["Windows Terminal"] = new Command("wt.exe", "-d \"%folder%\"");
54+
_defaultPresets["Custom"] = new Command(string.Empty, string.Empty);
55+
}
56+
1557
[Category("Command Preset")]
1658
[DisplayName("Select preset")]
1759
[Description("Select one of the predefined command configurations")]
@@ -114,32 +156,6 @@ public override void LoadSettingsFromStorage()
114156
Preset = "cmd";
115157
}
116158

117-
if (DefaultPresets.Count == 0)
118-
{
119-
string installDir = VsHelpers.GetInstallDirectory();
120-
string devPromptFile = Path.Combine(installDir, @"..\Tools\VsDevCmd.bat");
121-
122-
DefaultPresets["cmd"] = new Command("cmd.exe");
123-
DefaultPresets["Dev Cmd Prompt"] = new Command("cmd.exe", "/k \"" + devPromptFile + "\"");
124-
DefaultPresets["PowerShellCore"] = new Command("pwsh.exe", "-ExecutionPolicy Bypass -NoExit");
125-
DefaultPresets["PowerShell"] = new Command("powershell.exe", "-ExecutionPolicy Bypass -NoExit");
126-
DefaultPresets["PowerShell ISE"] = new Command("powershell_ise.exe");
127-
DefaultPresets["posh-git"] = new Command("powershell.exe", @"-ExecutionPolicy Bypass -NoExit -Command .(Resolve-Path ""$env:LOCALAPPDATA\GitHub\shell.ps1""); .(Resolve-Path ""$env:github_posh_git\profile.example.ps1"")");
128-
DefaultPresets["Git Bash"] = new Command(@"C:\Program Files\Git\git-bash.exe");
129-
DefaultPresets["Babun"] = new Command(@"%UserProfile%\.babun\cygwin\bin\mintty.exe", "/bin/env CHERE_INVOKING=1 /bin/zsh.exe");
130-
131-
string GitHubForWindowsPath = Path.Combine(Environment.GetEnvironmentVariable("LocalAppData"), "GitHub", "GitHub.appref-ms");
132-
if (File.Exists(GitHubForWindowsPath))
133-
{
134-
DefaultPresets["GitHub Console"] = new Command(@"%LOCALAPPDATA%\GitHub\GitHub.appref-ms", "-open-shell");
135-
}
136-
137-
DefaultPresets["cmder"] = new Command("cmder.exe", "/START \"%folder%\"");
138-
DefaultPresets["ConEmu"] = new Command("ConEmu64.exe", "/cmd PowerShell.exe");
139-
DefaultPresets["Windows Terminal"] = new Command("wt", "-d %folder%");
140-
DefaultPresets["Custom"] = new Command(string.Empty, string.Empty);
141-
}
142-
143159
_isLoading = false;
144160
}
145161

0 commit comments

Comments
 (0)