Skip to content

Commit f1482fe

Browse files
committed
Fix process arguments appearing outside proc box by replacing ASCII control codes with blankspace, issue #1080
1 parent f46a133 commit f1482fe

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/btop_draw.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,10 +1658,11 @@ namespace Proc {
16581658
out += Mv::to(d_y + 5 + i++, d_x + 1) + l;
16591659

16601660
out += Theme::c("main_fg") + Fx::ub;
1661-
const int cmd_size = ulen(detailed.entry.cmd, true);
1661+
const auto san_cmd = replace_ascii_control(detailed.entry.cmd);
1662+
const int cmd_size = ulen(san_cmd, true);
16621663
for (int num_lines = min(3, (int)ceil((double)cmd_size / (d_width - 5))), i = 0; i < num_lines; i++) {
16631664
out += Mv::to(d_y + 5 + (num_lines == 1 ? 1 : i), d_x + 3)
1664-
+ cjust(luresize(detailed.entry.cmd, cmd_size - (d_width - 5) * i, true), d_width - 5, true, true);
1665+
+ cjust(luresize(san_cmd, cmd_size - (d_width - 5) * i, true), d_width - 5, true, true);
16651666
}
16661667

16671668
}
@@ -1855,14 +1856,16 @@ namespace Proc {
18551856
}
18561857
}
18571858

1858-
if (not p_wide_cmd.contains(p.pid)) p_wide_cmd[p.pid] = ulen(p.cmd) != ulen(p.cmd, true);
1859+
const auto san_cmd = replace_ascii_control(p.cmd);
1860+
1861+
if (not p_wide_cmd.contains(p.pid)) p_wide_cmd[p.pid] = ulen(san_cmd) != ulen(san_cmd, true);
18591862

18601863
//? Normal view line
18611864
if (not proc_tree) {
18621865
out += Mv::to(y+2+lc, x+1)
18631866
+ g_color + rjust(to_string(p.pid), 8) + ' '
18641867
+ c_color + ljust(p.name, prog_size, true) + ' ' + end
1865-
+ (cmd_size > 0 ? g_color + ljust(p.cmd, cmd_size, true, p_wide_cmd[p.pid]) + Mv::to(y+2+lc, x+11+prog_size+cmd_size) + ' ' : "");
1868+
+ (cmd_size > 0 ? g_color + ljust(san_cmd, cmd_size, true, p_wide_cmd[p.pid]) + Mv::to(y+2+lc, x+11+prog_size+cmd_size) + ' ' : "");
18661869
}
18671870
//? Tree view line
18681871
else {
@@ -1875,7 +1878,7 @@ namespace Proc {
18751878
width_left -= (ulen(p.name) + 1);
18761879
}
18771880
if (width_left > 7) {
1878-
const string_view cmd = width_left > 40 ? rtrim(p.cmd) : p.short_cmd;
1881+
const string_view cmd = width_left > 40 ? rtrim(san_cmd) : p.short_cmd;
18791882
if (not cmd.empty() and cmd != p.name) {
18801883
out += g_color + '(' + uresize(string{cmd}, width_left - 3, p_wide_cmd[p.pid]) + ") ";
18811884
width_left -= (ulen(string{cmd}, true) + 3);

src/btop_tools.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ namespace Tools {
224224
//* Replace <from> in <str> with <to> and return new string
225225
string s_replace(const string& str, const string& from, const string& to);
226226

227+
//* Replace ascii control characters with <replacement> in <str> and return new string
228+
inline string replace_ascii_control(string str, const char replacement = ' ') {
229+
std::ranges::for_each(str, [&replacement](char& c) { if (c < 0x20) c = replacement; });
230+
return str;
231+
}
232+
227233
//* Capitalize <str>
228234
inline string capitalize(string str) {
229235
str.at(0) = toupper(str.at(0));

0 commit comments

Comments
 (0)