diff --git a/src/btop_config.cpp b/src/btop_config.cpp index 25b68ddc..60101890 100644 --- a/src/btop_config.cpp +++ b/src/btop_config.cpp @@ -17,11 +17,13 @@ tab-size = 4 */ #include -#include #include #include +#include #include +#include + #include "btop_config.hpp" #include "btop_shared.hpp" #include "btop_tools.hpp" @@ -193,7 +195,7 @@ namespace Config { "#* The level set includes all lower levels, i.e. \"DEBUG\" will show all logging info."} }; - unordered_flat_map strings = { + unordered_flat_map strings = { {"color_theme", "Default"}, {"shown_boxes", "cpu mem net proc"}, {"graph_symbol", "braille"}, @@ -219,9 +221,9 @@ namespace Config { {"proc_command", ""}, {"selected_name", ""}, }; - unordered_flat_map stringsTmp; + unordered_flat_map stringsTmp; - unordered_flat_map bools = { + unordered_flat_map bools = { {"theme_background", true}, {"truecolor", true}, {"rounded_corners", true}, @@ -267,9 +269,9 @@ namespace Config { {"show_detailed", false}, {"proc_filtering", false}, }; - 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}, @@ -280,9 +282,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; @@ -369,7 +371,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); @@ -399,7 +401,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; @@ -407,7 +409,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!"; @@ -456,7 +458,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)) @@ -466,7 +468,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))); diff --git a/src/btop_config.hpp b/src/btop_config.hpp index 95c6b680..e5f4ac1b 100644 --- a/src/btop_config.hpp +++ b/src/btop_config.hpp @@ -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();