diff --git a/main.cpp b/main.cpp index e4c499c..8ffe8d3 100644 --- a/main.cpp +++ b/main.cpp @@ -31,18 +31,17 @@ struct mem_usage_t { bool daemon_mode = false; /* DISPLAY FUNCS */ -std::string pBytes(double i, bool isKbytes = false) { - std::stringstream ss; - std::array suffix = {'K', 'M', 'G', 'T', 'P', 'E', 'Z'}; - for (auto &s : suffix) { - if (s == 'K' && isKbytes) continue; - i /= 1024.0; - if (i < 1024) { - uint8_t p = 2; - if (i > 10) p = 1; - if (i > 100) p = 0; - ss << std::fixed << std::setprecision(p) << i << s; - return ss.str(); +// converts large number in (k)bytes to human readable numer with suffix and sane decimal +std::string pBytes(double bytes, bool isKbytes = false) { + std::array suffix_list = {'K', 'M', 'G', 'T', 'P', 'E', 'Z'}; + for (auto &suffix : suffix_list) { + if (suffix == 'K' && isKbytes) continue; // skip first loop if already in Kbytes + bytes /= 1024.0; + if (bytes < 1024) { + uint8_t decs = 2; + if (bytes > 10) decs = 1; + if (bytes > 100) decs = 0; + return pNum(bytes, decs) + suffix; } } return "++++"; @@ -50,14 +49,15 @@ std::string pBytes(double i, bool isKbytes = false) { // converts number to string with given decimal places std::string pNum(auto num, uint8_t decs = 0) { - std::stringstream ss; - ss << std::fixed << std::setprecision(decs) << num; - return ss.str(); + std::stringstream ret; + ret << std::fixed << std::setprecision(decs) << num; + return ret.str(); } -// pads given string to size -std::string pad(std::string s, uint8_t w, bool ljust = false) { - if (w > s.size()) s.insert(ljust ? s.end() : s.begin(), w - s.size(), FILL_CHAR); +// pads given string to length +std::string pad(std::string s, uint8_t len, bool ljust = false) { + if (len > s.size()) + s.insert(ljust ? s.end() : s.begin(), len - s.size(), FILL_CHAR); return s; } @@ -66,16 +66,15 @@ std::string fill(uint8_t n) { return std::string(n, FILL_CHAR); } -/* getters */ +/* GETTERS */ std::string getAvgs() { - std::stringstream ss; + std::string ret; double avgs[3]; getloadavg(avgs, 3); - for (uint8_t i = 0; i < 3; i++) { - ss << std::fixed << std::setprecision(avgs[i] > 10 ? 1 : 2); - ss << " " << avgs[i]; - } - return ss.str(); + 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 getTimeDate() { @@ -189,7 +188,7 @@ int main(int argc, char *argv[]) { while (true) { auto td = getTimeDate(); auto ram = getRam(); - auto anime = getDisk("/mnt/anime"); + // auto anime = getDisk("/mnt/anime"); auto home = getDisk("/home/drath"); auto gpu = getGpu(gpu_mtr_rev, gpu_hwmon); auto cpu = getCpuTemp(cpu_hwmon);