Skip to content

Commit db4e25f

Browse files
committed
windows terminal fix
1 parent 3fafccf commit db4e25f

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

Program.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,50 @@
11
#pragma warning disable CS0162
22
#pragma warning disable IDE0060
33

4+
using System.Diagnostics;
5+
using System.Runtime.InteropServices;
6+
47
namespace MMM;
58

6-
public static class Program
9+
public static partial class Program
710
{
11+
const int STD_OUTPUT_HANDLE = -11;
12+
const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4;
13+
14+
[LibraryImport("kernel32.dll", SetLastError = true)]
15+
private static partial IntPtr GetStdHandle(int nStdHandle);
16+
17+
[LibraryImport("kernel32.dll")]
18+
[return: MarshalAs(UnmanagedType.Bool)]
19+
private static partial bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
20+
21+
[LibraryImport("kernel32.dll")]
22+
[return: MarshalAs(UnmanagedType.Bool)]
23+
private static partial bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
24+
25+
[Conditional("WIN")]
26+
static void FixWindowsConsole()
27+
{
28+
IntPtr handle = GetStdHandle(STD_OUTPUT_HANDLE);
29+
30+
if (!GetConsoleMode(handle, out uint mode))
31+
{
32+
Console.Error.WriteLine("Failed to get console mode");
33+
return;
34+
}
35+
36+
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
37+
if (!SetConsoleMode(handle, mode))
38+
{
39+
Console.Error.WriteLine("Failed to set console mode");
40+
return;
41+
}
42+
}
43+
844
public static void Main(string[] args)
945
{
46+
FixWindowsConsole();
47+
1048
CancellationTokenSource cts = new();
1149
Console.CancelKeyPress += (sender, e) =>
1250
{

mmm.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<PublishSingleFile>true</PublishSingleFile>
1010
<SelfContained>true</SelfContained>
1111
<PublishTrimmed>false</PublishTrimmed>
12+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1213
</PropertyGroup>
1314

1415
<ItemGroup>

publish.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22
dotnet publish ./mmm.csproj -c Release -o ./publish/linux-x64 -r linux-x64 /p:UseAppHost=true
3-
dotnet publish ./mmm.csproj -c Release -o ./publish/win-x64 -r win-x64 /p:UseAppHost=true
3+
dotnet publish ./mmm.csproj -c Release -o ./publish/win-x64 -r win-x64 /p:UseAppHost=true /p:DefineConstants=WIN
44

55
mv ./publish/linux-x64/mmm ./publish/mmm-linux-x64
66
rm -r ./publish/linux-x64

0 commit comments

Comments
 (0)