summaryrefslogtreecommitdiffstats
path: root/src/btop_tools.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/btop_tools.cpp')
-rw-r--r--src/btop_tools.cpp252
1 files changed, 126 insertions, 126 deletions
diff --git a/src/btop_tools.cpp b/src/btop_tools.cpp
index 926657e..648cf64 100644
--- a/src/btop_tools.cpp
+++ b/src/btop_tools.cpp
@@ -32,9 +32,9 @@ tab-size = 4
#include "robin_hood.h"
#include "widechar_width.hpp"
-#include <btop_shared.hpp>
-#include <btop_tools.hpp>
-#include <btop_config.hpp>
+#include "btop_shared.hpp"
+#include "btop_tools.hpp"
+#include "btop_config.hpp"
using std::cin;
using std::cout;
@@ -55,9 +55,9 @@ namespace rng = std::ranges;
//* Collection of escape codes and functions for terminal manipulation
namespace Term {
- atomic<bool> initialized{}; // defaults to false
- atomic<int> width{}; // defaults to 0
- atomic<int> height{}; // defaults to 0
+ atomic<bool> initialized{}; // defaults to false
+ atomic<int> width{}; // defaults to 0
+ atomic<int> height{}; // defaults to 0
string current_tty;
namespace {
@@ -80,7 +80,7 @@ namespace Term {
else settings.c_lflag &= ~(ICANON);
if (tcsetattr(STDIN_FILENO, TCSANOW, &settings)) return false;
if (on) setlinebuf(stdin);
- else setbuf(stdin, NULL);
+ else setbuf(stdin, nullptr);
return true;
}
}
@@ -127,15 +127,15 @@ namespace Term {
initialized = (bool)isatty(STDIN_FILENO);
if (initialized) {
tcgetattr(STDIN_FILENO, &initial_settings);
- current_tty = (ttyname(STDIN_FILENO) != NULL ? static_cast<string>(ttyname(STDIN_FILENO)) : "unknown");
+ current_tty = (ttyname(STDIN_FILENO) != nullptr ? static_cast<string>(ttyname(STDIN_FILENO)) : "unknown");
//? Disable stream sync
cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
//? Disable stream ties
- cin.tie(NULL);
- cout.tie(NULL);
+ cin.tie(nullptr);
+ cout.tie(nullptr);
echo(false);
linebuffered(false);
refresh();
@@ -212,9 +212,9 @@ namespace Tools {
return chars;
}
- string uresize(string str, const size_t len, bool wide) {
- if (len < 1 or str.empty())
- return "";
+ string uresize(string str, const size_t len, bool wide) {
+ if (len < 1 or str.empty())
+ return "";
if (wide) {
try {
@@ -242,9 +242,9 @@ namespace Tools {
return str;
}
- string luresize(string str, const size_t len, bool wide) {
- if (len < 1 or str.empty())
- return "";
+ string luresize(string str, const size_t len, bool wide) {
+ if (len < 1 or str.empty())
+ return "";
for (size_t x = 0, last_pos = 0, i = str.size() - 1; i > 0 ; i--) {
if (wide and static_cast<unsigned char>(str.at(i)) > 0xef) {
@@ -273,19 +273,19 @@ namespace Tools {
}
string ltrim(const string& str, const string& t_str) {
- std::string_view str_v{str};
- while (str_v.starts_with(t_str))
- str_v.remove_prefix(t_str.size());
+ std::string_view str_v{str};
+ while (str_v.starts_with(t_str))
+ str_v.remove_prefix(t_str.size());
- return string{str_v};
+ return string{str_v};
}
string rtrim(const string& str, const string& t_str) {
- std::string_view str_v{str};
- while (str_v.ends_with(t_str))
- str_v.remove_suffix(t_str.size());
+ std::string_view str_v{str};
+ while (str_v.ends_with(t_str))
+ str_v.remove_suffix(t_str.size());
- return string{str_v};
+ return string{str_v};
}
auto ssplit(const string& str, const char& delim) -> vector<string> {
@@ -299,56 +299,56 @@ namespace Tools {
return out;
}
- string ljust(string str, const size_t x, bool utf, bool wide, bool limit) {
+ string ljust(string str, const size_t x, bool utf, bool wide, bool limit) {
if (utf) {
- if (limit and ulen(str, wide) > x)
- return uresize(str, x, wide);
+ if (limit and ulen(str, wide) > x)
+ return uresize(str, x, wide);
return str + string(max((int)(x - ulen(str)), 0), ' ');
}
else {
- if (limit and str.size() > x) {
- str.resize(x);
- return str;
- }
+ if (limit and str.size() > x) {
+ str.resize(x);
+ return str;
+ }
return str + string(max((int)(x - str.size()), 0), ' ');
}
}
- string rjust(string str, const size_t x, bool utf, bool wide, bool limit) {
+ string rjust(string str, const size_t x, bool utf, bool wide, bool limit) {
if (utf) {
- if (limit and ulen(str, wide) > x)
- return uresize(str, x, wide);
+ if (limit and ulen(str, wide) > x)
+ return uresize(str, x, wide);
return string(max((int)(x - ulen(str)), 0), ' ') + str;
}
else {
- if (limit and str.size() > x) {
- str.resize(x);
- return str;
- };
+ if (limit and str.size() > x) {
+ str.resize(x);
+ return str;
+ };
return string(max((int)(x - str.size()), 0), ' ') + str;
}
}
- string cjust(string str, const size_t x, bool utf, bool wide, bool limit) {
+ string cjust(string str, const size_t x, bool utf, bool wide, bool limit) {
if (utf) {
- if (limit and ulen(str, wide) > x)
- return uresize(str, x, wide);
+ if (limit and ulen(str, wide) > x)
+ return uresize(str, x, wide);
return string(max((int)ceil((double)(x - ulen(str)) / 2), 0), ' ') + str + string(max((int)floor((double)(x - ulen(str)) / 2), 0), ' ');
}
else {
- if (limit and str.size() > x) {
- str.resize(x);
- return str;
- }
+ if (limit and str.size() > x) {
+ str.resize(x);
+ return str;
+ }
return string(max((int)ceil((double)(x - str.size()) / 2), 0), ' ') + str + string(max((int)floor((double)(x - str.size()) / 2), 0), ' ');
}
}
string trans(const string& str) {
- std::string_view oldstr{str};
+ std::string_view oldstr{str};
string newstr;
newstr.reserve(str.size());
for (size_t pos; (pos = oldstr.find(' ')) != string::npos;) {
@@ -358,7 +358,7 @@ namespace Tools {
newstr.append(Mv::r(x));
oldstr.remove_prefix(pos + x);
}
- return (newstr.empty()) ? str : newstr + string{oldstr};
+ return (newstr.empty()) ? str : newstr + string{oldstr};
}
string sec_to_dhms(size_t seconds, bool no_days, bool no_seconds) {
@@ -372,37 +372,37 @@ namespace Tools {
return out;
}
- string floating_humanizer(uint64_t value, bool shorten, size_t start, bool bit, bool per_second) {
+ string floating_humanizer(uint64_t value, bool shorten, size_t start, bool bit, bool per_second) {
string out;
const size_t mult = (bit) ? 8 : 1;
- bool mega = Config::getB("base_10_sizes");
-
- // taking advantage of type deduction for array creation (since C++17)
- // combined with string literals (operator""s)
- static const array mebiUnits_bit {
- "bit"s, "Kib"s, "Mib"s,
- "Gib"s, "Tib"s, "Pib"s,
- "Eib"s, "Zib"s, "Yib"s,
- "Bib"s, "GEb"s
- };
- static const array mebiUnits_byte {
- "Byte"s, "KiB"s, "MiB"s,
- "GiB"s, "TiB"s, "PiB"s,
- "EiB"s, "ZiB"s, "YiB"s,
- "BiB"s, "GEB"s
- };
- static const array megaUnits_bit {
- "bit"s, "Kb"s, "Mb"s,
- "Gb"s, "Tb"s, "Pb"s,
- "Eb"s, "Zb"s, "Yb"s,
- "Bb"s, "Gb"s
- };
- static const array megaUnits_byte {
- "Byte"s, "KB"s, "MB"s,
- "GB"s, "TB"s, "PB"s,
- "EB"s, "ZB"s, "YB"s,
- "BB"s, "GB"s
- };
+ bool mega = Config::getB("base_10_sizes");
+
+ // taking advantage of type deduction for array creation (since C++17)
+ // combined with string literals (operator""s)
+ static const array mebiUnits_bit {
+ "bit"s, "Kib"s, "Mib"s,
+ "Gib"s, "Tib"s, "Pib"s,
+ "Eib"s, "Zib"s, "Yib"s,
+ "Bib"s, "GEb"s
+ };
+ static const array mebiUnits_byte {
+ "Byte"s, "KiB"s, "MiB"s,
+ "GiB"s, "TiB"s, "PiB"s,
+ "EiB"s, "ZiB"s, "YiB"s,
+ "BiB"s, "GEB"s
+ };
+ static const array megaUnits_bit {
+ "bit"s, "Kb"s, "Mb"s,
+ "Gb"s, "Tb"s, "Pb"s,
+ "Eb"s, "Zb"s, "Yb"s,
+ "Bb"s, "Gb"s
+ };
+ static const array megaUnits_byte {
+ "Byte"s, "KB"s, "MB"s,
+ "GB"s, "TB"s, "PB"s,
+ "EB"s, "ZB"s, "YB"s,
+ "BB"s, "GB"s
+ };
const auto& units = (bit) ? ( mega ? megaUnits_bit : mebiUnits_bit) : ( mega ? megaUnits_byte : mebiUnits_byte);
value *= 100 * mult;
@@ -429,29 +429,29 @@ namespace Tools {
}
if (out.empty()) {
out = to_string(value);
- if (not mega and out.size() == 4 and start > 0) {
- out.pop_back();
- out.insert(2, ".");
- }
- else if (out.size() == 3 and start > 0) {
- out.insert(1, ".");
- }
- else if (out.size() >= 2) {
- out.resize(out.size() - 2);
- }
+ if (not mega and out.size() == 4 and start > 0) {
+ out.pop_back();
+ out.insert(2, ".");
+ }
+ else if (out.size() == 3 and start > 0) {
+ out.insert(1, ".");
+ }
+ else if (out.size() >= 2) {
+ out.resize(out.size() - 2);
+ }
}
if (shorten) {
auto f_pos = out.find('.');
- if (f_pos == 1 and out.size() > 3) {
- out = to_string(round(stof(out) * 10) / 10).substr(0,3);
- }
- else if (f_pos != string::npos) {
- out = to_string((int)round(stof(out)));
- }
- if (out.size() > 3) {
- out = to_string((int)(out[0] - '0') + 1);
- start++;
- }
+ if (f_pos == 1 and out.size() > 3) {
+ out = to_string(round(stod(out) * 10) / 10).substr(0,3);
+ }
+ else if (f_pos != string::npos) {
+ out = to_string((int)round(stod(out)));
+ }
+ if (out.size() > 3) {
+ out = to_string((int)(out[0] - '0') + 1);
+ start++;
+ }
out.push_back(units[start][0]);
}
else out += " " + units[start];
@@ -461,18 +461,18 @@ namespace Tools {
}
std::string operator*(const string& str, int64_t n) {
- if (n < 1 or str.empty()) {
- return "";
- }
- else if (n == 1) {
- return str;
- }
+ if (n < 1 or str.empty()) {
+ return "";
+ }
+ else if (n == 1) {
+ return str;
+ }
string new_str;
new_str.reserve(str.size() * n);
- for (; n > 0; n--)
- new_str.append(str);
+ for (; n > 0; n--)
+ new_str.append(str);
return new_str;
}
@@ -485,17 +485,17 @@ namespace Tools {
return ss.str();
}
- void atomic_wait(const atomic<bool>& atom, bool old) noexcept {
+ void atomic_wait(const atomic<bool>& atom, bool old) noexcept {
while (atom.load(std::memory_order_relaxed) == old ) busy_wait();
}
- void atomic_wait_for(const atomic<bool>& atom, bool old, const uint64_t wait_ms) noexcept {
+ void atomic_wait_for(const atomic<bool>& atom, bool old, const uint64_t wait_ms) noexcept {
const uint64_t start_time = time_ms();
while (atom.load(std::memory_order_relaxed) == old and (time_ms() - start_time < wait_ms)) sleep_ms(1);
}
atomic_lock::atomic_lock(atomic<bool>& atom, bool wait) : atom(atom) {
- if (wait) while (not this->atom.compare_exchange_strong(this->not_true, true));
+ if (wait) while (not this->atom.compare_exchange_strong(this->not_true, true));
else this->atom.store(true);
}
@@ -511,7 +511,7 @@ namespace Tools {
for (string readstr; getline(file, readstr); out += readstr);
}
catch (const std::exception& e) {
- Logger::error("readfile() : Exception when reading " + string{path} + " : " + e.what());
+ Logger::error("readfile() : Exception when reading " + string{path} + " : " + e.what());
return fallback;
}
return (out.empty() ? fallback : out);
@@ -532,13 +532,13 @@ namespace Tools {
string hostname() {
char host[HOST_NAME_MAX];
gethostname(host, HOST_NAME_MAX);
- return string{host};
+ return string{host};
}
string username() {
auto user = getenv("LOGNAME");
- if (user == NULL or strlen(user) == 0) user = getenv("USER");
- return (user != NULL ? user : "");
+ if (user == nullptr or strlen(user) == 0) user = getenv("USER");
+ return (user != nullptr ? user : "");
}
DebugTimer::DebugTimer(const string name, bool start, bool delayed_report) : name(name), delayed_report(delayed_report) {
@@ -625,14 +625,14 @@ namespace Logger {
int status = -1;
public:
lose_priv() {
- if (geteuid() != Global::real_uid) {
- this->status = seteuid(Global::real_uid);
- }
+ if (geteuid() != Global::real_uid) {
+ this->status = seteuid(Global::real_uid);
+ }
}
~lose_priv() {
- if (status == 0) {
- status = seteuid(Global::set_uid);
- }
+ if (status == 0) {
+ status = seteuid(Global::set_uid);
+ }
}
};
@@ -650,25 +650,25 @@ namespace Logger {
auto old_log = logfile;
old_log += ".1";
- if (fs::exists(old_log))
- fs::remove(old_log, ec);
+ if (fs::exists(old_log))
+ fs::remove(old_log, ec);
- if (not ec)
- fs::rename(logfile, old_log, ec);
+ if (not ec)
+ fs::rename(logfile, old_log, ec);
}
if (not ec) {
std::ofstream lwrite(logfile, std::ios::app);
- if (first) {
- first = false;
- lwrite << "\n" << strf_time(tdf) << "===> btop++ v." << Global::Version << "\n";
- }
+ if (first) {
+ first = false;
+ lwrite << "\n" << strf_time(tdf) << "===> btop++ v." << Global::Version << "\n";
+ }
lwrite << strf_time(tdf) << log_levels.at(level) << ": " << msg << "\n";
}
else logfile.clear();
}
catch (const std::exception& e) {
logfile.clear();
- throw std::runtime_error("Exception in Logger::log_write() : " + string{e.what()});
+ throw std::runtime_error("Exception in Logger::log_write() : " + string{e.what()});
}
}
}