From 65a43e2d53d0e9b8e57a58a76e1edb535d3ef21c Mon Sep 17 00:00:00 2001 From: Jan Date: Fri, 8 Dec 2017 15:47:07 +0100 Subject: [PATCH] issue #3741: attempt to fix terminal color output in Windows (#3753) (#3937) * issue #3741: attempt to fix terminal color output in Windows (#3753) * Fix for missing constant in older Windows versions (issue 3742) --- CHANGELOG | 2 ++ .../ShellColorsFeature.cpp | 27 +++++++++++++++++++ lib/ApplicationFeatures/ShellColorsFeature.h | 1 + lib/Basics/win-utils.cpp | 10 +++---- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 6cf0cd6a9a..ea195a9a90 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,8 @@ v3.3.rc7 (2017-12-07) --------------------- +* fixed issue #3741: fix terminal color output in Windows + * UI: fixed issue #3822: disabled name input field for system collections * fixed issue #3640: limit in subquery diff --git a/lib/ApplicationFeatures/ShellColorsFeature.cpp b/lib/ApplicationFeatures/ShellColorsFeature.cpp index d5f956c17e..0172953115 100644 --- a/lib/ApplicationFeatures/ShellColorsFeature.cpp +++ b/lib/ApplicationFeatures/ShellColorsFeature.cpp @@ -24,6 +24,9 @@ #ifdef _WIN32 #include "Basics/win-utils.h" +#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING +#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004 +#endif #endif using namespace arangodb; @@ -86,8 +89,32 @@ void ShellColorsFeature::prepare() { bool ShellColorsFeature::useColors() { #ifdef _WIN32 + if (!prepareConsole()) { + return false; + } return terminalKnowsANSIColors(); #else return true; #endif } + +bool ShellColorsFeature::prepareConsole() { +#ifdef _WIN32 + HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); + if (hStdout == INVALID_HANDLE_VALUE) { + return false; + } + + DWORD handleMode = 0; + if (!GetConsoleMode(hStdout, &handleMode)) { + return false; + } + handleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; + if (!SetConsoleMode(hStdout, handleMode)) { + return false; + } + return true; +#else + return true; +#endif +} diff --git a/lib/ApplicationFeatures/ShellColorsFeature.h b/lib/ApplicationFeatures/ShellColorsFeature.h index dbcabe4df6..2edc4304ad 100644 --- a/lib/ApplicationFeatures/ShellColorsFeature.h +++ b/lib/ApplicationFeatures/ShellColorsFeature.h @@ -35,6 +35,7 @@ class ShellColorsFeature final : public application_features::ApplicationFeature private: bool useColors(); + bool prepareConsole(); public: static char const* SHELL_COLOR_RED; diff --git a/lib/Basics/win-utils.cpp b/lib/Basics/win-utils.cpp index 4f2ed18b09..1ad2c3e845 100644 --- a/lib/Basics/win-utils.cpp +++ b/lib/Basics/win-utils.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include "Logger/Logger.h" @@ -558,15 +559,14 @@ void ADB_WindowsExitFunction(int exitCode, void* data) { } // Detect cygwin ssh / terminals -int _cyg_isatty(int fd) -{ +int _cyg_isatty(int fd) { // detect standard windows ttys: if (_isatty (fd)) { return 1; } // stupid hack to allow forcing a tty..need to understand this better - // and create a thorugh fix..without this the logging stuff will not + // and create a thorough fix..without this the logging stuff will not // log to the foreground which is super annoying for debugging the // resilience tests char* forcetty = getenv("FORCE_WINDOWS_TTY"); @@ -660,8 +660,7 @@ int _is_cyg_tty(int fd) return 0; } -bool terminalKnowsANSIColors() -{ +bool terminalKnowsANSIColors() { if (_is_cyg_tty (STDOUT_FILENO)) { // Its a cygwin shell, expected to understand ANSI color codes. return true; @@ -671,7 +670,6 @@ bool terminalKnowsANSIColors() return IsWindows8OrGreater(); } -#include std::string getFileNameFromHandle(HANDLE fileHandle) { char buff[sizeof(FILE_NAME_INFO) + sizeof(WCHAR)*MAX_PATH]; FILE_NAME_INFO *FileInformation = (FILE_NAME_INFO*) buff;