Skip to content

Commit d898bab

Browse files
committed
feat: enable VT processing in windows to make ANSI escape codes show correctly
1 parent b12641c commit d898bab

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "util/java.h"
99

1010
#ifdef _WIN32
11-
#include "util/os.h" // set_console_cp_utf8
11+
#include "util/os.h"
1212
#endif
1313

1414
using namespace allay_launcher;
@@ -107,6 +107,8 @@ auto parse_arguments(int argc, char* argv[]) {
107107
int main(int argc, char* argv[]) try {
108108
#ifdef _WIN32
109109
util::os::set_console_cp_utf8();
110+
// Make it able to use of ANSI escape codes in the console
111+
util::os::enable_virtual_terminal_processing();
110112
#endif
111113

112114
setup_logger();

src/util/os.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ bool is_legacy_windows();
1818

1919
#ifdef _WIN32
2020
void set_console_cp_utf8();
21+
22+
void enable_virtual_terminal_processing();
2123
#endif
2224

2325
} // namespace allay_launcher::util::os

src/util/os_win32.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,17 @@ void set_console_cp_utf8() {
3131

3232
bool is_legacy_windows() { return !IsWindows10OrGreater(); }
3333

34+
void enable_virtual_terminal_processing() {
35+
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
36+
if (hOut == INVALID_HANDLE_VALUE) {
37+
return;
38+
}
39+
40+
DWORD dwMode = 0;
41+
if (!GetConsoleMode(hOut, &dwMode)) {
42+
return;
43+
}
44+
45+
SetConsoleMode(hOut, dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
46+
}
3447
} // namespace allay_launcher::util::os

0 commit comments

Comments
 (0)