From 63a286d6a1eb5f9c87005fece53b560e6c80f839 Mon Sep 17 00:00:00 2001 From: aristocratos Date: Sat, 12 Jun 2021 00:35:15 +0200 Subject: [PATCH] Added config file descriptions and config write function --- btop.cpp | 4 +- src/btop_config.h | 95 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 79 insertions(+), 20 deletions(-) diff --git a/btop.cpp b/btop.cpp index 416b73d..8384276 100644 --- a/btop.cpp +++ b/btop.cpp @@ -260,8 +260,8 @@ int main(int argc, char **argv){ if (Global::debug) { Logger::loglevel = 4; Logger::debug("Starting in debug mode");} if (!string(getenv("LANG")).ends_with("UTF-8") && !string(getenv("LANG")).ends_with("utf-8")) { - string err_msg = "No UTF-8 locale was detected! Symbols might not look as intended.\n"s - + "Make sure your $LANG evironment variable is set and with a UTF-8 locale."s; + string err_msg = "No UTF-8 locale was detected! Symbols might not look as intended.\n" + "Make sure your $LANG evironment variable is set and with a UTF-8 locale."; Logger::warning(err_msg); cout << "WARNING: " << err_msg << endl; } diff --git a/src/btop_config.h b/src/btop_config.h index a8671a8..60bb750 100644 --- a/src/btop_config.h +++ b/src/btop_config.h @@ -21,12 +21,13 @@ tab-size = 4 #include #include +#include #include #include #include -using std::string, std::vector, robin_hood::unordered_flat_map; +using std::string, std::vector, robin_hood::unordered_flat_map, std::map; namespace fs = std::filesystem; using namespace Tools; @@ -40,7 +41,64 @@ namespace Config { atomic locked (false); atomic writelock (false); - unordered_flat_map changed; + bool changed; + + vector> descriptions = { + {"color_theme", "#* Color theme, looks for a .theme file in \"/usr/[local/]share/bpytop/themes\" and \"~/.config/bpytop/themes\", \"Default\" for builtin default theme.\n" + "#* Prefix name by a plus sign (+) for a theme located in user themes folder, i.e. color_theme=\"+monokai\"." }, + {"theme_background", "#* If the theme set background should be shown, set to False if you want terminal background transparency."}, + {"truecolor", "#* Sets if 24-bit truecolor should be used, will convert 24-bit colors to 256 color (6x6x6 color cube) if false."}, + {"shown_boxes", "#* Manually set which boxes to show. Available values are \"cpu mem net proc\", separate values with whitespace."}, + {"update_ms", "#* Update time in milliseconds, increases automatically if set below internal loops processing time, recommended 2000 ms or above for better sample times for graphs."}, + {"proc_update_mult", "#* Processes update multiplier, sets how often the process list is updated as a multiplier of \"update_ms\".\n" + "#* Set to 2 or higher to greatly decrease bpytop cpu usage. (Only integers)."}, + {"proc_sorting", "#* Processes sorting, \"pid\" \"program\" \"arguments\" \"threads\" \"user\" \"memory\" \"cpu lazy\" \"cpu responsive\",\n" + "#* \"cpu lazy\" updates top process over time, \"cpu responsive\" updates top process directly."}, + {"proc_reversed", "#* Reverse sorting order, True or False."}, + {"proc_tree", "#* Show processes as a tree."}, + {"tree_depth", "#* Which depth the tree view should auto collapse processes at."}, + {"proc_colors", "#* Use the cpu graph colors in the process list."}, + {"proc_gradient", "#* Use a darkening gradient in the process list."}, + {"proc_per_core", "#* If process cpu usage should be of the core it's running on or usage of the total available cpu power."}, + {"proc_mem_bytes", "#* Show process memory as bytes instead of percent."}, + {"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."}, + {"cpu_graph_lower", "#* Sets the CPU stat shown in lower half of the CPU graph, \"total\" is always available.\n" + "#* Select from a list of detected attributes from the options menu."}, + {"cpu_invert_lower", "#* Toggles if the lower CPU graph should be inverted."}, + {"cpu_single_graph", "#* Set to True to completely disable the lower CPU graph."}, + {"show_uptime", "#* Shows the system uptime in the CPU box."}, + {"check_temp", "#* Show cpu temperature."}, + {"cpu_sensor", "#* Which sensor to use for cpu temperature, use options menu to select from list of available sensors."}, + {"show_coretemp", "#* Show temperatures for cpu cores also if check_temp is True and sensors has been found."}, + {"temp_scale", "#* Which temperature scale to use, available values: \"celsius\", \"fahrenheit\", \"kelvin\" and \"rankine\"."}, + {"show_cpu_freq", "#* Show CPU frequency."}, + {"draw_clock", "#* Draw a clock at top of screen, formatting according to strftime, empty string to disable."}, + {"background_update", "#* Update main ui in background when menus are showing, set this to false if the menus is flickering too much for comfort."}, + {"custom_cpu_name", "#* Custom cpu model name, empty string to disable."}, + {"disks_filter", "#* Optional filter for shown disks, should be full path of a mountpoint, separate multiple values with a comma \",\".\n" + "#* Begin line with \"exclude=\" to change to exclude filter, otherwise defaults to \"most include\" filter. Example: disks_filter=\"exclude=/boot, /home/user\"."}, + {"mem_graphs", "#* Show graphs instead of meters for memory values."}, + {"show_swap", "#* If swap memory should be shown in memory box."}, + {"swap_disk", "#* Show swap as a disk, ignores show_swap value above, inserts itself after first disk."}, + {"show_disks", "#* If mem box should be split to also show disks info."}, + {"only_physical", "#* Filter out non physical disks. Set this to False to include network disks, RAM disks and similar."}, + {"use_fstab", "#* Read disks list from /etc/fstab. This also disables only_physical."}, + {"show_io_stat", "#* Toggles if io stats should be shown in regular disk usage view."}, + {"io_mode", "#* Toggles io mode for disks, showing only big graphs for disk read/write speeds."}, + {"io_graph_combined", "#* Set to True to show combined read/write io graphs in io mode."}, + {"io_graph_speeds", "#* Set the top speed for the io graphs in MiB/s (10 by default), use format \"device:speed\" separate disks with a comma \",\".\n" + "#* Example: \"/dev/sda:100, /dev/sdb:20\"."}, + {"net_download", "#* Set fixed values for network graphs, default \"10M\" = 10 Mibibytes, possible units \"K\", \"M\", \"G\", append with \"bit\" for bits instead of bytes, i.e \"100mbit\"."}, + {"net_upload", ""}, + {"net_auto", "#* Start in network graphs auto rescaling mode, ignores any values set above and rescales down to 10 Kibibytes at the lowest."}, + {"net_sync", "#* Sync the scaling for download and upload to whichever currently has the highest scale."}, + {"net_color_fixed", "#* If the network graphs color gradient should scale to bandwidth usage or auto scale, bandwidth usage is based on \"net_download\" and \"net_upload\" values."}, + {"net_iface", "#* Starts with the Network Interface specified here."}, + {"show_battery", "#* Show battery stats in top right if battery is present."}, + {"log_level", "#* Set loglevel for \"~/.config/bpytop/error.log\" levels are: \"ERROR\" \"WARNING\" \"INFO\" \"DEBUG\".\n" + "#* The level set includes all lower levels, i.e. \"DEBUG\" will show all logging info."} + }; unordered_flat_map strings = { {"color_theme", "Default"}, @@ -77,7 +135,6 @@ namespace Config { {"show_coretemp", true}, {"show_cpu_freq", true}, {"background_update", true}, - {"update_check", true}, {"mem_graphs", true}, {"show_swap", true}, {"swap_disk", true}, @@ -91,7 +148,6 @@ namespace Config { {"net_auto", true}, {"net_sync", false}, {"show_battery", true}, - {"show_init", false} }; unordered_flat_map boolsTmp; @@ -104,6 +160,7 @@ namespace Config { bool _locked(){ atomic_wait(writelock); + if (!changed) changed = true; return locked.load(); } } @@ -127,21 +184,18 @@ namespace Config { void set(string name, bool value){ if (_locked()) boolsTmp.insert_or_assign(name, value); else bools.at(name) = value; - changed.insert_or_assign(name, true); } //* Set config value to int void set(string name, int value){ if (_locked()) intsTmp.insert_or_assign(name, value); ints.at(name) = value; - changed.insert_or_assign(name, true); } //* Set config value to string void set(string name, string value){ if (_locked()) stringsTmp.insert_or_assign(name, value); else strings.at(name) = value; - changed.insert_or_assign(name, true); } //* Flip config bool @@ -151,7 +205,6 @@ namespace Config { else boolsTmp.insert_or_assign(name, (!bools.at(name))); } else bools.at(name) = !bools.at(name); - changed.insert_or_assign(name, true); } //* Wait if locked then lock config and cache changes until unlock @@ -180,24 +233,30 @@ namespace Config { } boolsTmp.clear(); } - writelock.store(false); - locked.store(false); + writelock = false; + locked = false; } + //* Load the config file from disk void load(){ if (conf_file.empty()) return; + else if (!fs::exists(conf_file)) { changed = true; return; } } + //* Write the config file to disk void write(){ - if (conf_file.empty() || changed.empty()) return; - - if (Logger::loglevel > 3) { - string items; - for (auto item : Config::changed) { - items += item.first + ", "; + if (conf_file.empty() || !changed) return; + Logger::debug("Writing new config file"); + std::ofstream cwrite(conf_file, std::ios::trunc); + if (cwrite.good()) { + cwrite << "#? Config file for btop v. " << Global::Version; + for (auto [name, description] : descriptions) { + cwrite << "\n\n" << (description.empty() ? "" : description + "\n") << name << "="; + if (strings.contains(name)) cwrite << "\"" << strings.at(name) << "\""; + else if (ints.contains(name)) cwrite << ints.at(name); + else if (bools.contains(name)) cwrite << (bools.at(name) ? "True" : "False"); } - items.pop_back(); items.pop_back(); - Logger::debug("Writing out new config values for: " + items); + cwrite.close(); } } }