From ce408dd8efa8a3bbbce847d13ca691808904f1a6 Mon Sep 17 00:00:00 2001 From: aristocratos Date: Fri, 1 May 2026 16:34:22 +0200 Subject: [PATCH] Created function toggle_tree_collapse() in shared.cpp to reduce code duplication --- src/btop_shared.cpp | 21 +++++++++++++++++++++ src/btop_shared.hpp | 3 +++ src/freebsd/btop_collect.cpp | 24 ++++-------------------- src/linux/btop_collect.cpp | 20 ++------------------ src/netbsd/btop_collect.cpp | 18 +----------------- src/openbsd/btop_collect.cpp | 18 +----------------- src/osx/btop_collect.cpp | 20 ++------------------ 7 files changed, 34 insertions(+), 90 deletions(-) diff --git a/src/btop_shared.cpp b/src/btop_shared.cpp index 0990fe68..462cba78 100644 --- a/src/btop_shared.cpp +++ b/src/btop_shared.cpp @@ -22,6 +22,7 @@ tab-size = 4 #include #include #include +#include #include "btop_config.hpp" #include "btop_shared.hpp" @@ -269,6 +270,26 @@ bool set_priority(pid_t pid, int priority) { is_filtered ? "": header + (is_last ? " ": " │ ")); } } + + void toggle_tree_collapse(std::vector& current_procs) { + //? Build sets of all pids and parent pids to identify root processes + std::unordered_set pid_set, parent_pids; + for (const auto& p : current_procs) { + pid_set.insert(p.pid); + parent_pids.insert(static_cast(p.ppid)); + } + //? If any non-root parent is expanded, collapse; otherwise expand + const bool do_collapse = rng::any_of(current_procs, [&parent_pids, &pid_set](const proc_info& p) { + return parent_pids.contains(p.pid) + and pid_set.contains(static_cast(p.ppid)) + and not p.collapsed; + }); + //? Root processes (parent not in tracked list) are never touched + for (auto& p : current_procs) { + if (not pid_set.contains(static_cast(p.ppid))) continue; + p.collapsed = do_collapse; + } + } } auto detect_container() -> std::optional { diff --git a/src/btop_shared.hpp b/src/btop_shared.hpp index 00020b11..c35a6818 100644 --- a/src/btop_shared.hpp +++ b/src/btop_shared.hpp @@ -464,6 +464,9 @@ namespace Proc { //* Build prefixes for tree view void _collect_prefixes(tree_proc& t, bool is_last, const string &header = ""); + + //* Toggle collapse/expand of all tree entries + void toggle_tree_collapse(std::vector& current_procs); } /// Detect container engine. diff --git a/src/freebsd/btop_collect.cpp b/src/freebsd/btop_collect.cpp index 0ce9a5ff..ef6410e1 100644 --- a/src/freebsd/btop_collect.cpp +++ b/src/freebsd/btop_collect.cpp @@ -630,7 +630,7 @@ namespace Mem { if (show_swap) { char buf[_POSIX2_LINE_MAX]; - Shared::KvmPtr kd {kvm_openfiles(nullptr, _PATH_DEVNULL, nullptr, O_RDONLY, buf)}; + Shared::KvmPtr kd {kvm_openfiles(nullptr, _PATH_DEVNULL, nullptr, O_RDONLY, buf)}; struct kvm_swap swap[16]; int nswap = kvm_getswapinfo(kd.get(), swap, 16, 0); int totalSwap = 0, usedSwap = 0; @@ -1045,7 +1045,7 @@ namespace Proc { struct timeval currentTime; gettimeofday(¤tTime, nullptr); // only interested in second granularity, so ignoring tc_usec - if (detailed.entry.state != 'X') detailed.elapsed = sec_to_dhms(currentTime.tv_sec - detailed.entry.cpu_s); + if (detailed.entry.state != 'X') detailed.elapsed = sec_to_dhms(currentTime.tv_sec - detailed.entry.cpu_s); else detailed.elapsed = sec_to_dhms(detailed.entry.death_time); if (detailed.elapsed.size() > 8) detailed.elapsed.resize(detailed.elapsed.size() - 3); @@ -1284,7 +1284,7 @@ namespace Proc { } toggle_children = -1; } - + if (auto find_pid = (collapse != -1 ? collapse : expand); find_pid != -1) { auto collapser = rng::find(current_procs, find_pid, &proc_info::pid); if (collapser != current_procs.end()) { @@ -1303,23 +1303,7 @@ namespace Proc { } if (collapse_all != -1) { - //? Build sets of all pids and parent pids to identify root processes - std::unordered_set pid_set, parent_pids; - for (const auto& p : current_procs) { - pid_set.insert(p.pid); - parent_pids.insert(static_cast(p.ppid)); - } - //? If any non-root parent is expanded, collapse; otherwise expand - const bool do_collapse = rng::any_of(current_procs, [&parent_pids, &pid_set](const proc_info& p) { - return parent_pids.contains(p.pid) - and pid_set.contains(static_cast(p.ppid)) - and not p.collapsed; - }); - //? Root processes (parent not in tracked list) are never touched - for (auto& p : current_procs) { - if (not pid_set.contains(static_cast(p.ppid))) continue; - p.collapsed = do_collapse; - } + toggle_tree_collapse(current_procs); collapse_all = -1; if (Config::ints.at("proc_selected") > 0) locate_selection = true; } diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 4997d975..64eaffef 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -3337,7 +3337,7 @@ namespace Proc { } toggle_children = -1; } - + if (auto find_pid = (collapse != -1 ? collapse : expand); find_pid != -1) { auto collapser = rng::find(current_procs, find_pid, &proc_info::pid); if (collapser != current_procs.end()) { @@ -3356,23 +3356,7 @@ namespace Proc { } if (collapse_all != -1) { - //? Build sets of all pids and parent pids to identify root processes - std::unordered_set pid_set, parent_pids; - for (const auto& p : current_procs) { - pid_set.insert(p.pid); - parent_pids.insert(static_cast(p.ppid)); - } - //? If any non-root parent is expanded, collapse; otherwise expand - const bool do_collapse = rng::any_of(current_procs, [&parent_pids, &pid_set](const proc_info& p) { - return parent_pids.contains(p.pid) - and pid_set.contains(static_cast(p.ppid)) - and not p.collapsed; - }); - //? Root processes (parent not in tracked list) are never touched - for (auto& p : current_procs) { - if (not pid_set.contains(static_cast(p.ppid))) continue; - p.collapsed = do_collapse; - } + toggle_tree_collapse(current_procs); collapse_all = -1; if (Config::ints.at("proc_selected") > 0) locate_selection = true; } diff --git a/src/netbsd/btop_collect.cpp b/src/netbsd/btop_collect.cpp index ba3233d8..8420d35e 100644 --- a/src/netbsd/btop_collect.cpp +++ b/src/netbsd/btop_collect.cpp @@ -1364,23 +1364,7 @@ namespace Proc { } if (collapse_all != -1) { - //? Build sets of all pids and parent pids to identify root processes - std::unordered_set pid_set, parent_pids; - for (const auto& p : current_procs) { - pid_set.insert(p.pid); - parent_pids.insert(static_cast(p.ppid)); - } - //? If any non-root parent is expanded, collapse; otherwise expand - const bool do_collapse = rng::any_of(current_procs, [&parent_pids, &pid_set](const proc_info& p) { - return parent_pids.contains(p.pid) - and pid_set.contains(static_cast(p.ppid)) - and not p.collapsed; - }); - //? Root processes (parent not in tracked list) are never touched - for (auto& p : current_procs) { - if (not pid_set.contains(static_cast(p.ppid))) continue; - p.collapsed = do_collapse; - } + toggle_tree_collapse(current_procs); collapse_all = -1; if (Config::ints.at("proc_selected") > 0) locate_selection = true; } diff --git a/src/openbsd/btop_collect.cpp b/src/openbsd/btop_collect.cpp index 38e70884..a2bba061 100644 --- a/src/openbsd/btop_collect.cpp +++ b/src/openbsd/btop_collect.cpp @@ -1267,23 +1267,7 @@ namespace Proc { } if (collapse_all != -1) { - //? Build sets of all pids and parent pids to identify root processes - std::unordered_set pid_set, parent_pids; - for (const auto& p : current_procs) { - pid_set.insert(p.pid); - parent_pids.insert(static_cast(p.ppid)); - } - //? If any non-root parent is expanded, collapse; otherwise expand - const bool do_collapse = rng::any_of(current_procs, [&parent_pids, &pid_set](const proc_info& p) { - return parent_pids.contains(p.pid) - and pid_set.contains(static_cast(p.ppid)) - and not p.collapsed; - }); - //? Root processes (parent not in tracked list) are never touched - for (auto& p : current_procs) { - if (not pid_set.contains(static_cast(p.ppid))) continue; - p.collapsed = do_collapse; - } + toggle_tree_collapse(current_procs); collapse_all = -1; if (Config::ints.at("proc_selected") > 0) locate_selection = true; } diff --git a/src/osx/btop_collect.cpp b/src/osx/btop_collect.cpp index 0c84a80a..ff0cedf2 100644 --- a/src/osx/btop_collect.cpp +++ b/src/osx/btop_collect.cpp @@ -1905,7 +1905,7 @@ namespace Proc { } toggle_children = -1; } - + if (auto find_pid = (collapse != -1 ? collapse : expand); find_pid != -1) { auto collapser = rng::find(current_procs, find_pid, &proc_info::pid); if (collapser != current_procs.end()) { @@ -1924,23 +1924,7 @@ namespace Proc { } if (collapse_all != -1) { - //? Build sets of all pids and parent pids to identify root processes - std::unordered_set pid_set, parent_pids; - for (const auto& p : current_procs) { - pid_set.insert(p.pid); - parent_pids.insert(static_cast(p.ppid)); - } - //? If any non-root parent is expanded, collapse; otherwise expand - const bool do_collapse = rng::any_of(current_procs, [&parent_pids, &pid_set](const proc_info& p) { - return parent_pids.contains(p.pid) - and pid_set.contains(static_cast(p.ppid)) - and not p.collapsed; - }); - //? Root processes (parent not in tracked list) are never touched - for (auto& p : current_procs) { - if (not pid_set.contains(static_cast(p.ppid))) continue; - p.collapsed = do_collapse; - } + toggle_tree_collapse(current_procs); collapse_all = -1; if (Config::ints.at("proc_selected") > 0) locate_selection = true; }