summaryrefslogtreecommitdiff
path: root/src/util_term/util_term.c
blob: e921eccf847b204aed85d609db05a867dd2fbd19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* By using this work you agree to the terms and conditions in 'LICENSE.txt' */

#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
}