diff --git a/src/btop_draw.cpp b/src/btop_draw.cpp index c105c62..eab7f4c 100644 --- a/src/btop_draw.cpp +++ b/src/btop_draw.cpp @@ -16,12 +16,13 @@ indent = tab tab-size = 4 */ -#include #include +#include #include #include #include #include +#include #include #include "btop_draw.hpp" @@ -31,6 +32,7 @@ tab-size = 4 #include "btop_tools.hpp" #include "btop_input.hpp" #include "btop_menu.hpp" +#include using std::array; @@ -151,7 +153,7 @@ namespace Draw { upos = ulen(this->text); } - bool TextEdit::command(const string& key) { + bool TextEdit::command(const std::string_view key) { if (key == "left" and upos > 0) { upos--; pos = uresize(text, upos).size(); @@ -193,7 +195,7 @@ namespace Draw { upos++; } else { - const string first = uresize(text, upos) + key; + const auto first = fmt::format("{}{}", uresize(text, upos), key); text = first + text.substr(pos); upos++; pos = first.size(); @@ -245,9 +247,10 @@ namespace Draw { 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 std::string_view title, + const std::string_view title2, const int num + ) { string out; if (line_color.empty()) @@ -283,12 +286,16 @@ namespace Draw { //? Draw titles if defined if (not title.empty()) { - out += Mv::to(y, x + 2) + Symbols::title_left + Fx::b + numbering + Theme::c("title") + title - + Fx::ub + line_color + Symbols::title_right; + out += fmt::format( + "{}{}{}{}{}{}{}{}{}", Mv::to(y, x + 2), Symbols::title_left, Fx::b, numbering, Theme::c("title"), title, Fx::ub, + line_color, Symbols::title_right + ); } if (not title2.empty()) { - out += Mv::to(y + height - 1, x + 2) + Symbols::title_left_down + Fx::b + numbering + Theme::c("title") + title2 - + Fx::ub + line_color + Symbols::title_right_down; + out += fmt::format( + "{}{}{}{}{}{}{}{}{}", Mv::to(y, x + 2), Symbols::title_left, Fx::b, numbering, Theme::c("title"), title2, Fx::ub, + line_color, Symbols::title_right_down + ); } return out + Fx::reset + Mv::to(y + 1, x + 1); @@ -1482,7 +1489,7 @@ namespace Proc { string box; - int selection(const string& cmd_key) { + int selection(const std::string_view cmd_key) { auto start = Config::getI("proc_start"); auto selected = Config::getI("proc_selected"); auto last_selected = Config::getI("proc_last_selected"); @@ -1526,7 +1533,7 @@ namespace Proc { if (selected > 0) selected = select_max; } else if (cmd_key.starts_with("mousey")) { - int mouse_y = std::stoi(cmd_key.substr(6)); + int mouse_y = std::atoi(cmd_key.substr(6).data()); start = clamp((int)round((double)mouse_y * (numpids - select_max - 2) / (select_max - 2)), 0, max(0, numpids - select_max)); } diff --git a/src/btop_draw.hpp b/src/btop_draw.hpp index 907b5ca..9c516ad 100644 --- a/src/btop_draw.hpp +++ b/src/btop_draw.hpp @@ -18,11 +18,12 @@ tab-size = 4 #pragma once -#include -#include #include -#include #include +#include +#include +#include +#include using std::array; using std::deque; @@ -72,15 +73,16 @@ namespace Draw { string text; TextEdit(); explicit TextEdit(string text, bool numeric=false); - bool command(const string& key); + bool command(const std::string_view key); string operator()(const size_t limit=0); void clear(); }; //* 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 std::string_view title = "", const std::string_view title2 = "", const int num = 0 + ); bool update_clock(bool force = false); diff --git a/src/btop_input.cpp b/src/btop_input.cpp index 7da063f..e7f08ab 100644 --- a/src/btop_input.cpp +++ b/src/btop_input.cpp @@ -21,6 +21,7 @@ tab-size = 4 #include #include #include +#include #include #include #include @@ -203,7 +204,7 @@ namespace Input { // do not need it, actually } - void process(const string& key) { + void process(const std::string_view key) { if (key.empty()) return; try { auto filtering = Config::getB("proc_filtering"); @@ -213,7 +214,7 @@ namespace Input { //? Global input actions if (not filtering) { bool keep_going = false; - if (str_to_lower(key) == "q") { + if (key == "q") { clean_quit(0); } else if (is_in(key, "escape", "m")) { @@ -229,7 +230,7 @@ namespace Input { return; } else if (key.size() == 1 and isint(key)) { - auto intKey = stoi(key); + auto intKey = std::atoi(key.data()); #ifdef GPU_SUPPORT static const array boxes = {"gpu5", "cpu", "mem", "net", "proc", "gpu0", "gpu1", "gpu2", "gpu3", "gpu4"}; if ((intKey == 0 and Gpu::count < 5) or (intKey >= 5 and intKey - 4 > Gpu::count)) @@ -540,8 +541,7 @@ namespace Input { } catch (const std::exception& e) { - throw std::runtime_error("Input::process(\"" + key + "\") : " + string{e.what()}); + throw std::runtime_error { fmt::format(R"(Input::process("{}"))", e.what()) }; } } - } diff --git a/src/btop_input.hpp b/src/btop_input.hpp index 91b9dc9..b646628 100644 --- a/src/btop_input.hpp +++ b/src/btop_input.hpp @@ -18,11 +18,12 @@ tab-size = 4 #pragma once -#include -#include #include -#include +#include #include +#include +#include +#include using std::array; using std::atomic; @@ -72,6 +73,6 @@ namespace Input { void clear(); //* Process actions for input - void process(const string& key); + void process(const std::string_view key); } diff --git a/src/btop_menu.cpp b/src/btop_menu.cpp index da4c34f..b4e36f1 100644 --- a/src/btop_menu.cpp +++ b/src/btop_menu.cpp @@ -16,20 +16,22 @@ indent = tab tab-size = 4 */ -#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" +#include "btop_theme.hpp" +#include "btop_tools.hpp" + +#include +#include + +#include +#include +#include +#include +#include using std::array; using std::ceil; @@ -798,7 +800,7 @@ namespace Menu { }; msgBox::msgBox() {} - msgBox::msgBox(int width, int boxtype, const vector& content, string title) + msgBox::msgBox(int width, int boxtype, const vector& content, const std::string_view title) : width(width), boxtype(boxtype) { auto tty_mode = Config::getB("tty_mode"); auto rounded = Config::getB("rounded_corners"); @@ -1597,7 +1599,7 @@ static int optionsMenu(const string& key) { }; bitset<8> menuMask; - void process(string key) { + void process(const std::string_view key) { if (menuMask.none()) { Menu::active = false; Global::overlay.clear(); @@ -1627,7 +1629,7 @@ static int optionsMenu(const string& key) { } - auto retCode = menuFunc.at(currentMenu)(key); + auto retCode = menuFunc.at(currentMenu)(key.data()); if (retCode == Closed) { menuMask.reset(currentMenu); mouse_mappings.clear(); diff --git a/src/btop_menu.hpp b/src/btop_menu.hpp index 471853e..c3a0ef3 100644 --- a/src/btop_menu.hpp +++ b/src/btop_menu.hpp @@ -18,10 +18,11 @@ tab-size = 4 #pragma once -#include #include -#include #include +#include +#include +#include #include "btop_input.hpp" @@ -61,7 +62,7 @@ namespace Menu { Select }; msgBox(); - msgBox(int width, int boxtype, const vector& content, string title); + msgBox(int width, int boxtype, const vector& content, std::string_view title); //? Draw and return box as a string string operator()(); @@ -87,7 +88,7 @@ namespace Menu { }; //* Handles redirection of input for menu functions and handles return codes - void process(string key=""); + void process(const std::string_view key = ""); //* Show a menu from enum Menu::Menus void show(int menu, int signal=-1); diff --git a/src/btop_shared.hpp b/src/btop_shared.hpp index 17a38a3..e45b46b 100644 --- a/src/btop_shared.hpp +++ b/src/btop_shared.hpp @@ -23,9 +23,11 @@ tab-size = 4 #include #include #include +#include #include -#include #include +#include + #include // From `man 3 getifaddrs`: must be included before @@ -67,7 +69,6 @@ namespace Global { } namespace Runner { - extern atomic active; extern atomic reading; extern atomic stopping; @@ -414,7 +415,7 @@ namespace Proc { 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); + int selection(const std::string_view cmd_key); //* Draw contents of proc box using as data source string draw(const vector& plist, bool force_redraw = false, bool data_same = false); diff --git a/src/btop_tools.cpp b/src/btop_tools.cpp index c9f9e3d..91e0d6e 100644 --- a/src/btop_tools.cpp +++ b/src/btop_tools.cpp @@ -26,6 +26,7 @@ tab-size = 4 #include #include #include +#include #include #include @@ -33,7 +34,6 @@ tab-size = 4 #include #include -#include "unordered_map" #include "widechar_width.hpp" #include "btop_shared.hpp" #include "btop_tools.hpp" @@ -211,11 +211,11 @@ namespace Term { namespace Tools { - size_t wide_ulen(const string& str) { + size_t wide_ulen(const std::string_view str) { unsigned int chars = 0; try { std::wstring_convert> conv; - auto w_str = conv.from_bytes((str.size() > 10000 ? str.substr(0, 10000).c_str() : str.c_str())); + auto w_str = conv.from_bytes((str.size() > 10000 ? str.substr(0, 10000).data() : str.data())); for (auto c : w_str) { chars += utf8::wcwidth(c); @@ -228,7 +228,7 @@ namespace Tools { return chars; } - size_t wide_ulen(const std::wstring& w_str) { + size_t wide_ulen(const std::wstring_view w_str) { unsigned int chars = 0; for (auto c : w_str) { @@ -682,7 +682,7 @@ namespace Logger { loglevel = v_index(log_levels, level); } - void log_write(const Level level, const string& msg) { + void log_write(const Level level, const std::string_view msg) { if (loglevel < level or !logfile.has_value()) { return; } diff --git a/src/btop_tools.hpp b/src/btop_tools.hpp index f0cdf70..973bd56 100644 --- a/src/btop_tools.hpp +++ b/src/btop_tools.hpp @@ -28,16 +28,16 @@ tab-size = 4 #include #include #include +#include +#include #include #include #include #include #include #include -#include -#include -#include #include +#include #ifdef BTOP_DEBUG #include #endif @@ -177,11 +177,23 @@ namespace Logger { //* Set log level, valid arguments: "DISABLED", "ERROR", "WARNING", "INFO" and "DEBUG" void set(const string& level); - void log_write(const Level level, const string& msg); - inline void error(const string msg) { log_write(ERROR, msg); } - inline void warning(const string msg) { log_write(WARNING, msg); } - inline void info(const string msg) { log_write(INFO, msg); } - inline void debug(const string msg) { log_write(DEBUG, msg); } + void log_write(const Level level, const std::string_view msg); + + inline void error(const std::string_view msg) { + log_write(ERROR, msg); + } + + inline void warning(const std::string_view msg) { + log_write(WARNING, msg); + } + + inline void info(const std::string_view msg) { + log_write(INFO, msg); + } + + inline void debug(const std::string_view msg) { + log_write(DEBUG, msg); + } } //? --------------------------------------------------- FUNCTIONS ----------------------------------------------------- @@ -195,11 +207,11 @@ namespace Tools { virtual std::string do_grouping() const override { return "\03"; } }; - size_t wide_ulen(const string& str); - size_t wide_ulen(const std::wstring& w_str); + size_t wide_ulen(const std::string_view str); + size_t wide_ulen(const std::wstring_view 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, bool wide = false) { + inline size_t ulen(const std::string_view str, bool wide = false) { return (wide ? wide_ulen(str) : std::ranges::count_if(str, [](char c) { return (static_cast(c) & 0xC0) != 0x80; })); } @@ -238,12 +250,12 @@ namespace Tools { //* Check if string contains value template - inline bool s_contains(const string& str, const T& find_val) { + constexpr bool s_contains(const std::string_view str, const T& find_val) { return str.find(find_val) != string::npos; } //* Check if string contains string , while ignoring case - inline bool s_contains_ic(const string& str, const string& find_val) { + inline bool s_contains_ic(const std::string_view str, const std::string_view find_val) { auto it = std::search( str.begin(), str.end(), find_val.begin(), find_val.end(), @@ -280,12 +292,12 @@ namespace Tools { } //* Check if a string is a valid bool value - inline bool isbool(const string& str) { + inline bool isbool(const std::string_view str) { return is_in(str, "true", "false", "True", "False"); } //* Convert string to bool, returning any value not equal to "true" or "True" as false - inline bool stobool(const string& str) { + inline bool stobool(const std::string_view str) { return is_in(str, "true", "True"); } diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 7fa98f7..a948fc1 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -388,7 +388,7 @@ namespace Cpu { fs::path add_path = fs::canonical(dir.path()); if (v_contains(search_paths, add_path) or v_contains(search_paths, add_path / "device")) continue; - if (s_contains(add_path, "coretemp")) + if (s_contains(add_path.c_str(), "coretemp")) got_coretemp = true; for (const auto & file : fs::directory_iterator(add_path)) {