aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoranzz1 <anzz1@live.com>2023-03-21 18:14:46 +0200
committerGitHub <noreply@github.com>2023-03-21 18:14:46 +0200
commitc86ba036e613d46815501a4c6775117c9fc7afce (patch)
treee03a99983ddb1a178d05f22c66c7683319983804
parent1daf4dd71235dbbf537738e7ad53daad8d97586f (diff)
Enable ANSI colors on Windows 10+ (#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 <ggerganov@gmail.com>
-rw-r--r--main.cpp15
1 files changed, 15 insertions, 0 deletions
diff --git a/main.cpp b/main.cpp
index bda824f..dd8e52d 100644
--- a/main.cpp
+++ b/main.cpp
@@ -20,6 +20,13 @@
#include <signal.h>
#endif
+#if defined (_WIN32)
+#pragma comment(lib,"kernel32.lib")
+extern "C" __declspec(dllimport) void* __stdcall GetStdHandle(unsigned long nStdHandle);
+extern "C" __declspec(dllimport) int __stdcall GetConsoleMode(void* hConsoleHandle, unsigned long* lpMode);
+extern "C" __declspec(dllimport) int __stdcall SetConsoleMode(void* hConsoleHandle, unsigned long dwMode);
+#endif
+
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
@@ -946,6 +953,14 @@ int main(int argc, char ** argv) {
// set the color for the prompt which will be output initially
if (params.use_color) {
+#if defined (_WIN32)
+ // Enable ANSI colors on Windows 10+
+ unsigned long dwMode = 0;
+ void* hConOut = GetStdHandle((unsigned long)-11); // STD_OUTPUT_HANDLE (-11)
+ if (hConOut && hConOut != (void*)-1 && GetConsoleMode(hConOut, &dwMode) && !(dwMode & 0x4)) {
+ SetConsoleMode(hConOut, dwMode | 0x4); // ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x4)
+ }
+#endif
printf(ANSI_COLOR_YELLOW);
}