overhaul of sys

This commit is contained in:
Drath
2025-09-22 22:17:51 +02:00
parent 1998fb6953
commit a9afb46c19
3 changed files with 25 additions and 41 deletions

View File

@@ -1,41 +1,17 @@
#include <sys.hpp>
std::string getAvgs() {
std::string ret;
double avgs[3];
getloadavg(avgs, 3);
for (uint8_t i = 0; i < 3; i++)
ret += pNum(avgs[i], avgs[i] > 10 ? 1 : 2) + " ";
ret.pop_back(); // remove trailing space
return ret;
}
std::array<std::string, 2> getTimeDate() {
std::array months = {"sty", "lut", "mar", "kwi", "maj", "cze", "lip", "sie", "wrz", "paz", "lis", "gru"};
std::array wdays = {"Nie", "Pon", "Wto", "Sro", "Czw", "Pia", "Sob"};
// std::array wdays = {"Ni", "Pn", "Wt", "Sr", "Cz", "Pi", "So"};
void sys::getTimeDate(TimeDate* td) {
auto now = std::time(nullptr);
auto tm = *std::localtime(&now);
std::stringstream time, date;
time << std::setfill('0');
time << std::setw(2) << tm.tm_hour << ":";
time << std::setw(2) << tm.tm_min << ":";
time << std::setw(2) << tm.tm_sec;
date << wdays[tm.tm_wday] << ", " << tm.tm_mday << " " << months[tm.tm_mon];
return {time.str(), date.str()};
sprintf(td->time, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min, tm.tm_sec);
sprintf(td->date, "%s, %d %s", wdays[tm.tm_wday], tm.tm_mday, months[tm.tm_mon]);
}
void sys::getRam(Memory* mem) {
std::ifstream proc_meminfo("/proc/meminfo");
if (proc_meminfo.good()) {
std::string content((std::istreambuf_iterator<char>(proc_meminfo)), std::istreambuf_iterator<char>());
mem->free = get_val("MemAvailable:", content);
mem->total = get_val("MemTotal:", content);
mem->used = mem->total - mem->free;
}
proc_meminfo.close();
return;
auto content = readFile("/proc/meminfo", 128); // file is longer than that but its all we need
mem->free = get_val("MemAvailable:", content);
mem->total = get_val("MemTotal:", content);
mem->used = mem->total - mem->free;
}
void sys::getDisk(Memory* disk, std::string mount) {
@@ -44,5 +20,4 @@ void sys::getDisk(Memory* disk, std::string mount) {
disk->free = diskData.f_bfree * diskData.f_frsize;
disk->used = (diskData.f_blocks - diskData.f_bfree) * diskData.f_frsize;
disk->total = diskData.f_blocks * diskData.f_frsize;
return;
}