Windows: fixed buffer resize causes no colors

This commit is contained in:
Gator96100 2021-06-01 16:05:28 +02:00 committed by Philippe Teuwen
commit ed69517252

View file

@ -171,6 +171,30 @@ static void terminate_handler(int signum) {
#endif #endif
#if defined(_WIN32)
static bool DetectWindowsAnsiSupport(void) {
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
// disable colors if stdin or stdout are redirected
if ((! session.stdinOnTTY) || (! session.stdoutOnTTY))
return false;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode = 0;
GetConsoleMode(hOut, &dwMode);
//ENABLE_VIRTUAL_TERMINAL_PROCESSING is already set
if((dwMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING))
return true;
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
return SetConsoleMode(hOut, dwMode) ? true : false;
}
#endif //_WIN32
// first slot is always NULL, indicating absence of script when idx=0 // first slot is always NULL, indicating absence of script when idx=0
static FILE *cmdscriptfile[MAX_NESTED_CMDSCRIPT + 1] = {0}; static FILE *cmdscriptfile[MAX_NESTED_CMDSCRIPT + 1] = {0};
static uint8_t cmdscriptfile_idx = 0; static uint8_t cmdscriptfile_idx = 0;
@ -360,6 +384,10 @@ check_script:
g_pendingPrompt = true; g_pendingPrompt = true;
#ifdef HAVE_READLINE #ifdef HAVE_READLINE
script_cmd = readline(prompt_filtered); script_cmd = readline(prompt_filtered);
#if defined(_WIN32)
//Check if color support needs to be enabled again in case the window buffer did change
session.supports_colors = DetectWindowsAnsiSupport();
#endif
if (script_cmd != NULL) { if (script_cmd != NULL) {
execCommand = true; execCommand = true;
stayInCommandLoop = true; stayInCommandLoop = true;
@ -705,21 +733,6 @@ finish2:
} }
#endif //LIBPM3 #endif //LIBPM3
#if defined(_WIN32)
static bool DetectWindowsAnsiSupport(void) {
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode = 0;
GetConsoleMode(hOut, &dwMode);
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
return SetConsoleMode(hOut, dwMode) ? true : false;
}
#endif //_WIN32
void pm3_init(void) { void pm3_init(void) {
srand(time(0)); srand(time(0));
@ -783,6 +796,9 @@ int main(int argc, char *argv[]) {
#if defined(__linux__) || defined(__APPLE__) #if defined(__linux__) || defined(__APPLE__)
session.supports_colors = true; session.supports_colors = true;
session.emoji_mode = EMO_EMOJI; session.emoji_mode = EMO_EMOJI;
#elif defined(_WIN32)
session.supports_colors = DetectWindowsAnsiSupport();
session.emoji_mode = EMO_ALTTEXT;
#endif #endif
} }
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
@ -996,11 +1012,6 @@ int main(int argc, char *argv[]) {
session.emoji_mode = EMO_ALTTEXT; session.emoji_mode = EMO_ALTTEXT;
} }
#if defined(_WIN32) //Color support on Windows has to be enabled each time and can fail, override prefs
session.supports_colors = DetectWindowsAnsiSupport();
session.emoji_mode = EMO_ALTTEXT;
#endif
// Let's take a baudrate ok for real UART, USB-CDC & BT don't use that info anyway // Let's take a baudrate ok for real UART, USB-CDC & BT don't use that info anyway
if (speed == 0) if (speed == 0)
speed = USART_BAUD_RATE; speed = USART_BAUD_RATE;