From bd5d697830f65adb0ce6c4bd7e292e34c10079db Mon Sep 17 00:00:00 2001 From: aristocratos Date: Sat, 26 Aug 2023 20:29:43 +0200 Subject: Squashed commit of the following: commit c296ac13cd4c16a11e137c309b7452bab096312e Merge: 9a1e760 091c30a Author: Jakob P. Liljenberg Date: Sat Aug 26 19:29:57 2023 +0200 Merge pull request #590 from nobounce/dangling-reference-config Convert parameters and config keys to std::string_view commit 9a1e760a661c9a160dd83e6d3ab710bf36b19b04 Merge: 9c8af4d 22e64ca Author: Jakob P. Liljenberg Date: Sat Aug 26 19:20:18 2023 +0200 Merge pull request #602 from jfouquart/main Fix getting zfs pool name with '.' char in freebsd commit 9c8af4df436c2847eefa66d2e0eb7ebfd75d70cf Merge: 8a49d8c 2217cbe Author: Jakob P. Liljenberg Date: Sat Aug 26 19:18:55 2023 +0200 Merge pull request #601 from joske/cleanup [macos] don't check /sys on macos commit 8a49d8cf456d0a15db65e7dc704d627b75a0fe43 Merge: 1556388 008fcd8 Author: Jakob P. Liljenberg Date: Sat Aug 26 19:18:07 2023 +0200 Merge pull request #600 from joske/makefile [macos/freebsd] support gcc13 commit 1556388c83644d122fab9241aa876232d94d1928 Merge: 1b126f5 d17e1a2 Author: Jakob P. Liljenberg Date: Sat Aug 26 19:14:00 2023 +0200 Merge pull request #599 from joske/main [macos] fix temp sensor on system with many cores commit d17e1a2dac79458940319d7117a21bdcd73ed53c Author: Jos Dehaes Date: Fri Aug 25 16:18:39 2023 +0200 fix some warnings commit 4d8aa6b11896dac99f81019e6dea11cc8d8856f1 Author: Jos Dehaes Date: Fri Aug 25 15:52:58 2023 +0200 fix core check commit 22e64caaff3d5877b7a494980a8ee3f17ea8f824 Author: Jonathan Fouquart Date: Fri Aug 25 09:37:49 2023 +0200 Fix getting zfs pool name with '.' char in freebsd commit 2217cbe143dd5aa45dbd50b4dc829577e2e1ccda Author: Jos Dehaes Date: Wed Aug 23 16:01:04 2023 +0200 [macos] don't check /sys on macos commit 008fcd889e862f1d378d331dab51b3d3ce9d9f3c Author: Jos Dehaes Date: Wed Aug 23 16:05:00 2023 +0200 also add g++13 commit 0fdca5eb0385253969e029fdfcf1fb9cff83ea33 Author: Jos Dehaes Date: Wed Aug 23 15:54:07 2023 +0200 support gcc13 commit dcbdb7360d44b4071ec0fe0757a0875a12147c8a Author: Jos Dehaes Date: Wed Aug 23 15:46:47 2023 +0200 [macos] fix temp sensor on system with many cores commit 1b126f55e38de76a2cca796593ef1554828d61e6 Author: aristocratos Date: Fri Aug 4 01:08:27 2023 +0200 Update Makefile for partial static compilation on freebsd commit c8ec6bbb000a865f14c50414e456955c473a2f3a Author: aristocratos Date: Thu Aug 3 23:08:33 2023 +0200 Fix freebsd nullptr changes and makefile for gcc12 and newer commit 8a33aab5885f828d7d0d2523aff31f9c33170332 Merge: 94e5c02 e4abcef Author: Jakob P. Liljenberg Date: Sun Jul 30 13:21:48 2023 +0200 Merge pull request #539 from nobounce/replace-NULL-nullptr Modernize using nullptr. commit 94e5c02d113f3fc8956d63ef4f0eecebfbf31b9d Author: aristocratos Date: Thu Jul 27 20:51:21 2023 +0200 Better text editing commit 091c30ab2be074836bb8d9a4f658cec9a5b36303 Author: nobounce Date: Thu Jul 27 14:17:54 2023 +0200 Convert parameters and config keys to std::string_view Using std::string_view instead of std::string& silences a new warning from GCC 13, -Wdangling-reference Also switch return type of `getI` from int& to int, trivial types are cheaper to copy by value commit e4abcefbf92e5d94ad169e1e47c0fbec7279fa6f Author: nobounce Date: Wed Jul 26 16:19:17 2023 +0200 Use nullptr instead of NULL. See https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2431.pdf TLDR: NULL is of type int and relies on proper implicit pointer conversion which may lead to issues when using overloaded functions It is also considered a 'best practise' for modern C++ and conveys the programmers intention more precisly. commit d53307f14cfa9cb416a3d1c8919d4f61cbb20bf7 Author: nobounce Date: Sun Jul 23 19:53:36 2023 +0200 Fix path to Linux CI file in itself The CI file has a list of dependent files including itself. The path was not updated when the CI was split into different files commit 594f42b9ebf886b70f6cdf0fb909d53eb6c5407f Merge: aca2e4b 53d6eba Author: Jakob P. Liljenberg Date: Wed Jul 26 15:38:01 2023 +0200 Merge pull request #584 from nobounce/nb/fix-ci-path Fix path to Linux CI file in itself commit aca2e4be7568186e122f1506fa25bdfb8c4f7f2a Author: aristocratos Date: Wed Jul 26 14:38:48 2023 +0200 Fix whitespace indent -> tab indent commit 33faa01910309895763011dce2a3194b889a0b6a Author: aristocratos Date: Wed Jul 26 14:34:15 2023 +0200 Revert fmt submodule to static fmt folder in include commit 53d6ebabc052a618001fa857eff25d5252a89210 Author: nobounce Date: Sun Jul 23 19:53:36 2023 +0200 Fix path to Linux CI file in itself The CI file has a list of dependent files including itself. The path was not updated when the CI was split into different files --- src/btop.cpp | 99 +++++++++-------- src/btop_config.cpp | 44 ++++---- src/btop_config.hpp | 39 +++---- src/btop_draw.cpp | 219 +++++++++++++++++++------------------ src/btop_draw.hpp | 28 ++--- src/btop_input.cpp | 36 +++---- src/btop_input.hpp | 4 +- src/btop_menu.cpp | 90 ++++++++-------- src/btop_menu.hpp | 14 +-- src/btop_shared.cpp | 14 +-- src/btop_shared.hpp | 106 +++++++++--------- src/btop_theme.cpp | 75 ++++++------- src/btop_theme.hpp | 6 +- src/btop_tools.cpp | 252 +++++++++++++++++++++---------------------- src/btop_tools.hpp | 67 ++++++------ src/freebsd/btop_collect.cpp | 160 +++++++++++++-------------- src/linux/btop_collect.cpp | 147 ++++++++++++------------- src/osx/btop_collect.cpp | 103 +++++++++--------- src/osx/sensors.cpp | 6 +- src/osx/sensors.hpp | 2 +- src/osx/smc.cpp | 38 ++++--- src/osx/smc.hpp | 4 +- 22 files changed, 781 insertions(+), 772 deletions(-) (limited to 'src') diff --git a/src/btop.cpp b/src/btop.cpp index 02e6af2..5b58a10 100644 --- a/src/btop.cpp +++ b/src/btop.cpp @@ -4,7 +4,7 @@ you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -43,18 +43,16 @@ tab-size = 4 #include #endif -#include -#include -#include -#include -#include -#include -#include +#include "btop_shared.hpp" +#include "btop_tools.hpp" +#include "btop_config.hpp" +#include "btop_input.hpp" +#include "btop_theme.hpp" +#include "btop_draw.hpp" +#include "btop_menu.hpp" using std::atomic; using std::cout; -using std::endl; -using std::endl; using std::flush; using std::min; using std::string; @@ -63,7 +61,6 @@ using std::to_string; using std::vector; namespace fs = std::filesystem; -namespace rng = std::ranges; using namespace Tools; using namespace std::chrono_literals; @@ -96,9 +93,9 @@ namespace Global { string exit_error_msg; atomic thread_exception (false); - bool debuginit{}; // defaults to false - bool debug{}; // defaults to false - bool utf_force{}; // defaults to false + bool debuginit{}; // defaults to false + bool debug{}; // defaults to false + bool utf_force{}; // defaults to false uint64_t start_time; @@ -108,8 +105,8 @@ namespace Global { atomic should_sleep (false); atomic _runner_started (false); - bool arg_tty{}; // defaults to false - bool arg_low_color{}; // defaults to false + bool arg_tty{}; // defaults to false + bool arg_low_color{}; // defaults to false int arg_preset = -1; } @@ -249,14 +246,14 @@ void clean_quit(int sig) { Runner::stop(); if (Global::_runner_started) { #ifdef __APPLE__ - if (pthread_join(Runner::runner_id, NULL) != 0) { + if (pthread_join(Runner::runner_id, nullptr) != 0) { Logger::warning("Failed to join _runner thread on exit!"); pthread_cancel(Runner::runner_id); } #else struct timespec ts; ts.tv_sec = 5; - if (pthread_timedjoin_np(Runner::runner_id, NULL, &ts) != 0) { + if (pthread_timedjoin_np(Runner::runner_id, nullptr, &ts) != 0) { Logger::warning("Failed to join _runner thread on exit!"); pthread_cancel(Runner::runner_id); } @@ -363,14 +360,14 @@ namespace Runner { pthread_mutex_t& pt_mutex; public: int status; - thread_lock(pthread_mutex_t& mtx) : pt_mutex(mtx) { - pthread_mutex_init(&pt_mutex, NULL); - status = pthread_mutex_lock(&pt_mutex); - } - ~thread_lock() { - if (status == 0) - pthread_mutex_unlock(&pt_mutex); - } + thread_lock(pthread_mutex_t& mtx) : pt_mutex(mtx) { + pthread_mutex_init(&pt_mutex, nullptr); + status = pthread_mutex_lock(&pt_mutex); + } + ~thread_lock() { + if (status == 0) + pthread_mutex_unlock(&pt_mutex); + } }; //* Wrapper for raising priviliges when using SUID bit @@ -378,18 +375,18 @@ namespace Runner { int status = -1; public: gain_priv() { - if (Global::real_uid != Global::set_uid) - this->status = seteuid(Global::set_uid); + if (Global::real_uid != Global::set_uid) + this->status = seteuid(Global::set_uid); } ~gain_priv() { - if (status == 0) - status = seteuid(Global::real_uid); + if (status == 0) + status = seteuid(Global::real_uid); } }; string output; string empty_bg; - bool pause_output{}; // defaults to false + bool pause_output{}; // defaults to false sigset_t mask; pthread_t runner_id; pthread_mutex_t mtx; @@ -454,14 +451,14 @@ namespace Runner { } //? ------------------------------- Secondary thread: async launcher and drawing ---------------------------------- - void * _runner(void *) { + void * _runner(void *) { //? Block some signals in this thread to avoid deadlock from any signal handlers trying to stop this thread sigemptyset(&mask); // sigaddset(&mask, SIGINT); // sigaddset(&mask, SIGTSTP); sigaddset(&mask, SIGWINCH); sigaddset(&mask, SIGTERM); - pthread_sigmask(SIG_BLOCK, &mask, NULL); + pthread_sigmask(SIG_BLOCK, &mask, nullptr); //? pthread_mutex_lock to lock thread and monitor health from main thread thread_lock pt_lck(mtx); @@ -549,7 +546,7 @@ namespace Runner { if (Global::debug) debug_timer("cpu", draw_done); } catch (const std::exception& e) { - throw std::runtime_error("Cpu:: -> " + string{e.what()}); + throw std::runtime_error("Cpu:: -> " + string{e.what()}); } } @@ -586,7 +583,7 @@ namespace Runner { if (Global::debug) debug_timer("mem", draw_done); } catch (const std::exception& e) { - throw std::runtime_error("Mem:: -> " + string{e.what()}); + throw std::runtime_error("Mem:: -> " + string{e.what()}); } } @@ -606,7 +603,7 @@ namespace Runner { if (Global::debug) debug_timer("net", draw_done); } catch (const std::exception& e) { - throw std::runtime_error("Net:: -> " + string{e.what()}); + throw std::runtime_error("Net:: -> " + string{e.what()}); } } @@ -626,13 +623,13 @@ namespace Runner { if (Global::debug) debug_timer("proc", draw_done); } catch (const std::exception& e) { - throw std::runtime_error("Proc:: -> " + string{e.what()}); + throw std::runtime_error("Proc:: -> " + string{e.what()}); } } } catch (const std::exception& e) { - Global::exit_error_msg = "Exception in runner thread -> " + string{e.what()}; + Global::exit_error_msg = "Exception in runner thread -> " + string{e.what()}; Global::thread_exception = true; Input::interrupt = true; stopping = true; @@ -711,14 +708,14 @@ 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, bool no_update, 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!"); active = false; // exit(1); pthread_cancel(Runner::runner_id); - if (pthread_create(&Runner::runner_id, NULL, &Runner::_runner, NULL) != 0) { + if (pthread_create(&Runner::runner_id, nullptr, &Runner::_runner, nullptr) != 0) { Global::exit_error_msg = "Failed to re-create _runner thread!"; clean_quit(1); } @@ -806,7 +803,7 @@ int main(int argc, char **argv) { //? Setup paths for config, log and user themes for (const auto& env : {"XDG_CONFIG_HOME", "HOME"}) { - if (std::getenv(env) != NULL and access(std::getenv(env), W_OK) != -1) { + if (std::getenv(env) != nullptr and access(std::getenv(env), W_OK) != -1) { Config::conf_dir = fs::path(std::getenv(env)) / (((string)env == "HOME") ? ".config/btop" : "btop"); break; } @@ -873,17 +870,17 @@ int main(int argc, char **argv) { } //? Try to find and set a UTF-8 locale - if (std::setlocale(LC_ALL, "") != NULL and not s_contains((string)std::setlocale(LC_ALL, ""), ";") + if (std::setlocale(LC_ALL, "") != nullptr and not s_contains((string)std::setlocale(LC_ALL, ""), ";") and str_to_upper(s_replace((string)std::setlocale(LC_ALL, ""), "-", "")).ends_with("UTF8")) { Logger::debug("Using locale " + (string)std::setlocale(LC_ALL, "")); } else { string found; - bool set_failure{}; // defaults to false + bool set_failure{}; // defaults to false for (const auto loc_env : array{"LANG", "LC_ALL"}) { - if (std::getenv(loc_env) != NULL and str_to_upper(s_replace((string)std::getenv(loc_env), "-", "")).ends_with("UTF8")) { + if (std::getenv(loc_env) != nullptr and str_to_upper(s_replace((string)std::getenv(loc_env), "-", "")).ends_with("UTF8")) { found = std::getenv(loc_env); - if (std::setlocale(LC_ALL, found.c_str()) == NULL) { + if (std::setlocale(LC_ALL, found.c_str()) == nullptr) { set_failure = true; Logger::warning("Failed to set locale " + found + " continuing anyway."); } @@ -896,7 +893,7 @@ int main(int argc, char **argv) { for (auto& l : ssplit(loc, ';')) { if (str_to_upper(s_replace(l, "-", "")).ends_with("UTF8")) { found = l.substr(l.find('=') + 1); - if (std::setlocale(LC_ALL, found.c_str()) != NULL) { + if (std::setlocale(LC_ALL, found.c_str()) != nullptr) { break; } } @@ -917,10 +914,10 @@ int main(int argc, char **argv) { if (cur_locale.empty()) { Logger::warning("No UTF-8 locale detected! Some symbols might not display correctly."); } - else if (std::setlocale(LC_ALL, string(cur_locale + ".UTF-8").c_str()) != NULL) { + else if (std::setlocale(LC_ALL, string(cur_locale + ".UTF-8").c_str()) != nullptr) { Logger::debug("Setting LC_ALL=" + cur_locale + ".UTF-8"); } - else if(std::setlocale(LC_ALL, "en_US.UTF-8") != NULL) { + else if(std::setlocale(LC_ALL, "en_US.UTF-8") != nullptr) { Logger::debug("Setting LC_ALL=en_US.UTF-8"); } else { @@ -975,7 +972,7 @@ int main(int argc, char **argv) { Shared::init(); } catch (const std::exception& e) { - Global::exit_error_msg = "Exception in Shared::init() -> " + string{e.what()}; + Global::exit_error_msg = "Exception in Shared::init() -> " + string{e.what()}; clean_quit(1); } @@ -992,7 +989,7 @@ int main(int argc, char **argv) { //? Start runner thread Runner::thread_sem_init(); - if (pthread_create(&Runner::runner_id, NULL, &Runner::_runner, NULL) != 0) { + if (pthread_create(&Runner::runner_id, nullptr, &Runner::_runner, nullptr) != 0) { Global::exit_error_msg = "Failed to create _runner thread!"; clean_quit(1); } @@ -1087,7 +1084,7 @@ int main(int argc, char **argv) { } } catch (const std::exception& e) { - Global::exit_error_msg = "Exception in main loop -> " + string{e.what()}; + Global::exit_error_msg = "Exception in main loop -> " + string{e.what()}; clean_quit(1); } diff --git a/src/btop_config.cpp b/src/btop_config.cpp index 15e6adb..34ae1d5 100644 --- a/src/btop_config.cpp +++ b/src/btop_config.cpp @@ -4,7 +4,7 @@ you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -17,14 +17,16 @@ tab-size = 4 */ #include -#include #include #include +#include #include -#include -#include -#include +#include + +#include "btop_config.hpp" +#include "btop_shared.hpp" +#include "btop_tools.hpp" using std::array; using std::atomic; @@ -105,7 +107,7 @@ namespace Config { {"proc_left", "#* Show proc box on left side of screen instead of right."}, - {"proc_filter_kernel", "#* (Linux) Filter processes tied to the Linux kernel(similar behavior to htop)."}, + {"proc_filter_kernel", "#* (Linux) Filter processes tied to the Linux kernel(similar behavior to htop)."}, {"cpu_graph_upper", "#* Sets the CPU stat shown in upper half of the CPU graph, \"total\" is always available.\n" "#* Select from a list of detected attributes from the options menu."}, @@ -207,7 +209,7 @@ namespace Config { {"custom_gpu_name5", "#* Custom gpu5 model name, empty string to disable."}, }; - unordered_flat_map strings = { + unordered_flat_map strings = { {"color_theme", "Default"}, {"shown_boxes", "cpu mem net proc"}, {"graph_symbol", "braille"}, @@ -240,9 +242,9 @@ namespace Config { {"custom_gpu_name4", ""}, {"custom_gpu_name5", ""}, }; - unordered_flat_map stringsTmp; + unordered_flat_map stringsTmp; - unordered_flat_map bools = { + unordered_flat_map bools = { {"theme_background", true}, {"truecolor", true}, {"rounded_corners", true}, @@ -255,7 +257,7 @@ namespace Config { {"proc_cpu_graphs", true}, {"proc_info_smaps", false}, {"proc_left", false}, - {"proc_filter_kernel", false}, + {"proc_filter_kernel", false}, {"cpu_invert_lower", true}, {"cpu_single_graph", false}, {"cpu_bottom", false}, @@ -290,9 +292,9 @@ namespace Config { {"nvml_measure_pcie_speeds", true}, {"gpu_mirror_graph", true}, }; - unordered_flat_map boolsTmp; + unordered_flat_map boolsTmp; - unordered_flat_map ints = { + unordered_flat_map ints = { {"update_ms", 2000}, {"net_download", 100}, {"net_upload", 100}, @@ -303,9 +305,9 @@ namespace Config { {"proc_selected", 0}, {"proc_last_selected", 0}, }; - unordered_flat_map intsTmp; + unordered_flat_map intsTmp; - bool _locked(const string& name) { + bool _locked(const std::string_view name) { atomic_wait(writelock, true); if (not write_new and rng::find_if(descriptions, [&name](const auto& a) { return a.at(0) == name; }) != descriptions.end()) write_new = true; @@ -392,7 +394,7 @@ namespace Config { string validError; - bool intValid(const string& name, const string& value) { + bool intValid(const std::string_view name, const string& value) { int i_value; try { i_value = stoi(value); @@ -406,7 +408,7 @@ namespace Config { return false; } catch (const std::exception& e) { - validError = string{e.what()}; + validError = string{e.what()}; return false; } @@ -422,7 +424,7 @@ namespace Config { return false; } - bool stringValid(const string& name, const string& value) { + bool stringValid(const std::string_view name, const string& value) { if (name == "log_level" and not v_contains(Logger::log_levels, value)) validError = "Invalid log_level: " + value; @@ -430,7 +432,7 @@ namespace Config { validError = "Invalid graph symbol identifier: " + value; else if (name.starts_with("graph_symbol_") and (value != "default" and not v_contains(valid_graph_symbols, value))) - validError = "Invalid graph symbol identifier for" + name + ": " + value; + validError = fmt::format("Invalid graph symbol identifier for {}: {}", name, value); else if (name == "shown_boxes" and not value.empty() and not check_boxes(value)) validError = "Invalid box name(s) in shown_boxes!"; @@ -479,7 +481,7 @@ namespace Config { return false; } - string getAsString(const string& name) { + string getAsString(const std::string_view name) { if (bools.contains(name)) return (bools.at(name) ? "True" : "False"); else if (ints.contains(name)) @@ -489,7 +491,7 @@ namespace Config { return ""; } - void flip(const string& name) { + void flip(const std::string_view name) { if (_locked(name)) { if (boolsTmp.contains(name)) boolsTmp.at(name) = not boolsTmp.at(name); else boolsTmp.insert_or_assign(name, (not bools.at(name))); @@ -526,7 +528,7 @@ namespace Config { boolsTmp.clear(); } catch (const std::exception& e) { - Global::exit_error_msg = "Exception during Config::unlock() : " + string{e.what()}; + Global::exit_error_msg = "Exception during Config::unlock() : " + string{e.what()}; clean_quit(1); } diff --git a/src/btop_config.hpp b/src/btop_config.hpp index d9a9c3a..c7fc993 100644 --- a/src/btop_config.hpp +++ b/src/btop_config.hpp @@ -4,7 +4,7 @@ you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -20,9 +20,10 @@ tab-size = 4 #include #include -#include #include +#include + using std::string; using std::vector; using robin_hood::unordered_flat_map; @@ -33,12 +34,12 @@ namespace Config { extern std::filesystem::path conf_dir; extern std::filesystem::path conf_file; - extern unordered_flat_map strings; - extern unordered_flat_map stringsTmp; - extern unordered_flat_map bools; - extern unordered_flat_map boolsTmp; - extern unordered_flat_map ints; - extern unordered_flat_map intsTmp; + extern unordered_flat_map strings; + extern unordered_flat_map stringsTmp; + extern unordered_flat_map bools; + extern unordered_flat_map boolsTmp; + extern unordered_flat_map ints; + extern unordered_flat_map intsTmp; const vector valid_graph_symbols = { "braille", "block", "tty" }; const vector valid_graph_symbols_def = { "default", "braille", "block", "tty" }; @@ -62,44 +63,44 @@ namespace Config { //* Apply selected preset void apply_preset(const string& preset); - bool _locked(const string& name); + bool _locked(const std::string_view name); //* Return bool for config key - inline bool getB(const string& name) { return bools.at(name); } + inline bool getB(const std::string_view name) { return bools.at(name); } //* Return integer for config key - inline const int& getI(const string& name) { return ints.at(name); } + inline const int& getI(const std::string_view name) { return ints.at(name); } //* Return string for config key - inline const string& getS(const string& name) { return strings.at(name); } + inline const string& getS(const std::string_view name) { return strings.at(name); } - string getAsString(const string& name); + string getAsString(const std::string_view name); extern string validError; - bool intValid(const string& name, const string& value); - bool stringValid(const string& name, const string& value); + bool intValid(const std::string_view name, const string& value); + bool stringValid(const std::string_view name, const string& value); //* Set config key to bool - inline void set(const string& name, bool value) { + inline void set(const std::string_view name, bool value) { if (_locked(name)) boolsTmp.insert_or_assign(name, value); else bools.at(name) = value; } //* Set config key to int - inline void set(const string& name, const int& value) { + inline void set(const std::string_view name, const int& value) { if (_locked(name)) intsTmp.insert_or_assign(name, value); else ints.at(name) = value; } //* Set config key to string - inline void set(const string& name, const string& value) { + inline void set(const std::string_view name, const string& value) { if (_locked(name)) stringsTmp.insert_or_assign(name, value); else strings.at(name) = value; } //* Flip config key bool - void flip(const string& name); + void flip(const std::string_view name); //* Lock config and cache changes until unlocked void lock(); diff --git a/src/btop_draw.cpp b/src/btop_draw.cpp index 989c052..5aa5b28 100644 --- a/src/btop_draw.cpp +++ b/src/btop_draw.cpp @@ -20,17 +20,18 @@ tab-size = 4 #include #include #include - -#include -#include -#include -#include -#include -#include -#include #include #include +#include "btop_draw.hpp" +#include "btop_config.hpp" +#include "btop_theme.hpp" +#include "btop_shared.hpp" +#include "btop_tools.hpp" +#include "btop_input.hpp" +#include "btop_menu.hpp" + + using std::array; using std::clamp; using std::cmp_equal; @@ -108,8 +109,8 @@ namespace Draw { if (redraw) banner.clear(); if (banner.empty()) { string b_color, bg, fg, oc, letter; - auto lowcolor = Config::getB("lowcolor"); - auto tty_mode = Config::getB("tty_mode"); + auto lowcolor = Config::getB("lowcolor"); + auto tty_mode = Config::getB("tty_mode"); for (size_t z = 0; const auto& line : Global::Banner_src) { if (const auto w = ulen(line[1]); w > width) width = w; if (tty_mode) { @@ -158,10 +159,10 @@ namespace Draw { upos++; pos = uresize(text, upos).size(); } - else if (key == "home" and pos > 0) { + else if (key == "home" and not text.empty() and pos > 0) { pos = upos = 0; } - else if (key == "end" and pos < text.size()) { + else if (key == "end" and not text.empty() and pos < text.size()) { pos = text.size(); upos = ulen(text); } @@ -204,6 +205,10 @@ namespace Draw { } string TextEdit::operator()(const size_t limit) { + string out; + size_t c_upos = upos; + if (text.empty()) + return Fx::ul + " " + Fx::uul; if (limit > 0 and ulen(text) + 1 > limit) { try { const size_t half = (size_t)round((double)limit / 2); @@ -216,29 +221,39 @@ namespace Draw { else first = luresize(text.substr(0, pos), half); - return first + Fx::bl + "█" + Fx::ubl + uresize(text.substr(pos), limit - ulen(first)); + out = first + uresize(text.substr(pos), limit - ulen(first)); + c_upos = ulen(first); } catch (const std::exception& e) { - Logger::error("In TextEdit::operator() : " + string{e.what()}); + Logger::error("In TextEdit::operator() : " + string{e.what()}); + return ""; } } - return text.substr(0, pos) + Fx::bl + "█" + Fx::ubl + text.substr(pos); + else + out = text; + + if (c_upos == 0) + return Fx::ul + uresize(out, 1) + Fx::uul + luresize(out, ulen(out) - 1); + else if (c_upos == ulen(out)) + return out + Fx::ul + " " + Fx::uul; + else + return uresize(out, c_upos) + Fx::ul + luresize(uresize(out, c_upos + 1), 1) + Fx::uul + luresize(out, ulen(out) - c_upos - 1); } void TextEdit::clear() { this->text.clear(); } - string createBox(const int x, const int y, const int width, - const int height, string line_color, bool fill, - const string title, const string title2, const int num) { + string createBox(const int x, const int y, const int width, + 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"); + 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))); const auto& right_up = (tty_mode or not rounded ? Symbols::right_up : Symbols::round_right_up); const auto& left_up = (tty_mode or not rounded ? Symbols::left_up : Symbols::round_left_up); @@ -248,12 +263,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; @@ -291,11 +306,11 @@ namespace Draw { {"/uptime", ""} }; - static time_t c_time{}; // defaults to 0 - static size_t clock_len{}; // defaults to 0 + static time_t c_time{}; // defaults to 0 + static size_t clock_len{}; // defaults to 0 static string clock_str; - if (auto n_time = time(NULL); not force and n_time == c_time) + if (auto n_time = time(nullptr); not force and n_time == c_time) return false; else { c_time = n_time; @@ -305,7 +320,7 @@ namespace Draw { } auto& out = Global::clock; - auto cpu_bottom = Config::getB("cpu_bottom"); + auto cpu_bottom = Config::getB("cpu_bottom"); const auto& x = Cpu::x; const auto y = (cpu_bottom ? Cpu::y + Cpu::height - 1 : Cpu::y); const auto& width = Cpu::width; @@ -345,8 +360,8 @@ namespace Draw { //* Meter class ------------------------------------------------------------------------------------------------------------> Meter::Meter() {} - Meter::Meter(const int width, const string& color_gradient, bool invert) - : width(width), color_gradient(color_gradient), invert(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) { if (width < 1) return ""; @@ -368,7 +383,7 @@ namespace Draw { //* Graph class ------------------------------------------------------------------------------------------------------------> void Graph::_create(const deque& data, int data_offset) { - 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; @@ -438,11 +453,11 @@ namespace Draw { Graph::Graph() {} - Graph::Graph(int width, int height, const string& color_gradient, - const deque& data, const string& symbol, - bool invert, bool no_zero, long long max_value, long long offset) - : width(width), height(height), color_gradient(color_gradient), - invert(invert), no_zero(no_zero), offset(offset) { + Graph::Graph(int width, int height, const string& color_gradient, + const deque& data, const string& symbol, + bool invert, bool no_zero, long long max_value, long long offset) + : width(width), height(height), color_gradient(color_gradient), + invert(invert), no_zero(no_zero), offset(offset) { if (Config::getB("tty_mode") or symbol == "tty") this->symbol = "tty"; else if (symbol != "default") this->symbol = symbol; else this->symbol = Config::getS("graph_symbol"); @@ -466,7 +481,7 @@ namespace Draw { this->_create(data, data_offset); } - string& Graph::operator()(const deque& data, bool data_same) { + string& Graph::operator()(const deque& data, bool data_same) { if (data_same) return out; //? Make room for new characters on graph @@ -514,23 +529,23 @@ namespace Cpu { string draw(const cpu_info& cpu, const vector& gpus, 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); - auto single_graph = Config::getB("cpu_single_graph"); - bool hide_cores = show_temps and (cpu_temp_only or not Config::getB("show_coretemp")); + bool show_temps = (Config::getB("check_temp") and got_sensors); + auto single_graph = Config::getB("cpu_single_graph"); + bool hide_cores = show_temps and (cpu_temp_only or not Config::getB("show_coretemp")); const int extra_width = (hide_cores ? max(6, 6 * b_column_size) : 0); auto& graph_up_field = Config::getS("cpu_graph_upper"); auto& graph_lo_field = Config::getS("cpu_graph_lower"); - auto tty_mode = Config::getB("tty_mode"); + auto tty_mode = Config::getB("tty_mode"); auto& graph_symbol = (tty_mode ? "tty" : Config::getS("graph_symbol_cpu")); auto& graph_bg = Symbols::graph_symbols.at((graph_symbol == "default" ? Config::getS("graph_symbol") + "_up" : graph_symbol + "_up")).at(6); auto& temp_scale = Config::getS("temp_scale"); - auto cpu_bottom = Config::getB("cpu_bottom"); + auto cpu_bottom = Config::getB("cpu_bottom"); const string& title_left = Theme::c("cpu_box") + (cpu_bottom ? Symbols::title_left_down : Symbols::title_left); const string& title_right = Theme::c("cpu_box") + (cpu_bottom ? Symbols::title_right_down : Symbols::title_right); static int bat_pos = 0, bat_len = 0; - if (cpu.cpu_percent.at("total").empty() - or cpu.core_percent.at(0).empty() - or (show_temps and cpu.temp.at(0).empty())) return ""; + if (cpu.cpu_percent.at("total").empty() + or cpu.core_percent.at(0).empty() + or (show_temps and cpu.temp.at(0).empty())) return ""; string out; out.reserve(width * height); @@ -664,8 +679,8 @@ namespace Cpu { //? Draw battery if enabled and present if (Config::getB("show_battery") and has_battery) { - static int old_percent{}; // defaults to = 0 - static long old_seconds{}; // defaults to = 0 + static int old_percent{}; // defaults to = 0 + static long old_seconds{}; // defaults to = 0 static string old_status; static Draw::Meter bat_meter {10, "cpu", true}; static const unordered_flat_map bat_symbols = { @@ -758,7 +773,7 @@ namespace Cpu { } out += Theme::c("div_line") + Symbols::v_line; - } catch (const std::exception& e) { throw std::runtime_error("graphs, clock, meter : " + string{e.what()}); } + } catch (const std::exception& e) { throw std::runtime_error("graphs, clock, meter : " + string{e.what()}); } //? Core text and graphs int cx = 0, cy = 1, cc = 0, core_width = (b_column_size == 0 ? 2 : 3); @@ -1048,17 +1063,17 @@ namespace Mem { unordered_flat_map disk_meters_free; unordered_flat_map io_graphs; - string draw(const mem_info& mem, bool force_redraw, 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"); - auto swap_disk = Config::getB("swap_disk"); - auto show_disks = Config::getB("show_disks"); - auto show_io_stat = Config::getB("show_io_stat"); - auto io_mode = Config::getB("io_mode"); - auto io_graph_combined = Config::getB("io_graph_combined"); - auto use_graphs = Config::getB("mem_graphs"); - auto tty_mode = Config::getB("tty_mode"); + auto show_swap = Config::getB("show_swap"); + auto swap_disk = Config::getB("swap_disk"); + auto show_disks = Config::getB("show_disks"); + auto show_io_stat = Config::getB("show_io_stat"); + auto io_mode = Config::getB("io_mode"); + auto io_graph_combined = Config::getB("io_graph_combined"); + auto use_graphs = Config::getB("mem_graphs"); + auto tty_mode = Config::getB("tty_mode"); auto& graph_symbol = (tty_mode ? "tty" : Config::getS("graph_symbol_mem")); auto& graph_bg = Symbols::graph_symbols.at((graph_symbol == "default" ? Config::getS("graph_symbol") + "_up" : graph_symbol + "_up")).at(6); auto totalMem = Mem::get_totalMem(); @@ -1124,20 +1139,20 @@ namespace Mem { if (io_graph_combined) { deque combined(disk.io_read.size(), 0); rng::transform(disk.io_read, disk.io_write, combined.begin(), std::plus()); - io_graphs[name] = Draw::Graph{ - disks_width - (io_mode ? 0 : 6), - disks_io_h, "available", combined, - graph_symbol, false, true, speed}; + io_graphs[name] = Draw::Graph{ + disks_width - (io_mode ? 0 : 6), + disks_io_h, "available", combined, + graph_symbol, false, true, speed}; } else { - io_graphs[name + "_read"] = Draw::Graph{ - disks_width, half_height, "free", - disk.io_read, graph_symbol, false, - true, speed}; - io_graphs[name + "_write"] = Draw::Graph{ - disks_width, disks_io_h - half_height, - "used", disk.io_write, graph_symbol, - true, true, speed}; + io_graphs[name + "_read"] = Draw::Graph{ + disks_width, half_height, "free", + disk.io_read, graph_symbol, false, + true, speed}; + io_graphs[name + "_write"] = Draw::Graph{ + disks_width, disks_io_h - half_height, + "used", disk.io_write, graph_symbol, + true, true, speed}; } } } @@ -1207,7 +1222,7 @@ namespace Mem { if (show_disks) { const auto& disks = mem.disks; cx = mem_width; cy = 0; - 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) { @@ -1302,12 +1317,12 @@ namespace Net { unordered_flat_map graphs; string box; - string draw(const net_info& net, bool force_redraw, 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"); - auto net_auto = Config::getB("net_auto"); - auto tty_mode = Config::getB("tty_mode"); + auto net_sync = Config::getB("net_sync"); + auto net_auto = Config::getB("net_auto"); + auto tty_mode = Config::getB("tty_mode"); auto& graph_symbol = (tty_mode ? "tty" : Config::getS("graph_symbol_net")); string ip_addr = (net.ipv4.empty() ? net.ipv6 : net.ipv4); if (old_ip != ip_addr) { @@ -1329,13 +1344,13 @@ namespace Net { graphs.clear(); if (net.bandwidth.at("download").empty() or net.bandwidth.at("upload").empty()) return out + Fx::reset; - graphs["download"] = Draw::Graph{ - width - b_width - 2, u_graph_height, "download", - net.bandwidth.at("download"), graph_symbol, - false, true, down_max}; - graphs["upload"] = Draw::Graph{ - width - b_width - 2, d_graph_height, "upload", - net.bandwidth.at("upload"), graph_symbol, true, true, up_max}; + graphs["download"] = Draw::Graph{ + width - b_width - 2, u_graph_height, "download", + net.bandwidth.at("download"), graph_symbol, + false, true, down_max}; + graphs["upload"] = Draw::Graph{ + width - b_width - 2, d_graph_height, "upload", + net.bandwidth.at("upload"), graph_symbol, true, true, up_max}; //? Interface selector and buttons @@ -1416,7 +1431,7 @@ namespace Proc { auto selected = Config::getI("proc_selected"); auto last_selected = Config::getI("proc_last_selected"); const int select_max = (Config::getB("show_detailed") ? Proc::select_max - 8 : Proc::select_max); - auto vim_keys = Config::getB("vim_keys"); + auto vim_keys = Config::getB("vim_keys"); int numpids = Proc::numpids; if ((cmd_key == "up" or (vim_keys and cmd_key == "k")) and selected > 0) { @@ -1471,18 +1486,18 @@ namespace Proc { return (not changed ? -1 : selected); } - string draw(const vector& plist, bool force_redraw, 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"); - 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 proc_tree = Config::getB("proc_tree"); + 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")); auto& graph_bg = Symbols::graph_symbols.at((graph_symbol == "default" ? Config::getS("graph_symbol") + "_up" : graph_symbol + "_up")).at(6); - auto mem_bytes = Config::getB("proc_mem_bytes"); - auto vim_keys = Config::getB("vim_keys"); - auto show_graphs = Config::getB("proc_cpu_graphs"); + auto mem_bytes = Config::getB("proc_mem_bytes"); + auto vim_keys = Config::getB("vim_keys"); + auto show_graphs = Config::getB("proc_cpu_graphs"); start = Config::getI("proc_start"); selected = Config::getI("proc_selected"); const int y = show_detailed ? Proc::y + 8 : Proc::y; @@ -1517,7 +1532,7 @@ namespace Proc { //? Detailed box if (show_detailed) { - 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; @@ -1589,7 +1604,7 @@ namespace Proc { } //? Filter - auto filtering = Config::getB("proc_filtering"); // ? filter(20) : Config::getS("proc_filter")) + auto filtering = Config::getB("proc_filtering"); // ? filter(20) : Config::getS("proc_filter")) const auto filter_text = (filtering) ? filter(max(6, width - 58)) : uresize(Config::getS("proc_filter"), max(6, width - 58)); out += Mv::to(y, x+9) + title_left + (not filter_text.empty() ? Fx::b : "") + Theme::c("hi_fg") + 'f' + Theme::c("title") + (not filter_text.empty() ? ' ' + filter_text : "ilter") @@ -1670,7 +1685,7 @@ namespace Proc { //? Draw details box if shown if (show_detailed) { - 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)); @@ -1729,7 +1744,7 @@ namespace Proc { } //? Update graphs for processes with above 0.0% cpu usage, delete if below 0.1% 10x times - 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}; @@ -1886,10 +1901,10 @@ namespace Draw { void calcSizes() { atomic_wait(Runner::active); Config::unlock(); - auto boxes = Config::getS("shown_boxes"); - auto cpu_bottom = Config::getB("cpu_bottom"); - auto mem_below_net = Config::getB("mem_below_net"); - auto proc_left = Config::getB("proc_left"); + auto boxes = Config::getS("shown_boxes"); + auto cpu_bottom = Config::getB("cpu_bottom"); + auto mem_below_net = Config::getB("mem_below_net"); + auto proc_left = Config::getB("proc_left"); Cpu::box.clear(); Gpu::box.clear(); @@ -2030,9 +2045,9 @@ namespace Draw { //* Calculate and draw mem box outlines if (Mem::shown) { using namespace Mem; - auto show_disks = Config::getB("show_disks"); - auto swap_disk = Config::getB("swap_disk"); - auto mem_graphs = Config::getB("mem_graphs"); + auto show_disks = Config::getB("show_disks"); + auto swap_disk = Config::getB("swap_disk"); + auto mem_graphs = Config::getB("mem_graphs"); width = round((double)Term::width * (Proc::shown ? width_p : 100) / 100); height = ceil((double)Term::height * (100 - Net::height_p * Net::shown*4 / ((Gpu::shown != 0 and Cpu::shown) + 4)) / 100) - Cpu::height - Gpu::height*Gpu::shown; diff --git a/src/btop_draw.hpp b/src/btop_draw.hpp index 5fafa63..af57ed8 100644 --- a/src/btop_draw.hpp +++ b/src/btop_draw.hpp @@ -66,8 +66,8 @@ namespace Draw { //* An editable text field class TextEdit { - size_t pos{}; // defaults to 0 - size_t upos{}; // defaults to 0 + size_t pos{}; // defaults to 0 + size_t upos{}; // defaults to 0 bool numeric; public: string text; @@ -79,11 +79,11 @@ 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 = "", bool fill = false, - const string title = "", const string title2 = "", const int num = 0); + string createBox(const int x, const int y, const int width, + 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 { @@ -93,7 +93,7 @@ namespace Draw { array cache; public: Meter(); - Meter(const int width, const string& color_gradient, 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); @@ -115,15 +115,15 @@ namespace Draw { public: Graph(); - Graph(int width, int height, - const string& color_gradient, - const deque& data, - const string& symbol="default", - bool invert=false, bool no_zero=false, - long long max_value=0, long long offset=0); + Graph(int width, int height, + const string& color_gradient, + const deque& data, + const string& symbol="default", + bool invert=false, bool no_zero=false, + 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, 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_input.cpp b/src/btop_input.cpp index 8c0904a..3ddc81b 100644 --- a/src/btop_input.cpp +++ b/src/btop_input.cpp @@ -4,7 +4,7 @@ you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -21,17 +21,17 @@ tab-size = 4 #include #include #include - -#include -#include -#include -#include -#include -#include #include +#include "btop_input.hpp" +#include "btop_tools.hpp" +#include "btop_config.hpp" +#include "btop_shared.hpp" +#include "btop_menu.hpp" +#include "btop_draw.hpp" + + using std::cin; -using std::vector; using namespace Tools; using namespace std::literals; // for operator""s @@ -238,8 +238,8 @@ namespace Input { void process(const string& key) { if (key.empty()) return; try { - auto filtering = Config::getB("proc_filtering"); - auto vim_keys = Config::getB("vim_keys"); + auto filtering = Config::getB("proc_filtering"); + auto vim_keys = Config::getB("vim_keys"); auto help_key = (vim_keys ? "H" : "h"); auto kill_key = (vim_keys ? "K" : "k"); //? Global input actions @@ -299,12 +299,12 @@ namespace Input { if (key == "enter" or key == "down") { Config::set("proc_filter", Proc::filter.text); Config::set("proc_filtering", false); - old_filter.clear(); - if(key == "down"){ - process("down"); - return; - } - } + old_filter.clear(); + if(key == "down"){ + process("down"); + return; + } + } else if (key == "escape" or key == "mouse_click") { Config::set("proc_filter", old_filter); Config::set("proc_filtering", false); @@ -551,7 +551,7 @@ namespace Input { } catch (const std::exception& e) { - throw std::runtime_error("Input::process(\"" + key + "\") : " + string{e.what()}); + throw std::runtime_error("Input::process(\"" + key + "\") : " + string{e.what()}); } } diff --git a/src/btop_input.hpp b/src/btop_input.hpp index 2e8c0a4..5a1a28e 100644 --- a/src/btop_input.hpp +++ b/src/btop_input.hpp @@ -4,7 +4,7 @@ you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -32,7 +32,7 @@ using std::string; /* The input functions relies on the following std::cin options being set: cin.sync_with_stdio(false); - cin.tie(NULL); + cin.tie(nullptr); These will automatically be set when running Term::init() from btop_tools.cpp */ diff --git a/src/btop_menu.cpp b/src/btop_menu.cpp index 5f7f10d..873b86a 100644 --- a/src/btop_menu.cpp +++ b/src/btop_menu.cpp @@ -19,24 +19,21 @@ tab-size = 4 #include #include #include -#include #include #include #include #include -#include -#include -#include -#include -#include -#include +#include "btop_menu.hpp" +#include "btop_tools.hpp" +#include "btop_config.hpp" +#include "btop_theme.hpp" +#include "btop_draw.hpp" +#include "btop_shared.hpp" using robin_hood::unordered_flat_map; using std::array; using std::ceil; -using std::clamp; -using std::deque; using std::max; using std::min; using std::ref; @@ -45,7 +42,6 @@ using std::views::iota; using namespace Tools; namespace fs = std::filesystem; -namespace rng = std::ranges; namespace Menu { @@ -685,19 +681,19 @@ namespace Menu { "Show cpu graph for each process.", "", "True or False"}, - {"proc_filter_kernel", - "(Linux) Filter kernel processes from output.", - "", - "Set to 'True' to filter out internal", - "processes started by the Linux kernel."}, + {"proc_filter_kernel", + "(Linux) Filter kernel processes from output.", + "", + "Set to 'True' to filter out internal", + "processes started by the Linux kernel."}, } }; msgBox::msgBox() {} msgBox::msgBox(int width, int boxtype, vector content, string title) : width(width), boxtype(boxtype) { - auto tty_mode = Config::getB("tty_mode"); - auto rounded = Config::getB("rounded_corners"); + auto tty_mode = Config::getB("tty_mode"); + auto rounded = Config::getB("rounded_corners"); const auto& right_up = (tty_mode or not rounded ? Symbols::right_up : Symbols::round_right_up); const auto& left_up = (tty_mode or not rounded ? Symbols::left_up : Symbols::round_left_up); const auto& right_down = (tty_mode or not rounded ? Symbols::right_down : Symbols::round_right_down); @@ -786,10 +782,10 @@ namespace Menu { }; int signalChoose(const string& key) { - auto s_pid = (Config::getB("show_detailed") and Config::getI("selected_pid") == 0 ? Config::getI("detailed_pid") : Config::getI("selected_pid")); - static int x{}; // defaults to 0 - static int y{}; // defaults to 0 - static int selected_signal = -1; + auto s_pid = (Config::getB("show_detailed") and Config::getI("selected_pid") == 0 ? Config::getI("detailed_pid") : Config::getI("selected_pid")); + static int x{}; // defaults to 0 + static int y{}; // defaults to 0 + static int selected_signal = -1; if (bg.empty()) selected_signal = -1; auto& out = Global::overlay; @@ -916,7 +912,7 @@ namespace Menu { } int signalSend(const string& key) { - auto s_pid = (Config::getB("show_detailed") and Config::getI("selected_pid") == 0 ? Config::getI("detailed_pid") : Config::getI("selected_pid")); + auto s_pid = (Config::getB("show_detailed") and Config::getI("selected_pid") == 0 ? Config::getI("detailed_pid") : Config::getI("selected_pid")); if (s_pid == 0) return Closed; if (redraw) { atomic_wait(Runner::active); @@ -989,11 +985,11 @@ namespace Menu { int mainMenu(const string& key) { enum MenuItems { Options, Help, Quit }; - static int y{}; // defaults to 0 - static int selected{}; // defaults to 0 + static int y{}; // defaults to 0 + static int selected{}; // defaults to 0 static vector colors_selected; static vector colors_normal; - auto tty_mode = Config::getB("tty_mode"); + auto tty_mode = Config::getB("tty_mode"); if (bg.empty()) selected = 0; int retval = Changed; @@ -1068,18 +1064,18 @@ namespace Menu { int optionsMenu(const string& key) { enum Predispositions { isBool, isInt, isString, is2D, isBrowseable, isEditable}; - static int y{}; // defaults to 0 - static int x{}; // defaults to 0 - static int height{}; // defaults to 0 - static int page{}; // defaults to 0 - static int pages{}; // defaults to 0 - static int selected{}; // defaults to 0 - static int select_max{}; // defaults to 0 - static int item_height{}; // defaults to 0 - static int selected_cat{}; // defaults to 0 - static int max_items{}; // defaults to 0 - static int last_sel{}; // defaults to 0 - static bool editing{}; // defaults to false + static int y{}; // defaults to 0 + static int x{}; // defaults to 0 + static int height{}; // defaults to 0 + static int page{}; // defaults to 0 + static int pages{}; // defaults to 0 + static int selected{}; // defaults to 0 + static int select_max{}; // defaults to 0 + static int item_height{}; // defaults to 0 + static int selected_cat{}; // defaults to 0 + static int max_items{}; // defaults to 0 + static int last_sel{}; // defaults to 0 + static bool editing{}; // defaults to false static Draw::TextEdit editor; static string warnings; static bitset<8> selPred; @@ -1099,8 +1095,8 @@ namespace Menu { {"cpu_sensor", std::cref(Cpu::available_sensors)}, {"selected_battery", std::cref(Config::available_batteries)}, }; - auto tty_mode = Config::getB("tty_mode"); - auto vim_keys = Config::getB("vim_keys"); + auto tty_mode = Config::getB("tty_mode"); + auto vim_keys = Config::getB("vim_keys"); if (max_items == 0) { for (const auto& cat : categories) { if ((int)cat.size() > max_items) max_items = cat.size(); @@ -1112,9 +1108,9 @@ namespace Menu { Theme::updateThemes(); } int retval = Changed; - bool recollect{}; // defaults to false - bool screen_redraw{}; // defaults to false - bool theme_refresh{}; // defaults to false + bool recollect{}; // defaults to false + bool screen_redraw{}; // defaults to false + bool theme_refresh{}; // defaults to false //? Draw background if needed else process input if (redraw) { @@ -1408,11 +1404,11 @@ namespace Menu { } int helpMenu(const string& key) { - static int y{}; // defaults to 0 - static int x{}; // defaults to 0 - static int height{}; // defaults to 0 - static int page{}; // defaults to 0 - static int pages{}; // defaults to 0 + static int y{}; // defaults to 0 + static int x{}; // defaults to 0 + static int height{}; // defaults to 0 + static int page{}; // defaults to 0 + static int pages{}; // defaults to 0 if (bg.empty()) page = 0; int retval = Changed; diff --git a/src/btop_menu.hpp b/src/btop_menu.hpp index 0017962..0e8832f 100644 --- a/src/btop_menu.hpp +++ b/src/btop_menu.hpp @@ -23,7 +23,7 @@ tab-size = 4 #include #include -#include +#include "btop_input.hpp" using std::atomic; using std::bitset; @@ -46,12 +46,12 @@ namespace Menu { //? Strings in content vector is not checked for box width overflow class msgBox { string box_contents, button_left, button_right; - int height{}; // defaults to 0 - int width{}; // defaults to 0 - int boxtype{}; // defaults to 0 - int selected{}; // defaults to 0 - int x{}; // defaults to 0 - int y{}; // defaults to 0 + int height{}; // defaults to 0 + int width{}; // defaults to 0 + int boxtype{}; // defaults to 0 + int selected{}; // defaults to 0 + int x{}; // defaults to 0 + int y{}; // defaults to 0 public: enum BoxTypes { OK, YES_NO, NO_YES }; enum msgReturn { diff --git a/src/btop_shared.cpp b/src/btop_shared.cpp index 1f1e5ac..e5c9027 100644 --- a/src/btop_shared.cpp +++ b/src/btop_shared.cpp @@ -18,15 +18,15 @@ tab-size = 4 #include -#include -#include +#include "btop_shared.hpp" +#include "btop_tools.hpp" namespace rng = std::ranges; using namespace Tools; namespace Proc { - void proc_sorter(vector& proc_vec, const string& sorting, bool reverse, 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, bool reverse, int& c_index, const int index_max, 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)) { @@ -98,8 +98,8 @@ namespace Proc { } } - void _tree_gen(proc_info& cur_proc, vector& in_procs, vector& out_procs, - int cur_depth, bool collapsed, const string& filter, bool found, bool no_update, bool should_filter) { + void _tree_gen(proc_info& cur_proc, vector& in_procs, vector& out_procs, + 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; @@ -132,7 +132,7 @@ namespace Proc { std::string_view cmd_view = cur_proc.cmd; cmd_view = cmd_view.substr((size_t)0, std::min(cmd_view.find(' '), cmd_view.size())); cmd_view = cmd_view.substr(std::min(cmd_view.find_last_of('/') + 1, cmd_view.size())); - cur_proc.short_cmd = string{cmd_view}; + cur_proc.short_cmd = string{cmd_view}; } } else { diff --git a/src/btop_shared.hpp b/src/btop_shared.hpp index 1901d41..cb6c0fe 100644 --- a/src/btop_shared.hpp +++ b/src/btop_shared.hpp @@ -68,7 +68,7 @@ namespace Runner { extern bool pause_output; extern string debug_bg; - void run(const string& box="", bool no_update = false, bool force_redraw = false); + void run(const string& box="", bool no_update = false, bool force_redraw = false); void stop(); } @@ -195,7 +195,7 @@ namespace Cpu { }; //* Collect cpu stats and temperatures - auto collect(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 vector& gpu, bool force_redraw = false, bool data_same = false); @@ -212,20 +212,20 @@ namespace Mem { extern string box; extern int x, y, width, height, min_width, min_height; extern bool has_swap, shown, redraw; - const array mem_names { "used"s, "available"s, "cached"s, "free"s }; - const array swap_names { "swap_used"s, "swap_free"s }; + const array mem_names { "used"s, "available"s, "cached"s, "free"s }; + const array swap_names { "swap_used"s, "swap_free"s }; extern int disk_ios; struct disk_info { std::filesystem::path dev; string name; - string fstype{}; // defaults to "" - std::filesystem::path stat{}; // defaults to "" - int64_t total{}; // defaults to 0 - int64_t used{}; // defaults to 0 - int64_t free{}; // defaults to 0 - int used_percent{}; // defaults to 0 - int free_percent{}; // defaults to 0 + string fstype{}; // defaults to "" + std::filesys