-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDisplayManager.cpp
More file actions
272 lines (249 loc) · 6.33 KB
/
Copy pathDisplayManager.cpp
File metadata and controls
272 lines (249 loc) · 6.33 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
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
/**
* \file DisplayManager.cpp
* This example of functions allows to personalize the display messages. \n
* - Implement Init Progress Bar function to add a progress bar.
* - Implement load Bar function to animate the loading process 0% to 100%.
* - Implement DisplayMessage function to ensure the display of internal DLL messages.
* - Implement logMessage function to personalize the display (user messages).
*.
* Go to the source code : \ref DisplayManager
\example DisplayManager
* This example of functions allows to personalize the display messages. \n
* - Implement Init Progress Bar function to add a progress bar.
* - Implement load Bar function to animate the loading process 0% to 100%.
* - Implement DisplayMessage function to ensure the display of internal DLL messages.
* - Implement logMessage function to personalize the display (user messages).
* \code{.cpp}
**/
#define _CRT_SECURE_NO_WARNINGS // will disable warnings to use vsprintf instead vsprintf_s
#include <stdio.h>
#include "DisplayManager.h"
#include <stdarg.h>
#include <string>
#include <iostream>
#ifdef _WIN32
#include <windows.h>
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);;
CONSOLE_SCREEN_BUFFER_INFO SBInfo, CurSBInfo;
#endif
#define WIDH 50
using namespace std;
unsigned int verbosityLevel;
unsigned int Progress = 0;
void InitPBar()
{
#ifdef _WIN32
if (verbosityLevel == VERBOSITY_LEVEL_1)
{
for (int idx = 0; idx < WIDH; idx++)
{
console = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(console, LIGHTGREY);
putc(177, stdout);
}
GetConsoleScreenBufferInfo(console, &SBInfo);
printf(" %d%c", Progress,'%');
SetConsoleTextAttribute(console, LIGHTCYAN);
putc('\r', stdout);
Progress = 0;
}
#endif
}
void lBar(int currProgress, int total)
{
unsigned int alreadyLoaded = 0;
if (total == 0) return;
if (currProgress > total)
currProgress = total;
/*Calculuate the ratio of complete-to-incomplete.*/
float ratio = currProgress / (float)total;
unsigned int counter = (unsigned int)ratio * WIDH;
if ((counter > alreadyLoaded) && (verbosityLevel == VERBOSITY_LEVEL_1))
{
#ifdef _WIN32
SetConsoleTextAttribute(console, FOREGROUND_GREEN);
for (DWORD Idx = Progress; Idx < (counter - alreadyLoaded); Idx++)
{
putc(219, stdout);
}
Progress = counter;
GetConsoleScreenBufferInfo(console, &CurSBInfo);
SetConsoleCursorPosition(console, SBInfo.dwCursorPosition);
printf(" %d%%", (int)(ratio * 100));
SetConsoleCursorPosition(console, CurSBInfo.dwCursorPosition);
#else
printf("\033[00;32m");
printf("[");
for (int i = 0; i < counter; i++)
{
printf("=");
}
for (int i = counter; i < WIDH; i++)
{
printf(" ");
}
// draw the percentage progress
printf("] %3d%% \r", (int)(ratio * 100)); /* Move to the first column*/
printf("\033[39;49m");
#endif
}
alreadyLoaded = counter;
}
void DisplayMessage(int msgType, const wchar_t* str)
{
#ifdef _WIN32
switch (msgType)
{
default:
case Normal:
SetConsoleTextAttribute(console, WHITE);
break;
case GreenInfo:
SetConsoleTextAttribute(console, GREEN);
break;
case Info:
SetConsoleTextAttribute(console, LIGHTGREY);
break;
case Title:
SetConsoleTextAttribute(console, LIGHTCYAN);
break;
case Error:
SetConsoleTextAttribute(console, RED);
break;
case Warning:
SetConsoleTextAttribute(console, YELLOW);
break;
case ErrorNoPopup:
SetConsoleTextAttribute(console, RED);
break;
case WarningNoPopup:
SetConsoleTextAttribute(console, YELLOW);
break;
case Verbosity_1:
SetConsoleTextAttribute(console, CYAN);
break;
case Verbosity_2:
SetConsoleTextAttribute(console, CYAN);
break;
case Verbosity_3:
SetConsoleTextAttribute(console, CYAN);
break;
}
if ((msgType != Verbosity_1 && msgType != Verbosity_2 && msgType != Verbosity_3) || ((msgType == Verbosity_1 && verbosityLevel >= VERBOSITY_LEVEL_1) || (msgType == Verbosity_2 && verbosityLevel >= VERBOSITY_LEVEL_2) || (msgType == Verbosity_3 && verbosityLevel == VERBOSITY_LEVEL_3))) {
wprintf(L"%s\n", str);
}
SetConsoleTextAttribute(console, WHITE);
#else
if (verbosityLevel == VERBOSITY_LEVEL_1)
{
if ((msgType == Verbosity_1 && verbosityLevel >= 1))
printf("\033[39;36m");
if ((msgType == Verbosity_2 && verbosityLevel >= 2))
printf("\033[39;36m");
if ((msgType == Verbosity_3 && verbosityLevel >= 3))
printf("\033[39;36m");
switch (msgType)
{
case GreenInfo:
printf("\033[00;32m");
break;
case Info:
printf("\e[90m");
break;
case Normal:
printf("\033[39;49m");
break;
case Warning:
printf("\033[00;33m");
break;
case Error:
printf("\033[00;31m");
break;
case Title:
printf("\e[36m\033[01m");
break;
}
}
if ((msgType != Verbosity_1 && msgType != Verbosity_2 && msgType != Verbosity_3) || ((msgType == Verbosity_1 && verbosityLevel >= 1) || (msgType == Verbosity_2 && verbosityLevel >= 2) || (msgType == Verbosity_3 && verbosityLevel == 3)))
{
std::wcout << str << std::endl;
}
printf("\033[39;49m");
#endif
}
void logMessage(int msgType, const char* str, ...)
{
#ifdef _WIN32
switch (msgType)
{
default:
case Normal:
SetConsoleTextAttribute(console, WHITE);
break;
case GreenInfo:
SetConsoleTextAttribute(console, GREEN);
break;
case Info:
SetConsoleTextAttribute(console, LIGHTGREY);
break;
case Title:
SetConsoleTextAttribute(console, LIGHTCYAN);
break;
case Error:
SetConsoleTextAttribute(console, RED);
break;
case Warning:
SetConsoleTextAttribute(console, YELLOW);
break;
case ErrorNoPopup:
SetConsoleTextAttribute(console, RED);
break;
case WarningNoPopup:
SetConsoleTextAttribute(console, YELLOW);
break;
case Verbosity_1:
SetConsoleTextAttribute(console, CYAN);
break;
case Verbosity_2:
SetConsoleTextAttribute(console, CYAN);
break;
case Verbosity_3:
SetConsoleTextAttribute(console, CYAN);
break;
}
#else
switch (msgType)
{
case GreenInfo:
printf("\033[00;32m");
break;
case Info:
printf("\e[90m");
break;
case Normal:
printf("\033[39;49m");
break;
case Warning:
printf("\033[00;33m");
break;
case Error:
printf("\033[00;31m");
break;
case Title:
printf("\e[36m\033[01m");
break;
}
#endif
va_list args;
va_start(args, str);
char buffer[256];
vsprintf(buffer, str, args);
printf("%s", buffer);
va_end(args);
#ifdef _WIN32
SetConsoleTextAttribute(console, WHITE);
#else
printf("\033[39;49m");
#endif
}
/** \endcode **/