summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authoraristocratos <gnmjpl@gmail.com>2023-08-26 20:29:43 +0200
committeraristocratos <gnmjpl@gmail.com>2023-08-26 20:29:43 +0200
commitbd5d697830f65adb0ce6c4bd7e292e34c10079db (patch)
tree0566c25feb85c5e0ce4b76ad06055d180f8b9e6e /src
parent346c9e479be1328c2fb133a457259b09884a1db3 (diff)
Squashed commit of the following:
commit c296ac13cd4c16a11e137c309b7452bab096312e Merge: 9a1e760 091c30a Author: Jakob P. Liljenberg <admin@qvantnet.com> 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 <admin@qvantnet.com> 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 <admin@qvantnet.com> 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 <admin@qvantnet.com> 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 <admin@qvantnet.com> 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 <jos.dehaes@gmail.com> Date: Fri Aug 25 16:18:39 2023 +0200 fix some warnings commit 4d8aa6b11896dac99f81019e6dea11cc8d8856f1 Author: Jos Dehaes <jos.dehaes@gmail.com> Date: Fri Aug 25 15:52:58 2023 +0200 fix core check commit 22e64caaff3d5877b7a494980a8ee3f17ea8f824 Author: Jonathan Fouquart <jfouquart@hotmail.fr> Date: Fri Aug 25 09:37:49 2023 +0200 Fix getting zfs pool name with '.' char in freebsd commit 2217cbe143dd5aa45dbd50b4dc829577e2e1ccda Author: Jos Dehaes <jos.dehaes@gmail.com> Date: Wed Aug 23 16:01:04 2023 +0200 [macos] don't check /sys on macos commit 008fcd889e862f1d378d331dab51b3d3ce9d9f3c Author: Jos Dehaes <jos.dehaes@gmail.com> Date: Wed Aug 23 16:05:00 2023 +0200 also add g++13 commit 0fdca5eb0385253969e029fdfcf1fb9cff83ea33 Author: Jos Dehaes <jos.dehaes@gmail.com> Date: Wed Aug 23 15:54:07 2023 +0200 support gcc13 commit dcbdb7360d44b4071ec0fe0757a0875a12147c8a Author: Jos Dehaes <jos.dehaes@gmail.com> Date: Wed Aug 23 15:46:47 2023 +0200 [macos] fix temp sensor on system with many cores commit 1b126f55e38de76a2cca796593ef1554828d61e6 Author: aristocratos <gnmjpl@gmail.com> Date: Fri Aug 4 01:08:27 2023 +0200 Update Makefile for partial static compilation on freebsd commit c8ec6bbb000a865f14c50414e456955c473a2f3a Author: aristocratos <gnmjpl@gmail.com> 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 <admin@qvantnet.com> 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 <gnmjpl@gmail.com> Date: Thu Jul 27 20:51:21 2023 +0200 Better text editing commit 091c30ab2be074836bb8d9a4f658cec9a5b36303 Author: nobounce <steffen.winter@proton.me> 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 <steffen.winter@proton.me> 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 <steffen.winter@proton.me> 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 <admin@qvantnet.com> 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 <gnmjpl@gmail.com> Date: Wed Jul 26 14:38:48 2023 +0200 Fix whitespace indent -> tab indent commit 33faa01910309895763011dce2a3194b889a0b6a Author: aristocratos <gnmjpl@gmail.com> Date: Wed Jul 26 14:34:15 2023 +0200 Revert fmt submodule to static fmt folder in include commit 53d6ebabc052a618001fa857eff25d5252a89210 Author: nobounce <steffen.winter@proton.me> 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
Diffstat (limited to 'src')
-rw-r--r--src/btop.cpp99
-rw-r--r--src/btop_config.cpp44
-rw-r--r--src/btop_config.hpp39
-rw-r--r--src/btop_draw.cpp219
-rw-r--r--src/btop_draw.hpp28
-rw-r--r--src/btop_input.cpp36
-rw-r--r--src/btop_input.hpp4
-rw-r--r--src/btop_menu.cpp90
-rw-r--r--src/btop_menu.hpp14
-rw-r--r--src/btop_shared.cpp14
-rw-r--r--src/btop_shared.hpp106
-rw-r--r--src/btop_theme.cpp75
-rw-r--r--src/btop_theme.hpp6
-rw-r--r--src/btop_tools.cpp252
-rw-r--r--src/btop_tools.hpp67
-rw-r--r--src/freebsd/btop_collect.cpp160
-rw-r--r--src/linux/btop_collect.cpp147
-rw-r--r--src/osx/btop_collect.cpp103
-rw-r--r--src/osx/sensors.cpp6
-rw-r--r--src/osx/sensors.hpp2
-rw-r--r--src/osx/smc.cpp38
-rw-r--r--src/osx/smc.hpp4
22 files changed, 781 insertions, 772 deletions
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 <semaphore>
#endif
-#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>
+#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<bool> 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<bool> should_sleep (false);
atomic<bool> _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 <array>
-#include <ranges>
#include <atomic>
#include <fstream>
+#include <ranges>
#include <string_view>
-#include <btop_config.hpp>
-#include <btop_shared.hpp>
-#include <btop_tools.hpp>
+#include <fmt/core.h>
+
+#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<string, string> strings = {
+ unordered_flat_map<std::string_view, string> 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<string, string> stringsTmp;
+ unordered_flat_map<std::string_view, string> stringsTmp;
- unordered_flat_map<string, bool> bools = {
+ unordered_flat_map<std::string_view, bool> 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<string, bool> boolsTmp;
+ unordered_flat_map<std::string_view, bool> boolsTmp;
- unordered_flat_map<string, int> ints = {
+ unordered_flat_map<std::string_view, int> 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<string, int> intsTmp;
+ unordered_flat_map<std::string_view, int> 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 <string>
#include <vector>
-#include <robin_hood.h>
#include <filesystem>
+#include <robin_hood.h>
+
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<string, string> strings;
- extern unordered_flat_map<string, string> stringsTmp;
- extern unordered_flat_map<string, bool> bools;
- extern unordered_flat_map<string, bool> boolsTmp;
- extern unordered_flat_map<string, int> ints;
- extern unordered_flat_map<string, int> intsTmp;
+ extern unordered_flat_map<std::string_view, string> strings;
+ extern unordered_flat_map<std::string_view, string> stringsTmp;
+ extern unordered_flat_map<std::string_view, bool> bools;
+ extern unordered_flat_map<std::string_view, bool> boolsTmp;
+ extern unordered_flat_map<std::string_view, int> ints;
+ extern unordered_flat_map<std::string_view, int> intsTmp;
const vector<string> valid_graph_symbols = { "braille", "block", "tty" };
const vector<string> 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 <name>
- 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 <name>
- 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 <name>
- 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 <name> to bool <value>
- 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 <name> to int <value>
- 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 <name> to string <value>
- 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 <name>
- 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 <algorithm>
#include <cmath>
#include <ranges>
-
-#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>
#include <stdexcept>
#include <string>
+#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<long long>& 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<int, 2> result;