sophuwu.site > sysgraph
added background color to graphs
made graphs colourful
using functions to plot graphs instead of messy code
sophuwu sophie@sophuwu.site
Fri, 04 Aug 2023 20:46:16 +0200
commit

5d496d751fb3d7ec2aba01150097c2ee86db1ca8

parent

6295a28ccb7334463a1bd5519a39adb54ab29c14

1 files changed, 24 insertions(+), 11 deletions(-)

jump to
M main.cppmain.cpp

@@ -115,14 +115,15 @@ if (s.length() < len) for (int i = 0; i <= len - s.length(); i++) s = " " + s;

return s; } -void plot(int x, int y){ - std::cout << "\033[" << y-- << ";" << x << "H\033[1;31m*" << std::endl; - for (;y>h-1;) { - std::cout << "\033[" << y-- << ";" << x << "H|" << std::endl; +int graphscale(int n) {return h-1-(n*(h-6)/100);} + +void plot(int x, int y, int color){ + if (color > 7 || color < 1) color = 7; + if (x%2 == 0) color+=8; + for (;y<h;) { + std::cout << "\033[" << y++ << ";" << x << "H\033[48;5;" << color << "m \033[0m" << std::endl; } } - -int graphscale(int n) {return h-1-(n*(h-6)/100);} int main(){ meminfo mem;

@@ -130,7 +131,8 @@ cpuinfo cpu;

int cpugraph[150] = {0}; int ramgraph[150] = {0}; int i; - std::string pad; + int j; + std::string graphnum; std::cout << "\033[?25l\033[?1049h\033[2J" << std::endl; signal(SIGINT, signal_callback_handler); for (;;) {

@@ -139,7 +141,7 @@

mem.loadmem(); cpu.loadcpu(); // contains usleep(1000000) - for (int i = 150; i > 0; i--) { + for (i = 150; i > 0; i--) { cpugraph[i] = cpugraph[i-1]; ramgraph[i] = ramgraph[i-1]; }

@@ -158,9 +160,20 @@ << "\033[2J"

<< "\033[2;3HLoad: " << padint(cpu.loadavg, 3) << "% Memory:\033[3;3HTemp: " << padint(cpu.temp, 3) << "C" << "\033[2;22HUsed by apps: " << padint(mem.taken(), 5) << " (" << mem.percent() << "%)\033[2;48HUsed: " << padint(mem.used(), 5) << "\033[3;22HCan be freed: " << padint(mem.available, 5) << "\033[3;48HFree: " << padint(mem.free, 5) << std::endl; - - for (i = 3; i < w/2-3; i++) std::cout << "\033[" << graphscale(cpugraph[w/2-i-3]) << ";" << i << "H*\033[" << graphscale(ramgraph[w/2-i-3]) << ";" << i+w/2 << "H*" << std::endl; - for (i = 5; i < h; i++) std::cout << "\033[" << i << ";2H|\033[" << i << ";" << w/2-3 << "H|" << padint(100-(i-5)*100/(h-6), 3) << " |" << std::endl; + for (i = 3; i < w/2-2; i++) { + for (j = 5; j < h; j++) { + std::cout << "\033[" << j << ";" << i << "H\033[48;5;235m \033[0m" + << "\033[" << j << ";" << i+w/2+2 << "H\033[48;5;235m \033[0m" << std::endl; + } + } + for (i = 3; i < w/2-2; i++) { + plot(i, graphscale(cpugraph[w/2+1-i-3]), 4); + plot(i+w/2+2, graphscale(ramgraph[w/2+1-i-3]), 6); + } + for (i = 5; i < h; i++) { + graphnum = padint(100-(i-5)*100/(h-6), 3); + std::cout << "\033[" << i << ";" << w/2-1 << "H-" << graphnum << "-" << std::endl; + } } return 0; }