-
Notifications
You must be signed in to change notification settings - Fork 0
/
temps.cpp
151 lines (135 loc) · 3.35 KB
/
temps.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
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <regex>
#include <string>
#include <chrono>
#include <thread>
#include <iomanip>
#include <math.h>
#include <map>
#include "center.cpp"
#include "cpu.cpp"
#include "gpu.cpp"
using namespace std;
int col_width = 5;
bool supportsColors() {
return true;
}
map<int,int> colorTemps = {
{ 1, 34 },//blue 34 44
{ 2, 36 },//cyan 36 46
{ 3, 32 },//green 32 42
{ 4, 33 },//yellow 33 43
{ 5, 31 },//red 31 41
{ 6, 35 },//magenta 35 45
{ 7, 37 },//white 37 47
{ 8, 37 },//white 37 47
{ 9, 37 },//white 37 47
{ 10, 37 }//white 37 47
};
void printColoredTempBar(const int &width, const int &step, ostringstream &rendered) {
int color = colorTemps[step];
string colorString = "\033[1;" + to_string(color) + "m";
rendered << colorString << setw(width);
rendered << centered("==");
rendered << "\033[0m";
};
void render(CPU cpu, GPU gpu, const int &height)
{
ostringstream rendered;
int title_height = 1;
int footer_height = 1;
int graph_height = height - title_height - footer_height;
int section_padding_right = 1;
string cpu_header = "CPU";
vector<double> cpu_temps = cpu.getTemps();
int cpu_section_width = col_width * cpu_temps.size() + section_padding_right;
string gpu_header = "GPU";
vector<double> gpu_temps = gpu.getTemps();
int gpu_section_width = col_width * gpu_temps.size() + section_padding_right;
// Header
if (cpu_temps.size()) {
rendered << "|" << setw(cpu_section_width) << centered(cpu_header);
}
if (gpu_temps.size()) {
rendered << "|" << setw(gpu_section_width) << centered(gpu_header);
}
rendered << "|\n";
// Bar graphs
for (int i = graph_height; i > 0; i--)
{
if (cpu_temps.size()) {
rendered << "| ";
}
// CPU temp
for (int j = 0; j < cpu_temps.size(); j++)
{
if (cpu_temps[j] >= i * 10)
{
printColoredTempBar(col_width, i, rendered);
}
else
{
rendered << setw(col_width) << centered("..");
}
}
if (gpu_temps.size()) {
rendered << "|";
}
// GPU temp
for (int j = 0; j < gpu_temps.size(); j++)
{
if (gpu_temps[j] >= i * 10)
{
printColoredTempBar(col_width + 1, i, rendered);
}
else
{
rendered << setw(col_width + 1) << centered("..");
}
}
rendered << "|\n";
}
// Footer
if (cpu_temps.size()) {
rendered << "| ";
}
// CPU temp
for (int i = 0; i < cpu_temps.size(); i++) {
ostringstream temp;
temp << cpu_temps.at(i);
rendered << setw(col_width) << centered(temp.str());
}
if (gpu_temps.size()) {
rendered << "|";
}
// GPU temp
for (int i = 0; i < gpu_temps.size(); i++) {
ostringstream temp;
temp << gpu_temps.at(i);
rendered << setw(col_width + 1) << centered(temp.str());
}
rendered << "|\n";
cout << rendered.str();
}
void clearScreen(const int &height) {
for (int i = 0; i < height; i++) {
cout << "\033[A\033[2K";
}
}
int main()
{
int height = 12;
CPU cpu {};
GPU gpu {};
while(true) {
cpu.update();
gpu.update();
render(cpu, gpu, height);
this_thread::sleep_for(chrono::seconds(1));
clearScreen(height);
}
return 0;
}