diff options
Diffstat (limited to 'src/util_term/util_term.c')
-rw-r--r-- | src/util_term/util_term.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/util_term/util_term.c b/src/util_term/util_term.c new file mode 100644 index 0000000..6ea6a12 --- /dev/null +++ b/src/util_term/util_term.c @@ -0,0 +1,45 @@ + +#include "commonbase.h" + +/* System */ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <unistd.h> + + +#ifdef _WIN32 +static HANDLE +fdToHandle( int fd ){ + switch( fd ){ + case 1: return GetStdHandle(STD_OUTPUT_HANDLE); + case 2: return GetStdHandle(STD_ERROR_HANDLE ); + default: assert(0); return NULL; + } +} +#endif + + +#ifdef _WIN32 +static void +termColorsEnable( int fd ) +{ + HANDLE hOut = fdToHandle( fd ); + if( hOut==INVALID_HANDLE_VALUE ){ return; } + DWORD dwMode = 0; + if( ! GetConsoleMode(hOut,&dwMode) ){ return; } + dwMode |= 0x0004; /* <- ENABLE_VIRTUAL_TERMINAL_PROCESSING */ + if( ! SetConsoleMode(hOut, dwMode) ){ return; } +} +#endif + + +void +util_term_init() +{ + #ifdef _WIN32 + if( isatty(1) ){ termColorsEnable(1); } + if( isatty(2) ){ termColorsEnable(2); } + #endif +} + |