-
Notifications
You must be signed in to change notification settings - Fork 68
/
console.c
314 lines (254 loc) · 5.92 KB
/
console.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "console.h"
#include <stdlib.h>
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
static int get_console_info(HANDLE *h, COORD *pos, DWORD *size) {
CONSOLE_SCREEN_BUFFER_INFO info;
*h = GetStdHandle(STD_OUTPUT_HANDLE);
if( *h == INVALID_HANDLE_VALUE || GetConsoleScreenBufferInfo(*h, &info) == FALSE )
return -1;
*pos = info.dwCursorPosition;
*size = (DWORD)info.dwSize.X * info.dwSize.Y;
return 0;
}
#endif /* _WIN32 */
void clear_screen() {
#ifdef _WIN32
static COORD upper_left = {0, 0};
COORD dummy1;
DWORD dummy2, size;
HANDLE hstd_out;
if( (get_console_info(&hstd_out, &dummy1, &size)) == -1 )
return;
FillConsoleOutputCharacter(hstd_out, ' ', size, upper_left, &dummy2);
SetConsoleCursorPosition(hstd_out, upper_left);
#else /* ANSI */
fputs("\033[2J\033[;H", stdout);
#endif /* _WIN32 */
}
void up_cursor() {
#ifdef _WIN32
COORD pos;
DWORD dummy;
HANDLE hstd_out;
if( (get_console_info(&hstd_out, &pos, &dummy)) == -1 )
return;
--pos.Y;
SetConsoleCursorPosition(hstd_out, pos);
#else /* ANSI */
fputs("\033[A", stdout);
#endif /* _WIN32 */
}
void down_cursor() {
#ifdef _WIN32
COORD pos;
DWORD dummy;
HANDLE hstd_out;
if( (get_console_info(&hstd_out, &pos, &dummy)) == -1 )
return;
++pos.Y;
SetConsoleCursorPosition(hstd_out, pos);
#else /* ANSI */
fputs("\033[B", stdout);
#endif /* _WIN32 */
}
void left_cursor() {
#ifdef _WIN32
COORD pos;
DWORD dummy;
HANDLE hstd_out;
if( (get_console_info(&hstd_out, &pos, &dummy)) == -1 )
return;
--pos.X;
SetConsoleCursorPosition(hstd_out, pos);
#else /* ANSI */
fputs("\033[D", stdout);
#endif /* _WIN32 */
}
void right_cursor() {
#ifdef _WIN32
COORD pos;
DWORD dummy;
HANDLE hstd_out;
if( (get_console_info(&hstd_out, &pos, &dummy)) == -1 )
return;
++pos.X;
SetConsoleCursorPosition(hstd_out, pos);
#else /* ANSI */
fputs("\033[C", stdout);
#endif /* _WIN32 */
}
void move_cursor(int x, int y) {
#ifdef _WIN32
COORD dummy1;
DWORD dummy2;
HANDLE hstd_out;
const COORD pos = {x-1, y-1};
if( (get_console_info(&hstd_out, &dummy1, &dummy2)) == -1 )
return;
SetConsoleCursorPosition(hstd_out, pos);
#else /* ANSI */
printf("\033[%d;%df", y, x);
#endif /* _WIN32 */
}
void get_cursor(int* x, int* y)
{
#ifdef _WIN32
COORD pos;
DWORD dummy;
HANDLE hstd_out;
if( (get_console_info(&hstd_out, &pos, &dummy)) == -1 )
{
*x = 0;
*y = 0;
}
else
{
*x = pos.X;
*y = pos.Y;
}
#else /* ANSI */
/* TODO we always return 0/0 as the cursor position */
*x = 0;
*y = 0;
#endif /* _WIN32 */
}
void reverse_screen(int c) {
#ifdef _WIN32
/* No implementation for Windows yet */
#else /* ANSI */
if (c == 1) {
fputs("\033[7m", stdout);
} else {
fputs("\033[0m", stdout);
}
#endif
}
void set_color(int c)
{
#ifdef _WIN32
HANDLE h;
CONSOLE_SCREEN_BUFFER_INFO info;
WORD bg;
h = GetStdHandle(STD_OUTPUT_HANDLE);
if( h == INVALID_HANDLE_VALUE || GetConsoleScreenBufferInfo(h, &info) == FALSE )
return;
bg = info.wAttributes & (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
switch (c)
{
case COLOR_WHITE:
info.wAttributes = bg | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
break;
case COLOR_RED:
info.wAttributes = bg | FOREGROUND_RED;
break;
case COLOR_GREEN:
info.wAttributes = bg | FOREGROUND_GREEN;
break;
case COLOR_BLUE:
info.wAttributes = bg | FOREGROUND_BLUE;
break;
case COLOR_BLACK:
info.wAttributes = bg;
break;
case COLOR_PURPLE:
info.wAttributes = bg | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
break;
case COLOR_YELLOW:
info.wAttributes = bg | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
break;
case COLOR_CYAN:
info.wAttributes = bg | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
break;
case COLOR_ORANGE:
info.wAttributes = bg | FOREGROUND_GREEN | FOREGROUND_BLUE;
break;
case COLOR_BROWN:
info.wAttributes = bg | FOREGROUND_RED | FOREGROUND_GREEN;
break;
case COLOR_LTRED:
info.wAttributes = bg | FOREGROUND_RED | FOREGROUND_INTENSITY;
break;
case COLOR_GREY1:
info.wAttributes = bg | FOREGROUND_RED | FOREGROUND_BLUE;
break;
case COLOR_GREY2:
info.wAttributes = bg | FOREGROUND_INTENSITY;
break;
case COLOR_LTGREEN:
info.wAttributes = bg | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
break;
case COLOR_LTBLUE:
info.wAttributes = bg | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
break;
case COLOR_GREY3:
info.wAttributes = bg | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN;
break;
}
SetConsoleTextAttribute(h, info.wAttributes);
#else /* ANSI */
int fg,attr;
fg=37;
attr=0;
switch (c)
{
case COLOR_WHITE: /* Bold white */
fg=37;
attr=1;
break;
case COLOR_GREY1: /* Bold black is a dark grey */
case COLOR_BROWN:
fg=30;
attr=1;
break;
case COLOR_GREY2:
case COLOR_GREY3:
fg=37;
attr=0;
break;
case COLOR_RED:
fg=31;
attr=0;
break;
case COLOR_ORANGE:
case COLOR_LTRED:
attr=1;
fg=31;
break;
case COLOR_GREEN:
fg=32;
attr=1;
break;
case COLOR_LTGREEN:
fg=32;
attr=0;
break;
case COLOR_BLUE:
attr=1;
fg=34;
break;
case COLOR_BLACK:
fg=30;
attr=0;
break;
case COLOR_PURPLE:
attr=1;
fg=35; /* Magenta */
break;
case COLOR_YELLOW:
attr=1;
fg=33;
break;
case COLOR_CYAN:
attr=1;
fg=36;
break;
case COLOR_LTBLUE:
attr=0;
fg=36;
break;
}
printf("\033[%d;%d;40m", attr,fg);
#endif /* _WIN32 */
}