From e8c6994f40c9c69089e9f80abb2f895d2e077c7e Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 17 Dec 2020 19:08:56 -0300 Subject: [PATCH] Add "Tree view is always sorted by PID" option to mimic htop 2 behavior --- Action.c | 3 +++ DisplayOptionsPanel.c | 1 + MainPanel.c | 5 ++++- Process.c | 7 ++++++- ProcessList.c | 16 ++++++++++++---- Settings.c | 3 +++ Settings.h | 1 + 7 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Action.c b/Action.c index 7949b8a3..3335a430 100644 --- a/Action.c +++ b/Action.c @@ -160,6 +160,9 @@ static bool collapseIntoParent(Panel* panel) { Htop_Reaction Action_setSortKey(Settings* settings, ProcessField sortKey) { settings->sortKey = sortKey; settings->direction = 1; + if (settings->treeViewAlwaysByPID) { + settings->treeView = false; + } return HTOP_REFRESH | HTOP_SAVE_SETTINGS | HTOP_UPDATE_PANELHDR | HTOP_KEEP_FOLLOWING; } diff --git a/DisplayOptionsPanel.c b/DisplayOptionsPanel.c index ed373190..49beb5a6 100644 --- a/DisplayOptionsPanel.c +++ b/DisplayOptionsPanel.c @@ -97,6 +97,7 @@ DisplayOptionsPanel* DisplayOptionsPanel_new(Settings* settings, ScreenManager* Panel_setHeader(super, "Display options"); Panel_add(super, (Object*) CheckItem_newByRef("Tree view", &(settings->treeView))); + Panel_add(super, (Object*) CheckItem_newByRef("- Tree view is always sorted by PID (htop 2 behavior)", &(settings->treeViewAlwaysByPID))); Panel_add(super, (Object*) CheckItem_newByRef("Shadow other users' processes", &(settings->shadowOtherUsers))); Panel_add(super, (Object*) CheckItem_newByRef("Hide kernel threads", &(settings->hideKernelThreads))); Panel_add(super, (Object*) CheckItem_newByRef("Hide userland process threads", &(settings->hideUserlandThreads))); diff --git a/MainPanel.c b/MainPanel.c index 6c0c9fd1..f2b71e68 100644 --- a/MainPanel.c +++ b/MainPanel.c @@ -25,7 +25,7 @@ static const char* const MainFunctions[] = {"Help ", "Setup ", "Search", "Filt void MainPanel_updateTreeFunctions(MainPanel* this, bool mode) { FunctionBar* bar = MainPanel_getFunctionBar(this); - FunctionBar_setLabel(bar, KEY_F(5), mode ? "Sorted" : "Tree "); + FunctionBar_setLabel(bar, KEY_F(5), mode ? "List " : "Tree "); } void MainPanel_pidSearch(MainPanel* this, int ch) { @@ -65,6 +65,9 @@ static HandlerResult MainPanel_eventHandler(Panel* super, int ch) { } else { reaction |= Action_setSortKey(settings, field); } + if (settings->treeViewAlwaysByPID) { + settings->treeView = false; + } reaction |= HTOP_RECALCULATE | HTOP_REDRAW_BAR | HTOP_SAVE_SETTINGS; result = HANDLED; } else if (ch != ERR && this->inc->active) { diff --git a/Process.c b/Process.c index 89fc8da4..a65918f5 100644 --- a/Process.c +++ b/Process.c @@ -505,7 +505,12 @@ long Process_compare(const void* v1, const void* v2) { p1 = (const Process*)v2; } - switch (settings->sortKey) { + ProcessField key = settings->sortKey; + if (settings->treeView && settings->treeViewAlwaysByPID) { + key = PID; + } + + switch (key) { case PERCENT_CPU: case PERCENT_NORM_CPU: return SPACESHIP_NUMBER(p2->percent_cpu, p1->percent_cpu); diff --git a/ProcessList.c b/ProcessList.c index 78ec2b64..54b69184 100644 --- a/ProcessList.c +++ b/ProcessList.c @@ -82,7 +82,8 @@ void ProcessList_setPanel(ProcessList* this, Panel* panel) { void ProcessList_printHeader(ProcessList* this, RichString* header) { RichString_prune(header); - const ProcessField* fields = this->settings->fields; + const Settings* settings = this->settings; + const ProcessField* fields = settings->fields; for (int i = 0; fields[i]; i++) { const char* field = Process_fields[fields[i]].title; @@ -90,10 +91,17 @@ void ProcessList_printHeader(ProcessList* this, RichString* header) { field = "- "; } - int color = (this->settings->sortKey == fields[i]) ? - CRT_colors[PANEL_SELECTION_FOCUS] : CRT_colors[PANEL_HEADER_FOCUS]; + int color; + if (settings->treeView && settings->treeViewAlwaysByPID) { + color = CRT_colors[PANEL_HEADER_FOCUS]; + } else if (settings->sortKey == fields[i]) { + color = CRT_colors[PANEL_SELECTION_FOCUS]; + } else { + color = CRT_colors[PANEL_HEADER_FOCUS]; + } + RichString_appendWide(header, color, field); - if (COMM == fields[i] && this->settings->showMergedCommand) { + if (COMM == fields[i] && settings->showMergedCommand) { RichString_appendAscii(header, color, "(merged)"); } } diff --git a/Settings.c b/Settings.c index 0b4d0ed6..3b629a87 100644 --- a/Settings.c +++ b/Settings.c @@ -141,6 +141,8 @@ static bool Settings_read(Settings* this, const char* fileName, int initialCpuCo this->direction = atoi(option[1]); } else if (String_eq(option[0], "tree_view")) { this->treeView = atoi(option[1]); + } else if (String_eq(option[0], "tree_view_always_by_pid")) { + this->treeViewAlwaysByPID = atoi(option[1]); } else if (String_eq(option[0], "hide_kernel_threads")) { this->hideKernelThreads = atoi(option[1]); } else if (String_eq(option[0], "hide_userland_threads")) { @@ -287,6 +289,7 @@ bool Settings_write(Settings* this) { fprintf(fd, "strip_exe_from_cmdline=%d\n", (int) this->stripExeFromCmdline); fprintf(fd, "show_merged_command=%d\n", (int) this->showMergedCommand); fprintf(fd, "tree_view=%d\n", (int) this->treeView); + fprintf(fd, "tree_view_always_by_pid=%d\n", (int) this->treeViewAlwaysByPID); fprintf(fd, "header_margin=%d\n", (int) this->headerMargin); fprintf(fd, "detailed_cpu_time=%d\n", (int) this->detailedCPUTime); fprintf(fd, "cpu_count_from_one=%d\n", (int) this->countCPUsFromOne); diff --git a/Settings.h b/Settings.h index 752970a1..41a60f59 100644 --- a/Settings.h +++ b/Settings.h @@ -44,6 +44,7 @@ typedef struct Settings_ { bool degreeFahrenheit; #endif bool treeView; + bool treeViewAlwaysByPID; bool showProgramPath; bool shadowOtherUsers; bool showThreadNames;