Try to enable ANSI escape codes in Windows terminal

This commit is contained in:
Robert Bendun 2022-11-21 01:52:26 +01:00
parent a6b54d231d
commit a32bfa24d3
2 changed files with 27 additions and 0 deletions

View File

@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Index operation using booleans behaves like a mask and not fancy way of spelling 0 and 1 - Index operation using booleans behaves like a mask and not fancy way of spelling 0 and 1
- Blocks are check against beeing a collection at runtime to prevent treating anonymous functions as collections and cousing assertions - Blocks are check against beeing a collection at runtime to prevent treating anonymous functions as collections and cousing assertions
- On Windows default terminal emulator ansi escape codes are conditionally supported. Review musique/pretty.cc for details
### Removed ### Removed

View File

@ -1,5 +1,11 @@
#include <musique/pretty.hh> #include <musique/pretty.hh>
#ifdef _WIN32
extern "C" {
#include <windows.h>
}
#endif
namespace starters namespace starters
{ {
static std::string_view Error; static std::string_view Error;
@ -30,6 +36,26 @@ std::ostream& pretty::end(std::ostream& os)
void pretty::terminal_mode() void pretty::terminal_mode()
{ {
// Windows 10 default commandline window doesn't support ANSI escape codes
// by default in terminal output. This feature can be enabled via
// ENABLE_VIRTUAL_TERMINAL_INPUT flag. Windows Terminal shouldn't have this issue.
#ifdef _WIN32 // FIXME replace this preprocessor hack with if constexpr
{
auto standard_output = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD mode;
if (!GetConsoleMode(standard_output, &mode) ) {
return;
}
if ((mode & ENABLE_VIRTUAL_TERMINAL_INPUT) != ENABLE_VIRTUAL_TERMINAL_INPUT) {
mode |= ENABLE_VIRTUAL_TERMINAL_INPUT;
if (!SetConsoleMode(standard_output, mode)) {
return;
}
}
}
#endif
starters::Error = "\x1b[31;1m"; starters::Error = "\x1b[31;1m";
starters::Path = "\x1b[34;1m"; starters::Path = "\x1b[34;1m";
starters::Comment = "\x1b[30;1m"; starters::Comment = "\x1b[30;1m";