From 820391494be8f6bdd1a044f37c08cdd120643eb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=A3=CF=84=CE=AD=CF=86=CE=B1=CE=BD=CE=BF=CF=82?= Date: Tue, 4 Oct 2022 16:20:20 +0300 Subject: [PATCH 1/4] Avoid the unnecessary use of copy constructor When you want to pass a `std::string` to `std::string_view`, prefer to do such operation during object initialization via `{}` so you can avoid to use the copy constructor, which can be expensive under certain situations. --- src/btop_tools.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/btop_tools.cpp b/src/btop_tools.cpp index 7e64f74b..9dd73333 100644 --- a/src/btop_tools.cpp +++ b/src/btop_tools.cpp @@ -262,7 +262,7 @@ namespace Tools { } string ltrim(const string& str, const string& t_str) { - string_view str_v = str; + string_view str_v{str}; while (str_v.starts_with(t_str)) str_v.remove_prefix(t_str.size()); @@ -270,7 +270,7 @@ namespace Tools { } string rtrim(const string& str, const string& t_str) { - string_view str_v = str; + string_view str_v{str}; while (str_v.ends_with(t_str)) str_v.remove_suffix(t_str.size()); From afeef173fc459c361dc963bc244d5c77d775af62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=A3=CF=84=CE=AD=CF=86=CE=B1=CE=BD=CE=BF=CF=82?= Date: Tue, 4 Oct 2022 17:06:39 +0300 Subject: [PATCH 2/4] Remove const from bool variables --- src/btop_tools.cpp | 83 +++++++++++++++++++++++++++++++++------------- src/btop_tools.hpp | 20 +++++------ 2 files changed, 70 insertions(+), 33 deletions(-) diff --git a/src/btop_tools.cpp b/src/btop_tools.cpp index 9dd73333..2d4c70e3 100644 --- a/src/btop_tools.cpp +++ b/src/btop_tools.cpp @@ -288,41 +288,56 @@ namespace Tools { return out; } - string ljust(string str, const size_t x, const bool utf, const bool wide, const 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, const bool utf, const bool wide, const 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, const bool utf, const bool wide, const 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) { - string_view oldstr = str; + string_view oldstr{str}; string newstr; newstr.reserve(str.size()); for (size_t pos; (pos = oldstr.find(' ')) != string::npos;) { @@ -346,7 +361,7 @@ namespace Tools { return out; } - string floating_humanizer(uint64_t value, const bool shorten, size_t start, const bool bit, const 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"); @@ -403,15 +418,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(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++; + } out.push_back(units[start][0]); } else out += " " + units[start]; @@ -421,11 +450,19 @@ 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; } @@ -437,11 +474,11 @@ namespace Tools { return ss.str(); } - void atomic_wait(const atomic& atom, const bool old) noexcept { + void atomic_wait(const atomic& atom, bool old) noexcept { while (atom.load(std::memory_order_relaxed) == old ) busy_wait(); } - void atomic_wait_for(const atomic& atom, const bool old, const uint64_t wait_ms) noexcept { + void atomic_wait_for(const atomic& 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); } @@ -463,7 +500,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); diff --git a/src/btop_tools.hpp b/src/btop_tools.hpp index c4d42794..52c8e653 100644 --- a/src/btop_tools.hpp +++ b/src/btop_tools.hpp @@ -150,7 +150,7 @@ namespace Tools { size_t wide_ulen(const std::wstring& w_str); //* Return number of UTF8 characters in a string (wide=true for column size needed on terminal) - inline size_t ulen(const string& str, const bool wide=false) { + inline size_t ulen(const string& str, bool wide = false) { return (wide ? wide_ulen(str) : std::ranges::count_if(str, [](char c) { return (static_cast(c) & 0xC0) != 0x80; })); } @@ -270,25 +270,25 @@ namespace Tools { } //* Left justify string if is greater than length, limit return size to by default - string ljust(string str, const size_t x, const bool utf=false, const bool wide=false, const bool limit=true); + string ljust(string str, const size_t x, bool utf = false, bool wide = false, bool limit = true); //* Right justify string if is greater than length, limit return size to by default - string rjust(string str, const size_t x, const bool utf=false, const bool wide=false, const bool limit=true); + string rjust(string str, const size_t x, bool utf = false, bool wide = false, bool limit = true); //* Center justify string if is greater than length, limit return size to by default - string cjust(string str, const size_t x, const bool utf=false, const bool wide=false, const bool limit=true); + string cjust(string str, const size_t x, bool utf = false, bool wide = false, bool limit = true); //* Replace whitespaces " " with escape code for move right string trans(const string& str); //* Convert seconds to format "d ::" and return string - string sec_to_dhms(size_t seconds, bool no_days=false, bool no_seconds=false); + string sec_to_dhms(size_t seconds, bool no_days = false, bool no_seconds = false); //* Scales up in steps of 1024 to highest positive value unit and returns string with unit suffixed //* bit=True or defaults to bytes //* start=int to set 1024 multiplier starting unit //* short=True always returns 0 decimals and shortens unit to 1 character - string floating_humanizer(uint64_t value, const bool shorten=false, size_t start=0, const bool bit=false, const bool per_second=false); + string floating_humanizer(uint64_t value, bool shorten = false, size_t start=0, bool bit = false, bool per_second = false); //* Add std::string operator * : Repeat string number of times std::string operator*(const string& str, int64_t n); @@ -311,21 +311,21 @@ namespace Tools { #endif } - void atomic_wait(const atomic& atom, const bool old=true) noexcept; + void atomic_wait(const atomic& atom, bool old = true) noexcept; - void atomic_wait_for(const atomic& atom, const bool old=true, const uint64_t wait_ms=0) noexcept; + void atomic_wait_for(const atomic& atom, bool old = true, const uint64_t wait_ms = 0) noexcept; //* Sets atomic to true on construct, sets to false on destruct class atomic_lock { atomic& atom; bool not_true{}; // defaults to false public: - atomic_lock(atomic& atom, bool wait=false); + atomic_lock(atomic& atom, bool wait = false); ~atomic_lock(); }; //* Read a complete file and return as a string - string readfile(const std::filesystem::path& path, const string& fallback=""); + string readfile(const std::filesystem::path& path, const string& fallback = ""); //* Convert a celsius value to celsius, fahrenheit, kelvin or rankin and return tuple with new value and unit. auto celsius_to(const long long& celsius, const string& scale) -> tuple; From 8331cb36f8ae9d65ffdccdba7fe916b62d740f60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=A3=CF=84=CE=AD=CF=86=CE=B1=CE=BD=CE=BF=CF=82?= Date: Tue, 4 Oct 2022 17:33:53 +0300 Subject: [PATCH 3/4] More const bool cleanup --- src/btop_draw.cpp | 11 +++++++---- src/btop_draw.hpp | 6 +++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/btop_draw.cpp b/src/btop_draw.cpp index abb68fec..ec0750f0 100644 --- a/src/btop_draw.cpp +++ b/src/btop_draw.cpp @@ -228,10 +228,13 @@ namespace Draw { } string createBox(const int x, const int y, const int width, - const int height, string line_color, const bool fill, + const int height, string line_color, bool fill, const string title, const string title2, const int num) { string out; - if (line_color.empty()) line_color = Theme::c("div_line"); + + if (line_color.empty()) + line_color = Theme::c("div_line"); + auto tty_mode = Config::getB("tty_mode"); auto rounded = Config::getB("rounded_corners"); const string numbering = (num == 0) ? "" : Theme::c("hi_fg") + (tty_mode ? std::to_string(num) : Symbols::superscript.at(clamp(num, 0, 9))); @@ -243,12 +246,12 @@ namespace Draw { out = Fx::reset + line_color; //? Draw horizontal lines - for (const int& hpos : {y, y + height - 1}) { + for (const int& hpos : {y, y + height - 1}) { out += Mv::to(hpos, x) + Symbols::h_line * (width - 1); } //? Draw vertical lines and fill if enabled - for (const int& hpos : iota(y + 1, y + height - 1)) { + for (const int& hpos : iota(y + 1, y + height - 1)) { out += Mv::to(hpos, x) + Symbols::v_line + ((fill) ? string(width - 2, ' ') : Mv::r(width - 2)) + Symbols::v_line; diff --git a/src/btop_draw.hpp b/src/btop_draw.hpp index 6da4af50..59a0e69e 100644 --- a/src/btop_draw.hpp +++ b/src/btop_draw.hpp @@ -80,10 +80,10 @@ namespace Draw { //* Create a box and return as a string string createBox(const int x, const int y, const int width, - const int height, string line_color="", const bool fill=false, - const string title="", const string title2="", const int num=0); + const int height, string line_color = "", bool fill = false, + const string title = "", const string title2 = "", const int num = 0); - bool update_clock(bool force=false); + bool update_clock(bool force = false); //* Class holding a percentage meter class Meter { From f0e413ed6fd91f794394e60073819cb81b0492f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=A3=CF=84=CE=AD=CF=86=CE=B1=CE=BD=CE=BF=CF=82?= Date: Tue, 4 Oct 2022 21:36:04 +0300 Subject: [PATCH 4/4] All but two places `const bool` have been updated I haven't touched `include/robin_hood.h` as I don't know whether it's a third-party header file that could get rewritten at a later time of a possible future release by the author(s) of it. --- src/btop.cpp | 2 +- src/btop_draw.cpp | 26 ++++++++++++------------ src/btop_draw.hpp | 4 ++-- src/btop_shared.cpp | 6 +++--- src/btop_shared.hpp | 27 ++++++++++++------------- src/btop_tools.cpp | 38 ++++++++++++++++++++++++------------ src/btop_tools.hpp | 6 +++--- src/freebsd/btop_collect.cpp | 12 ++++++------ src/linux/btop_collect.cpp | 10 +++++----- src/osx/btop_collect.cpp | 12 ++++++------ 10 files changed, 77 insertions(+), 66 deletions(-) diff --git a/src/btop.cpp b/src/btop.cpp index 858d668f..d75ef223 100644 --- a/src/btop.cpp +++ b/src/btop.cpp @@ -604,7 +604,7 @@ namespace Runner { //? ------------------------------------------ Secondary thread end ----------------------------------------------- //* Runs collect and draw in a secondary thread, unlocks and locks config to update cached values - void run(const string& box, const bool no_update, const bool force_redraw) { + void run(const string& box, bool no_update, bool force_redraw) { atomic_wait_for(active, true, 5000); if (active) { Logger::error("Stall in Runner thread, restarting!"); diff --git a/src/btop_draw.cpp b/src/btop_draw.cpp index ec0750f0..bc550fdb 100644 --- a/src/btop_draw.cpp +++ b/src/btop_draw.cpp @@ -343,7 +343,7 @@ namespace Draw { //* Meter class ------------------------------------------------------------------------------------------------------------> Meter::Meter() {} - Meter::Meter(const int width, const string& color_gradient, const bool invert) + Meter::Meter(const int width, const string& color_gradient, bool invert) : width(width), color_gradient(color_gradient), invert(invert) {} string Meter::operator()(int value) { @@ -366,7 +366,7 @@ namespace Draw { //* Graph class ------------------------------------------------------------------------------------------------------------> void Graph::_create(const deque& data, int data_offset) { - const bool mult = (data.size() - data_offset > 1); + bool mult = (data.size() - data_offset > 1); const auto& graph_symbol = Symbols::graph_symbols.at(symbol + '_' + (invert ? "down" : "up")); array result; const float mod = (height == 1) ? 0.3 : 0.1; @@ -464,7 +464,7 @@ namespace Draw { this->_create(data, data_offset); } - string& Graph::operator()(const deque& data, const bool data_same) { + string& Graph::operator()(const deque& data, bool data_same) { if (data_same) return out; //? Make room for new characters on graph @@ -503,7 +503,7 @@ namespace Cpu { vector core_graphs; vector temp_graphs; - string draw(const cpu_info& cpu, const bool force_redraw, const bool data_same) { + string draw(const cpu_info& cpu, bool force_redraw, bool data_same) { if (Runner::stopping) return ""; if (force_redraw) redraw = true; bool show_temps = (Config::getB("check_temp") and got_sensors); @@ -725,7 +725,7 @@ namespace Mem { unordered_flat_map disk_meters_free; unordered_flat_map io_graphs; - string draw(const mem_info& mem, const bool force_redraw, const bool data_same) { + string draw(const mem_info& mem, bool force_redraw, bool data_same) { if (Runner::stopping) return ""; if (force_redraw) redraw = true; auto show_swap = Config::getB("show_swap"); @@ -884,7 +884,7 @@ namespace Mem { if (show_disks) { const auto& disks = mem.disks; cx = mem_width; cy = 0; - const bool big_disk = disks_width >= 25; + bool big_disk = disks_width >= 25; divider = Mv::l(1) + Theme::c("div_line") + Symbols::div_left + Symbols::h_line * disks_width + Theme::c("mem_box") + Fx::ub + Symbols::div_right + Mv::l(disks_width); const string hu_div = Theme::c("div_line") + Symbols::h_line + Theme::c("main_fg"); if (io_mode) { @@ -979,7 +979,7 @@ namespace Net { unordered_flat_map graphs; string box; - string draw(const net_info& net, const bool force_redraw, const bool data_same) { + string draw(const net_info& net, bool force_redraw, bool data_same) { if (Runner::stopping) return ""; if (force_redraw) redraw = true; auto net_sync = Config::getB("net_sync"); @@ -1148,11 +1148,11 @@ namespace Proc { return (not changed ? -1 : selected); } - string draw(const vector& plist, const bool force_redraw, const bool data_same) { + string draw(const vector& plist, bool force_redraw, bool data_same) { if (Runner::stopping) return ""; auto proc_tree = Config::getB("proc_tree"); - const bool show_detailed = (Config::getB("show_detailed") and cmp_equal(Proc::detailed.last_pid, Config::getI("detailed_pid"))); - const bool proc_gradient = (Config::getB("proc_gradient") and not Config::getB("lowcolor") and Theme::gradients.contains("proc")); + bool show_detailed = (Config::getB("show_detailed") and cmp_equal(Proc::detailed.last_pid, Config::getI("detailed_pid"))); + bool proc_gradient = (Config::getB("proc_gradient") and not Config::getB("lowcolor") and Theme::gradients.contains("proc")); auto proc_colors = Config::getB("proc_colors"); auto tty_mode = Config::getB("tty_mode"); auto& graph_symbol = (tty_mode ? "tty" : Config::getS("graph_symbol_proc")); @@ -1194,7 +1194,7 @@ namespace Proc { //? Detailed box if (show_detailed) { - const bool alive = detailed.status != "Dead"; + bool alive = detailed.status != "Dead"; dgraph_x = x; dgraph_width = max(width / 3, width - 121); d_width = width - dgraph_width - 1; @@ -1347,7 +1347,7 @@ namespace Proc { //? Draw details box if shown if (show_detailed) { - const bool alive = detailed.status != "Dead"; + bool alive = detailed.status != "Dead"; const int item_fit = floor((double)(d_width - 2) / 10); const int item_width = floor((double)(d_width - 2) / min(item_fit, 8)); @@ -1406,7 +1406,7 @@ namespace Proc { } //? Update graphs for processes with above 0.0% cpu usage, delete if below 0.1% 10x times - const bool has_graph = show_graphs ? p_counters.contains(p.pid) : false; + bool has_graph = show_graphs ? p_counters.contains(p.pid) : false; if (show_graphs and ((p.cpu_p > 0 and not has_graph) or (not data_same and has_graph))) { if (not has_graph) { p_graphs[p.pid] = Draw::Graph{5, 1, "", {}, graph_symbol}; diff --git a/src/btop_draw.hpp b/src/btop_draw.hpp index 59a0e69e..5fafa631 100644 --- a/src/btop_draw.hpp +++ b/src/btop_draw.hpp @@ -93,7 +93,7 @@ namespace Draw { array cache; public: Meter(); - Meter(const int width, const string& color_gradient, const bool invert = false); + Meter(const int width, const string& color_gradient, bool invert = false); //* Return a string representation of the meter with given value string operator()(int value); @@ -123,7 +123,7 @@ namespace Draw { long long max_value=0, long long offset=0); //* Add last value from back of and return string representation of graph - string& operator()(const deque& data, const bool data_same=false); + string& operator()(const deque& data, bool data_same=false); //* Return string representation of graph string& operator()(); diff --git a/src/btop_shared.cpp b/src/btop_shared.cpp index 0e597a1f..1f1e5ac3 100644 --- a/src/btop_shared.cpp +++ b/src/btop_shared.cpp @@ -26,7 +26,7 @@ using namespace Tools; namespace Proc { - void proc_sorter(vector& proc_vec, const string& sorting, const bool reverse, const bool tree) { + void proc_sorter(vector& proc_vec, const string& sorting, bool reverse, bool tree) { if (reverse) { switch (v_index(sort_vector, sorting)) { case 0: rng::stable_sort(proc_vec, rng::less{}, &proc_info::pid); break; @@ -70,7 +70,7 @@ namespace Proc { } } - void tree_sort(vector& proc_vec, const string& sorting, const bool reverse, int& c_index, const int index_max, const bool collapsed) { + void tree_sort(vector& proc_vec, const string& sorting, bool reverse, int& c_index, const int index_max, bool collapsed) { if (proc_vec.size() > 1) { if (reverse) { switch (v_index(sort_vector, sorting)) { @@ -99,7 +99,7 @@ namespace Proc { } void _tree_gen(proc_info& cur_proc, vector& in_procs, vector& out_procs, - int cur_depth, const bool collapsed, const string& filter, bool found, const bool no_update, const bool should_filter) { + int cur_depth, bool collapsed, const string& filter, bool found, bool no_update, bool should_filter) { auto cur_pos = out_procs.size(); bool filtering = false; diff --git a/src/btop_shared.hpp b/src/btop_shared.hpp index 2fed8a78..a5f5d388 100644 --- a/src/btop_shared.hpp +++ b/src/btop_shared.hpp @@ -67,7 +67,7 @@ namespace Runner { extern bool pause_output; extern string debug_bg; - void run(const string& box="", const bool no_update=false, const bool force_redraw=false); + void run(const string& box="", bool no_update = false, bool force_redraw = false); void stop(); } @@ -115,10 +115,10 @@ namespace Cpu { }; //* Collect cpu stats and temperatures - auto collect(const bool no_update=false) -> cpu_info&; + auto collect(bool no_update = false) -> cpu_info&; //* Draw contents of cpu box using as source - string draw(const cpu_info& cpu, const bool force_redraw=false, const bool data_same=false); + string draw(const cpu_info& cpu, bool force_redraw = false, bool data_same = false); //* Parse /proc/cpu info for mapping of core ids auto get_core_mapping() -> unordered_flat_map; @@ -168,10 +168,10 @@ namespace Mem { uint64_t get_totalMem(); //* Collect mem & disks stats - auto collect(const bool no_update=false) -> mem_info&; + auto collect(bool no_update = false) -> mem_info&; //* Draw contents of mem box using as source - string draw(const mem_info& mem, const bool force_redraw=false, const bool data_same=false); + string draw(const mem_info& mem, bool force_redraw = false, bool data_same = false); } @@ -204,10 +204,10 @@ namespace Net { extern unordered_flat_map current_net; //* Collect net upload/download stats - auto collect(const bool no_update=false) -> net_info&; + auto collect(bool no_update=false) -> net_info&; //* Draw contents of net box using as source - string draw(const net_info& net, const bool force_redraw=false, const bool data_same=false); + string draw(const net_info& net, bool force_redraw = false, bool data_same = false); } namespace Proc { @@ -287,13 +287,13 @@ namespace Proc { extern detail_container detailed; //* Collect and sort process information from /proc - auto collect(const bool no_update=false) -> vector&; + auto collect(bool no_update = false) -> vector&; //* Update current selection and view, returns -1 if no change otherwise the current selection int selection(const string& cmd_key); //* Draw contents of proc box using as data source - string draw(const vector& plist, const bool force_redraw=false, const bool data_same=false); + string draw(const vector& plist, bool force_redraw = false, bool data_same = false); struct tree_proc { std::reference_wrapper entry; @@ -301,15 +301,14 @@ namespace Proc { }; //* Sort vector of proc_info's - void proc_sorter(vector& proc_vec, const string& sorting, - const bool reverse, const bool tree = false); + void proc_sorter(vector& proc_vec, const string& sorting, bool reverse, bool tree = false); //* Recursive sort of process tree void tree_sort(vector& proc_vec, const string& sorting, - const bool reverse, int& c_index, const int index_max, const bool collapsed = false); + bool reverse, int& c_index, const int index_max, bool collapsed = false); //* Generate process tree list void _tree_gen(proc_info& cur_proc, vector& in_procs, vector& out_procs, - int cur_depth, const bool collapsed, const string& filter, - bool found=false, const bool no_update=false, const bool should_filter=false); + int cur_depth, bool collapsed, const string& filter, + bool found = false, bool no_update = false, bool should_filter = false); } diff --git a/src/btop_tools.cpp b/src/btop_tools.cpp index 2d4c70e3..6e8becbc 100644 --- a/src/btop_tools.cpp +++ b/src/btop_tools.cpp @@ -98,10 +98,10 @@ namespace Term { } auto get_min_size(const string& boxes) -> array { - const bool cpu = boxes.find("cpu") != string::npos; - const bool mem = boxes.find("mem") != string::npos; - const bool net = boxes.find("net") != string::npos; - const bool proc = boxes.find("proc") != string::npos; + bool cpu = boxes.find("cpu") != string::npos; + bool mem = boxes.find("mem") != string::npos; + bool net = boxes.find("net") != string::npos; + bool proc = boxes.find("proc") != string::npos; int width = 0; if (mem) width = Mem::min_width; else if (net) width = Mem::min_width; @@ -205,8 +205,10 @@ namespace Tools { return chars; } - string uresize(string str, const size_t len, const 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 { std::wstring_convert> conv; @@ -233,8 +235,10 @@ namespace Tools { return str; } - string luresize(string str, const size_t len, const 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(str.at(i)) > 0xef) { x += 2; @@ -484,7 +488,7 @@ namespace Tools { } atomic_lock::atomic_lock(atomic& 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); } @@ -546,10 +550,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); + } } }; @@ -566,8 +574,12 @@ namespace Logger { if (fs::exists(logfile) and fs::file_size(logfile, ec) > 1024 << 10 and not ec) { auto old_log = logfile; old_log += ".1"; - if (fs::exists(old_log)) fs::remove(old_log, ec); - if (not ec) fs::rename(logfile, 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) { std::ofstream lwrite(logfile, std::ios::app); diff --git a/src/btop_tools.hpp b/src/btop_tools.hpp index 52c8e653..d42c2400 100644 --- a/src/btop_tools.hpp +++ b/src/btop_tools.hpp @@ -155,10 +155,10 @@ namespace Tools { } //* Resize a string consisting of UTF8 characters (only reduces size) - string uresize(const string str, const size_t len, const bool wide=false); + string uresize(const string str, const size_t len, bool wide = false); //* Resize a string consisting of UTF8 characters from left (only reduces size) - string luresize(const string str, const size_t len, const bool wide=false); + string luresize(const string str, const size_t len, bool wide = false); //* Replace in with and return new string string s_replace(const string& str, const string& from, const string& to); @@ -288,7 +288,7 @@ namespace Tools { //* bit=True or defaults to bytes //* start=int to set 1024 multiplier starting unit //* short=True always returns 0 decimals and shortens unit to 1 character - string floating_humanizer(uint64_t value, bool shorten = false, size_t start=0, bool bit = false, bool per_second = false); + string floating_humanizer(uint64_t value, bool shorten = false, size_t start = 0, bool bit = false, bool per_second = false); //* Add std::string operator * : Repeat string number of times std::string operator*(const string& str, int64_t n); diff --git a/src/freebsd/btop_collect.cpp b/src/freebsd/btop_collect.cpp index bc013330..c0d5163f 100644 --- a/src/freebsd/btop_collect.cpp +++ b/src/freebsd/btop_collect.cpp @@ -385,7 +385,7 @@ namespace Cpu { return {percent, seconds, status}; } - auto collect(const bool no_update) -> cpu_info & { + auto collect(bool no_update) -> cpu_info & { if (Runner::stopping or (no_update and not current_cpu.cpu_percent.at("total").empty())) return current_cpu; auto &cpu = current_cpu; @@ -610,7 +610,7 @@ namespace Mem { } - auto collect(const bool no_update) -> mem_info & { + auto collect(bool no_update) -> mem_info & { if (Runner::stopping or (no_update and not current_mem.percent.at("used").empty())) return current_mem; @@ -618,7 +618,7 @@ namespace Mem { auto show_disks = Config::getB("show_disks"); auto swap_disk = Config::getB("swap_disk"); auto &mem = current_mem; - static const bool snapped = (getenv("BTOP_SNAPPED") != NULL); + static bool snapped = (getenv("BTOP_SNAPPED") != NULL); int mib[4]; u_int memActive, memWire, cachedMem, freeMem; @@ -803,7 +803,7 @@ namespace Net { auto operator()() -> struct ifaddrs * { return ifaddr; } }; - auto collect(const bool no_update) -> net_info & { + auto collect(bool no_update) -> net_info & { auto &net = current_net; auto &config_iface = Config::getS("net_iface"); auto net_sync = Config::getB("net_sync"); @@ -1078,7 +1078,7 @@ namespace Proc { }; //* Collects and sorts process information from /proc - auto collect(const bool no_update) -> vector & { + auto collect(bool no_update) -> vector & { const auto &sorting = Config::getS("proc_sorting"); auto reverse = Config::getB("proc_reversed"); const auto &filter = Config::getS("proc_filter"); @@ -1088,7 +1088,7 @@ namespace Proc { const size_t detailed_pid = Config::getI("detailed_pid"); bool should_filter = current_filter != filter; if (should_filter) current_filter = filter; - const bool sorted_change = (sorting != current_sort or reverse != current_rev or should_filter); + bool sorted_change = (sorting != current_sort or reverse != current_rev or should_filter); if (sorted_change) { current_sort = sorting; current_rev = reverse; diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index b8b53f11..5bbae49c 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -659,7 +659,7 @@ namespace Cpu { return {percent, seconds, status}; } - auto collect(const bool no_update) -> cpu_info& { + auto collect(bool no_update) -> cpu_info& { if (Runner::stopping or (no_update and not current_cpu.cpu_percent.at("total").empty())) return current_cpu; auto& cpu = current_cpu; @@ -806,7 +806,7 @@ namespace Mem { return totalMem; } - auto collect(const bool no_update) -> mem_info& { + auto collect(bool no_update) -> mem_info& { if (Runner::stopping or (no_update and not current_mem.percent.at("used").empty())) return current_mem; auto show_swap = Config::getB("show_swap"); auto swap_disk = Config::getB("swap_disk"); @@ -1324,7 +1324,7 @@ namespace Net { auto operator()() -> struct ifaddrs* { return ifaddr; } }; - auto collect(const bool no_update) -> net_info& { + auto collect(bool no_update) -> net_info& { auto& net = current_net; auto& config_iface = Config::getS("net_iface"); auto net_sync = Config::getB("net_sync"); @@ -1612,7 +1612,7 @@ namespace Proc { } //* Collects and sorts process information from /proc - auto collect(const bool no_update) -> vector& { + auto collect(bool no_update) -> vector& { const auto& sorting = Config::getS("proc_sorting"); auto reverse = Config::getB("proc_reversed"); const auto& filter = Config::getS("proc_filter"); @@ -1623,7 +1623,7 @@ namespace Proc { const size_t detailed_pid = Config::getI("detailed_pid"); bool should_filter = current_filter != filter; if (should_filter) current_filter = filter; - const bool sorted_change = (sorting != current_sort or reverse != current_rev or should_filter); + bool sorted_change = (sorting != current_sort or reverse != current_rev or should_filter); if (sorted_change) { current_sort = sorting; current_rev = reverse; diff --git a/src/osx/btop_collect.cpp b/src/osx/btop_collect.cpp index 1ae33c4b..b0f09b61 100644 --- a/src/osx/btop_collect.cpp +++ b/src/osx/btop_collect.cpp @@ -437,7 +437,7 @@ namespace Cpu { return {percent, seconds, status}; } - auto collect(const bool no_update) -> cpu_info & { + auto collect(bool no_update) -> cpu_info & { if (Runner::stopping or (no_update and not current_cpu.cpu_percent.at("total").empty())) return current_cpu; auto &cpu = current_cpu; @@ -662,7 +662,7 @@ namespace Mem { } } - auto collect(const bool no_update) -> mem_info & { + auto collect(bool no_update) -> mem_info & { if (Runner::stopping or (no_update and not current_mem.percent.at("used").empty())) return current_mem; @@ -670,7 +670,7 @@ namespace Mem { auto show_disks = Config::getB("show_disks"); auto swap_disk = Config::getB("swap_disk"); auto &mem = current_mem; - static const bool snapped = (getenv("BTOP_SNAPPED") != NULL); + static bool snapped = (getenv("BTOP_SNAPPED") != NULL); vm_statistics64 p; mach_msg_type_number_t info_size = HOST_VM_INFO64_COUNT; @@ -842,7 +842,7 @@ namespace Net { auto operator()() -> struct ifaddrs * { return ifaddr; } }; - auto collect(const bool no_update) -> net_info & { + auto collect(bool no_update) -> net_info & { auto &net = current_net; auto &config_iface = Config::getS("net_iface"); auto net_sync = Config::getB("net_sync"); @@ -1106,7 +1106,7 @@ namespace Proc { } //* Collects and sorts process information from /proc - auto collect(const bool no_update) -> vector & { + auto collect(bool no_update) -> vector & { const auto &sorting = Config::getS("proc_sorting"); auto reverse = Config::getB("proc_reversed"); const auto &filter = Config::getS("proc_filter"); @@ -1116,7 +1116,7 @@ namespace Proc { const size_t detailed_pid = Config::getI("detailed_pid"); bool should_filter = current_filter != filter; if (should_filter) current_filter = filter; - const bool sorted_change = (sorting != current_sort or reverse != current_rev or should_filter); + bool sorted_change = (sorting != current_sort or reverse != current_rev or should_filter); if (sorted_change) { current_sort = sorting; current_rev = reverse;