不启用虚拟终端会导致:
无法使用 ANSI 转义序列, 无法通过 \e \1b 逃逸字符打印彩色或格式化内容
通过以下代码来在 Windows 上启用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| internal class PlatformUtils
{
private const int STD_OUTPUT_HANDLE = -11;
private const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
private const uint DISABLE_NEWLINE_AUTO_RETURN = 0x0008;
[DllImport("kernel32.dll")]
private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
[DllImport("kernel32.dll")]
private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
private static extern uint GetLastError();
public static uint EnableVirtualTerminalProcessingOnWindows()
{
var iStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (!GetConsoleMode(iStdOut, out uint outConsoleMode))
{
return GetLastError();
}
outConsoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN;
if (!SetConsoleMode(iStdOut, outConsoleMode))
{
return GetLastError();
}
return 0;
}
}
|
在运行前加上以下代码即可:
1
2
| if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
PlatformUtils.EnableVirtualTerminalProcessingOnWindows();
|