From 1cae3158119a8821c703053dce09c3dac7fd9eb6 Mon Sep 17 00:00:00 2001 From: denizantip Date: Sun, 6 Oct 2024 01:36:08 +0300 Subject: [PATCH] Correct process tree --- src/btop_shared.cpp | 30 +++++++++++++++++------------- src/btop_shared.hpp | 2 ++ src/freebsd/btop_collect.cpp | 12 ++++-------- src/linux/btop_collect.cpp | 12 ++++-------- src/netbsd/btop_collect.cpp | 12 ++++-------- src/openbsd/btop_collect.cpp | 12 ++++-------- src/osx/btop_collect.cpp | 12 ++++-------- 7 files changed, 39 insertions(+), 53 deletions(-) diff --git a/src/btop_shared.cpp b/src/btop_shared.cpp index 1b8e526ab..dc4aca790 100644 --- a/src/btop_shared.cpp +++ b/src/btop_shared.cpp @@ -130,8 +130,7 @@ namespace Proc { } void _tree_gen(proc_info& cur_proc, vector& in_procs, vector& 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 @@ -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 ? " ": " │ "))); + } + } } diff --git a/src/btop_shared.hpp b/src/btop_shared.hpp index 7e24078a2..b02ecfa35 100644 --- a/src/btop_shared.hpp +++ b/src/btop_shared.hpp @@ -431,4 +431,6 @@ namespace Proc { void _tree_gen(proc_info& cur_proc, vector& in_procs, vector& 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); } diff --git a/src/freebsd/btop_collect.cpp b/src/freebsd/btop_collect.cpp index 29e54bf2b..88224ce50 100644 --- a/src/freebsd/btop_collect.cpp +++ b/src/freebsd/btop_collect.cpp @@ -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); diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 4b3ab9541..808e7ce28 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -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); diff --git a/src/netbsd/btop_collect.cpp b/src/netbsd/btop_collect.cpp index 234b7d567..e0a8a41a2 100644 --- a/src/netbsd/btop_collect.cpp +++ b/src/netbsd/btop_collect.cpp @@ -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); diff --git a/src/openbsd/btop_collect.cpp b/src/openbsd/btop_collect.cpp index 480b83465..7b585e6b3 100644 --- a/src/openbsd/btop_collect.cpp +++ b/src/openbsd/btop_collect.cpp @@ -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); diff --git a/src/osx/btop_collect.cpp b/src/osx/btop_collect.cpp index 4d54e328e..36836ce39 100644 --- a/src/osx/btop_collect.cpp +++ b/src/osx/btop_collect.cpp @@ -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);