From cd475afc63adca233e4d296ce9c2e67e114bcb01 Mon Sep 17 00:00:00 2001 From: Anders Petersen Date: Fri, 21 Jan 2022 00:49:45 +0100 Subject: [PATCH] Make colored output respect background color on Windows --- server_wfh/server_wfh.d | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/server_wfh/server_wfh.d b/server_wfh/server_wfh.d index 3cf0d75..06af61e 100644 --- a/server_wfh/server_wfh.d +++ b/server_wfh/server_wfh.d @@ -190,11 +190,12 @@ version(Windows){ import core.sys.windows.wincon; enum Color : ushort { - tcp_info = FOREGROUND_RED | FOREGROUND_GREEN, - tcp_data = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY, - udp_info = FOREGROUND_BLUE | FOREGROUND_GREEN, - udp_data = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY, + tcp_info = FOREGROUND_RED | FOREGROUND_BLUE, + tcp_data = FOREGROUND_RED | FOREGROUND_GREEN, + udp_info = FOREGROUND_GREEN, + udp_data = FOREGROUND_GREEN | FOREGROUND_BLUE, } + enum BACKGROUND_MASK = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY; void writefcln(Char, A...)(Color c, in Char[] fmt, A args){ synchronized(consoleMtx){ @@ -204,7 +205,10 @@ version(Windows){ GetConsoleScreenBufferInfo(hConsole, &consoleInfo); scope(exit) SetConsoleTextAttribute(hConsole, consoleInfo.wAttributes); - SetConsoleTextAttribute(hConsole, c); + SetConsoleTextAttribute(hConsole, + c | + (consoleInfo.wAttributes & BACKGROUND_MASK) | + ((consoleInfo.wAttributes & BACKGROUND_INTENSITY) ? 0 : FOREGROUND_INTENSITY)); writefln(fmt, args); } }