From 2352d980e11349548b873cf60711bad70e0d36c9 Mon Sep 17 00:00:00 2001 From: lvxnull <86745229+lvxnull@users.noreply.github.com> Date: Thu, 12 Oct 2023 17:30:58 +0200 Subject: [PATCH] Rewrite config access --- src/btop.cpp | 41 +++++---- src/btop_draw.cpp | 174 ++++++++++++++++++----------------- src/btop_input.cpp | 125 +++++++++++++------------ src/btop_menu.cpp | 69 ++++++++------ src/btop_shared.cpp | 2 +- src/btop_theme.cpp | 16 ++-- src/btop_tools.cpp | 3 +- src/freebsd/btop_collect.cpp | 60 ++++++------ src/linux/btop_collect.cpp | 75 ++++++++------- src/osx/btop_collect.cpp | 62 +++++++------ 10 files changed, 347 insertions(+), 280 deletions(-) diff --git a/src/btop.cpp b/src/btop.cpp index e53ec020e..fbd30d2f6 100644 --- a/src/btop.cpp +++ b/src/btop.cpp @@ -138,11 +138,11 @@ void argumentParser(const int& argc, char **argv) { Global::arg_low_color = true; } else if (is_in(argument, "-t", "--tty_on")) { - Config::set("tty_mode", true); + CONFIG_SET(tty_mode, true); Global::arg_tty = true; } else if (is_in(argument, "+t", "--tty_off")) { - Config::set("tty_mode", false); + CONFIG_SET(tty_mode, false); Global::arg_tty = true; } else if (is_in(argument, "-p", "--preset")) { @@ -172,6 +172,7 @@ void argumentParser(const int& argc, char **argv) { //* Handler for SIGWINCH and general resizing events, does nothing if terminal hasn't been resized unless force=true void term_resize(bool force) { + auto& config = Config::get(); static atomic resizing (false); if (Input::polling) { Global::resized = true; @@ -190,7 +191,7 @@ void term_resize(bool force) { Term::refresh(); Config::unlock(); - auto boxes = Config::getS("shown_boxes"); + auto boxes = config.shown_boxes; auto min_size = Term::get_min_size(boxes); auto minWidth = min_size.at(0), minHeight = min_size.at(1); @@ -225,7 +226,7 @@ void term_resize(bool force) { else if (is_in(key, "1", "2", "3", "4")) { Config::current_preset = -1; Config::toggle_box(all_boxes.at(std::stoi(key) - 1)); - boxes = Config::getS("shown_boxes"); + boxes = config.shown_boxes; } } min_size = Term::get_min_size(boxes); @@ -661,6 +662,7 @@ namespace Runner { //* 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) { atomic_wait_for(active, true, 5000); + auto& config = Config::get(); if (active) { Logger::error("Stall in Runner thread, restarting!"); active = false; @@ -686,7 +688,7 @@ namespace Runner { current_conf = { (box == "all" ? Config::current_boxes : vector{box}), no_update, force_redraw, - (not Config::getB("tty_mode") and Config::getB("background_update")), + (not config.tty_mode and config.background_update), Global::overlay, Global::clock }; @@ -806,19 +808,21 @@ int main(int argc, char **argv) { { vector load_warnings; Config::load(Config::conf_file, load_warnings); - if (Config::current_boxes.empty()) Config::check_boxes(Config::getS("shown_boxes")); - Config::set("lowcolor", (Global::arg_low_color ? true : not Config::getB("truecolor"))); + auto& config = Config::get(); + if (Config::current_boxes.empty()) Config::check_boxes(config.shown_boxes); + CONFIG_SET(lowcolor, (Global::arg_low_color or not config.truecolor)); if (Global::debug) { Logger::set("DEBUG"); Logger::debug("Starting in DEBUG mode!"); } - else Logger::set(Config::getS("log_level")); + else Logger::set(config.log_level); - Logger::info("Logger set to " + (Global::debug ? "DEBUG" : Config::getS("log_level"))); + Logger::info("Logger set to " + (Global::debug ? "DEBUG" : config.log_level)); for (const auto& err_str : load_warnings) Logger::warning(err_str); } + auto& config = Config::get(); //? Try to find and set a UTF-8 locale if (std::setlocale(LC_ALL, "") != nullptr and not s_contains((string)std::setlocale(LC_ALL, ""), ";") @@ -894,13 +898,13 @@ int main(int argc, char **argv) { } if (Term::current_tty != "unknown") Logger::info("Running on " + Term::current_tty); - if (not Global::arg_tty and Config::getB("force_tty")) { - Config::set("tty_mode", true); + if (not Global::arg_tty and config.force_tty) { + CONFIG_SET(tty_mode, true); Logger::info("Forcing tty mode: setting 16 color mode and using tty friendly graph symbols"); } #ifndef __APPLE__ else if (not Global::arg_tty and Term::current_tty.starts_with("/dev/tty")) { - Config::set("tty_mode", true); + CONFIG_SET(tty_mode, true); Logger::info("Real tty detected: setting 16 color mode and using tty friendly graph symbols"); } #endif @@ -949,14 +953,14 @@ int main(int argc, char **argv) { } //? Calculate sizes of all boxes - Config::presetsValid(Config::getS("presets")); + Config::presetsValid(config.presets); if (Global::arg_preset >= 0) { Config::current_preset = min(Global::arg_preset, (int)Config::preset_list.size() - 1); Config::apply_preset(Config::preset_list.at(Config::current_preset)); } { - const auto [x, y] = Term::get_min_size(Config::getS("shown_boxes")); + const auto [x, y] = Term::get_min_size(config.shown_boxes); if (Term::height < y or Term::width < x) { term_resize(true); Global::resized = false; @@ -973,9 +977,10 @@ int main(int argc, char **argv) { //? ------------------------------------------------ MAIN LOOP ---------------------------------------------------- - uint64_t update_ms = Config::getI("update_ms"); + uint64_t update_ms = config.update_ms; auto future_time = time_ms(); + try { while (not true not_eq not false) { //? Check for exceptions in secondary thread and exit with fail signal if true @@ -1004,7 +1009,7 @@ int main(int argc, char **argv) { //? Start secondary collect & draw thread at the interval set by config value if (time_ms() >= future_time and not Global::resized) { Runner::run("all"); - update_ms = Config::getI("update_ms"); + update_ms = config.update_ms; future_time = time_ms() + update_ms; } @@ -1012,8 +1017,8 @@ int main(int argc, char **argv) { for (auto current_time = time_ms(); current_time < future_time; current_time = time_ms()) { //? Check for external clock changes and for changes to the update timer - if (std::cmp_not_equal(update_ms, Config::getI("update_ms"))) { - update_ms = Config::getI("update_ms"); + if (update_ms != config.update_ms) { + update_ms = config.update_ms; future_time = time_ms() + update_ms; } else if (future_time - current_time > update_ms) diff --git a/src/btop_draw.cpp b/src/btop_draw.cpp index b7ddc7429..a693dfa8a 100644 --- a/src/btop_draw.cpp +++ b/src/btop_draw.cpp @@ -107,8 +107,9 @@ 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& config = Config::get(); + auto lowcolor = config.lowcolor; + auto tty_mode = config.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) { @@ -250,8 +251,9 @@ namespace Draw { if (line_color.empty()) line_color = Theme::c("div_line"); - auto tty_mode = Config::getB("tty_mode"); - auto rounded = Config::getB("rounded_corners"); + auto& config = Config::get(); + auto tty_mode = config.tty_mode; + auto rounded = config.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); @@ -292,7 +294,8 @@ namespace Draw { } bool update_clock(bool force) { - const auto& clock_format = Config::getS("clock_format"); + auto& config = Config::get(); + const auto& clock_format = config.clock_format; if (not Cpu::shown or clock_format.empty()) { if (clock_format.empty() and not Global::clock.empty()) Global::clock.clear(); return false; @@ -318,7 +321,7 @@ namespace Draw { } auto& out = Global::clock; - auto cpu_bottom = Config::getB("cpu_bottom"); + auto cpu_bottom = config.cpu_bottom; const auto& x = Cpu::x; const auto y = (cpu_bottom ? Cpu::y + Cpu::height - 1 : Cpu::y); const auto& width = Cpu::width; @@ -340,7 +343,7 @@ namespace Draw { } - clock_str = uresize(clock_str, std::max(10, width - 66 - (Term::width >= 100 and Config::getB("show_battery") and Cpu::has_battery ? 22 : 0))); + clock_str = uresize(clock_str, std::max(10, width - 66 - (Term::width >= 100 and config.show_battery and Cpu::has_battery ? 22 : 0))); out.clear(); if (clock_str.size() != clock_len) { @@ -456,9 +459,10 @@ namespace Draw { 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"; + auto& config = Config::get(); + if (config.tty_mode or symbol == "tty") this->symbol = "tty"; else if (symbol != "default") this->symbol = symbol; - else this->symbol = Config::getS("graph_symbol"); + else this->symbol = config.graph_symbol; if (this->symbol == "tty") tty_mode = true; if (max_value == 0 and offset > 0) max_value = 100; @@ -522,17 +526,18 @@ namespace Cpu { string draw(const cpu_info& cpu, bool force_redraw, bool data_same) { if (Runner::stopping) return ""; if (force_redraw) redraw = true; - bool show_temps = (Config::getB("check_temp") and got_sensors); - auto single_graph = Config::getB("cpu_single_graph"); - bool hide_cores = show_temps and (cpu_temp_only or not Config::getB("show_coretemp")); + auto& config = Config::get(); + bool show_temps = (config.check_temp and got_sensors); + auto single_graph = config.cpu_single_graph; + bool hide_cores = show_temps and (cpu_temp_only or not config.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& 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& graph_up_field = config.cpu_graph_upper; + auto& graph_lo_field = config.cpu_graph_lower; + auto tty_mode = config.tty_mode; + auto& graph_symbol = (tty_mode ? "tty" : config.graph_symbol_cpu); + auto& graph_bg = Symbols::graph_symbols.at((graph_symbol == "default" ? config.graph_symbol + "_up" : graph_symbol + "_up")).at(6); + auto& temp_scale = config.temp_scale; + auto cpu_bottom = config.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; @@ -556,7 +561,7 @@ namespace Cpu { out += Mv::to(button_y, x + 16) + title_left + Theme::c("hi_fg") + Fx::b + 'p' + Theme::c("title") + "reset " + (Config::current_preset < 0 ? "*" : to_string(Config::current_preset)) + Fx::ub + title_right; Input::mouse_mappings["p"] = {button_y, x + 17, 1, 8}; - const string update = to_string(Config::getI("update_ms")) + "ms"; + const string update = to_string(config.update_ms) + "ms"; out += Mv::to(button_y, x + width - update.size() - 8) + title_left + Fx::b + Theme::c("hi_fg") + "- " + Theme::c("title") + update + Theme::c("hi_fg") + " +" + Fx::ub + title_right; Input::mouse_mappings["-"] = {button_y, x + width - (int)update.size() - 7, 1, 2}; @@ -571,7 +576,7 @@ namespace Cpu { graph_low_height, "cpu", cpu.cpu_percent.at(graph_lo_field), graph_symbol, - Config::getB("cpu_invert_lower"), true + config.cpu_invert_lower, true }; } @@ -601,7 +606,7 @@ namespace Cpu { } //? Draw battery if enabled and present - if (Config::getB("show_battery") and has_battery) { + if (config.show_battery and has_battery) { static int old_percent{}; // defaults to = 0 static long old_seconds{}; // defaults to = 0 static string old_status; @@ -622,7 +627,7 @@ namespace Cpu { const string str_time = (seconds > 0 ? sec_to_dhms(seconds, true, true) : ""); const string str_percent = to_string(percent) + '%'; const auto& bat_symbol = bat_symbols.at((bat_symbols.contains(status) ? status : "unknown")); - const int current_len = (Term::width >= 100 ? 11 : 0) + str_time.size() + str_percent.size() + to_string(Config::getI("update_ms")).size(); + const int current_len = (Term::width >= 100 ? 11 : 0) + str_time.size() + str_percent.size() + to_string(config.update_ms).size(); const int current_pos = Term::width - current_len - 17; if ((bat_pos != current_pos or bat_len != current_len) and bat_pos > 0 and not redraw) @@ -647,18 +652,18 @@ namespace Cpu { out += Mv::to( y + graph_up_height + 1 + (mid_line ? 1 : 0), x + 1) + graph_lower(cpu.cpu_percent.at(graph_lo_field), (data_same or redraw)); //? Uptime - if (Config::getB("show_uptime")) { + if (config.show_uptime) { string upstr = sec_to_dhms(system_uptime()); if (upstr.size() > 8) { upstr.resize(upstr.size() - 3); upstr = trans(upstr); } - out += Mv::to(y + (single_graph or not Config::getB("cpu_invert_lower") ? 1 : height - 2), x + 2) + out += Mv::to(y + (single_graph or not config.cpu_invert_lower ? 1 : height - 2), x + 2) + Theme::c("graph_text") + "up" + Mv::r(1) + upstr; } //? Cpu clock and cpu meter - if (Config::getB("show_cpu_freq") and not cpuHz.empty()) + if (config.show_cpu_freq and not cpuHz.empty()) out += Mv::to(b_y, b_x + b_width - 10) + Fx::ub + Theme::c("div_line") + Symbols::h_line * (7 - cpuHz.size()) + Symbols::title_left + Fx::b + Theme::c("title") + cpuHz + Fx::ub + Theme::c("div_line") + Symbols::title_right; @@ -759,16 +764,17 @@ namespace Mem { 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& 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& config = Config::get(); + auto show_swap = config.show_swap; + auto swap_disk = config.swap_disk; + auto show_disks = config.show_disks; + auto show_io_stat = config.show_io_stat; + auto io_mode = config.io_mode; + auto io_graph_combined = config.io_graph_combined; + auto use_graphs = config.mem_graphs; + auto tty_mode = config.tty_mode; + auto& graph_symbol = (tty_mode ? "tty" : config.graph_symbol_mem); + auto& graph_bg = Symbols::graph_symbols.at((graph_symbol == "default" ? config.graph_symbol + "_up" : graph_symbol + "_up")).at(6); auto totalMem = Mem::get_totalMem(); string out; out.reserve(height * width); @@ -808,8 +814,8 @@ namespace Mem { disks_io_h = max((int)floor((double)(height - 2 - (disk_ios * 2)) / max(1, disk_ios)), (io_graph_combined ? 1 : 2)); half_height = ceil((double)disks_io_h / 2); - if (not Config::getS("io_graph_speeds").empty()) { - auto split = ssplit(Config::getS("io_graph_speeds")); + if (not config.io_graph_speeds.empty()) { + auto split = ssplit(config.io_graph_speeds); for (const auto& entry : split) { auto vals = ssplit(entry); if (vals.size() == 2 and mem.disks.contains(vals.at(0)) and isint(vals.at(1))) @@ -1013,10 +1019,11 @@ namespace Net { 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& graph_symbol = (tty_mode ? "tty" : Config::getS("graph_symbol_net")); + auto& config = Config::get(); + auto net_sync = config.net_sync; + auto net_auto = config.net_auto; + auto tty_mode = config.tty_mode; + auto& graph_symbol = (tty_mode ? "tty" : config.graph_symbol_net); string ip_addr = (net.ipv4.empty() ? net.ipv6 : net.ipv4); if (old_ip != ip_addr) { old_ip = ip_addr; @@ -1027,8 +1034,8 @@ namespace Net { const string title_left = Theme::c("net_box") + Fx::ub + Symbols::title_left; const string title_right = Theme::c("net_box") + Fx::ub + Symbols::title_right; const int i_size = min((int)selected_iface.size(), 10); - const long long down_max = (net_auto ? graph_max.at("download") : ((long long)(Config::getI("net_download")) << 20) / 8); - const long long up_max = (net_auto ? graph_max.at("upload") : ((long long)(Config::getI("net_upload")) << 20) / 8); + const long long down_max = (net_auto ? graph_max.at("download") : ((long long)(config.net_download) << 20) / 8); + const long long up_max = (net_auto ? graph_max.at("upload") : ((long long)(config.net_upload) << 20) / 8); //* Redraw elements not needed to be updated every cycle if (redraw) { @@ -1120,17 +1127,18 @@ namespace Proc { string box; int selection(const string& cmd_key) { - auto start = Config::getI("proc_start"); - 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& config = Config::get(); + auto start = config.proc_start; + auto selected = config.proc_selected; + auto last_selected = config.proc_last_selected; + const int select_max = (config.show_detailed ? Proc::select_max - 8 : Proc::select_max); + auto vim_keys = config.vim_keys; int numpids = Proc::numpids; if ((cmd_key == "up" or (vim_keys and cmd_key == "k")) and selected > 0) { if (start > 0 and selected == 1) start--; else selected--; - if (Config::getI("proc_last_selected") > 0) Config::set("proc_last_selected", 0); + if (config.proc_last_selected > 0) CONFIG_SET(proc_last_selected, 0); } else if (cmd_key == "mouse_scroll_up" and start > 0) { start = max(0, start - 3); @@ -1142,7 +1150,7 @@ namespace Proc { if (start < numpids - select_max and selected == select_max) start++; else if (selected == 0 and last_selected > 0) { selected = last_selected; - Config::set("proc_last_selected", 0); + CONFIG_SET(proc_last_selected, 0); } else selected++; } @@ -1168,12 +1176,12 @@ namespace Proc { } bool changed = false; - if (start != Config::getI("proc_start")) { - Config::set("proc_start", start); + if (start != config.proc_start) { + CONFIG_SET(proc_start, start); changed = true; } - if (selected != Config::getI("proc_selected")) { - Config::set("proc_selected", selected); + if (selected != config.proc_selected) { + CONFIG_SET(proc_selected, selected); changed = true; } return (not changed ? -1 : selected); @@ -1181,18 +1189,19 @@ namespace Proc { 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& 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"); - start = Config::getI("proc_start"); - selected = Config::getI("proc_selected"); + auto& config = Config::get(); + auto proc_tree = config.proc_tree; + bool show_detailed = (config.show_detailed and cmp_equal(Proc::detailed.last_pid, config.detailed_pid)); + bool proc_gradient = (config.proc_gradient and not config.lowcolor and Theme::gradients.contains("proc")); + auto proc_colors = config.proc_colors; + auto tty_mode = config.tty_mode; + auto& graph_symbol = (tty_mode ? "tty" : config.graph_symbol_proc); + auto& graph_bg = Symbols::graph_symbols.at((graph_symbol == "default" ? config.graph_symbol + "_up" : graph_symbol + "_up")).at(6); + auto mem_bytes = config.proc_mem_bytes; + auto vim_keys = config.vim_keys; + auto show_graphs = config.proc_cpu_graphs; + start = config.proc_start; + selected = config.proc_selected; const int y = show_detailed ? Proc::y + 8 : Proc::y; const int height = show_detailed ? Proc::height - 8 : Proc::height; const int select_max = show_detailed ? Proc::select_max - 8 : Proc::select_max; @@ -1297,8 +1306,8 @@ namespace 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)); + auto filtering = config.proc_filtering; // ? filter(20) : Config::getS("proc_filter")) + const auto filter_text = (filtering) ? filter(max(6, width - 58)) : uresize(config.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") + (not filtering and not filter_text.empty() ? Theme::c("hi_fg") + " del" : "") @@ -1313,22 +1322,22 @@ namespace Proc { } //? per-core, reverse, tree and sorting - const auto& sorting = Config::getS("proc_sorting"); + const auto& sorting = config.proc_sorting; const int sort_len = sorting.size(); const int sort_pos = x + width - sort_len - 8; if (width > 55 + sort_len) { - out += Mv::to(y, sort_pos - 25) + title_left + (Config::getB("proc_per_core") ? Fx::b : "") + Theme::c("title") + out += Mv::to(y, sort_pos - 25) + title_left + (config.proc_per_core ? Fx::b : "") + Theme::c("title") + "per-" + Theme::c("hi_fg") + 'c' + Theme::c("title") + "ore" + Fx::ub + title_right; Input::mouse_mappings["c"] = {y, sort_pos - 24, 1, 8}; } if (width > 45 + sort_len) { - out += Mv::to(y, sort_pos - 15) + title_left + (Config::getB("proc_reversed") ? Fx::b : "") + Theme::c("hi_fg") + out += Mv::to(y, sort_pos - 15) + title_left + (config.proc_reversed ? Fx::b : "") + Theme::c("hi_fg") + 'r' + Theme::c("title") + "everse" + Fx::ub + title_right; Input::mouse_mappings["r"] = {y, sort_pos - 14, 1, 7}; } if (width > 35 + sort_len) { - out += Mv::to(y, sort_pos - 6) + title_left + (Config::getB("proc_tree") ? Fx::b : "") + Theme::c("title") + "tre" + out += Mv::to(y, sort_pos - 6) + title_left + (config.proc_tree ? Fx::b : "") + Theme::c("title") + "tre" + Theme::c("hi_fg") + 'e' + Fx::ub + title_right; Input::mouse_mappings["e"] = {y, sort_pos - 5, 1, 4}; } @@ -1607,10 +1616,11 @@ 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& config = Config::get(); + auto boxes = config.shown_boxes; + auto cpu_bottom = config.cpu_bottom; + auto mem_below_net = config.mem_below_net; + auto proc_left = config.proc_left; Cpu::box.clear(); Mem::box.clear(); @@ -1640,7 +1650,7 @@ namespace Draw { //* Calculate and draw cpu box outlines if (Cpu::shown) { using namespace Cpu; - bool show_temp = (Config::getB("check_temp") and got_sensors); + bool show_temp = (config.check_temp and got_sensors); width = round((double)Term::width * width_p / 100); height = max(8, (int)ceil((double)Term::height * (trim(boxes) == "cpu" ? 100 : height_p) / 100)); x = 1; @@ -1671,7 +1681,7 @@ namespace Draw { box = createBox(x, y, width, height, Theme::c("cpu_box"), true, (cpu_bottom ? "" : "cpu"), (cpu_bottom ? "cpu" : ""), 1); - auto& custom = Config::getS("custom_cpu_name"); + auto& custom = config.custom_cpu_name; const string cpu_title = uresize((custom.empty() ? Cpu::cpuName : custom) , b_width - 14); box += createBox(b_x, b_y, b_width, b_height, "", false, cpu_title); } @@ -1679,9 +1689,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.show_disks; + auto swap_disk = config.swap_disk; + auto mem_graphs = config.mem_graphs; width = round((double)Term::width * (Proc::shown ? width_p : 100) / 100); height = ceil((double)Term::height * (100 - Cpu::height_p * Cpu::shown - Net::height_p * Net::shown) / 100) + 1; diff --git a/src/btop_input.cpp b/src/btop_input.cpp index 21aff58f4..e39e1eadf 100644 --- a/src/btop_input.cpp +++ b/src/btop_input.cpp @@ -152,6 +152,7 @@ namespace Input { } string get() { + auto& config = Config::get(); string key = InputThr::instance().get(); if (not key.empty()) { //? Remove escape code prefix if present @@ -181,7 +182,7 @@ namespace Input { else key.clear(); - if (Config::getB("proc_filtering")) { + if (config.proc_filtering) { if (mouse_event == "mouse_click") return mouse_event; else return ""; } @@ -238,8 +239,9 @@ 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& config = Config::get(); + auto filtering = config.proc_filtering; + auto vim_keys = config.vim_keys; auto help_key = (vim_keys ? "H" : "h"); auto kill_key = (vim_keys ? "K" : "k"); //? Global input actions @@ -295,8 +297,8 @@ namespace Input { bool redraw = true; if (filtering) { if (key == "enter" or key == "down") { - Config::set("proc_filter", Proc::filter.text); - Config::set("proc_filtering", false); + CONFIG_SET(proc_filter, Proc::filter.text); + CONFIG_SET(proc_filtering, false); old_filter.clear(); if(key == "down"){ process("down"); @@ -304,65 +306,68 @@ namespace Input { } } else if (key == "escape" or key == "mouse_click") { - Config::set("proc_filter", old_filter); - Config::set("proc_filtering", false); + CONFIG_SET(proc_filter, old_filter); + CONFIG_SET(proc_filtering, false); old_filter.clear(); } else if (Proc::filter.command(key)) { - if (Config::getS("proc_filter") != Proc::filter.text) - Config::set("proc_filter", Proc::filter.text); + if (config.proc_filter != Proc::filter.text) + CONFIG_SET(proc_filter, Proc::filter.text); } else return; } else if (key == "left" or (vim_keys and key == "h")) { - int cur_i = v_index(Proc::sort_vector, Config::getS("proc_sorting")); + int cur_i = v_index(Proc::sort_vector, config.proc_sorting); if (--cur_i < 0) cur_i = Proc::sort_vector.size() - 1; - Config::set("proc_sorting", Proc::sort_vector.at(cur_i)); + CONFIG_SET(proc_sorting, Proc::sort_vector.at(cur_i)); } else if (key == "right" or (vim_keys and key == "l")) { - int cur_i = v_index(Proc::sort_vector, Config::getS("proc_sorting")); + int cur_i = v_index(Proc::sort_vector, config.proc_sorting); if (std::cmp_greater(++cur_i, Proc::sort_vector.size() - 1)) cur_i = 0; - Config::set("proc_sorting", Proc::sort_vector.at(cur_i)); + CONFIG_SET(proc_sorting, Proc::sort_vector.at(cur_i)); } else if (is_in(key, "f", "/")) { - Config::flip("proc_filtering"); - Proc::filter = { Config::getS("proc_filter") }; + CONFIG_SET(proc_filtering, not config.proc_filtering); + Proc::filter = { config.proc_filter }; old_filter = Proc::filter.text; } else if (key == "e") { - Config::flip("proc_tree"); + CONFIG_SET(proc_tree, not config.proc_tree); no_update = false; } - else if (key == "r") - Config::flip("proc_reversed"); + else if (key == "r") { + CONFIG_SET(proc_reversed, not config.proc_reversed); + } - else if (key == "c") - Config::flip("proc_per_core"); + else if (key == "c") { + CONFIG_SET(proc_per_core, not config.proc_per_core); + } - else if (key == "%") - Config::flip("proc_mem_bytes"); + else if (key == "%") { + CONFIG_SET(proc_mem_bytes, not config.proc_mem_bytes); + } - else if (key == "delete" and not Config::getS("proc_filter").empty()) - Config::set("proc_filter", ""s); + else if (key == "delete" and not config.proc_filter.empty()) + CONFIG_SET(proc_filter, ""s); else if (key.starts_with("mouse_")) { redraw = false; const auto& [col, line] = mouse_pos; - const int y = (Config::getB("show_detailed") ? Proc::y + 8 : Proc::y); - const int height = (Config::getB("show_detailed") ? Proc::height - 8 : Proc::height); + const int y = (config.show_detailed ? Proc::y + 8 : Proc::y); + const int height = (config.show_detailed ? Proc::height - 8 : Proc::height); if (col >= Proc::x + 1 and col < Proc::x + Proc::width and line >= y + 1 and line < y + height - 1) { if (key == "mouse_click") { if (col < Proc::x + Proc::width - 2) { - const auto& current_selection = Config::getI("proc_selected"); + const auto& current_selection = config.proc_selected; if (current_selection == line - y - 1) { redraw = true; - if (Config::getB("proc_tree")) { + if (config.proc_tree) { const int x_pos = col - Proc::x; - const int offset = Config::getI("selected_depth") * 3; + const int offset = config.selected_depth * 3; if (x_pos > offset and x_pos < 4 + offset) { process("space"); return; @@ -373,7 +378,7 @@ namespace Input { } else if (current_selection == 0 or line - y - 1 == 0) redraw = true; - Config::set("proc_selected", line - y - 1); + CONFIG_SET(proc_selected, line - y - 1); } else if (line == y + 1) { if (Proc::selection("page_up") == -1) return; @@ -387,53 +392,53 @@ namespace Input { else goto proc_mouse_scroll; } - else if (key == "mouse_click" and Config::getI("proc_selected") > 0) { - Config::set("proc_selected", 0); + else if (key == "mouse_click" and config.proc_selected > 0) { + CONFIG_SET(proc_selected, 0); redraw = true; } else keep_going = true; } else if (key == "enter") { - if (Config::getI("proc_selected") == 0 and not Config::getB("show_detailed")) { + if (config.proc_selected == 0 and not config.show_detailed) { return; } - else if (Config::getI("proc_selected") > 0 and Config::getI("detailed_pid") != Config::getI("selected_pid")) { - Config::set("detailed_pid", Config::getI("selected_pid")); - Config::set("proc_last_selected", Config::getI("proc_selected")); - Config::set("proc_selected", 0); - Config::set("show_detailed", true); + else if (config.proc_selected > 0 and config.detailed_pid != config.selected_pid) { + CONFIG_SET(detailed_pid, config.selected_pid); + CONFIG_SET(proc_last_selected, config.proc_selected); + CONFIG_SET(proc_selected, 0); + CONFIG_SET(show_detailed, true); } - else if (Config::getB("show_detailed")) { - if (Config::getI("proc_last_selected") > 0) Config::set("proc_selected", Config::getI("proc_last_selected")); - Config::set("proc_last_selected", 0); - Config::set("detailed_pid", 0); - Config::set("show_detailed", false); + else if (config.show_detailed) { + if (config.proc_last_selected > 0) CONFIG_SET(proc_selected, config.proc_last_selected); + CONFIG_SET(proc_last_selected, 0); + CONFIG_SET(detailed_pid, 0); + CONFIG_SET(show_detailed, false); } } - else if (is_in(key, "+", "-", "space") and Config::getB("proc_tree") and Config::getI("proc_selected") > 0) { + else if (is_in(key, "+", "-", "space") and config.proc_tree and config.proc_selected > 0) { atomic_wait(Runner::active); - auto& pid = Config::getI("selected_pid"); + auto& pid = config.selected_pid; if (key == "+" or key == "space") Proc::expand = pid; if (key == "-" or key == "space") Proc::collapse = pid; no_update = false; } - else if (is_in(key, "t", kill_key) and (Config::getB("show_detailed") or Config::getI("selected_pid") > 0)) { + else if (is_in(key, "t", kill_key) and (config.show_detailed or config.selected_pid > 0)) { atomic_wait(Runner::active); - if (Config::getB("show_detailed") and Config::getI("proc_selected") == 0 and Proc::detailed.status == "Dead") return; + if (config.show_detailed and config.proc_selected == 0 and Proc::detailed.status == "Dead") return; Menu::show(Menu::Menus::SignalSend, (key == "t" ? SIGTERM : SIGKILL)); return; } - else if (key == "s" and (Config::getB("show_detailed") or Config::getI("selected_pid") > 0)) { + else if (key == "s" and (config.show_detailed or config.selected_pid > 0)) { atomic_wait(Runner::active); - if (Config::getB("show_detailed") and Config::getI("proc_selected") == 0 and Proc::detailed.status == "Dead") return; + if (config.show_detailed and config.proc_selected == 0 and Proc::detailed.status == "Dead") return; Menu::show(Menu::Menus::SignalChoose); return; } else if (is_in(key, "up", "down", "page_up", "page_down", "home", "end") or (vim_keys and is_in(key, "j", "k", "g", "G"))) { proc_mouse_scroll: redraw = false; - auto old_selected = Config::getI("proc_selected"); + auto old_selected = config.proc_selected; auto new_selected = Proc::selection(key); if (new_selected == -1) return; @@ -455,19 +460,19 @@ namespace Input { bool redraw = true; static uint64_t last_press = 0; - if (key == "+" and Config::getI("update_ms") <= 86399900) { - int add = (Config::getI("update_ms") <= 86399000 and last_press >= time_ms() - 200 + if (key == "+" and config.update_ms <= 86399900) { + int add = (config.update_ms <= 86399000 and last_press >= time_ms() - 200 and rng::all_of(Input::history, [](const auto& str){ return str == "+"; }) ? 1000 : 100); - Config::set("update_ms", Config::getI("update_ms") + add); + CONFIG_SET(update_ms, config.update_ms + add); last_press = time_ms(); redraw = true; } - else if (key == "-" and Config::getI("update_ms") >= 200) { - int sub = (Config::getI("update_ms") >= 2000 and last_press >= time_ms() - 200 + else if (key == "-" and config.update_ms >= 200) { + int sub = (config.update_ms >= 2000 and last_press >= time_ms() - 200 and rng::all_of(Input::history, [](const auto& str){ return str == "-"; }) ? 1000 : 100); - Config::set("update_ms", Config::getI("update_ms") - sub); + CONFIG_SET(update_ms, config.update_ms - sub); last_press = time_ms(); redraw = true; } @@ -486,10 +491,10 @@ namespace Input { bool redraw = true; if (key == "i") { - Config::flip("io_mode"); + CONFIG_SET(io_mode, not config.io_mode); } else if (key == "d") { - Config::flip("show_disks"); + CONFIG_SET(show_disks, not config.show_disks); no_update = false; Draw::calcSizes(); } @@ -522,11 +527,11 @@ namespace Input { } } else if (key == "y") { - Config::flip("net_sync"); + CONFIG_SET(net_sync, not config.net_sync); Net::rescale = true; } else if (key == "a") { - Config::flip("net_auto"); + CONFIG_SET(net_auto, not config.net_auto); Net::rescale = true; } else if (key == "z") { diff --git a/src/btop_menu.cpp b/src/btop_menu.cpp index e17463fdd..3d38daa24 100644 --- a/src/btop_menu.cpp +++ b/src/btop_menu.cpp @@ -692,8 +692,9 @@ namespace Menu { 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& config = Config::get(); + auto tty_mode = config.tty_mode; + auto rounded = config.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); @@ -782,7 +783,8 @@ 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")); + auto& config = Config::get(); + auto s_pid = (config.show_detailed and config.selected_pid == 0 ? config.detailed_pid : config.selected_pid); static int x{}; // defaults to 0 static int y{}; // defaults to 0 static int selected_signal = -1; @@ -796,7 +798,7 @@ namespace Menu { y = Term::height/2 - 9; bg = Draw::createBox(x + 2, y, 78, 19, Theme::c("hi_fg"), true, "signals"); bg += Mv::to(y+2, x+3) + Theme::c("title") + Fx::b + cjust("Send signal to PID " + to_string(s_pid) + " (" - + uresize((s_pid == Config::getI("detailed_pid") ? Proc::detailed.entry.name : Config::getS("selected_name")), 30) + ")", 76); + + uresize((s_pid == config.detailed_pid ? Proc::detailed.entry.name : config.selected_name), 30) + ")", 76); } else if (is_in(key, "escape", "q")) { return Closed; @@ -912,11 +914,12 @@ 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& config = Config::get(); + auto s_pid = (config.show_detailed and config.selected_pid == 0 ? config.detailed_pid : config.selected_pid); if (s_pid == 0) return Closed; if (redraw) { atomic_wait(Runner::active); - auto& p_name = (s_pid == Config::getI("detailed_pid") ? Proc::detailed.entry.name : Config::getS("selected_name")); + auto& p_name = (s_pid == config.detailed_pid ? Proc::detailed.entry.name : config.selected_name); vector cont_vec = { Fx::b + Theme::c("main_fg") + "Send signal: " + Fx::ub + Theme::c("hi_fg") + to_string(signalToSend) + (signalToSend > 0 and signalToSend <= 32 ? Theme::c("main_fg") + " (" + P_Signals.at(signalToSend) + ')' : ""), @@ -984,12 +987,13 @@ namespace Menu { } int mainMenu(const string& key) { + auto& config = Config::get(); enum MenuItems { Options, Help, Quit }; 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.tty_mode; if (bg.empty()) selected = 0; int retval = Changed; @@ -1094,8 +1098,9 @@ 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& config = Config::get(); + auto tty_mode = config.tty_mode; + auto vim_keys = config.vim_keys; if (max_items == 0) { for (const auto& cat : categories) { if ((int)cat.size() > max_items) max_items = cat.size(); @@ -1142,8 +1147,9 @@ namespace Menu { } else if (key == "enter") { const auto& option = categories[selected_cat][item_height * page + selected][0]; + auto& entry = Config::parse_table.at(option); if (selPred.test(isString) and Config::stringValid(option, editor.text)) { - Config::set(option, editor.text); + Config::set(entry.offset, option, editor.text, true); if (option == "custom_cpu_name") screen_redraw = true; else if (is_in(option, "shown_boxes", "presets")) { screen_redraw = true; @@ -1159,7 +1165,7 @@ namespace Menu { } } else if (selPred.test(isInt) and Config::intValid(option, editor.text)) { - Config::set(option, stoi(editor.text)); + Config::set(entry.offset, option, stoi(editor.text), true); } else warnings = Config::validError; @@ -1232,27 +1238,29 @@ namespace Menu { else if (is_in(key, "left", "right") or (vim_keys and is_in(key, "h", "l"))) { const auto& option = categories[selected_cat][item_height * page + selected][0]; if (selPred.test(isInt)) { + auto& entry = Config::parse_table.at(option); const int mod = (option == "update_ms" ? 100 : 1); - long value = Config::getI(option); + long value = Config::dynamic_get(config, entry.offset); if (key == "right" or (vim_keys and key == "l")) value += mod; else value -= mod; if (Config::intValid(option, to_string(value))) - Config::set(option, static_cast(value)); + Config::set(entry.offset, option, static_cast(value), true); else { warnings = Config::validError; } } else if (selPred.test(isBool)) { - Config::flip(option); + auto& entry = Config::parse_table.at(option); + Config::set(entry.offset, option, not Config::dynamic_get(config, entry.offset), true); screen_redraw = true; if (option == "truecolor") { theme_refresh = true; - Config::flip("lowcolor"); + CONFIG_SET(lowcolor, not config.lowcolor); } else if (option == "force_tty") { theme_refresh = true; - Config::flip("tty_mode"); + CONFIG_SET(tty_mode, not config.tty_mode); } else if (is_in(option, "rounded_corners", "theme_background")) theme_refresh = true; @@ -1265,11 +1273,12 @@ namespace Menu { } else if (selPred.test(isBrowseable)) { auto& optList = optionsList.at(option).get(); - int i = v_index(optList, Config::getS(option)); + auto& entry = Config::parse_table.at(option); + int i = v_index(optList, Config::dynamic_get(config, entry.offset)); if ((key == "right" or (vim_keys and key == "l")) and ++i >= (int)optList.size()) i = 0; else if ((key == "left" or (vim_keys and key == "h")) and --i < 0) i = optList.size() - 1; - Config::set(option, optList.at(i)); + Config::set(entry.offset, option, optList.at(i), true); if (option == "color_theme") theme_refresh = true; @@ -1305,12 +1314,20 @@ namespace Menu { selPred.reset(); last_sel = (selected_cat << 8) + selected; const auto& selOption = categories[selected_cat][item_height * page + selected][0]; - if (Config::ints.contains(selOption)) - selPred.set(isInt); - else if (Config::bools.contains(selOption)) - selPred.set(isBool); - else - selPred.set(isString); + auto& entry = Config::parse_table.at(selOption); + switch(entry.type) { + case Config::ConfigType::INT: + selPred.set(isInt); + break; + case Config::ConfigType::BOOL: + selPred.set(isBool); + break; + case Config::ConfigType::STRING: + selPred.set(isString); + break; + default: + throw std::logic_error("Not implemented"); + } if (not selPred.test(isString)) selPred.set(is2D); @@ -1340,12 +1357,12 @@ namespace Menu { auto cy = y+9; for (int c = 0, i = max(0, item_height * page); c++ < item_height and i < (int)categories[selected_cat].size(); i++) { const auto& option = categories[selected_cat][i][0]; - const auto& value = (option == "color_theme" ? (string) fs::path(Config::getS("color_theme")).stem() : Config::getAsString(option)); + const auto& value = (option == "color_theme" ? (string) fs::path(config.color_theme).stem() : Config::getAsString(option)); out += Mv::to(cy++, x + 1) + (c-1 == selected ? Theme::c("selected_bg") + Theme::c("selected_fg") : Theme::c("title")) + Fx::b + cjust(capitalize(s_replace(option, "_", " ")) + (c-1 == selected and selPred.test(isBrowseable) - ? ' ' + to_string(v_index(optionsList.at(option).get(), (option == "color_theme" ? Config::getS("color_theme") : value)) + 1) + '/' + to_string(optionsList.at(option).get().size()) + ? ' ' + to_string(v_index(optionsList.at(option).get(), (option == "color_theme" ? config.color_theme : value)) + 1) + '/' + to_string(optionsList.at(option).get().size()) : ""), 29); out += Mv::to(cy++, x + 1) + (c-1 == selected ? "" : Theme::c("main_fg")) + Fx::ub + " " + (c-1 == selected and editing ? cjust(editor(24), 34, true) : cjust(value, 25, true)) + " "; diff --git a/src/btop_shared.cpp b/src/btop_shared.cpp index 98c24ceec..f2c513149 100644 --- a/src/btop_shared.cpp +++ b/src/btop_shared.cpp @@ -157,7 +157,7 @@ namespace Proc { filter_found++; p.filtered = true; } - else if (Config::getB("proc_aggregate")) { + else if (Config::get().proc_aggregate) { cur_proc.cpu_p += p.cpu_p; cur_proc.cpu_c += p.cpu_c; cur_proc.mem += p.mem; diff --git a/src/btop_theme.cpp b/src/btop_theme.cpp index 8a641fed0..2b878206e 100644 --- a/src/btop_theme.cpp +++ b/src/btop_theme.cpp @@ -225,12 +225,13 @@ namespace Theme { //* Generate colors and rgb decimal vectors for the theme void generateColors(const unordered_flat_map& source) { + auto& config = Config::get(); vector t_rgb; string depth; - bool t_to_256 = Config::getB("lowcolor"); + bool t_to_256 = config.lowcolor; colors.clear(); rgbs.clear(); for (const auto& [name, color] : Default_theme) { - if (name == "main_bg" and not Config::getB("theme_background")) { + if (name == "main_bg" and not config.theme_background) { colors[name] = "\x1b[49m"; rgbs[name] = {-1, -1, -1}; continue; @@ -289,8 +290,9 @@ namespace Theme { //* Generate color gradients from two or three colors, 101 values indexed 0-100 void generateGradients() { + auto& config = Config::get(); gradients.clear(); - bool t_to_256 = Config::getB("lowcolor"); + bool t_to_256 = config.lowcolor; //? Insert values for processes greyscale gradient and processes color gradient rgbs.insert({ @@ -349,10 +351,11 @@ namespace Theme { //* Set colors and generate gradients for the TTY theme void generateTTYColors() { + auto& config = Config::get(); rgbs.clear(); gradients.clear(); colors = TTY_theme; - if (not Config::getB("theme_background")) + if (not config.theme_background) colors["main_bg"] = "\x1b[49m"; for (const auto& c : colors) { @@ -422,7 +425,8 @@ namespace Theme { } void setTheme() { - const auto& theme = Config::getS("color_theme"); + auto& config = Config::get(); + const auto& theme = config.color_theme; fs::path theme_path; for (const fs::path p : themes) { if (p == theme or p.stem() == theme or p.filename() == theme) { @@ -430,7 +434,7 @@ namespace Theme { break; } } - if (theme == "TTY" or Config::getB("tty_mode")) + if (theme == "TTY" or config.tty_mode) generateTTYColors(); else { generateColors((theme == "Default" or theme_path.empty() ? Default_theme : loadFile(theme_path))); diff --git a/src/btop_tools.cpp b/src/btop_tools.cpp index f6be1741d..b00b1ff62 100644 --- a/src/btop_tools.cpp +++ b/src/btop_tools.cpp @@ -368,8 +368,9 @@ namespace Tools { string floating_humanizer(uint64_t value, bool shorten, size_t start, bool bit, bool per_second) { string out; + auto& config = Config::get(); const size_t mult = (bit) ? 8 : 1; - bool mega = Config::getB("base_10_sizes"); + bool mega = config.base_10_sizes; // taking advantage of type deduction for array creation (since C++17) // combined with string literals (operator""s) diff --git a/src/freebsd/btop_collect.cpp b/src/freebsd/btop_collect.cpp index 47598f09f..24c3d7df4 100644 --- a/src/freebsd/btop_collect.cpp +++ b/src/freebsd/btop_collect.cpp @@ -260,8 +260,9 @@ namespace Cpu { } bool get_sensors() { + auto &config = Config::get(); got_sensors = false; - if (Config::getB("show_coretemp") and Config::getB("check_temp")) { + if (config.show_coretemp and config.check_temp) { int32_t temp; size_t size = sizeof(temp); if (sysctlbyname("dev.cpu.0.temperature", &temp, &size, nullptr, 0) < 0) { @@ -324,6 +325,7 @@ namespace Cpu { } auto get_core_mapping() -> unordered_flat_map { + auto &config = Config::get(); unordered_flat_map core_map; if (cpu_temp_only) return core_map; @@ -348,7 +350,7 @@ namespace Cpu { } //? Apply user set custom mapping if any - const auto &custom_map = Config::getS("cpu_core_map"); + const auto &custom_map = config.cpu_core_map; if (not custom_map.empty()) { try { for (const auto &split : ssplit(custom_map)) { @@ -401,6 +403,7 @@ namespace Cpu { auto collect(bool no_update) -> cpu_info & { if (Runner::stopping or (no_update and not current_cpu.cpu_percent.at("total").empty())) return current_cpu; + auto &config = Config::get(); auto &cpu = current_cpu; if (getloadavg(cpu.load_avg.data(), cpu.load_avg.size()) < 0) { @@ -476,17 +479,17 @@ namespace Cpu { //? Reduce size if there are more values than needed for graph while (cmp_greater(cpu.cpu_percent.at("total").size(), width * 2)) cpu.cpu_percent.at("total").pop_front(); - if (Config::getB("show_cpu_freq")) { + if (config.show_cpu_freq) { auto hz = get_cpuHz(); if (hz != "") { cpuHz = hz; } } - if (Config::getB("check_temp") and got_sensors) + if (config.check_temp and got_sensors) update_sensors(); - if (Config::getB("show_battery") and has_battery) + if (config.show_battery and has_battery) current_bat = get_battery(); return cpu; @@ -624,10 +627,11 @@ namespace Mem { auto collect(bool no_update) -> mem_info & { if (Runner::stopping or (no_update and not current_mem.percent.at("used").empty())) return current_mem; + auto &config = Config::get(); - auto show_swap = Config::getB("show_swap"); - auto show_disks = Config::getB("show_disks"); - auto swap_disk = Config::getB("swap_disk"); + auto show_swap = config.show_swap; + auto show_disks = config.show_disks; + auto swap_disk = config.swap_disk; auto &mem = current_mem; static bool snapped = (getenv("BTOP_SNAPPED") != nullptr); @@ -693,9 +697,9 @@ namespace Mem { if (show_disks) { unordered_flat_map mapping; // keep mapping from device -> mountpoint, since IOKit doesn't give us the mountpoint double uptime = system_uptime(); - auto &disks_filter = Config::getS("disks_filter"); + auto &disks_filter = config.disks_filter; bool filter_exclude = false; - // auto only_physical = Config::getB("only_physical"); + // auto only_physical = config.only_physical; auto &disks = mem.disks; vector filter; if (not disks_filter.empty()) { @@ -830,9 +834,10 @@ namespace Net { auto collect(bool no_update) -> net_info & { auto &net = current_net; - auto &config_iface = Config::getS("net_iface"); - auto net_sync = Config::getB("net_sync"); - auto net_auto = Config::getB("net_auto"); + auto &config = Config::get(); + auto &config_iface = config.net_iface; + auto net_sync = config.net_sync; + auto net_auto = config.net_auto; auto new_timestamp = time_ms(); if (not no_update and errors < 3) { @@ -1063,10 +1068,11 @@ namespace Proc { //* Get detailed info for selected process void _collect_details(const size_t pid, vector &procs) { + auto &config = Config::get(); if (pid != detailed.last_pid) { detailed = {}; detailed.last_pid = pid; - detailed.skip_smaps = not Config::getB("proc_info_smaps"); + detailed.skip_smaps = not config.proc_cpu_smaps; } //? Copy proc_info for process from proc vector @@ -1074,7 +1080,7 @@ namespace Proc { detailed.entry = *p_info; //? Update cpu percent deque for process cpu graph - if (not Config::getB("proc_per_core")) detailed.entry.cpu_p *= Shared::coreCount; + if (not config.proc_per_core) detailed.entry.cpu_p *= Shared::coreCount; detailed.cpu_percent.push_back(clamp((long long)round(detailed.entry.cpu_p), 0ll, 100ll)); while (cmp_greater(detailed.cpu_percent.size(), width)) detailed.cpu_percent.pop_front(); @@ -1113,13 +1119,14 @@ namespace Proc { //* Collects and sorts process information from /proc auto collect(bool no_update) -> vector & { - const auto &sorting = Config::getS("proc_sorting"); - auto reverse = Config::getB("proc_reversed"); - const auto &filter = Config::getS("proc_filter"); - auto per_core = Config::getB("proc_per_core"); - auto tree = Config::getB("proc_tree"); - auto show_detailed = Config::getB("show_detailed"); - const size_t detailed_pid = Config::getI("detailed_pid"); + auto &config = Config::get(); + const auto &sorting = config.proc_sorting; + auto reverse = config.proc_reversed; + const auto &filter = config.proc_filter; + auto per_core = config.proc_per_core; + auto tree = config.proc_tree; + auto show_detailed = config.show_detailed; + const size_t detailed_pid = config.detailed_pid; bool should_filter = current_filter != filter; if (should_filter) current_filter = filter; bool sorted_change = (sorting != current_sort or reverse != current_rev or should_filter); @@ -1289,7 +1296,7 @@ namespace Proc { else if (expand > -1) { collapser->collapsed = false; } - if (Config::ints.at("proc_selected") > 0) locate_selection = true; + if (config.proc_selected > 0) locate_selection = true; } collapse = expand = -1; } @@ -1327,10 +1334,11 @@ namespace Proc { //? Move current selection/view to the selected process when collapsing/expanding in the tree if (locate_selection) { + auto &config = Config::get_mut(false, false); int loc = rng::find(current_procs, Proc::selected_pid, &proc_info::pid)->tree_index; - if (Config::ints.at("proc_start") >= loc or Config::ints.at("proc_start") <= loc - Proc::select_max) - Config::ints.at("proc_start") = max(0, loc - 1); - Config::ints.at("proc_selected") = loc - Config::ints.at("proc_start") + 1; + if (config.proc_start >= loc or config.proc_start <= loc - Proc::select_max) + config.proc_start = max(0, loc - 1); + config.proc_selected = loc - config.proc_start + 1; } } diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index b5b1cea46..443299051 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -389,15 +389,16 @@ namespace Cpu { void update_sensors() { if (cpu_sensor.empty()) return; + auto& config = Config::get(); - const auto& cpu_sensor = (not Config::getS("cpu_sensor").empty() and found_sensors.contains(Config::getS("cpu_sensor")) ? Config::getS("cpu_sensor") : Cpu::cpu_sensor); + const auto& cpu_sensor = (not config.cpu_sensor.empty() and found_sensors.contains(config.cpu_sensor) ? config.cpu_sensor : Cpu::cpu_sensor); found_sensors.at(cpu_sensor).temp = stol(readfile(found_sensors.at(cpu_sensor).path, "0")) / 1000; current_cpu.temp.at(0).push_back(found_sensors.at(cpu_sensor).temp); current_cpu.temp_max = found_sensors.at(cpu_sensor).crit; if (current_cpu.temp.at(0).size() > 20) current_cpu.temp.at(0).pop_front(); - if (Config::getB("show_coretemp") and not cpu_temp_only) { + if (config.show_coretemp and not cpu_temp_only) { vector done; for (const auto& sensor : core_sensors) { if (v_contains(done, sensor)) continue; @@ -473,6 +474,7 @@ namespace Cpu { auto get_core_mapping() -> unordered_flat_map { unordered_flat_map core_map; if (cpu_temp_only) return core_map; + auto& config = Config::get(); //? Try to get core mapping from /proc/cpuinfo ifstream cpuinfo(Shared::procPath / "cpuinfo"); @@ -517,7 +519,7 @@ namespace Cpu { } //? Apply user set custom mapping if any - const auto& custom_map = Config::getS("cpu_core_map"); + const auto& custom_map = config.cpu_core_map; if (not custom_map.empty()) { try { for (const auto& split : ssplit(custom_map)) { @@ -543,6 +545,7 @@ namespace Cpu { auto get_battery() -> tuple { if (not has_battery) return {0, 0, ""}; + auto& config = Config::get(); static string auto_sel; static unordered_flat_map batteries; @@ -605,7 +608,7 @@ namespace Cpu { } } - auto& battery_sel = Config::getS("selected_battery"); + auto& battery_sel = config.selected_battery; if (auto_sel.empty()) { for (auto& [name, bat] : batteries) { @@ -674,9 +677,10 @@ namespace Cpu { auto collect(bool no_update) -> cpu_info& { if (Runner::stopping or (no_update and not current_cpu.cpu_percent.at("total").empty())) return current_cpu; + auto& config = Config::get(); auto& cpu = current_cpu; - if (Config::getB("show_cpu_freq")) + if (config.show_cpu_freq) cpuHz = get_cpuHz(); if (getloadavg(cpu.load_avg.data(), cpu.load_avg.size()) < 0) { @@ -803,10 +807,10 @@ namespace Cpu { else throw std::runtime_error("Cpu::collect() : " + string{e.what()}); } - if (Config::getB("check_temp") and got_sensors) + if (config.check_temp and got_sensors) update_sensors(); - if (Config::getB("show_battery") and has_battery) + if (config.show_battery and has_battery) current_bat = get_battery(); return cpu; @@ -844,10 +848,11 @@ namespace Mem { auto collect(bool no_update) -> mem_info& { if (Runner::stopping or (no_update and not current_mem.percent.at("used").empty())) return current_mem; - auto show_swap = Config::getB("show_swap"); - auto swap_disk = Config::getB("swap_disk"); - auto show_disks = Config::getB("show_disks"); - auto zfs_arc_cached = Config::getB("zfs_arc_cached"); + auto& config = Config::get(); + auto show_swap = config.show_swap; + auto swap_disk = config.swap_disk; + auto show_disks = config.show_disks; + auto zfs_arc_cached = config.zfs_arc_cached; auto totalMem = get_totalMem(); auto& mem = current_mem; @@ -937,13 +942,13 @@ namespace Mem { if (show_disks) { static vector ignore_list; double uptime = system_uptime(); - auto free_priv = Config::getB("disk_free_priv"); + auto free_priv = config.disk_free_priv; try { - auto& disks_filter = Config::getS("disks_filter"); + auto& disks_filter = config.disks_filter; bool filter_exclude = false; - auto use_fstab = Config::getB("use_fstab"); - auto only_physical = Config::getB("only_physical"); - auto zfs_hide_datasets = Config::getB("zfs_hide_datasets"); + auto use_fstab = config.use_fstab; + auto only_physical = config.only_physical; + auto zfs_hide_datasets = config.zfs_hide_datasets; auto& disks = mem.disks; static unordered_flat_map>> disks_stats_promises; ifstream diskread; @@ -1399,10 +1404,11 @@ namespace Net { auto collect(bool no_update) -> net_info& { if (Runner::stopping) return empty_net; + auto& config = Config::get(); auto& net = current_net; - auto& config_iface = Config::getS("net_iface"); - auto net_sync = Config::getB("net_sync"); - auto net_auto = Config::getB("net_auto"); + auto& config_iface = config.net_iface; + auto net_sync = config.net_sync; + auto net_auto = config.net_auto; auto new_timestamp = time_ms(); if (not no_update and errors < 3) { @@ -1611,12 +1617,13 @@ namespace Proc { //* Get detailed info for selected process void _collect_details(const size_t pid, const uint64_t uptime, vector& procs) { + auto& config = Config::get(); fs::path pid_path = Shared::procPath / std::to_string(pid); if (pid != detailed.last_pid) { detailed = {}; detailed.last_pid = pid; - detailed.skip_smaps = not Config::getB("proc_info_smaps"); + detailed.skip_smaps = not config.proc_cpu_smaps; } //? Copy proc_info for process from proc vector @@ -1624,7 +1631,7 @@ namespace Proc { detailed.entry = *p_info; //? Update cpu percent deque for process cpu graph - if (not Config::getB("proc_per_core")) detailed.entry.cpu_p *= Shared::coreCount; + if (not config.proc_per_core) detailed.entry.cpu_p *= Shared::coreCount; detailed.cpu_percent.push_back(clamp((long long)round(detailed.entry.cpu_p), 0ll, 100ll)); while (cmp_greater(detailed.cpu_percent.size(), width)) detailed.cpu_percent.pop_front(); @@ -1709,14 +1716,15 @@ namespace Proc { //* Collects and sorts process information from /proc auto collect(bool no_update) -> vector& { if (Runner::stopping) return current_procs; - const auto& sorting = Config::getS("proc_sorting"); - auto reverse = Config::getB("proc_reversed"); - const auto& filter = Config::getS("proc_filter"); - auto per_core = Config::getB("proc_per_core"); - auto should_filter_kernel = Config::getB("proc_filter_kernel"); - auto tree = Config::getB("proc_tree"); - auto show_detailed = Config::getB("show_detailed"); - const size_t detailed_pid = Config::getI("detailed_pid"); + auto& config = Config::get(); + const auto& sorting = config.proc_sorting; + auto reverse = config.proc_reversed; + const auto& filter = config.proc_filter; + auto per_core = config.proc_per_core; + auto should_filter_kernel = config.proc_filter_kernel; + auto tree = config.proc_tree; + auto show_detailed = config.show_detailed; + const size_t detailed_pid = config.detailed_pid; bool should_filter = current_filter != filter; if (should_filter) current_filter = filter; bool sorted_change = (sorting != current_sort or reverse != current_rev or should_filter); @@ -2029,7 +2037,7 @@ namespace Proc { else if (expand > -1) { collapser->collapsed = false; } - if (Config::ints.at("proc_selected") > 0) locate_selection = true; + if (config.proc_selected > 0) locate_selection = true; } collapse = expand = -1; } @@ -2068,9 +2076,10 @@ namespace Proc { //? Move current selection/view to the selected process when collapsing/expanding in the tree if (locate_selection) { int loc = rng::find(current_procs, Proc::selected_pid, &proc_info::pid)->tree_index; - if (Config::ints.at("proc_start") >= loc or Config::ints.at("proc_start") <= loc - Proc::select_max) - Config::ints.at("proc_start") = max(0, loc - 1); - Config::ints.at("proc_selected") = loc - Config::ints.at("proc_start") + 1; + auto& config = Config::get_mut(false, false); + if (config.proc_start >= loc or config.proc_start <= loc - Proc::select_max) + config.proc_start = max(0, loc - 1) + config.proc_selected = loc - config.proc_start + 1; } } diff --git a/src/osx/btop_collect.cpp b/src/osx/btop_collect.cpp index 26a93d30e..38a70edc9 100644 --- a/src/osx/btop_collect.cpp +++ b/src/osx/btop_collect.cpp @@ -247,9 +247,10 @@ namespace Cpu { } bool get_sensors() { - Logger::debug("get_sensors(): show_coretemp=" + std::to_string(Config::getB("show_coretemp")) + " check_temp=" + std::to_string(Config::getB("check_temp"))); + auto &config = Config::get(); + Logger::debug("get_sensors(): show_coretemp=" + std::to_string(config.show_coretemp) + " check_temp=" + std::to_string(config.check_temp)); got_sensors = false; - if (Config::getB("show_coretemp") and Config::getB("check_temp")) { + if (config.show_coretemp and config.check_temp) { ThermalSensors sensors; if (sensors.getSensors() > 0) { Logger::debug("M1 sensors found"); @@ -332,6 +333,7 @@ namespace Cpu { auto get_core_mapping() -> unordered_flat_map { unordered_flat_map core_map; if (cpu_temp_only) return core_map; + auto &config = Config::get(); natural_t cpu_count; natural_t i; @@ -364,7 +366,7 @@ namespace Cpu { } //? Apply user set custom mapping if any - const auto &custom_map = Config::getS("cpu_core_map"); + const auto &custom_map = config.cpu_core_map; if (not custom_map.empty()) { try { for (const auto &split : ssplit(custom_map)) { @@ -444,6 +446,7 @@ namespace Cpu { auto collect(bool no_update) -> cpu_info & { if (Runner::stopping or (no_update and not current_cpu.cpu_percent.at("total").empty())) return current_cpu; + auto &config = Config::get(); auto &cpu = current_cpu; if (getloadavg(cpu.load_avg.data(), cpu.load_avg.size()) < 0) { @@ -524,17 +527,17 @@ namespace Cpu { //? Reduce size if there are more values than needed for graph while (cmp_greater(cpu.cpu_percent.at("total").size(), width * 2)) cpu.cpu_percent.at("total").pop_front(); - if (Config::getB("show_cpu_freq")) { + if (config.show_cpu_freq) { auto hz = get_cpuHz(); if (hz != "") { cpuHz = hz; } } - if (Config::getB("check_temp") and got_sensors) + if (config.check_temp and got_sensors) update_sensors(); - if (Config::getB("show_battery") and has_battery) + if (config.show_battery and has_battery) current_bat = get_battery(); return cpu; @@ -666,9 +669,10 @@ namespace Mem { if (Runner::stopping or (no_update and not current_mem.percent.at("used").empty())) return current_mem; - auto show_swap = Config::getB("show_swap"); - auto show_disks = Config::getB("show_disks"); - auto swap_disk = Config::getB("swap_disk"); + auto &config = Config::get(); + auto show_swap = config.show_swap; + auto show_disks = config.show_disks; + auto swap_disk = config.swap_disk; auto &mem = current_mem; static bool snapped = (getenv("BTOP_SNAPPED") != nullptr); @@ -710,9 +714,9 @@ namespace Mem { if (show_disks) { unordered_flat_map mapping; // keep mapping from device -> mountpoint, since IOKit doesn't give us the mountpoint double uptime = system_uptime(); - auto &disks_filter = Config::getS("disks_filter"); + auto &disks_filter = config.disks_filter; bool filter_exclude = false; - // auto only_physical = Config::getB("only_physical"); + // auto only_physical = config.only_physical; auto &disks = mem.disks; vector filter; if (not disks_filter.empty()) { @@ -843,10 +847,11 @@ namespace Net { }; auto collect(bool no_update) -> net_info & { + auto &config = Config::get(); auto &net = current_net; - auto &config_iface = Config::getS("net_iface"); - auto net_sync = Config::getB("net_sync"); - auto net_auto = Config::getB("net_auto"); + auto &config_iface = config.net_iface; + auto net_sync = config.net_sync; + auto net_auto = config.net_auto; auto new_timestamp = time_ms(); if (not no_update and errors < 3) { @@ -1075,10 +1080,11 @@ namespace Proc { //* Get detailed info for selected process void _collect_details(const size_t pid, vector &procs) { + auto &config = Config::get(); if (pid != detailed.last_pid) { detailed = {}; detailed.last_pid = pid; - detailed.skip_smaps = not Config::getB("proc_info_smaps"); + detailed.skip_smaps = not config.proc_cpu_smaps; } //? Copy proc_info for process from proc vector @@ -1086,7 +1092,7 @@ namespace Proc { detailed.entry = *p_info; //? Update cpu percent deque for process cpu graph - if (not Config::getB("proc_per_core")) detailed.entry.cpu_p *= Shared::coreCount; + if (not config.proc_per_core) detailed.entry.cpu_p *= Shared::coreCount; detailed.cpu_percent.push_back(clamp((long long)round(detailed.entry.cpu_p), 0ll, 100ll)); while (cmp_greater(detailed.cpu_percent.size(), width)) detailed.cpu_percent.pop_front(); @@ -1125,13 +1131,14 @@ namespace Proc { //* Collects and sorts process information from /proc auto collect(bool no_update) -> vector & { - const auto &sorting = Config::getS("proc_sorting"); - auto reverse = Config::getB("proc_reversed"); - const auto &filter = Config::getS("proc_filter"); - auto per_core = Config::getB("proc_per_core"); - auto tree = Config::getB("proc_tree"); - auto show_detailed = Config::getB("show_detailed"); - const size_t detailed_pid = Config::getI("detailed_pid"); + auto &config = Config::get(); + const auto &sorting = config.proc_sorting; + auto reverse = config.proc_reversed; + const auto &filter = config.proc_filter; + auto per_core = config.proc_per_core; + auto tree = config.proc_tree; + auto show_detailed = config.show_detailed; + const size_t detailed_pid = config.detailed_pid; bool should_filter = current_filter != filter; if (should_filter) current_filter = filter; bool sorted_change = (sorting != current_sort or reverse != current_rev or should_filter); @@ -1321,7 +1328,7 @@ namespace Proc { else if (expand > -1) { collapser->collapsed = false; } - if (Config::ints.at("proc_selected") > 0) locate_selection = true; + if (config.proc_selected > 0) locate_selection = true; } collapse = expand = -1; } @@ -1359,10 +1366,11 @@ namespace Proc { //? Move current selection/view to the selected process when collapsing/expanding in the tree if (locate_selection) { + auto &config = Config::get_mut(false, false); int loc = rng::find(current_procs, Proc::selected_pid, &proc_info::pid)->tree_index; - if (Config::ints.at("proc_start") >= loc or Config::ints.at("proc_start") <= loc - Proc::select_max) - Config::ints.at("proc_start") = max(0, loc - 1); - Config::ints.at("proc_selected") = loc - Config::ints.at("proc_start") + 1; + if (config.proc_start >= loc or config.proc_start <= loc - Proc::select_max) + config.proc_start = max(0, loc - 1); + config.proc_selected = loc - config.proc_start + 1; } }