Skip to content

Commit 8aefe6b

Browse files
committed
fix: terminal mode window doesnt work on windows terminal; ha ;-)
this fixes #22
1 parent c0bcded commit 8aefe6b

3 files changed

Lines changed: 74 additions & 24 deletions

File tree

src/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("tests")]

src/Services/Terminals/WindowsTerminalProvider.cs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Diagnostics;
2+
using System.Text;
23
using Hypr.Configuration;
34
using Microsoft.Extensions.Logging;
45

@@ -54,30 +55,7 @@ public bool Open(string workingDirectory, TerminalMode mode, string? initCommand
5455
{
5556
try
5657
{
57-
var psi = new ProcessStartInfo
58-
{
59-
FileName = "wt",
60-
UseShellExecute = false
61-
};
62-
63-
var hasInitCommand = !string.IsNullOrEmpty(initCommand);
64-
65-
if (hasInitCommand)
66-
{
67-
psi.ArgumentList.Add(mode == TerminalMode.Tab ? "new-tab" : "new-window");
68-
psi.ArgumentList.Add("-d");
69-
psi.ArgumentList.Add(workingDirectory);
70-
psi.ArgumentList.Add("powershell");
71-
psi.ArgumentList.Add("-NoExit");
72-
psi.ArgumentList.Add("-Command");
73-
psi.ArgumentList.Add(initCommand!);
74-
}
75-
else
76-
{
77-
psi.ArgumentList.Add(mode == TerminalMode.Tab ? "new-tab" : "new-window");
78-
psi.ArgumentList.Add("-d");
79-
psi.ArgumentList.Add(workingDirectory);
80-
}
58+
var psi = CreateStartInfo(workingDirectory, mode, initCommand);
8159

8260
Process.Start(psi);
8361
_logger.LogInformation("Opened {Path} in Windows Terminal ({Mode})", workingDirectory, mode);
@@ -89,4 +67,33 @@ public bool Open(string workingDirectory, TerminalMode mode, string? initCommand
8967
return false;
9068
}
9169
}
70+
71+
internal static ProcessStartInfo CreateStartInfo(string workingDirectory, TerminalMode mode, string? initCommand = null)
72+
{
73+
var psi = new ProcessStartInfo
74+
{
75+
FileName = "wt",
76+
UseShellExecute = false
77+
};
78+
79+
if (mode == TerminalMode.Window)
80+
{
81+
psi.ArgumentList.Add("-w");
82+
psi.ArgumentList.Add("new");
83+
}
84+
85+
psi.ArgumentList.Add("new-tab");
86+
psi.ArgumentList.Add("-d");
87+
psi.ArgumentList.Add(workingDirectory);
88+
89+
if (!string.IsNullOrWhiteSpace(initCommand))
90+
{
91+
psi.ArgumentList.Add("powershell");
92+
psi.ArgumentList.Add("-NoExit");
93+
psi.ArgumentList.Add("-EncodedCommand");
94+
psi.ArgumentList.Add(Convert.ToBase64String(Encoding.Unicode.GetBytes(initCommand)));
95+
}
96+
97+
return psi;
98+
}
9299
}

tests/Services/Terminals/TerminalProviderTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using FakeItEasy;
2+
using System.Text;
23
using Hypr.Configuration;
34
using Hypr.Services.Terminals;
45
using Microsoft.Extensions.Logging;
@@ -271,6 +272,45 @@ public void SupportsMode_ReturnsExpectedValue(TerminalMode mode, bool expected)
271272
{
272273
Assert.Equal(expected, _provider.SupportsMode(mode));
273274
}
275+
276+
[Fact]
277+
public void CreateStartInfo_WindowModeWithInitCommand_UsesNewWindowSyntaxSupportedByWt()
278+
{
279+
var psi = WindowsTerminalProvider.CreateStartInfo(
280+
@"E:\Code\hypr-worktrees\fix-windows-terminal",
281+
TerminalMode.Window,
282+
"Set-Content -Path .hypr-switch-test.txt -Value ok");
283+
284+
Assert.Equal(
285+
[
286+
"-w",
287+
"new",
288+
"new-tab",
289+
"-d",
290+
@"E:\Code\hypr-worktrees\fix-windows-terminal",
291+
"powershell",
292+
"-NoExit",
293+
"-EncodedCommand"
294+
],
295+
psi.ArgumentList.Take(8).ToArray());
296+
297+
var decodedCommand = Encoding.Unicode.GetString(Convert.FromBase64String(psi.ArgumentList[8]));
298+
Assert.Equal("Set-Content -Path .hypr-switch-test.txt -Value ok", decodedCommand);
299+
}
300+
301+
[Fact]
302+
public void CreateStartInfo_WithMultipleCommands_EncodesEntirePowerShellScript()
303+
{
304+
const string command = "Set-Content -Path .hypr-a.txt -Value a; Set-Content -Path .hypr-b.txt -Value b";
305+
306+
var psi = WindowsTerminalProvider.CreateStartInfo(
307+
@"E:\Code\hypr-worktrees\fix-windows-terminal",
308+
TerminalMode.Window,
309+
command);
310+
311+
var decodedCommand = Encoding.Unicode.GetString(Convert.FromBase64String(psi.ArgumentList[8]));
312+
Assert.Equal(command, decodedCommand);
313+
}
274314
}
275315

276316
[Trait("Area", "Terminal")]

0 commit comments

Comments
 (0)