-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
127 lines (86 loc) · 3 KB
/
Utils.cpp
File metadata and controls
127 lines (86 loc) · 3 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
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
#include "Utils.h"
void Utils::setCursor(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void Utils::setColor(unsigned short int code) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, code);
}
void Utils::resetColor() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, DEFAULT_TXT);
}
void Utils::showConsoleCursor(bool showFlag) {
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(out, &cursorInfo);
// Set the cursor visibility
cursorInfo.bVisible = showFlag;
SetConsoleCursorInfo(out, &cursorInfo);
}
void Utils::clear() {
// system("cls");
/* This works far better than cls
It takes the cursor to first word of top line and starts to overwrite the text
so the flickering just stops */
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {0, 0});
}
void Utils::clearScreen() {
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
COORD coord = { 0, 0 };
DWORD count;
if (GetConsoleScreenBufferInfo(hStdOut, &csbi)) {
DWORD consoleSize = csbi.dwSize.X * csbi.dwSize.Y;
// Reset text and background color to default
// WORD defaultAttributes = csbi.wAttributes;
// FillConsoleOutputAttribute(hStdOut, defaultAttributes, consoleSize, coord, &count);
// Clear screen buffer with white blank spaces
FillConsoleOutputCharacter(hStdOut, ' ', consoleSize, coord, &count);
// Reset cursor on initial position
SetConsoleCursorPosition(hStdOut, coord);
}
}
void Utils::drawElement(const char &element) {
std::cout << element;
}
void Utils::drawElement(const std::string &element) {
std::cout << element << std::endl;
}
void Utils::drawElement(const char &element, int color) {
setColor(color);
std::cout << element;
setColor(DEFAULT_TXT);
}
void Utils::drawElement(const std::string &element, int color) {
setColor(color);
std::cout << element << std::endl;
setColor(DEFAULT_TXT);
}
int Utils::genRandomInt(int min, int max) {
// Obtain a random number from hardware
std::random_device rd;
// Seed the generator
std::mt19937 gen(rd());
// Define the range
std::uniform_int_distribution<> distr(min, max);
// Generate number
return distr(gen);
}
bool Utils::pauseRoutine(const std::string &diagMsg, const std::string &question, const std::string &opt1, const std::string &opt2) {
/* Returns true if user choose opt1, false if choose opt2 */
bool ret = true;
showConsoleCursor(true);
std::cout << diagMsg << "\n";
std::string choice = "";
while (choice != opt1 && choice != opt2) {
std::cout << question << " (" << opt1 << "/" << opt2 << ") ";
std::cin >> choice;
if (choice == opt2)
ret = false;
}
return ret;
}