From 9b837535bda6f64467a794bb2b9925d67a903b61 Mon Sep 17 00:00:00 2001 From: aristocratos Date: Tue, 18 May 2021 01:16:22 +0200 Subject: [PATCH] Switch from vectors to arrays when size is known --- btop.cpp | 39 ++++++++++++++++++++--------------- src/btop_draw.h | 10 ++++++--- src/btop_theme.h | 53 ++++++++++++++++++++++++------------------------ src/btop_tools.h | 2 +- 4 files changed, 58 insertions(+), 46 deletions(-) diff --git a/btop.cpp b/btop.cpp index 44e76e1..d6c2644 100644 --- a/btop.cpp +++ b/btop.cpp @@ -189,19 +189,17 @@ int main(int argc, char **argv){ cout << Theme::c("main_fg") << Theme::c("main_bg") << Term::clear << endl; cout << Mv::r(Term::width / 2 - Global::banner_width / 2) << Global::banner << endl; - cout << string(Term::width - 1, '-') << endl; - - - //* Test boxes - if (true){ - cout << Box::draw(Box::Conf(10, 5, 50, 10, Theme::c("title"), "testing", "testagain", true, 7)) << Mv::d(12) << endl; - exit(0); + // cout << string(Term::width - 1, '-') << endl; + int ill; + for (int i : iota(0, (int)Term::width)){ + ill = (i <= (int)Term::width / 2) ? i : ill - 1; + cout << Theme::g("used")[ill] << "-"; } - + cout << Fx::reset << endl; //* Test theme - if (false) { + if (true) { cout << "Theme generation took " << time_ms() - thts << "ms" << endl; cout << "Colors:" << endl; @@ -287,11 +285,20 @@ int main(int argc, char **argv){ vector sorting; bool reversing = false; int sortint = Proc::sort_map["cpu lazy"]; + vector greyscale; string filter; string filter_cur; string key; - cout << rjust("Pid:", 8) << " " << ljust("Program:", 16) << " " << ljust("Command:", Term::width - 69) << " Threads: " << - ljust("User:", 10) << " " << rjust("MemB", 5) << " " << rjust("Cpu%", 14) << "\n" << Mv::save << flush; + + int xc; + for (uint i : iota(0, (int)Term::height - 19)){ + xc = 230 - i * 150 / (Term::height - 20); + greyscale.push_back(Theme::dec_to_color(xc, xc, xc)); + } + + string pbox = Box::draw(Box::Conf(0, 10, Term::width, Term::height - 16, Theme::c("proc_box"), "testbox", "below", true, 7)); + pbox += rjust("Pid:", 8) + " " + ljust("Program:", 16) + " " + ljust("Command:", Term::width - 69) + " Threads: " + + ljust("User:", 10) + " " + rjust("MemB", 5) + " " + rjust("Cpu%", 14) + "\n"; while (key != "q") { timestamp = time_ms(); @@ -302,18 +309,18 @@ int main(int argc, char **argv){ ostring.clear(); lc = 0; filter_cur = (filtering) ? Fx::bl + "█" + Fx::reset : ""; - cout << Mv::restore << Mv::u(2) << Mv::r(20) << rjust("Filter: " + filter + filter_cur + string(Term::width / 3, ' ') + - "Sorting: " + Proc::sort_vector[sortint], Term::width - 22, true, filtering) << Mv::restore << flush; + 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; for (Proc::proc_info& procs : plist){ - ostring += rjust(to_string(procs.pid), 8) + " " + ljust(procs.name, 16) + " " + ljust(procs.cmd, Term::width - 66, true) + " " + + 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); ostring += "\n"; - if (lc++ > Term::height - 20) break; + if (lc++ > Term::height - 21) break; } - cout << Mv::restore << ostring << Term::clear_end << endl; + cout << pbox << ostring << Fx::reset << "\n" << endl; cout << "Processes call took: " << timestamp << "ms. Drawing took: " << time_ms() - timestamp2 << "ms." << endl; while (time_ms() < tsl) { diff --git a/src/btop_draw.h b/src/btop_draw.h index de2e897..d17bdd6 100644 --- a/src/btop_draw.h +++ b/src/btop_draw.h @@ -64,17 +64,21 @@ namespace Box { //* Draw corners out += Mv::to(c.y, c.x) + Symbols::left_up + - Mv::to(c.y, c.x + c.width - 1) + Symbols::right_up + + Mv::to(c.y, c.x + c.width) + Symbols::right_up + Mv::to(c.y + c.height - 1, c.x) + Symbols::left_down + - Mv::to(c.y + c.height - 1, c.x + c.width - 1) + Symbols::right_down; + Mv::to(c.y + c.height - 1, c.x + c.width) + Symbols::right_down; //* Draw titles if defined if (!c.title.empty()){ out += Mv::to(c.y, c.x + 2) + Symbols::title_left + Fx::b + numbering + Theme::c("title") + c.title + Fx::ub + lcolor + Symbols::title_right; } + if (!c.title2.empty()){ + out += Mv::to(c.y + c.height - 1, c.x + 2) + Symbols::title_left + Theme::c("title") + c.title2 + + Fx::ub + lcolor + Symbols::title_right; + } - return out + Fx::reset + Mv::to(c.y + 1, c.x + 1); + return out + Fx::reset + Mv::to(c.y + 1, c.x + 2); } } diff --git a/src/btop_theme.h b/src/btop_theme.h index d0f4854..a8791f4 100644 --- a/src/btop_theme.h +++ b/src/btop_theme.h @@ -29,7 +29,7 @@ tab-size = 4 #include #include -using std::string, std::round, std::vector, std::map, std::stoi, std::views::iota; +using std::string, std::round, std::vector, std::map, std::stoi, std::views::iota, std::array; using namespace Tools; namespace Theme { @@ -116,28 +116,28 @@ namespace Theme { namespace { map colors; - map> rgbs; - map> gradients; + map> rgbs; + map> gradients; - //* Convert hex color to a vector of decimals - vector hex_to_dec(string hexa){ + //* Convert hex color to a array of decimals + array hex_to_dec(string hexa){ if (hexa.size() > 1){ hexa.erase(0, 1); - for (auto& c : hexa) if (!isxdigit(c)) return vector{-1, -1, -1}; + for (auto& c : hexa) if (!isxdigit(c)) return array{-1, -1, -1}; if (hexa.size() == 2){ int h_int = stoi(hexa, 0, 16); - return vector{h_int, h_int, h_int}; + return array{h_int, h_int, h_int}; } else if (hexa.size() == 6){ - return vector{ + return array{ stoi(hexa.substr(0, 2), 0, 16), stoi(hexa.substr(2, 2), 0, 16), stoi(hexa.substr(4, 2), 0, 16) }; } } - return vector{-1 ,-1 ,-1}; + return array{-1 ,-1 ,-1}; } //* Generate colors and rgb decimal vectors for the theme @@ -155,13 +155,13 @@ namespace Theme { else { t_rgb = ssplit(source.at(name), " "); colors[name] = dec_to_color(stoi(t_rgb[0]), stoi(t_rgb[1]), stoi(t_rgb[2]), !Config::getB("truecolor"), depth); - rgbs[name] = vector{stoi(t_rgb[0]), stoi(t_rgb[1]), stoi(t_rgb[2])}; + rgbs[name] = array{stoi(t_rgb[0]), stoi(t_rgb[1]), stoi(t_rgb[2])}; } } else colors[name] = ""; if (colors[name].empty()) { colors[name] = hex_to_color(color, !Config::getB("truecolor"), depth); - rgbs[name] = vector{-1, -1, -1}; + rgbs[name] = array{-1, -1, -1}; } } } @@ -169,37 +169,38 @@ namespace Theme { //* Generate color gradients from one, two or three colors, 101 values indexed 0-100 void generateGradients(){ gradients.clear(); - vector c_gradient; + array c_gradient; string wname; - vector> rgb_vec; - map> dec_map; - int f, s, r, o; - for (auto& [name, s_vector] : rgbs){ - dec_map.clear(); c_gradient.clear(); + array, 3> rgb_arr; + array, 101> dec_arr; + int f, s, r, o, y; + for (auto& [name, source_arr] : rgbs){ if (!name.ends_with("_start")) continue; + dec_arr[0][0] = -1; wname = rtrim(name, "_start"); - rgb_vec = {s_vector, rgbs[wname + "_mid"], rgbs[wname + "_end"]}; + rgb_arr = {source_arr, rgbs[wname + "_mid"], rgbs[wname + "_end"]}; //? Only start iteration if gradient has a _end color value defined - if (rgb_vec[2][0] >= 0) { + if (rgb_arr[2][0] >= 0) { //? Split iteration in two passes of 50 + 51 instead of 101 if gradient has _start, _mid and _end values defined - r = (rgb_vec[1][0] >= 0) ? 50 : 100; + r = (rgb_arr[1][0] >= 0) ? 50 : 100; for (int i : iota(0, 3)){ f = 0; s = (r == 50) ? 1 : 2; o = 0; for (int c : iota(0, 101)){ - dec_map[c].push_back(rgb_vec[f][i] + (c - o) * (rgb_vec[s][i] - rgb_vec[f][i]) / r); + dec_arr[c][i] = rgb_arr[f][i] + (c - o) * (rgb_arr[s][i] - rgb_arr[f][i]) / r; - //? Switch source vectors from _start/_mid to _mid/_end at 50 passes if _mid is defined + //? Switch source arrays from _start/_mid to _mid/_end at 50 passes if _mid is defined if (c == r) { ++f; ++s; o = 50;} } } } - if (!dec_map.empty()) { - for (auto& vec : dec_map) c_gradient.push_back(dec_to_color(vec.second[0], vec.second[1], vec.second[2], !Config::getB("truecolor"))); + y = 0; + if (dec_arr[0][0] != -1) { + for (auto& vec : dec_arr) c_gradient[y++] = dec_to_color(vec[0], vec[1], vec[2], !Config::getB("truecolor")); } else { - //? If only _start was defined create vector of 101 copies of _start color - c_gradient = vector(101, colors[name]); + //? If only _start was defined fill array with _start color + c_gradient.fill(colors[name]); } gradients[wname] = c_gradient; } diff --git a/src/btop_tools.h b/src/btop_tools.h index 90fabfe..a2a9f54 100644 --- a/src/btop_tools.h +++ b/src/btop_tools.h @@ -331,7 +331,7 @@ namespace Tools { newstr.append(Mv::r(pos)); str.erase(0, pos); } - return (newstr.empty()) ? str : newstr; + return (newstr.empty()) ? str : newstr + str; } //* Convert seconds to format "Xd HH:MM:SS" and return string