|
1 | 1 | #pragma warning disable CS0162 |
2 | 2 | #pragma warning disable IDE0060 |
3 | 3 |
|
| 4 | +using System.Diagnostics; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | + |
4 | 7 | namespace MMM; |
5 | 8 |
|
6 | | -public static class Program |
| 9 | +public static partial class Program |
7 | 10 | { |
| 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 | + |
8 | 44 | public static void Main(string[] args) |
9 | 45 | { |
| 46 | + FixWindowsConsole(); |
| 47 | + |
10 | 48 | CancellationTokenSource cts = new(); |
11 | 49 | Console.CancelKeyPress += (sender, e) => |
12 | 50 | { |
|
0 commit comments