-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor-detect.c
52 lines (52 loc) · 1.32 KB
/
color-detect.c
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
47
48
49
50
51
52
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#endif
#include <stdio.h>
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
bool is_support_ansi_color(FILE *file) {
if (file == NULL)
file = stdout;
#ifdef _WIN32
if (_isatty(_fileno(file)) == 0)
return false;
#else
if (isatty(fileno(file)) == 0)
return false;
#endif
const char *no_color_env = getenv("NO_COLOR");
if (no_color_env != NULL && no_color_env[0] != '\0')
return false;
const char *term_env = getenv("TERM");
size_t term_len = 0;
if (term_env)
term_len = strlen(term_env);
if (term_env != NULL &&
(term_len < 5 ||
(term_len >= 5 && strcmp(term_env + term_len - 5, "color") != 0)))
return false;
#ifdef _WIN32
DWORD console_mode;
if (GetConsoleMode((HANDLE)_get_osfhandle(_fileno(file)), &console_mode) &&
(ENABLE_VIRTUAL_TERMINAL_PROCESSING & console_mode) !=
ENABLE_VIRTUAL_TERMINAL_PROCESSING &&
SetConsoleMode((HANDLE)_get_osfhandle(_fileno(file)),
console_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING) ==
FALSE)
return false;
#endif
return true;
}
/*
#include <stdio.h>
int main() {
printf("%d %d %d\n", is_support_ansi_color(stdout),
is_support_ansi_color(stderr),is_support_ansi_color(NULL));
}
*/