-
Notifications
You must be signed in to change notification settings - Fork 13
/
wac.c
81 lines (73 loc) · 2.69 KB
/
wac.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include <windows.h>
int main(void) {
const int W_BLACK = 0;
const int W_BLUE = 1;
const int W_GREEN = 2;
const int W_RED = 4;
const int W_INTENSITY = 8;
const int W_CYAN = W_BLUE | W_GREEN;
const int W_MAGENTA = W_BLUE | W_RED;
const int W_YELLOW = W_GREEN | W_RED;
const int W_WHITE = W_BLUE | W_GREEN | W_RED;
const int ANSI2WIN[] = {W_BLACK, W_RED, W_GREEN, W_YELLOW, W_BLUE, W_MAGENTA, W_CYAN, W_WHITE};
const int RESET = 0;
const int BOLD = 1;
const int BLACK = 30;
const int RED = 31;
const int GREEN = 32;
const int YELLOW = 33;
const int BLUE = 34;
const int MAGENTA = 35;
const int CYAN = 36;
const int GRAY = 37;
const int WHITE = 37;
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo);
int initialColor = consoleInfo.wAttributes;
int state, ansiColor, multiplication, pos, winColor, winIntensity = -1;
int colors[] = {-1,-1};
int colorPos = 1;
int ch;
while ((ch = getchar()) != EOF) {
if(ch == '\e') {
state = '\e';
} else if (state == '\e' && ch == '[') {
state = '[';
} else if (state == '[') {
if (ch != 'm') {
colors[colorPos] = ch;
colorPos--;
} else {
// Find ANSI Color
ansiColor = 0;
multiplication = 1;
for (pos = colorPos + 1; pos < 2; pos++) {
ansiColor += (colors[pos] - 48) * multiplication;
multiplication *= 10;
}
// Convert ANSI Color to Windows Color
if (ansiColor == BOLD) {
winIntensity = W_INTENSITY;
} else if (ansiColor == RESET) {
winIntensity = W_BLACK;
winColor = initialColor;
} else if (BLACK <= ansiColor && ansiColor <= WHITE) {
winColor = ANSI2WIN[ansiColor - 30];
} else if (ansiColor == 90) {
// Special case for gray (it's really white)
winColor = W_WHITE;
winIntensity = W_BLACK;
}
// initialColor & 0xF0 is to keep the background color
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), winColor | winIntensity | (initialColor & 0xF0));
colorPos = 1;
state = -1;
}
} else {
putchar(ch);
}
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), initialColor);
return 0;
}