From b9e0da92007a733357ce7a2893168b31a9665a9f Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 5 Apr 2018 19:31:18 -0300 Subject: [PATCH] Collapse current subtree pressing Backspace --- Action.c | 24 ++++++++++++++++++++++++ Process.c | 2 ++ Process.h | 2 ++ ProcessList.c | 2 +- 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Action.c b/Action.c index dc830b57..f7d7d4da 100644 --- a/Action.c +++ b/Action.c @@ -155,6 +155,21 @@ static bool expandCollapse(Panel* panel) { return true; } +static bool collapseIntoParent(Panel* panel) { + Process* p = (Process*) Panel_getSelected(panel); + if (!p) return false; + pid_t ppid = Process_getParentPid(p); + for (int i = 0; i < Panel_size(panel); i++) { + Process* q = (Process*) Panel_get(panel, i); + if (q->pid == ppid) { + q->showChildren = false; + Panel_setSelected(panel, i); + return true; + } + } + return false; +} + Htop_Reaction Action_setSortKey(Settings* settings, ProcessField sortKey) { ScreenSettings* ss = settings->ss; ss->sortKey = sortKey; @@ -264,6 +279,14 @@ static Htop_Reaction actionExpandOrCollapse(State* st) { return changed ? HTOP_RECALCULATE : HTOP_OK; } +static Htop_Reaction actionCollapseIntoParent(State* st) { + if (!st->settings->treeView) { + return HTOP_OK; + } + bool changed = collapseIntoParent(st->panel); + return changed ? HTOP_RECALCULATE : HTOP_OK; +} + static Htop_Reaction actionExpandCollapseOrSortColumn(State* st) { return st->settings->ss->treeView ? actionExpandOrCollapse(st) : actionSetSortColumn(st); } @@ -570,6 +593,7 @@ void Action_setBindings(Htop_Action* keys) { keys['+'] = actionExpandOrCollapse; keys['='] = actionExpandOrCollapse; keys['-'] = actionExpandOrCollapse; + keys['\177'] = actionCollapseIntoParent; keys['u'] = actionFilterByUser; keys['F'] = Action_follow; keys['S'] = actionSetup; diff --git a/Process.c b/Process.c index 0a184b70..28c53dbc 100644 --- a/Process.c +++ b/Process.c @@ -179,6 +179,8 @@ typedef struct ProcessClass_ { #define As_Process(this_) ((ProcessClass*)((this_)->super.klass)) +#define Process_getParentPid(process_) (process_->tgid == process_->pid ? process_->ppid : process_->tgid) + #define Process_isChildOf(process_, pid_) (process_->tgid == pid_ || (process_->tgid == process_->pid && process_->ppid == pid_)) #define Process_sortState(state) ((state) == 'I' ? 0x100 : (state)) diff --git a/Process.h b/Process.h index 3ff67d4c..b3b5e9e0 100644 --- a/Process.h +++ b/Process.h @@ -157,6 +157,8 @@ typedef struct ProcessClass_ { #define As_Process(this_) ((ProcessClass*)((this_)->super.klass)) +#define Process_getParentPid(process_) (process_->tgid == process_->pid ? process_->ppid : process_->tgid) + #define Process_isChildOf(process_, pid_) (process_->tgid == pid_ || (process_->tgid == process_->pid && process_->ppid == pid_)) #define Process_sortState(state) ((state) == 'I' ? 0x100 : (state)) diff --git a/ProcessList.c b/ProcessList.c index 5b6a98f3..3d65bf4b 100644 --- a/ProcessList.c +++ b/ProcessList.c @@ -229,7 +229,7 @@ void ProcessList_sort(ProcessList* this) { ProcessList_buildTree(this, process->pid, 0, 0, direction, false); break; } - pid_t ppid = process->tgid == process->pid ? process->ppid : process->tgid; + pid_t ppid = Process_getParentPid(process); // Bisect the process vector to find parent int l = 0, r = size; // If PID corresponds with PPID (e.g. "kernel_task" (PID:0, PPID:0)