Skip to content

Commit c86ba03

Browse files
anzz1ggerganov
andauthored
Enable ANSI colors on Windows 10+ (abetlen#311)
* Enable ANSI colors on Windows 10+ On older versions function will silently fail without any ill effects * Do not call SetConsoleMode if the mode is already set * Update main.cpp --------- Co-authored-by: Georgi Gerganov <[email protected]>
1 parent 1daf4dd commit c86ba03

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
#include <signal.h>
2121
#endif
2222

23+
#if defined (_WIN32)
24+
#pragma comment(lib,"kernel32.lib")
25+
extern "C" __declspec(dllimport) void* __stdcall GetStdHandle(unsigned long nStdHandle);
26+
extern "C" __declspec(dllimport) int __stdcall GetConsoleMode(void* hConsoleHandle, unsigned long* lpMode);
27+
extern "C" __declspec(dllimport) int __stdcall SetConsoleMode(void* hConsoleHandle, unsigned long dwMode);
28+
#endif
29+
2330
#define ANSI_COLOR_RED "\x1b[31m"
2431
#define ANSI_COLOR_GREEN "\x1b[32m"
2532
#define ANSI_COLOR_YELLOW "\x1b[33m"
@@ -946,6 +953,14 @@ int main(int argc, char ** argv) {
946953

947954
// set the color for the prompt which will be output initially
948955
if (params.use_color) {
956+
#if defined (_WIN32)
957+
// Enable ANSI colors on Windows 10+
958+
unsigned long dwMode = 0;
959+
void* hConOut = GetStdHandle((unsigned long)-11); // STD_OUTPUT_HANDLE (-11)
960+
if (hConOut && hConOut != (void*)-1 && GetConsoleMode(hConOut, &dwMode) && !(dwMode & 0x4)) {
961+
SetConsoleMode(hConOut, dwMode | 0x4); // ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x4)
962+
}
963+
#endif
949964
printf(ANSI_COLOR_YELLOW);
950965
}
951966

0 commit comments

Comments
 (0)