Skip to content

Commit 0a4ea8b

Browse files
committed
Variable width proc box
This PR adds keybinds for Shift+Left, Shift+Right, Ctrl+Shift+Left, Ctrl+Shift+Right, and Ctrl+Shift+Down. Shift+Left makes the proc box wider by 1% if it is on the right and narrower by 1% if it is on the left. Shift+Right makes the proc box narrower by 1% if it on the right and wider by 1% if is on the left Alt+Shift+Left makes the proc box wider by 10% if it is on the right and narrower by 10% if it is on the left.l Alt+Shift+Right makes the proc box narrower by 10% if it on the right and wider by 10% if is on the left Ctrl+Shift+Left sets the proc box width to its maximum if on the right and minimum if on the left. Ctrl+Shift+Right sets the proc box width to its minimum if on the right and maximum if on the left. Ctrl+Shift+Down sets the proc box width to the original default from before this PR (55%)
1 parent 5521ba3 commit 0a4ea8b

6 files changed

Lines changed: 74 additions & 3 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,9 @@ proc_cpu_graphs = true
14001400
#* Use /proc/[pid]/smaps for memory information in the process info box (very slow but more accurate)
14011401
proc_info_smaps = false
14021402
1403+
#* Percentage value for proc box width when mem or net is shown.
1404+
proc_box_width_percent = 55
1405+
14031406
#* Show proc box on left side of screen instead of right.
14041407
proc_left = false
14051408

src/btop_config.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ namespace Config {
116116

117117
{"proc_info_smaps", "#* Use /proc/[pid]/smaps for memory information in the process info box (very slow but more accurate)"},
118118

119+
{"proc_box_width_percent", "#* Percentage value for proc box width when mem or net is shown."},
120+
119121
{"proc_left", "#* Show proc box on left side of screen instead of right."},
120122

121123
{"proc_filter_kernel", "#* (Linux) Filter processes tied to the Linux kernel(similar behavior to htop)."},
@@ -365,6 +367,7 @@ namespace Config {
365367
{"proc_selected", 0},
366368
{"proc_last_selected", 0},
367369
{"proc_followed", 0},
370+
{"proc_box_width_percent", Proc::width_p},
368371
};
369372
std::unordered_map<std::string_view, int> intsTmp;
370373

src/btop_draw.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,6 +2316,7 @@ namespace Draw {
23162316
b_width - (Config::getB("show_cpu_freq") and hasCpuHz ? (freq_range ? 24 : 14) : 5)
23172317
);
23182318
box += createBox(b_x, b_y, b_width, b_height, "", false, cpu_title);
2319+
update_clock(true);
23192320
}
23202321

23212322
#ifdef GPU_SUPPORT
@@ -2376,7 +2377,8 @@ namespace Draw {
23762377
auto swap_disk = Config::getB("swap_disk");
23772378
auto mem_graphs = Config::getB("mem_graphs");
23782379

2379-
width = round((double)Term::width * (Proc::shown ? width_p : 100) / 100);
2380+
const int new_width = std::round(static_cast<double>(Term::width) * (100 - Config::getI("proc_box_width_percent")) / 100);
2381+
width = Proc::shown ? std::clamp(new_width, min_width, Term::width - Proc::min_width) : static_cast<int>(Term::width);
23802382
#ifdef GPU_SUPPORT
23812383
height = ceil((double)Term::height * (100 - Net::height_p * Net::shown*4 / ((Gpu::shown != 0 and Cpu::shown) + 4)) / 100) - Cpu::height - Gpu::total_height;
23822384
#else
@@ -2440,7 +2442,8 @@ namespace Draw {
24402442
//* Calculate and draw net box outlines
24412443
if (Net::shown) {
24422444
using namespace Net;
2443-
width = round((double)Term::width * (Proc::shown ? width_p : 100) / 100);
2445+
const int new_width = std::round(static_cast<double>(Term::width) * (100 - Config::getI("proc_box_width_percent")) / 100);
2446+
width = Proc::shown ? std::clamp(new_width, min_width, Term::width - Proc::min_width) : static_cast<int>(Term::width);
24442447
#ifdef GPU_SUPPORT
24452448
height = Term::height - Cpu::height - Gpu::total_height - Mem::height;
24462449
#else

src/btop_input.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,17 @@ namespace Input {
5252
{"OA", "up"},
5353
{"[B", "down"},
5454
{"OB", "down"},
55+
{"[1;6B", "ctrl_shift_down"},
5556
{"[D", "left"},
5657
{"OD", "left"},
58+
{"[1;2D", "shift_left"},
59+
{"[1;4D", "alt_shift_left"},
60+
{"[1;6D", "ctrl_shift_left"},
5761
{"[C", "right"},
5862
{"OC", "right"},
63+
{"[1;2C", "shift_right"},
64+
{"[1;4C", "alt_shift_right"},
65+
{"[1;6C", "ctrl_shift_right"},
5966
{"[2~", "insert"},
6067
{"[4h", "insert"},
6168
{"[3~", "delete"},
@@ -275,6 +282,33 @@ namespace Input {
275282
Draw::calcSizes();
276283
Runner::run("all", false, true);
277284
return;
285+
}
286+
else if (is_in(key, "shift_left", "shift_right", "alt_shift_left", "alt_shift_right", "ctrl_shift_left", "ctrl_shift_right", "ctrl_shift_down")) {
287+
if (Proc::shown and (Mem::shown or Net::shown)) {
288+
const int proc_box_width_percent = Config::getI("proc_box_width_percent");
289+
int new_proc_box_width_percent = proc_box_width_percent;
290+
const int min_p = std::round((static_cast<double>(Proc::min_width) / Term::width) * 100);
291+
const int max_p = std::round(100 - ((static_cast<double>(Mem::shown ? Mem::min_width : Net::min_width) / Term::width) * 100));
292+
int offset = key.at(0) == 'a' ? (min_p + 10 <= max_p ? 10 : max_p - min_p) : 1;
293+
if (is_in(key, "shift_left", "alt_shift_left"))
294+
new_proc_box_width_percent = Config::getB("proc_left") ? std::clamp(proc_box_width_percent - offset, min_p, max_p - offset) : std::clamp(proc_box_width_percent + offset, min_p + offset, max_p);
295+
else if (is_in(key, "shift_right", "alt_shift_right"))
296+
new_proc_box_width_percent = Config::getB("proc_left") ? std::clamp(proc_box_width_percent + offset, min_p + offset, max_p) : std::clamp(proc_box_width_percent - offset, min_p, max_p - offset);
297+
else if (key == "ctrl_shift_left")
298+
new_proc_box_width_percent = Config::getB("proc_left") ? min_p : max_p;
299+
else if (key == "ctrl_shift_right")
300+
new_proc_box_width_percent = Config::getB("proc_left") ? max_p : min_p;
301+
else
302+
new_proc_box_width_percent = Proc::width_p; // Default width
303+
304+
if (proc_box_width_percent != new_proc_box_width_percent) {
305+
atomic_wait(Runner::active);
306+
Config::set("proc_box_width_percent", new_proc_box_width_percent);
307+
Draw::calcSizes();
308+
Runner::run("all", true, true);
309+
}
310+
}
311+
return;
278312
} else if (is_in(key, "ctrl_r")) {
279313
kill(getpid(), SIGUSR2);
280314
return;

src/btop_menu.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ namespace Menu {
211211
{"Selected k", "Kill selected process with SIGKILL - 9."},
212212
{"Selected s", "Select or enter signal to send to process."},
213213
{"Selected N", "Select new nice value for selected process."},
214+
{"Shift + Left", "Adjust the proc box width by 1% when mem or net shown."},
215+
{"Shift + Right", "Adjust the proc box width by 1% when mem or net shown."},
216+
{"Alt+Shift+Left", "Adjust the proc box width by 10% when mem or net shown."},
217+
{"Alt+Shift+Right", "Adjust the proc box width by 10% when mem or net shown."},
218+
{"Ctrl+Shift+Left", "Set the proc box to max width or min for proc left."},
219+
{"Ctrl+Shift+Right", "Set the proc box to min width or max for proc left."},
220+
{"Ctrl+Shift+Down", fmt::format("Reset the proc box width percentage to {}% (default).", Proc::width_p)},
214221
{"", " "},
215222
{"", "For bug reporting and project updates, visit:"},
216223
{"", "https://github.com/aristocratos/btop"},
@@ -790,6 +797,17 @@ namespace Menu {
790797
"True, False, or Auto",},
791798
},
792799
{
800+
{"proc_box_width_percent",
801+
"Proc box width percentage",
802+
"",
803+
"Set the width of the proc box as a",
804+
"percentage when the mem or net boxes",
805+
"are shown.",
806+
"",
807+
"A value larger or smaller then the",
808+
"maximum or minimum width for the the",
809+
"window size will set the width to the",
810+
"widest or narrowest that it can."},
793811
{"proc_left",
794812
"Proc box location.",
795813
"",
@@ -1356,6 +1374,10 @@ static int optionsMenu(const string& key) {
13561374
}
13571375
}
13581376
else if (selPred.test(isInt) and Config::intValid(option, editor.text)) {
1377+
if (option == "proc_box_width_percent") {
1378+
editor.text = fmt::format("{}", std::clamp(stoi(editor.text), 0, 100));
1379+
screen_redraw = true;
1380+
}
13591381
Config::set(option, stoi(editor.text));
13601382
}
13611383
else
@@ -1440,8 +1462,13 @@ static int optionsMenu(const string& key) {
14401462
if (key == "right" or (vim_keys and key == "l")) value += mod;
14411463
else value -= mod;
14421464

1443-
if (Config::intValid(option, to_string(value)))
1465+
if (Config::intValid(option, to_string(value))) {
1466+
if (option == "proc_box_width_percent") {
1467+
value = std::clamp(static_cast<int>(value), 0, 100);
1468+
screen_redraw = true;
1469+
}
14441470
Config::set(option, static_cast<int>(value));
1471+
}
14451472
else {
14461473
warnings = Config::validError;
14471474
}

src/btop_shared.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ namespace Proc {
350350

351351
extern string box;
352352
extern int x, y, width, height, min_width, min_height;
353+
extern int width_p;
353354
extern bool shown, redraw;
354355
extern int select_max;
355356
extern atomic<int> detailed_pid;

0 commit comments

Comments
 (0)