-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUserInterface.cpp
224 lines (199 loc) · 7.35 KB
/
UserInterface.cpp
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
#include <conio.h>
#include "UserInterface.h"
bool UserInterface::isElevated() {
BOOL bRet = FALSE;
HANDLE hToken = nullptr;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
TOKEN_ELEVATION teElevation;
DWORD lSize = sizeof(TOKEN_ELEVATION);
if (GetTokenInformation(hToken, TokenElevation, &teElevation, sizeof(teElevation), &lSize)) {
bRet = (bool) teElevation.TokenIsElevated;
}
}
if (hToken) {
CloseHandle(hToken);
}
return bRet;
}
COORD UserInterface::_getScreenSize() const {
CONSOLE_SCREEN_BUFFER_INFO csbiConsoleScreenBufferInfo;
GetConsoleScreenBufferInfo(_hConsoleHandle, &csbiConsoleScreenBufferInfo);
int iWidth = csbiConsoleScreenBufferInfo.srWindow.Right - csbiConsoleScreenBufferInfo.srWindow.Left + 1;
int iHeight = csbiConsoleScreenBufferInfo.srWindow.Bottom - csbiConsoleScreenBufferInfo.srWindow.Top + 1;
return {static_cast<SHORT>(iWidth), static_cast<SHORT>(iHeight)};
}
COORD UserInterface::_getCursorPosition() const {
CONSOLE_SCREEN_BUFFER_INFO csbiConsoleScreenBufferInfo;
if (!GetConsoleScreenBufferInfo(_hConsoleHandle, &csbiConsoleScreenBufferInfo)) {
return {-1, -1};
}
return {csbiConsoleScreenBufferInfo.dwCursorPosition.X, csbiConsoleScreenBufferInfo.dwCursorPosition.Y};
}
void UserInterface::_setCursorPosition(int x, int y) const {
SetConsoleCursorPosition(_hConsoleHandle, {static_cast<short>(x), static_cast<short>(y)});
}
void UserInterface::_moveCursor(int dx, int dy) const {
COORD coordPos = _getCursorPosition();
_setCursorPosition(coordPos.X + dx, coordPos.Y + dy);
}
void UserInterface::_hideInput() const {
DWORD lMode = 0;
GetConsoleMode(_hInputHandle, &lMode);
SetConsoleMode(_hInputHandle, lMode & (~ENABLE_ECHO_INPUT));
}
void UserInterface::_showInput() const {
DWORD lMode = 0;
GetConsoleMode(_hInputHandle, &lMode);
SetConsoleMode(_hInputHandle, lMode | ENABLE_ECHO_INPUT);
}
UserInterface::UserInterface() {
if (!isElevated()) {
printf("Please run this program as administrator!\n");
system("pause");
exit(0);
}
_hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
_hInputHandle = GetStdHandle(STD_INPUT_HANDLE);
clearScreen();
// disable wrap
DWORD lMode;
GetConsoleMode(_hConsoleHandle, &lMode);
lMode &= ~ENABLE_WRAP_AT_EOL_OUTPUT;
SetConsoleMode(_hConsoleHandle, lMode);
setColor(DEFAULT_COLOR);
}
UserInterface::~UserInterface() {
CloseHandle(_hConsoleHandle);
CloseHandle(_hInputHandle);
}
void UserInterface::clearScreen() const {
COORD coordTopLeft = {0, 0};
DWORD lCount;
CONSOLE_SCREEN_BUFFER_INFO csbiConsoleScreenBufferInfo;
GetConsoleScreenBufferInfo(_hConsoleHandle, &csbiConsoleScreenBufferInfo);
FillConsoleOutputCharacterA(_hConsoleHandle, ' ',
csbiConsoleScreenBufferInfo.dwSize.X * csbiConsoleScreenBufferInfo.dwSize.Y,
coordTopLeft, &lCount);
SetConsoleCursorPosition(_hConsoleHandle, coordTopLeft);
}
void UserInterface::setColor(const int color) const {
SetConsoleTextAttribute(_hConsoleHandle, color);
}
void UserInterface::print(const std::string& text, const int color, const bool newLine) const {
setColor(color);
printf("%s", text.c_str());
if (newLine) {
printf("\n");
}
setColor(DEFAULT_COLOR);
}
void UserInterface::print(const std::vector<std::string>& items, int color, bool newLine, char separator) const {
for (const std::string& strItem: items) {
print(strItem, color, false);
if (strItem != items.back() && items.size() > 1) {
print(std::string(1, separator), color, false);
}
}
if (newLine) {
printLineBreak();
}
}
void UserInterface::printTable(const std::vector<std::vector<std::string>>& items, int color, bool newLine,
char separator, bool center
) const {
std::vector<int> vtMaxWidth;
for (const std::vector<std::string>& vtItem: items) {
for (int i = 0; i < vtItem.size(); i++) {
if (vtMaxWidth.size() <= i) {
vtMaxWidth.push_back(0);
}
if (vtItem[i].length() > vtMaxWidth[i]) {
vtMaxWidth[i] = (int) vtItem[i].length();
}
}
}
std::vector<std::string> vtStrItems;
for (const std::vector<std::string>& vtItem: items) {
std::string strItem;
for (const auto & s : vtItem) {
strItem += s + std::string(vtMaxWidth[&s - &vtItem[0]] - s.length(), ' ') + separator;
}
vtStrItems.push_back(strItem);
}
for (const std::string& strItem: vtStrItems) {
if (center) printCentered(strItem, color, false);
else print(strItem, color, false);
if (strItem != vtStrItems.back() && vtStrItems.size() > 1) {
printLineBreak();
}
}
if (newLine) {
printLineBreak();
}
}
void UserInterface::printCentered(const std::string& text, int color, bool newLine, char padding, char cap,
int capColor
) const {
COORD coordScreenSize = _getScreenSize();
int iPaddingLengthLeft = static_cast<int>((coordScreenSize.X - text.length()) / 2);
int iPaddingLengthRight = (int) (coordScreenSize.X - text.length()) - iPaddingLengthLeft;
std::string strPaddingLeft(iPaddingLengthLeft, padding);
std::string strPaddingRight(iPaddingLengthRight, padding);
strPaddingLeft[0] = cap;
strPaddingRight[strPaddingRight.length() - 1] = cap;
print(strPaddingLeft, capColor, false);
print(text, color, false);
print(strPaddingRight, capColor, newLine);
}
void UserInterface::printLineBreak(const char c, const int color, bool newLine) const {
COORD coordScreenSize = _getScreenSize();
setColor(color);
for (int i = 0; i < coordScreenSize.X; i++) {
printf("%c", c);
}
if (newLine) {
_setCursorPosition(0, _getCursorPosition().Y + 1);
}
setColor(DEFAULT_COLOR);
}
void UserInterface::printTitle(const std::string& text, int textColor, int borderColor,
bool cap
) const {
printLineBreak(cBorder, borderColor);
if (cap) {
printCentered(" ", textColor, true, ' ', cBorder, borderColor);
printCentered(text, textColor, true, ' ', cBorder, borderColor);
printCentered(" ", textColor, true, ' ', cBorder, borderColor);
} else {
printLineBreak();
printCentered(text, textColor, true);
printLineBreak();
}
printLineBreak(cBorder, borderColor);
}
void UserInterface::printMultiLine(const std::vector<std::string>& items, int color) const {
for (const std::string& strItem: items) {
print(strItem, color, false);
printLineBreak();
}
}
std::string UserInterface::input(const std::string& message, int color, bool hideInput, char hideChar) const {
setColor(color);
printf("%s ", message.c_str());
std::string strInput;
char c;
while ((int) (c = (char) _getch()) != 13) { // enter
if (c == 8) { // backspace
if (!strInput.empty()) {
printf("\b \b");
strInput.pop_back();
}
} else {
printf("%c", hideInput ? hideChar : c);
strInput.push_back(c);
}
}
printf("\n");
setColor(DEFAULT_COLOR);
return strInput;
}