Skip to content

Commit

Permalink
Correct process tree
Browse files Browse the repository at this point in the history
  • Loading branch information
Denizantip committed Oct 5, 2024
1 parent 2e7208d commit 1cae315
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 53 deletions.
30 changes: 17 additions & 13 deletions src/btop_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ namespace Proc {
}

void _tree_gen(proc_info& cur_proc, vector<proc_info>& in_procs, vector<tree_proc>& out_procs,
int cur_depth, bool collapsed, const string& filter, bool found, bool no_update, bool should_filter) {
auto cur_pos = out_procs.size();
int cur_depth, bool collapsed, const string &filter, bool found, bool no_update, bool should_filter) {
bool filtering = false;

//? If filtering, include children of matching processes
Expand Down Expand Up @@ -191,17 +190,22 @@ namespace Proc {
cur_proc.threads += p.threads;
}
}
if (collapsed or filtering) {
return;
}

//? Add tree terminator symbol if it's the last child in a sub-tree
if (out_procs.back().children.size() > 0 and out_procs.back().children.back().entry.get().prefix.size() >= 8 and not out_procs.back().children.back().entry.get().prefix.ends_with("]─"))
out_procs.back().children.back().entry.get().prefix.replace(out_procs.back().children.back().entry.get().prefix.size() - 8, 8, " └─ ");

//? Add collapse/expand symbols if process have any children
out_procs.at(cur_pos).entry.get().prefix = ""s * cur_depth + (out_procs.at(cur_pos).children.size() > 0 ? (cur_proc.collapsed ? "[+]─" : "[-]─") : " ├─ ");

}

void _collect_prefixes(tree_proc &t, const bool is_last, const string &header) {
bool is_filtered = t.entry.get().filtered;
if (is_filtered) {
t.entry.get().depth = 0;
}
if (!t.children.empty()) {
t.entry.get().prefix = header + (t.entry.get().collapsed ? "[+]─": "[-]─");
}
else {
t.entry.get().prefix = header + (is_last ? " └─": " ├─");
}
for (auto child = t.children.begin(); child != t.children.end(); ++child) {
_collect_prefixes(*child, child==(t.children.end() - 1),
(is_filtered ? "": header + (is_last ? " ": "")));
}
}
}
2 changes: 2 additions & 0 deletions src/btop_shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,4 +431,6 @@ namespace Proc {
void _tree_gen(proc_info& cur_proc, vector<proc_info>& in_procs, vector<tree_proc>& out_procs,
int cur_depth, bool collapsed, const string& filter,
bool found = false, bool no_update = false, bool should_filter = false);

void _collect_prefixes(tree_proc& t, bool is_last, const string &header);
}
12 changes: 4 additions & 8 deletions src/freebsd/btop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1297,18 +1297,14 @@ namespace Proc {
_tree_gen(p, current_procs, tree_procs, 0, false, filter, false, no_update, should_filter);
}

for (auto t = tree_procs.begin(); t != tree_procs.end(); ++t) {
_collect_prefixes(*t, t == tree_procs.end() - 1, "");
}

//? Recursive sort over tree structure to account for collapsed processes in the tree
int index = 0;
tree_sort(tree_procs, sorting, reverse, index, current_procs.size());

//? Add tree begin symbol to first item if childless
if (tree_procs.front().children.empty())
tree_procs.front().entry.get().prefix.replace(tree_procs.front().entry.get().prefix.size() - 8, 8, " ┌─ ");

//? Add tree terminator symbol to last item if childless
if (tree_procs.back().children.empty())
tree_procs.back().entry.get().prefix.replace(tree_procs.back().entry.get().prefix.size() - 8, 8, " └─ ");

//? Final sort based on tree index
rng::sort(current_procs, rng::less{}, & proc_info::tree_index);

Expand Down
12 changes: 4 additions & 8 deletions src/linux/btop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2998,18 +2998,14 @@ namespace Proc {
_tree_gen(p, current_procs, tree_procs, 0, false, filter, false, no_update, should_filter);
}

for (auto t = tree_procs.begin(); t != tree_procs.end(); ++t) {
_collect_prefixes(*t, t == tree_procs.end() - 1, "");
}

//? Recursive sort over tree structure to account for collapsed processes in the tree
int index = 0;
tree_sort(tree_procs, sorting, reverse, index, current_procs.size());

//? Add tree begin symbol to first item if childless
if (tree_procs.size() > 0 and tree_procs.front().children.empty() and tree_procs.front().entry.get().prefix.size() >= 8)
tree_procs.front().entry.get().prefix.replace(tree_procs.front().entry.get().prefix.size() - 8, 8, " ┌─ ");

//? Add tree terminator symbol to last item if childless
if (tree_procs.size() > 0 and tree_procs.back().children.empty() and tree_procs.back().entry.get().prefix.size() >= 8)
tree_procs.back().entry.get().prefix.replace(tree_procs.back().entry.get().prefix.size() - 8, 8, " └─ ");

//? Final sort based on tree index
rng::sort(current_procs, rng::less{}, & proc_info::tree_index);

Expand Down
12 changes: 4 additions & 8 deletions src/netbsd/btop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1377,18 +1377,14 @@ namespace Proc {
_tree_gen(p, current_procs, tree_procs, 0, false, filter, false, no_update, should_filter);
}

for (auto t = tree_procs.begin(); t != tree_procs.end(); ++t) {
_collect_prefixes(*t, t == tree_procs.end() - 1, "");
}

//? Recursive sort over tree structure to account for collapsed processes in the tree
int index = 0;
tree_sort(tree_procs, sorting, reverse, index, current_procs.size());

//? Add tree begin symbol to first item if childless
if (tree_procs.front().children.empty())
tree_procs.front().entry.get().prefix.replace(tree_procs.front().entry.get().prefix.size() - 8, 8, " ┌─ ");

//? Add tree terminator symbol to last item if childless
if (tree_procs.back().children.empty())
tree_procs.back().entry.get().prefix.replace(tree_procs.back().entry.get().prefix.size() - 8, 8, " └─ ");

//? Final sort based on tree index
rng::sort(current_procs, rng::less{}, & proc_info::tree_index);

Expand Down
12 changes: 4 additions & 8 deletions src/openbsd/btop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1229,18 +1229,14 @@ namespace Proc {
_tree_gen(p, current_procs, tree_procs, 0, false, filter, false, no_update, should_filter);
}

for (auto t = tree_procs.begin(); t != tree_procs.end(); ++t) {
_collect_prefixes(*t, t == tree_procs.end() - 1, "");
}

//? Recursive sort over tree structure to account for collapsed processes in the tree
int index = 0;
tree_sort(tree_procs, sorting, reverse, index, current_procs.size());

//? Add tree begin symbol to first item if childless
if (tree_procs.front().children.empty())
tree_procs.front().entry.get().prefix.replace(tree_procs.front().entry.get().prefix.size() - 8, 8, " ┌─ ");

//? Add tree terminator symbol to last item if childless
if (tree_procs.back().children.empty())
tree_procs.back().entry.get().prefix.replace(tree_procs.back().entry.get().prefix.size() - 8, 8, " └─ ");

//? Final sort based on tree index
rng::sort(current_procs, rng::less{}, & proc_info::tree_index);

Expand Down
12 changes: 4 additions & 8 deletions src/osx/btop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1363,18 +1363,14 @@ namespace Proc {
_tree_gen(p, current_procs, tree_procs, 0, false, filter, false, no_update, should_filter);
}

for (auto t = tree_procs.begin(); t != tree_procs.end(); ++t) {
_collect_prefixes(*t, t == tree_procs.end() - 1, "");
}

//? Recursive sort over tree structure to account for collapsed processes in the tree
int index = 0;
tree_sort(tree_procs, sorting, reverse, index, current_procs.size());

//? Add tree begin symbol to first item if childless
if (tree_procs.front().children.empty())
tree_procs.front().entry.get().prefix.replace(tree_procs.front().entry.get().prefix.size() - 8, 8, " ┌─ ");

//? Add tree terminator symbol to last item if childless
if (tree_procs.back().children.empty())
tree_procs.back().entry.get().prefix.replace(tree_procs.back().entry.get().prefix.size() - 8, 8, " └─ ");

//? Final sort based on tree index
rng::sort(current_procs, rng::less{}, & proc_info::tree_index);

Expand Down

0 comments on commit 1cae315

Please sign in to comment.