-
Notifications
You must be signed in to change notification settings - Fork 0
/
console_writer.cpp
350 lines (278 loc) · 7.87 KB
/
console_writer.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Copyright (C) 2015 Mountassir El Hafi, ([email protected])
*
* Writer.cpp: Part of gmonitor
*
* gmonitor is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* gmonitor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with gmonitor. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <sstream>
#include <sys/ioctl.h>
#include "console_writer.h"
using namespace std;
//text in colour
string ConsoleWriter::colourText(Color color, const string &text)
{
switch(color)
{
case BLUE: { return inBlue(text); }
case YELLOW: { return inYellow(text); }
case GREEN: { return inGreen(text); }
case RED: { return inRed(text); }
case LBLUE: { return inLightBlue(text);}
default: { return text; }
}
}
//light blue coloured text
string ConsoleWriter::inLightBlue(const string &text)
{
ostringstream oss;
oss << "\033[1;36m" << text << "\033[0m";
return oss.str();
}
//red coloured text
string ConsoleWriter::inRed(const string &text)
{
ostringstream oss;
oss << "\033[1;31m" << text << "\033[0m";
return oss.str();
}
//green coloured text
string ConsoleWriter::inGreen(const string &text)
{
ostringstream oss;
oss << "\033[1;32m" << text << "\033[0m";
return oss.str();
}
//yellow coloured text
string ConsoleWriter::inYellow(const string &text)
{
ostringstream oss;
oss << "\033[1;33m" << text << "\033[0m";
return oss.str();
}
//blue coloured text
string ConsoleWriter::inBlue(const string &text)
{
ostringstream oss;
oss << "\033[1;34m" << text << "\033[0m";
return oss.str();
}
//return the correct colour for a value (0 - 100)
Color ConsoleWriter::getColorByValue(int value)
{
if(value < 25 - resolution){ return BLUE;}
else
if(value < 50 - resolution){ return GREEN;}
else
if(value < 75 - resolution){ return YELLOW;}
else
{ return RED;}
return NO_COLOUR;
}
//return the correct colour for a state
Color ConsoleWriter::getColorByState(States toPrint)
{
switch(toPrint)
{
case GPU: { return BLUE; }
case VRAM: { return YELLOW; }
case MBUS: { return GREEN; }
case PBUS: { return LBLUE; }
case TEMP: { return RED; }
default: { return NO_COLOUR; }
}
}
//get the pre and post labels for each state;
//GPU for gpu usage, VRAM for video RAM ...
//% for percentage value and c for temperature
void ConsoleWriter::getLabelsByState(States state,
string *preLabel,
string *postLabel)
{
switch(state)
{
case GPU: { *preLabel = "GPU: "; *postLabel = "%"; break;}
case VRAM: { *preLabel = "VRAM: "; *postLabel = "%"; break;}
case MBUS: { *preLabel = "MBUS: "; *postLabel = "%"; break;}
case PBUS: { *preLabel = "PBUS: "; *postLabel = "%"; break;}
case TEMP: { *preLabel = "TEMP: "; *postLabel = "c"; break;}
default: { *preLabel = ""; *postLabel = ""; break;}
}
}
//print a line of '='
string ConsoleWriter::printSeperator(int length /* = 0 */)
{
ostringstream oss;
if(length != 0)
{
oss << "\n" << string((length), '=');
}
else
{
oss << "\n" << string((terminalColms), '=');
}
return oss.str();
}
//refresh the available terminal size to print on
void ConsoleWriter::refreshTerminalDimensions()
{
int columns, rows;
getTerminalDim(&columns, &rows);
updateTerminalDimensions(columns, rows);
}
//get the current window size
void ConsoleWriter::getTerminalDim(int *xDim, int *yDim)
{
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
*yDim = w.ws_row;
*xDim = w.ws_col;
}
//update the size of the terminal window
void ConsoleWriter::updateTerminalDimensions(int colms, int rows)
{
terminalRows = rows;
terminalColms = colms;
combinedBarSize = (terminalColms - NUMBER_OF_STATES) / NUMBER_OF_STATES;
resolution = 100 / (terminalColms - 11) + 1;
}
//clear the terminal
void ConsoleWriter::clearConsole()
{
cout << "\x1B[2J\x1B[H";
}
//print gpu identifiers
void ConsoleWriter::printGpuIdentifier(const string &gpuId,
const string &gpuName,
int totalAvailableMemory)
{
cout << "\nID: " << gpuId
<< ", Name: " << gpuName
<< " ("<< totalAvailableMemory << " MB)";
}
//print the current state of the gpu
void ConsoleWriter::printCurrentGpuState(const GpuStates &state)
{
refreshTerminalDimensions();
int line = 0;
ostringstream oss;
oss << printSeperator();
oss << "\n |";
while(line < NUMBER_OF_STATES)
{
switch(line)
{
case 0: { oss << printOneStateInOneLine(GPU, state.gpuUsage); break;}
case 1: { oss << printOneStateInOneLine(VRAM, state.memoryUsage); break;}
case 2: { oss << printOneStateInOneLine(MBUS, state.memoryBandwidth); break;}
case 3: { oss << printOneStateInOneLine(PBUS, state.pciBandwidth); break;}
case 4: { oss << printOneStateInOneLine(TEMP, state.coreTemp); break;}
}
++line;
}
oss << "\n |";
cout << oss.str();
}
//print one state, value is represented by a list
//of '|', 10 -> |||||||||| (for resolution = 1)
string ConsoleWriter::printOneStateInOneLine(States state, int value)
{
string preLabel, postLabel;
getLabelsByState(state, &preLabel, &postLabel);
ostringstream oss;
int character = 0;
while(character <= (terminalColms ) * resolution)
{
if(character == 0)
{
oss << "\n" << preLabel << "|";
}
if(character < value)
{
oss << colourText(getColorByValue(character), "|");
}
character += resolution;
}
oss << value << postLabel;
return oss.str();
}
//print all previous states of the gpu
void ConsoleWriter::printHistoryStates(const vector<GpuStates> &states)
{
refreshTerminalDimensions();
ostringstream oss;
oss << printSeperator();
oss << printHistoryStatesHeading(GPU, combinedBarSize) << " "
<< printHistoryStatesHeading(VRAM, combinedBarSize) << " "
<< printHistoryStatesHeading(MBUS, combinedBarSize) << " "
<< printHistoryStatesHeading(PBUS, combinedBarSize) << " "
<< printHistoryStatesHeading(TEMP, combinedBarSize);
for(size_t i = 0; i < states.size(); ++i)
{
oss << "\n"
<< printOneHistoryStateInOneLine(GPU, states[i].gpuUsage, combinedBarSize) << " "
<< printOneHistoryStateInOneLine(VRAM, states[i].memoryUsage, combinedBarSize) << " "
<< printOneHistoryStateInOneLine(MBUS, states[i].memoryBandwidth, combinedBarSize) << " "
<< printOneHistoryStateInOneLine(PBUS, states[i].pciBandwidth, combinedBarSize) << " "
<< printOneHistoryStateInOneLine(TEMP, states[i].coreTemp, combinedBarSize);
}
cout << oss.str();
}
//print one state, value is represented by a list
//of '|', resolution depends on the line size
string ConsoleWriter::printOneHistoryStateInOneLine(States state, int value, double lineSize)
{
Color color = getColorByState(state);
ostringstream oss;
double character = 1;
while(character <= lineSize)
{
double percentage = (character / lineSize) * 100;
if( percentage < value)
{
oss << colourText(color, "|");
}
else
{
oss << "|";
}
++character;
}
return oss.str();
}
//print heading for history view
//GPU~~~ VRAM~~~ ...
string ConsoleWriter::printHistoryStatesHeading(States state, int barSize)
{
ostringstream oss;
Color color = getColorByState(state);
switch(state)
{
case GPU: { oss << "GPU"; break;}
case VRAM: { oss << "VRAM"; break;}
case MBUS: { oss << "MBUS"; break;}
case PBUS: { oss << "PBUS"; break;}
case TEMP: { oss << "TEMP"; break;}
default: { oss << ""; break;}
}
int textSize = oss.str().length();
if(textSize > barSize)
{
return "";
}
for(int i = 0; i < (barSize - textSize); ++i)
{
oss << "~";
}
return colourText(color, oss.str());
}