This repository has been archived by the owner on Sep 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPConsoleLib.c
228 lines (204 loc) · 4.51 KB
/
APConsoleLib.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
/* APConsoleLib.c
* Library for portable (win & linux) console functions with 16 colors
* Copyright (C) 2008 Anton Pirogov
*/
#include "APConsoleLib.h"
/* TODO: Background colors set (in windows just higher numbers, linux escape seq
* "\033[4nm" where n = 0..7 */
enum ConsoleColors {
BLACK,
DARK_GRAY,
GRAY,
WHITE,
RED,
LIGHT_RED,
YELLOW,
DARK_YELLOW,
LIGHT_GREEN,
GREEN,
LIGHT_CYAN,
CYAN,
LIGHT_BLUE,
BLUE,
LIGHT_PURPLE,
PURPLE
};
#ifdef WIN32 /* Windows version */
void clearConsole(void)
{
COORD coordScreen = {0, 0}; /* upper left corner */
DWORD cCharsWritten;
DWORD dwConSize;
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hCon, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
/* fill with spaces */
FillConsoleOutputCharacter(hCon, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hCon, &csbi);
FillConsoleOutputAttribute(hCon, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
/* cursor to upper left corner */
SetConsoleCursorPosition(hCon, coordScreen);
}
void consoleGotoXY(short x, short y)
{
COORD coord = {x, y};
HANDLE StdOutHwnd = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(StdOutHwnd, coord);
}
void setConsoleColor(short clr)
{
HANDLE StdOutHwnd = GetStdHandle(STD_OUTPUT_HANDLE);
WORD color;
switch (clr) {
case BLACK: color = 0;
break;
case RED: color = 4;
break;
case GREEN: color = 2;
break;
case DARK_YELLOW: color = 6;
break;
case BLUE: color = 1;
break;
case PURPLE: color = 5;
break;
case CYAN: color = 3;
break;
case GRAY: color = 7;
break;
case DARK_GRAY: color = 8;
break;
case LIGHT_RED: color = 12;
break;
case LIGHT_GREEN: color = 10;
break;
case YELLOW: color = 14;
break;
case LIGHT_BLUE: color = 9;
break;
case LIGHT_PURPLE: color = 13;
break;
case LIGHT_CYAN: color = 11;
break;
case WHITE: color = 15;
break;
}
SetConsoleTextAttribute(StdOutHwnd, color);
}
void setConsoleTitle(char* title)
{
SetConsoleTitle(title);
}
void setConsoleSize(short x, short y)
{
HANDLE wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT windowSize = {0, 0, x - 1, y - 1};
SetConsoleWindowInfo(wHnd, TRUE, &windowSize);
COORD bufferSize = {x, y};
SetConsoleScreenBufferSize(wHnd, bufferSize);
}
void getConsoleSize(short *x, short *y)
{
HANDLE hOut;
CONSOLE_SCREEN_BUFFER_INFO SBInfo;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hOut, &SBInfo);
*x = SBInfo.dwSize.X;
*y = SBInfo.dwSize.Y;
}
#else /* Linux version */
int getch(void)
{
static int ch = -1, fd = 0;
struct termios neu, alt;
fd = fileno(stdin);
tcgetattr(fd, &alt);
neu = alt;
neu.c_lflag &= ~(ICANON | ECHO);
tcsetattr(fd, TCSANOW, &neu);
ch = getchar();
tcsetattr(fd, TCSANOW, &alt);
return ch;
}
int kbhit(void)
{
struct termios term, oterm;
int fd = 0;
int c = 0;
tcgetattr(fd, &oterm);
memcpy(&term, &oterm, sizeof (term));
term.c_lflag = term.c_lflag & (!ICANON);
term.c_cc[VMIN] = 0;
term.c_cc[VTIME] = 1;
tcsetattr(fd, TCSANOW, &term);
c = getchar();
tcsetattr(fd, TCSANOW, &oterm);
if (c != -1)
ungetc(c, stdin);
return ((c != -1) ? 1 : 0);
}
void clearConsole(void)
{
/*char a[80];*/
printf("\033[2J"); /* Clear the entire screen. */
printf("\033[0;0f"); /* Move cursor to the top left hand corner */
}
void consoleGotoXY(short x, short y)
{
printf("\033[%i;%if", y, x);
}
void setConsoleColor(short clr)
{
switch (clr) {
case BLACK: printf("\033[0;30m");
break;
case RED: printf("\033[0;31m");
break;
case GREEN: printf("\033[0;32m");
break;
case DARK_YELLOW: printf("\033[0;33m");
break;
case BLUE: printf("\033[0;34m");
break;
case PURPLE: printf("\033[0;35m");
break;
case CYAN: printf("\033[0;36m");
break;
case GRAY: printf("\033[0;37m");
break;
case DARK_GRAY: printf("\033[1;30m");
break;
case LIGHT_RED: printf("\033[1;31m");
break;
case LIGHT_GREEN: printf("\033[1;32m");
break;
case YELLOW: printf("\033[1;33m");
break;
case LIGHT_BLUE: printf("\033[1;34m");
break;
case LIGHT_PURPLE: printf("\033[1;35m");
break;
case LIGHT_CYAN: printf("\033[1;36m");
break;
case WHITE: printf("\033[1;37m");
break;
}
}
void setConsoleTitle(char* title)
{
printf("\033]0;%s\007", title);
}
void setConsoleSize(short xsize, short ysize)
{
char rcmd[32];
sprintf(rcmd, "resize -s %i %i > /dev/null", ysize, xsize);
system(rcmd);
}
void getConsoleSize(short *xsize, short *ysize)
{
FILE *pipe = popen("stty size", "r");
fscanf(pipe, "%hi%hi", ysize, xsize);
pclose(pipe);
}
#endif