-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwin32_con.c
More file actions
70 lines (58 loc) · 1.4 KB
/
win32_con.c
File metadata and controls
70 lines (58 loc) · 1.4 KB
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
/* win32_con.c */
HANDLE handle_stdout;
CONSOLE_SCREEN_BUFFER_INFO console_info;
WORD old_text_attributes;
/* private: */
void init_con() {
handle_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(handle_stdout, &console_info);
old_text_attributes = console_info.wAttributes;
}
int windows_console_color(int color) {
return color = color & 10 | (color & 1) << 2 | (color & 4) >> 2;
}
/* public: */
bool con_is_stdout_tty() {
return GetFileType(handle_stdout) == FILE_TYPE_CHAR;
}
void con_clear() {
COORD coord = {0, 0};
DWORD count = console_info.dwSize.X * console_info.dwSize.Y;
FillConsoleOutputCharacter(
handle_stdout, (TCHAR) ' ',
count, coord, &count
);
FillConsoleOutputAttribute(
handle_stdout, old_text_attributes,
count, coord, &count
);
SetConsoleCursorPosition(handle_stdout, coord);
}
void con_set_color(int fg, int bg) {
if (fg < 0) {
fg = old_text_attributes & 0xf;
} else {
fg = windows_console_color(fg);
}
if (bg < 0) {
bg = old_text_attributes & 0xf0;
} else {
bg = windows_console_color(bg);
}
SetConsoleTextAttribute(handle_stdout, bg << 4 | fg);
}
void con_reset_color() {
SetConsoleTextAttribute(handle_stdout, old_text_attributes);
}
void con_set_pos(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(handle_stdout, coord);
}
void con_alternate() {
con_clear();
}
void con_alternate_exit() {
con_clear();
}