From 729f714bf987ae16d27afe6a3a0387ce98eee8ec Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Mon, 21 Apr 2025 02:59:51 -0400 Subject: [PATCH] feature: allow left to collapse trees and right to expand them (#1306) Allow using the left arrow to collapse a tree branch in the process widget, or a right arrow to expand it. --------- Co-authored-by: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> --- CHANGELOG.md | 1 + docs/content/usage/widgets/process.md | 4 +++- src/app.rs | 18 ++++++++++++++++++ src/constants.rs | 8 +++++--- src/widgets/process_table.rs | 22 ++++++++++++++++++++++ 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f79b355c..b14fa08c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ That said, these are more guidelines rather than hardset rules, though the proje - [#1641](https://github.com/ClementTsang/bottom/pull/1641): Support AMD GPU data collection on Linux. - [#1642](https://github.com/ClementTsang/bottom/pull/1642): Support changing the widget borders. - [#1717](https://github.com/ClementTsang/bottom/pull/1717): Support delete key (fn + delete on macOS) to kill processes. +- [#1306](https://github.com/ClementTsang/bottom/pull/1306): Support using left/right key to collapse/expand process trees respectively. ### Bug Fixes diff --git a/docs/content/usage/widgets/process.md b/docs/content/usage/widgets/process.md index bfaa7ecc..c1f7b921 100644 --- a/docs/content/usage/widgets/process.md +++ b/docs/content/usage/widgets/process.md @@ -99,7 +99,9 @@ Pressing ++t++ or ++f5++ in the table toggles tree mode in the process widget, d A picture of tree mode in a process widget. -A process in tree mode can also be "collapsed", hiding its children and any descendants, using either the ++minus++ or ++plus++ keys, or double-clicking on an entry. +A process in tree mode can also be "collapsed", hiding its children and any descendants, using the either the ++minus++, +++plus++, or ++left++ keys, or clicking on an entry. It can be expanded by using the ++minus++, ++plus++, or ++right++ +keys, or by clicking on the entry again. !!! info diff --git a/src/app.rs b/src/app.rs index c0fbcde3..c114708c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -687,6 +687,15 @@ impl App { pub fn on_left_key(&mut self) { if !self.is_in_dialog() { match self.current_widget.widget_type { + BottomWidgetType::Proc => { + if let Some(proc_widget_state) = self + .states + .proc_state + .get_mut_widget_state(self.current_widget.widget_id) + { + proc_widget_state.collapse_current_tree_branch_entry(); + } + } BottomWidgetType::ProcSearch => { let is_in_search_widget = self.is_in_search_widget(); if let Some(proc_widget_state) = self @@ -749,6 +758,15 @@ impl App { pub fn on_right_key(&mut self) { if !self.is_in_dialog() { match self.current_widget.widget_type { + BottomWidgetType::Proc => { + if let Some(proc_widget_state) = self + .states + .proc_state + .get_mut_widget_state(self.current_widget.widget_id) + { + proc_widget_state.expand_current_tree_branch_entry(); + } + } BottomWidgetType::ProcSearch => { let is_in_search_widget = self.is_in_search_widget(); if let Some(proc_widget_state) = self diff --git a/src/constants.rs b/src/constants.rs index c3cfa7c4..c52c9142 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -77,9 +77,9 @@ const CPU_HELP_TEXT: [&str; 2] = [ "Mouse scroll Scrolling over an CPU core/average shows only that entry on the chart", ]; -const PROCESS_HELP_TEXT: [&str; 17] = [ +const PROCESS_HELP_TEXT: [&str; 19] = [ "3 - Process widget", - "dd, F9 Kill the selected process", + "dd, F9, Delete Kill the selected process", "c Sort by CPU usage, press again to reverse", "m Sort by memory usage, press again to reverse", "p Sort by PID name, press again to reverse", @@ -91,7 +91,9 @@ const PROCESS_HELP_TEXT: [&str; 17] = [ "I Invert current sort", "% Toggle between values and percentages for memory usage", "t, F5 Toggle tree mode", - "+, -, click Collapse/expand a branch while in tree mode", + "Right Collapse a branch while in tree mode", + "Left Expand a branch while in tree mode", + "+, -, click Toggle whether a branch is expanded or collapsed in tree mode", "click on header Sorts the entries by that column, click again to invert the sort", "C Sort by GPU usage, press again to reverse", "M Sort by GPU memory usage, press again to reverse", diff --git a/src/widgets/process_table.rs b/src/widgets/process_table.rs index 00e18da7..919ef3f2 100644 --- a/src/widgets/process_table.rs +++ b/src/widgets/process_table.rs @@ -817,6 +817,28 @@ impl ProcWidgetState { } } + pub fn collapse_current_tree_branch_entry(&mut self) { + if let ProcWidgetMode::Tree { collapsed_pids } = &mut self.mode { + if let Some(process) = self.table.current_item() { + let pid = process.pid; + + collapsed_pids.insert(pid); + self.force_data_update(); + } + } + } + + pub fn expand_current_tree_branch_entry(&mut self) { + if let ProcWidgetMode::Tree { collapsed_pids } = &mut self.mode { + if let Some(process) = self.table.current_item() { + let pid = process.pid; + + collapsed_pids.remove(&pid); + self.force_data_update(); + } + } + } + pub fn toggle_current_tree_branch_entry(&mut self) { if let ProcWidgetMode::Tree { collapsed_pids } = &mut self.mode { if let Some(process) = self.table.current_item() {