Skip to content

Commit

Permalink
Rewrite theme access
Browse files Browse the repository at this point in the history
  • Loading branch information
lvxnull committed Oct 31, 2023
1 parent 5606705 commit 043f05c
Show file tree
Hide file tree
Showing 12 changed files with 469 additions and 271 deletions.
24 changes: 12 additions & 12 deletions src/btop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ using namespace std::chrono_literals;
using namespace std::literals;

namespace Global {
const vector<array<string, 2>> Banner_src = {
{"#E62525", "██████╗ ████████╗ ██████╗ ██████╗"},
{"#CD2121", "██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗ ██╗ ██╗"},
{"#B31D1D", "██████╔╝ ██║ ██║ ██║██████╔╝ ██████╗██████╗"},
{"#9A1919", "██╔══██╗ ██║ ██║ ██║██╔═══╝ ╚═██╔═╝╚═██╔═╝"},
{"#801414", "██████╔╝ ██║ ╚██████╔╝██║ ╚═╝ ╚═╝"},
{"#000000", "╚═════╝ ╚═╝ ╚═════╝ ╚═╝"},
};
const array<BannerLine, 6> Banner_src = {{
{ .color = 0xE62525, .text = "██████╗ ████████╗ ██████╗ ██████╗"},
{ .color = 0xCD2121, .text = "██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗ ██╗ ██╗"},
{ .color = 0xB31D1D, .text = "██████╔╝ ██║ ██║ ██║██████╔╝ ██████╗██████╗"},
{ .color = 0x9A1919, .text = "██╔══██╗ ██║ ██║ ██║██╔═══╝ ╚═██╔═╝╚═██╔═╝"},
{ .color = 0x801414, .text = "██████╔╝ ██║ ╚██████╔╝██║ ╚═╝ ╚═╝"},
{ .color = 0x000000, .text = "╚═════╝ ╚═╝ ╚═════╝ ╚═╝"},
}};
const string Version = "1.2.13";

int coreCount;
Expand Down Expand Up @@ -613,7 +613,7 @@ namespace Runner {
"{mv6}{hiFg}esc {mainFg}| Show menu"
"{mv7}{hiFg}q {mainFg}| Quit",
"banner"_a = Draw::banner_gen(y, 0, true),
"titleFg"_a = Theme::c("title"), "b"_a = Fx::b, "hiFg"_a = Theme::c("hi_fg"), "mainFg"_a = Theme::c("main_fg"),
"titleFg"_a = Theme::c().title, "b"_a = Fx::b, "hiFg"_a = Theme::c().hi_fg, "mainFg"_a = Theme::c().main_fg,
"mv1"_a = Mv::to(y+6, x),
"mv2"_a = Mv::to(y+8, x),
"mv3"_a = Mv::to(y+9, x),
Expand All @@ -629,9 +629,9 @@ namespace Runner {
//! DEBUG stats -->
if (Global::debug and not Menu::active) {
output += fmt::format("{pre}{box:5.5} {collect:>12.12} {draw:>12.12}{post}",
"pre"_a = debug_bg + Theme::c("title") + Fx::b,
"pre"_a = debug_bg + Theme::c().title + Fx::b,
"box"_a = "box", "collect"_a = "collect", "draw"_a = "draw",
"post"_a = Theme::c("main_fg") + Fx::ub
"post"_a = Theme::c().main_fg + Fx::ub
);
static auto loc = std::locale(std::locale::classic(), new MyNumPunct);
for (const string name : {"cpu", "mem", "net", "proc", "total"}) {
Expand All @@ -650,7 +650,7 @@ namespace Runner {
//? If overlay isn't empty, print output without color and then print overlay on top
cout << Term::sync_start << (conf.overlay.empty()
? output
: (output.empty() ? "" : Fx::ub + Theme::c("inactive_fg") + Fx::uncolor(output)) + conf.overlay)
: (output.empty() ? "" : Fx::ub + Theme::c().inactive_fg + Fx::uncolor(output)) + conf.overlay)
<< Term::sync_end << flush;
}
//* ----------------------------------------------- THREAD LOOP -----------------------------------------------
Expand Down
358 changes: 187 additions & 171 deletions src/btop_draw.cpp

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/btop_draw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ namespace Draw {
//* Class holding a percentage meter
class Meter {
int width;
string color_gradient;
uint32_t gradient_offset;
bool invert;
array<string, 101> cache;
public:
Meter();
Meter(const int width, const string& color_gradient, bool invert = false);
Meter(const int width, uint32_t gradient_offset, bool invert = false);

//* Return a string representation of the meter with given value
string operator()(int value);
Expand All @@ -102,7 +102,7 @@ namespace Draw {
//* Class holding a percentage graph
class Graph {
int width, height;
string color_gradient;
int32_t gradient_offset;
string out, symbol = "default";
bool invert, no_zero;
long long offset;
Expand All @@ -116,7 +116,7 @@ namespace Draw {
public:
Graph();
Graph(int width, int height,
const string& color_gradient,
const int32_t gradient_offset,
const deque<long long>& data,
const string& symbol="default",
bool invert=false, bool no_zero=false,
Expand Down
90 changes: 45 additions & 45 deletions src/btop_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ namespace Menu {
button_left = left_up + Symbols::h_line * 6 + Mv::l(7) + Mv::d(2) + left_down + Symbols::h_line * 6 + Mv::l(7) + Mv::u(1) + Symbols::v_line;
button_right = Symbols::v_line + Mv::l(7) + Mv::u(1) + Symbols::h_line * 6 + right_up + Mv::l(7) + Mv::d(2) + Symbols::h_line * 6 + right_down + Mv::u(2);

box_contents = Draw::createBox(x, y, width, height, Theme::c("hi_fg"), true, title) + Mv::d(1);
box_contents = Draw::createBox(x, y, width, height, Theme::c().hi_fg, true, title) + Mv::d(1);
for (const auto& line : content) {
box_contents += Mv::save + Mv::r(max((size_t)0, (width / 2) - (Fx::uncolor(line).size() / 2) - 1)) + line + Mv::restore + Mv::d(1);
}
Expand All @@ -716,13 +716,13 @@ namespace Menu {
string msgBox::operator()() {
string out;
int pos = width / 2 - (boxtype == 0 ? 6 : 14);
auto& first_color = (selected == 0 ? Theme::c("hi_fg") : Theme::c("div_line"));
out = Mv::d(1) + Mv::r(pos) + Fx::b + first_color + button_left + (selected == 0 ? Theme::c("title") : Theme::c("main_fg") + Fx::ub)
auto& first_color = (selected == 0 ? Theme::c().hi_fg : Theme::c().div_line);
out = Mv::d(1) + Mv::r(pos) + Fx::b + first_color + button_left + (selected == 0 ? Theme::c().title : Theme::c().main_fg + Fx::ub)
+ (boxtype == 0 ? " Ok " : " Yes ") + first_color + button_right;
mouse_mappings["button1"] = Input::Mouse_loc{y + height - 4, x + pos + 1, 3, 12 + (boxtype > 0 ? 1 : 0)};
if (boxtype > 0) {
auto& second_color = (selected == 1 ? Theme::c("hi_fg") : Theme::c("div_line"));
out += Mv::r(2) + second_color + button_left + (selected == 1 ? Theme::c("title") : Theme::c("main_fg") + Fx::ub)
auto& second_color = (selected == 1 ? Theme::c().hi_fg : Theme::c().div_line);
out += Mv::r(2) + second_color + button_left + (selected == 1 ? Theme::c().title : Theme::c().main_fg + Fx::ub)
+ " No " + second_color + button_right;
mouse_mappings["button2"] = Input::Mouse_loc{y + height - 4, x + pos + 15 + (boxtype > 0 ? 1 : 0), 3, 12};
}
Expand Down Expand Up @@ -794,8 +794,8 @@ namespace Menu {
if (redraw) {
x = Term::width/2 - 40;
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) + " ("
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);
}
else if (is_in(key, "escape", "q")) {
Expand Down Expand Up @@ -860,27 +860,27 @@ namespace Menu {

if (retval == Changed) {
int cy = y+4, cx = x+4;
out = bg + Mv::to(cy++, x+3) + Theme::c("main_fg") + Fx::ub
+ rjust("Enter signal number: ", 48) + Theme::c("hi_fg") + (selected_signal >= 0 ? to_string(selected_signal) : "") + Theme::c("main_fg") + Fx::bl + "" + Fx::ubl;
out = bg + Mv::to(cy++, x+3) + Theme::c().main_fg + Fx::ub
+ rjust("Enter signal number: ", 48) + Theme::c().hi_fg + (selected_signal >= 0 ? to_string(selected_signal) : "") + Theme::c().main_fg + Fx::bl + "" + Fx::ubl;

auto sig_str = to_string(selected_signal);
for (int count = 0, i = 0; const auto& sig : P_Signals) {
if (count == 0 or count == 16) { count++; continue; }
if (i++ % 5 == 0) { ++cy; cx = x+4; }
out += Mv::to(cy, cx);
if (count == selected_signal) out += Theme::c("selected_bg") + Theme::c("selected_fg") + Fx::b + ljust(to_string(count), 3) + ljust('(' + sig + ')', 12) + Fx::reset;
else out += Theme::c("hi_fg") + ljust(to_string(count), 3) + Theme::c("main_fg") + ljust('(' + sig + ')', 12);
if (count == selected_signal) out += Theme::c().selected_bg + Theme::c().selected_fg + Fx::b + ljust(to_string(count), 3) + ljust('(' + sig + ')', 12) + Fx::reset;
else out += Theme::c().hi_fg + ljust(to_string(count), 3) + Theme::c().main_fg + ljust('(' + sig + ')', 12);
if (redraw) mouse_mappings["button_" + to_string(count)] = {cy, cx, 1, 15};
count++;
cx += 15;
}

cy++;
out += Mv::to(++cy, x+3) + Fx::b + Theme::c("hi_fg") + rjust( "↑ ↓ ← →", 33, true) + Theme::c("main_fg") + Fx::ub + " | To choose signal.";
out += Mv::to(++cy, x+3) + Fx::b + Theme::c("hi_fg") + rjust("0-9", 33) + Theme::c("main_fg") + Fx::ub + " | Enter manually.";
out += Mv::to(++cy, x+3) + Fx::b + Theme::c("hi_fg") + rjust("ENTER", 33) + Theme::c("main_fg") + Fx::ub + " | To send signal.";
out += Mv::to(++cy, x+3) + Fx::b + Theme::c().hi_fg + rjust( "↑ ↓ ← →", 33, true) + Theme::c().main_fg + Fx::ub + " | To choose signal.";
out += Mv::to(++cy, x+3) + Fx::b + Theme::c().hi_fg + rjust("0-9", 33) + Theme::c().main_fg + Fx::ub + " | Enter manually.";
out += Mv::to(++cy, x+3) + Fx::b + Theme::c().hi_fg + rjust("ENTER", 33) + Theme::c().main_fg + Fx::ub + " | To send signal.";
mouse_mappings["enter"] = {cy, x, 1, 73};
out += Mv::to(++cy, x+3) + Fx::b + Theme::c("hi_fg") + rjust("ESC or \"q\"", 33) + Theme::c("main_fg") + Fx::ub + " | To abort.";
out += Mv::to(++cy, x+3) + Fx::b + Theme::c().hi_fg + rjust("ESC or \"q\"", 33) + Theme::c().main_fg + Fx::ub + " | To abort.";
mouse_mappings["escape"] = {cy, x, 1, 73};

out += Fx::reset;
Expand All @@ -892,7 +892,7 @@ namespace Menu {
int sizeError(const string& key) {
if (redraw) {
vector<string> cont_vec;
cont_vec.push_back(Fx::b + Theme::g("used")[100] + "Error:" + Theme::c("main_fg") + Fx::ub);
cont_vec.push_back(Fx::b + Theme::g().used[100] + "Error:" + Theme::c().main_fg + Fx::ub);
cont_vec.push_back("Terminal size to small to" + Fx::reset);
cont_vec.push_back("display menu or box!" + Fx::reset);

Expand All @@ -918,10 +918,10 @@ namespace Menu {
atomic_wait(Runner::active);
auto& p_name = (s_pid == Config::getI("detailed_pid") ? Proc::detailed.entry.name : Config::getS("selected_name"));
vector<string> 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) + ')' : ""),
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) + ')' : ""),

Fx::b + Theme::c("main_fg") + "To PID: " + Fx::ub + Theme::c("hi_fg") + to_string(s_pid) + Theme::c("main_fg") + " ("
Fx::b + Theme::c().main_fg + "To PID: " + Fx::ub + Theme::c().hi_fg + to_string(s_pid) + Theme::c().main_fg + " ("
+ uresize(p_name, 16) + ')' + Fx::reset,
};
messageBox = Menu::msgBox{50, 1, cont_vec, (signalToSend > 1 and signalToSend <= 32 and signalToSend != 17 ? P_Signals.at(signalToSend) : "signal")};
Expand Down Expand Up @@ -954,7 +954,7 @@ namespace Menu {
int signalReturn(const string& key) {
if (redraw) {
vector<string> cont_vec;
cont_vec.push_back(Fx::b + Theme::g("used")[100] + "Failure:" + Theme::c("main_fg") + Fx::ub);
cont_vec.push_back(Fx::b + Theme::g().used[100] + "Failure:" + Theme::c().main_fg + Fx::ub);
if (signalKillRet == EINVAL) {
cont_vec.push_back("Unsupported signal!" + Fx::reset);
}
Expand Down Expand Up @@ -998,14 +998,14 @@ namespace Menu {
bg = Draw::banner_gen(y, 0, true);
if (not tty_mode) {
colors_selected = {
Theme::hex_to_color(Global::Banner_src.at(0).at(0)),
Theme::hex_to_color(Global::Banner_src.at(2).at(0)),
Theme::hex_to_color(Global::Banner_src.at(4).at(0))
Theme::color_to_escape(Global::Banner_src[0].color),
Theme::color_to_escape(Global::Banner_src[2].color),
Theme::color_to_escape(Global::Banner_src[4].color),
};
colors_normal = {
Theme::hex_to_color("#CC"),
Theme::hex_to_color("#AA"),
Theme::hex_to_color("#80")
Theme::color_to_escape(0xCCCCCC),
Theme::color_to_escape(0xAAAAAA),
Theme::color_to_escape(0x808080),
};
}
}
Expand Down Expand Up @@ -1048,7 +1048,7 @@ namespace Menu {
out = bg + Fx::reset + Fx::b;
auto cy = y + 7;
for (const auto& i : iota(0, 3)) {
if (tty_mode) out += (i == selected ? Theme::c("hi_fg") : Theme::c("main_fg"));
if (tty_mode) out += (i == selected ? Theme::c().hi_fg : Theme::c().main_fg);
const auto& menu = (not tty_mode and i == selected ? menu_selected[i] : menu_normal[i]);
const auto& colors = (i == selected ? colors_selected : colors_normal);
if (redraw) mouse_mappings["button_" + to_string(i)] = {cy, Term::width/2 - menu_width[i]/2, 3, menu_width[i]};
Expand Down Expand Up @@ -1120,10 +1120,10 @@ namespace Menu {
height = min(Term::height - 7, max_items * 2 + 4);
if (height % 2 != 0) height--;
bg = Draw::banner_gen(y, 0, true)
+ Draw::createBox(x, y + 6, 78, height, Theme::c("hi_fg"), true, "tab" + Symbols::right)
+ Mv::to(y+8, x) + Theme::c("hi_fg") + Symbols::div_left + Theme::c("div_line") + Symbols::h_line * 29
+ Symbols::div_up + Symbols::h_line * (78 - 32) + Theme::c("hi_fg") + Symbols::div_right
+ Mv::to(y+6+height - 1, x+30) + Symbols::div_down + Theme::c("div_line");
+ Draw::createBox(x, y + 6, 78, height, Theme::c().hi_fg, true, "tab" + Symbols::right)
+ Mv::to(y+8, x) + Theme::c().hi_fg + Symbols::div_left + Theme::c().div_line + Symbols::h_line * 29
+ Symbols::div_up + Symbols::h_line * (78 - 32) + Theme::c().hi_fg + Symbols::div_right
+ Mv::to(y+6+height - 1, x+30) + Symbols::div_down + Theme::c().div_line;
for (const auto& i : iota(0, height - 4)) {
bg += Mv::to(y+9 + i, x + 30) + Symbols::v_line;
}
Expand Down Expand Up @@ -1325,29 +1325,29 @@ namespace Menu {
out += Mv::to(y+7, x+4);
for (int i = 0; const auto& m : {"general", "cpu", "mem", "net", "proc"}) {
out += Fx::b + (i == selected_cat
? Theme::c("hi_fg") + '[' + Theme::c("title") + m + Theme::c("hi_fg") + ']'
: Theme::c("hi_fg") + to_string(i + 1) + Theme::c("title") + m + ' ')
? Theme::c().hi_fg + '[' + Theme::c().title + m + Theme::c().hi_fg + ']'
: Theme::c().hi_fg + to_string(i + 1) + Theme::c().title + m + ' ')
+ Mv::r(10);
if (string button_name = "select_cat_" + to_string(i + 1); not editing and not mouse_mappings.contains(button_name))
mouse_mappings[button_name] = {y+6, x+2 + 15*i, 3, 15};
i++;
}
if (pages > 1) {
out += Mv::to(y+6 + height - 1, x+2) + Theme::c("hi_fg") + Symbols::title_left_down + Fx::b + Symbols::up + Theme::c("title") + " page "
+ to_string(page+1) + '/' + to_string(pages) + ' ' + Theme::c("hi_fg") + Symbols::down + Fx::ub + Symbols::title_right_down;
out += Mv::to(y+6 + height - 1, x+2) + Theme::c().hi_fg + Symbols::title_left_down + Fx::b + Symbols::up + Theme::c().title + " page "
+ to_string(page+1) + '/' + to_string(pages) + ' ' + Theme::c().hi_fg + Symbols::down + Fx::ub + Symbols::title_right_down;
}
//? Option name and value
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));

out += Mv::to(cy++, x + 1) + (c-1 == selected ? Theme::c("selected_bg") + Theme::c("selected_fg") : Theme::c("title"))
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())
: ""), 29);
out += Mv::to(cy++, x + 1) + (c-1 == selected ? "" : Theme::c("main_fg")) + Fx::ub + " "
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)) + " ";

if (c-1 == selected) {
Expand All @@ -1360,10 +1360,10 @@ namespace Menu {
out += Fx::b + Mv::to(cy-1, x+28 - (not editing and selPred.test(isInt) ? 2 : 0)) + (tty_mode ? "E" : Symbols::enter);
}
//? Description of selected option
out += Fx::reset + Theme::c("title") + Fx::b;
out += Fx::reset + Theme::c().title + Fx::b;
for (int cyy = y+7; const auto& desc : categories[selected_cat][i]) {
if (cyy++ == y+7) continue;
else if (cyy == y+10) out += Theme::c("main_fg") + Fx::ub;
else if (cyy == y+10) out += Theme::c().main_fg + Fx::ub;
else if (cyy > y + height + 4) break;
out += Mv::to(cyy, x+32) + desc;
}
Expand Down Expand Up @@ -1418,7 +1418,7 @@ namespace Menu {
pages = ceil((double)help_text.size() / (height - 3));
page = 0;
bg = Draw::banner_gen(y, 0, true);
bg += Draw::createBox(x, y + 6, 78, height, Theme::c("hi_fg"), true, "help");
bg += Draw::createBox(x, y + 6, 78, height, Theme::c().hi_fg, true, "help");
}
else if (is_in(key, "escape", "q", "h", "backspace", "space", "enter", "mouse_click")) {
return Closed;
Expand All @@ -1438,14 +1438,14 @@ namespace Menu {
auto& out = Global::overlay;
out = bg;
if (pages > 1) {
out += Mv::to(y+height+6, x + 2) + Theme::c("hi_fg") + Symbols::title_left_down + Fx::b + Symbols::up + Theme::c("title") + " page "
+ to_string(page+1) + '/' + to_string(pages) + ' ' + Theme::c("hi_fg") + Symbols::down + Fx::ub + Symbols::title_right_down;
out += Mv::to(y+height+6, x + 2) + Theme::c().hi_fg + Symbols::title_left_down + Fx::b + Symbols::up + Theme::c().title + " page "
+ to_string(page+1) + '/' + to_string(pages) + ' ' + Theme::c().hi_fg + Symbols::down + Fx::ub + Symbols::title_right_down;
}
auto cy = y+7;
out += Mv::to(cy++, x + 1) + Theme::c("title") + Fx::b + cjust("Key:", 20) + "Description:";
out += Mv::to(cy++, x + 1) + Theme::c().title + Fx::b + cjust("Key:", 20) + "Description:";
for (int c = 0, i = max(0, (height - 3) * page); c++ < height - 3 and i < (int)help_text.size(); i++) {
out += Mv::to(cy++, x + 1) + Theme::c("hi_fg") + Fx::b + cjust(help_text[i][0], 20)
+ Theme::c("main_fg") + Fx::ub + help_text[i][1];
out += Mv::to(cy++, x + 1) + Theme::c().hi_fg + Fx::b + cjust(help_text[i][0], 20)
+ Theme::c().main_fg + Fx::ub + help_text[i][1];
}
out += Fx::reset;
}
Expand Down
Loading

0 comments on commit 043f05c

Please sign in to comment.