diff --git a/Makefile b/Makefile index 5eb9277..87d8918 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ PREFIX ?= /usr/local DOCDIR ?= $(PREFIX)/share/btop/doc CXX = g++ -override CXXFLAGS += -std=c++20 -pthread -Wall -Wextra +override CXXFLAGS += -std=c++20 -O3 -pthread -Wall -Wextra -pedantic INCLUDES = -I./src btop: btop.cpp diff --git a/btop.cpp b/btop.cpp index d6c2644..893c272 100644 --- a/btop.cpp +++ b/btop.cpp @@ -18,6 +18,7 @@ tab-size = 4 #include +#include #include #include #include @@ -45,14 +46,14 @@ tab-size = 4 #endif #endif -using std::string, std::vector, std::map, std::atomic, std::endl, std::cout, std::views::iota; +using std::string, std::vector, std::array, std::map, std::atomic, std::endl, std::cout, std::views::iota; using namespace Tools; //? ------------------------------------------------- GLOBALS --------------------------------------------------------- namespace Global { - const vector> Banner_src = { + const vector> Banner_src = { {"#E62525", "██████╗ ████████╗ ██████╗ ██████╗"}, {"#CD2121", "██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗ ██╗ ██╗"}, {"#B31D1D", "██████╔╝ ██║ ██║ ██║██████╔╝ ██████╗██████╗"}, @@ -64,7 +65,7 @@ namespace Global { const string Version = "0.0.1"; string banner; - uint banner_width; + const uint banner_width = 49; } @@ -90,22 +91,16 @@ void argumentParser(int argc, char **argv){ } //* Generate the btop++ banner -auto createBanner(){ - struct out_vals { - uint w; - string s; - }; +string createBanner(){ size_t z = 0; - uint width=0, new_len=0; string b_color, bg, fg, out, oc, letter; bool truecolor = Config::getB("truecolor"); int bg_i; for (auto line: Global::Banner_src) { - if ((new_len = ulen(line[1])) > width) width = new_len; fg = Theme::hex_to_color(line[0], !truecolor); bg_i = 120-z*12; bg = Theme::dec_to_color(bg_i, bg_i, bg_i, !truecolor); - for (uint i = 0; i < line[1].size(); i += 3) { + for (size_t i = 0; i < line[1].size(); i += 3) { if (line[1][i] == ' '){ letter = ' '; i -= 2; @@ -117,12 +112,11 @@ auto createBanner(){ out += letter; oc = b_color; } - z++; - if (z < Global::Banner_src.size()) out += Mv::l(new_len) + Mv::d(1); + if (++z < Global::Banner_src.size()) out += Mv::l(ulen(line[1])) + Mv::d(1); } out += Mv::r(18 - Global::Version.size()) + Fx::i + Theme::dec_to_color(0,0,0, !truecolor, "bg") + - Theme::dec_to_color(150, 150, 150, !truecolor) + "v" + Global::Version + Fx::reset; - return out_vals {width, out}; + Theme::dec_to_color(150, 150, 150, !truecolor) + "v" + Global::Version + Fx::ui; + return out; } @@ -171,9 +165,7 @@ int main(int argc, char **argv){ Theme::set(Global::Default_theme); //? Create the btop++ banner - auto [banner_width, banner] = createBanner(); - Global::banner_width = move(banner_width); - Global::banner = move(banner); + Global::banner = createBanner(); //* ------------------------------------------------ TESTING ------------------------------------------------------ @@ -190,7 +182,7 @@ int main(int argc, char **argv){ cout << Mv::r(Term::width / 2 - Global::banner_width / 2) << Global::banner << endl; // cout << string(Term::width - 1, '-') << endl; - int ill; + int ill = 0; for (int i : iota(0, (int)Term::width)){ ill = (i <= (int)Term::width / 2) ? i : ill - 1; cout << Theme::g("used")[ill] << "-"; @@ -198,8 +190,7 @@ int main(int argc, char **argv){ cout << Fx::reset << endl; //* Test theme - - if (true) { + if (false) { cout << "Theme generation took " << time_ms() - thts << "ms" << endl; cout << "Colors:" << endl; @@ -282,7 +273,6 @@ int main(int argc, char **argv){ uint64_t tsl, timestamp2; uint timer = 2000; bool filtering = false; - vector sorting; bool reversing = false; int sortint = Proc::sort_map["cpu lazy"]; vector greyscale; @@ -303,19 +293,19 @@ int main(int argc, char **argv){ while (key != "q") { timestamp = time_ms(); tsl = timestamp + timer; - auto plist = Proc::collect(Proc::sort_vector[sortint], reversing, filter); + auto plist = Proc::collect(Proc::sort_array[sortint], reversing, filter); timestamp2 = time_ms(); timestamp = timestamp2 - timestamp; ostring.clear(); lc = 0; filter_cur = (filtering) ? Fx::bl + "█" + Fx::reset : ""; ostring = Mv::save + Mv::u(2) + Mv::r(20) + trans(rjust("Filter: " + filter + filter_cur + string(Term::width / 3, ' ') + - "Sorting: " + string(Proc::sort_vector[sortint]), Term::width - 25, true, filtering)) + Mv::restore; + "Sorting: " + string(Proc::sort_array[sortint]), Term::width - 25, true, filtering)) + Mv::restore; - for (Proc::proc_info& procs : plist){ - ostring += Mv::r(1) + greyscale[lc] + rjust(to_string(procs.pid), 8) + " " + ljust(procs.name, 16) + " " + ljust(procs.cmd, Term::width - 66, true) + " " + - rjust(to_string(procs.threads), 5) + " " + ljust(procs.user, 10) + " " + rjust(floating_humanizer(procs.mem, true), 5) + string(11, ' '); - ostring += (procs.cpu_p > 100) ? rjust(to_string(procs.cpu_p), 3) + " " : rjust(to_string(procs.cpu_p), 4); + for (auto& p : plist){ + ostring += Mv::r(1) + greyscale[lc] + rjust(to_string(p.pid), 8) + " " + ljust(p.name, 16) + " " + ljust(p.cmd, Term::width - 66, true) + " " + + rjust(to_string(p.threads), 5) + " " + ljust(p.user, 10) + " " + rjust(floating_humanizer(p.mem, true), 5) + string(11, ' '); + ostring += (p.cpu_p > 100) ? rjust(to_string(p.cpu_p), 3) + " " : rjust(to_string(p.cpu_p), 4); ostring += "\n"; if (lc++ > Term::height - 21) break; } @@ -326,18 +316,17 @@ int main(int argc, char **argv){ while (time_ms() < tsl) { if (Input::poll(tsl - time_ms())) key = Input::get(); else { key.clear() ; continue; } - // if (key != "") continue; if (filtering) { if (key == "enter") filtering = false; else if (key == "backspace") {if (!filter.empty()) filter = uresize(filter, ulen(filter) - 1);} else if (key == "space") filter.push_back(' '); else if (ulen(key) == 1 ) filter.append(key); - else { key.clear() ; continue; } + else { key.clear(); continue; } break; } else if (key == "q") break; - else if (key == "left") { if (--sortint < 0) sortint = (int)Proc::sort_vector.size() - 1; } - else if (key == "right") { if (++sortint > (int)Proc::sort_vector.size() - 1) sortint = 0; } + else if (key == "left") { if (--sortint < 0) sortint = (int)Proc::sort_array.size() - 1; } + else if (key == "right") { if (++sortint > (int)Proc::sort_array.size() - 1) sortint = 0; } else if (key == "f") filtering = true; else if (key == "r") reversing = !reversing; else if (key == "delete") filter.clear(); diff --git a/src/btop_config.h b/src/btop_config.h index 41f95f1..e510a1d 100644 --- a/src/btop_config.h +++ b/src/btop_config.h @@ -21,11 +21,11 @@ tab-size = 4 #include #include -#include +#include #include -using std::string, std::vector, std::map; +using std::string, std::vector, std::unordered_map; using namespace Tools; @@ -35,7 +35,7 @@ namespace Config { bool changed = false; - map strings = { + unordered_map strings = { {"color_theme", "Default"}, {"shown_boxes", "cpu mem net proc"}, {"proc_sorting", "cpu lazy"}, @@ -52,7 +52,7 @@ namespace Config { {"net_iface", ""}, {"log_level", "WARNING"} }; - map bools = { + unordered_map bools = { {"theme_background", true}, {"truecolor", true}, {"proc_reversed", false}, @@ -84,12 +84,12 @@ namespace Config { {"show_battery", true}, {"show_init", false} }; - map ints = { + unordered_map ints = { {"update_ms", 2000}, {"proc_update_mult", 2}, {"tree_depth", 3} }; - }; + } //* Return config value as a bool bool& getB(string name){ @@ -128,6 +128,6 @@ namespace Config { (void)source; return true; } -}; +} #endif \ No newline at end of file diff --git a/src/btop_globs.h b/src/btop_globs.h index 5443367..53bc2a7 100644 --- a/src/btop_globs.h +++ b/src/btop_globs.h @@ -20,17 +20,18 @@ tab-size = 4 #define _btop_globs_included_ 1 #include -#include #include +#include #include +#include -using std::string, std::vector, std::map, std::atomic; +using std::string, std::vector, std::unordered_map, std::array, std::atomic; namespace Global { atomic stop_all(false); - const map Default_theme = { + const unordered_map Default_theme = { { "main_bg", "#00" }, { "main_fg", "#cc" }, { "title", "#ee" }, @@ -75,7 +76,7 @@ namespace Global { { "process_end", "#d45454" } }; - const map>> Menus = { + const unordered_map>> Menus = { { "options", { { "normal", { "┌─┐┌─┐┌┬┐┬┌─┐┌┐┌┌─┐", @@ -115,8 +116,8 @@ namespace Global { }; //? Units for floating_humanizer function - const vector Units_bit = {"bit", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib", "Bib", "GEb"}; - const vector Units_byte = {"Byte", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB", "BiB", "GEB"}; + const array Units_bit = {"bit", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib", "Bib", "GEb"}; + const array Units_byte = {"Byte", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB", "BiB", "GEB"}; } @@ -132,7 +133,7 @@ namespace Symbols { const string div_up = "┬"; const string div_down = "┴"; - const vector superscript = { "⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹" }; + const array superscript = { "⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹" }; } #endif diff --git a/src/btop_input.h b/src/btop_input.h index f712408..cf7235e 100644 --- a/src/btop_input.h +++ b/src/btop_input.h @@ -20,13 +20,13 @@ tab-size = 4 #define _btop_input_included_ 1 #include -#include +#include #include #include #include -using std::string, std::map, std::cin; +using std::string, std::unordered_map, std::cin; using namespace Tools; @@ -34,7 +34,7 @@ using namespace Tools; namespace Input { namespace { //* Map for translating key codes to readable values - const map Key_escapes = { + const unordered_map Key_escapes = { {"\033", "escape"}, {"\n", "enter"}, {" ", "space"}, @@ -69,7 +69,7 @@ namespace Input { {"[23~", "f11"}, {"[24~", "f12"} }; - }; + } //* Last entered key string last = ""; @@ -108,6 +108,6 @@ namespace Input { last.clear(); } -}; +} #endif \ No newline at end of file diff --git a/src/btop_linux.h b/src/btop_linux.h index 56a504b..8fae095 100644 --- a/src/btop_linux.h +++ b/src/btop_linux.h @@ -21,7 +21,8 @@ tab-size = 4 #include #include -#include +#include +#include #include #include #include @@ -34,7 +35,7 @@ tab-size = 4 #include -using std::string, std::vector, std::map, std::ifstream, std::atomic, std::numeric_limits, std::streamsize; +using std::string, std::vector, std::array, std::ifstream, std::atomic, std::numeric_limits, std::streamsize, std::unordered_map; namespace fs = std::filesystem; using namespace Tools; @@ -65,8 +66,8 @@ namespace Proc { string name, cmd, user; uint64_t cpu_t = 0, cpu_s = 0; }; - map cache; - map uid_user; + unordered_map cache; + unordered_map uid_user; fs::path passwd_path; fs::file_time_type passwd_time; uint counter = 0; @@ -76,7 +77,7 @@ namespace Proc { atomic stop (false); atomic running (false); - vector sort_vector = { + array sort_array = { "pid", "name", "command", @@ -86,7 +87,7 @@ namespace Proc { "cpu direct", "cpu lazy", }; - map sort_map; + unordered_map sort_map; //* proc_info: pid, name, cmd, threads, user, mem, cpu_p, cpu_c struct proc_info { @@ -146,8 +147,8 @@ namespace Proc { return procs; } pid_str = fs::path(d.path()).filename(); - cpu = 0.0; - rss_mem = 0; + cpu = 0.0; cpu_s = 0.0; cpu_t = 0; + rss_mem = 0; threads = 0; new_cache = false; if (d.is_directory() && isdigit(pid_str[0])) { pid = stoul(pid_str); @@ -284,7 +285,7 @@ namespace Proc { //* Clear dead processes from cache at a regular interval if (++counter >= 10000 || (filter.empty() && cache.size() > procs.size() + 100)) { - map r_cache; + unordered_map r_cache; counter = 0; for (auto& p : c_pids) r_cache[p] = cache[p]; cache = move(r_cache); @@ -301,9 +302,9 @@ namespace Proc { tstamp = time_ms(); passwd_path = (fs::exists(fs::path("/etc/passwd"))) ? fs::path("/etc/passwd") : passwd_path; uint i = 0; - for (auto& item : sort_vector) sort_map[item] = i++; + for (auto& item : sort_array) sort_map[item] = i++; } -}; +} diff --git a/src/btop_theme.h b/src/btop_theme.h index a8791f4..98d23eb 100644 --- a/src/btop_theme.h +++ b/src/btop_theme.h @@ -23,13 +23,14 @@ tab-size = 4 #include #include #include +#include #include #include #include #include -using std::string, std::round, std::vector, std::map, std::stoi, std::views::iota, std::array; +using std::string, std::round, std::vector, std::map, std::stoi, std::views::iota, std::array, std::unordered_map; using namespace Tools; namespace Theme { @@ -98,16 +99,16 @@ namespace Theme { else return pre + to_string(r) + ";" + to_string(g) + ";" + to_string(b) + "m"; } - //* Return a map of "r", "g", "b", 0-255 values for a 24-bit color escape string - map esc_to_rgb(string c_string){ - map rgb = {{"r", 0}, {"g", 0}, {"b", 0}}; + //* Return an array of red, green and blue, 0-255 values for a 24-bit color escape string + auto esc_to_rgb(string c_string){ + array rgb = {-1, -1, -1}; if (c_string.size() >= 14){ c_string.erase(0, 7); auto c_split = ssplit(c_string, ";"); if (c_split.size() == 3){ - rgb["r"] = stoi(c_split[0]); - rgb["g"] = stoi(c_split[1]); - rgb["b"] = stoi(c_split[2].erase(c_split[2].size())); + rgb[0] = stoi(c_split[0]); + rgb[1] = stoi(c_split[1]); + rgb[2] = stoi(c_split[2].erase(c_split[2].size())); } } return rgb; @@ -115,9 +116,9 @@ namespace Theme { namespace { - map colors; - map> rgbs; - map> gradients; + unordered_map colors; + unordered_map> rgbs; + unordered_map> gradients; //* Convert hex color to a array of decimals array hex_to_dec(string hexa){ @@ -141,7 +142,7 @@ namespace Theme { } //* Generate colors and rgb decimal vectors for the theme - void generateColors(map& source){ + void generateColors(unordered_map& source){ vector t_rgb; string depth; colors.clear(); rgbs.clear(); @@ -209,7 +210,7 @@ namespace Theme { //* Set current theme using map - void set(map source){ + void set(unordered_map source){ generateColors(source); generateGradients(); Term::fg = colors.at("main_fg"); @@ -222,16 +223,16 @@ namespace Theme { return colors.at(name); } - //* Return vector of escape codes for color gradient + //* Return array of escape codes for color gradient auto g(string name){ return gradients.at(name); } - //* Return vector of red, green and blue in decimal for color + //* Return array of red, green and blue in decimal for color auto dec(string name){ return rgbs.at(name); } -}; +} #endif \ No newline at end of file diff --git a/src/btop_tools.h b/src/btop_tools.h index a2a9f54..aad41ba 100644 --- a/src/btop_tools.h +++ b/src/btop_tools.h @@ -33,7 +33,7 @@ tab-size = 4 #include -using std::string, std::vector, std::map, std::regex, std::max, std::to_string, std::cin; +using std::string, std::vector, std::regex, std::max, std::to_string, std::cin; //? ------------------------------------------------- NAMESPACES ------------------------------------------------------ @@ -82,7 +82,7 @@ namespace Fx { string uncolor(string& s){ return regex_replace(s, color_regex, ""); } -}; +} //* Collection of escape codes and functions for cursor manipulation namespace Mv { @@ -106,7 +106,7 @@ namespace Mv { //* Restore saved cursor postion const string restore = Fx::e + "u"; -}; +} //* Collection of escape codes and functions for terminal manipulation namespace Term { @@ -138,7 +138,7 @@ namespace Term { height = w.ws_row; return resized; } - }; + } //* Hide terminal cursor @@ -214,7 +214,7 @@ namespace Term { initialized = false; } } -}; +} //? --------------------------------------------------- FUNCTIONS -----------------------------------------------------