Skip to content

Commit

Permalink
Make formatter enable Windows ANSI colors (CleverRaven#67021)
Browse files Browse the repository at this point in the history
  • Loading branch information
irwiss authored Jul 20, 2023
1 parent 520db0c commit d7ab3d9
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tools/format/format_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,28 @@
#include <string>

#if defined(_WIN32)
#include <Windows.h>

static void erase_char( std::string &s, const char &c )
{
s.erase( std::remove( s.begin(), s.end(), c ), s.end() );
}
#endif

static bool enable_stdout_ansi_colors()
{
#if defined(_WIN32)
// enable ANSI colors on windows consoles https://superuser.com/a/1529908
DWORD dwMode;
GetConsoleMode( GetStdHandle( STD_OUTPUT_HANDLE ), &dwMode );
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
// may fail on Windows 10 earlier than 1511
return SetConsoleMode( GetStdHandle( STD_OUTPUT_HANDLE ), dwMode );
#else
return true;
#endif
}

int main( int argc, char *argv[] )
{
#if defined(_MSC_VER)
Expand All @@ -36,6 +52,10 @@ int main( int argc, char *argv[] )
? json_error_output_colors_t::ansi_escapes
: json_error_output_colors_t::no_colors;

if( !enable_stdout_ansi_colors() ) {
json_error_output_colors = json_error_output_colors_t::no_colors;
}

std::stringstream in;
std::stringstream out;
std::string filename;
Expand Down

0 comments on commit d7ab3d9

Please sign in to comment.