From fbf6424e645614121b9506352b9553aacef51420 Mon Sep 17 00:00:00 2001 From: laydervus <20249311+layderv@users.noreply.github.com> Date: Tue, 6 Oct 2020 16:22:58 -0400 Subject: [PATCH] Option to set initial filter Closes #219 --- htop.1.in | 5 ++++- htop.c | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/htop.1.in b/htop.1.in index 215d92c5..48e5cee5 100644 --- a/htop.1.in +++ b/htop.1.in @@ -4,7 +4,7 @@ htop \- interactive process viewer .SH "SYNOPSIS" .LP .B htop -.RB [ \-dChpustv ] +.RB [ \-dCFhpustv ] .SH "DESCRIPTION" .LP .B htop @@ -36,6 +36,9 @@ Start .B htop in monochrome mode .TP +\fB\-F \-\-filter=FILTER +Filter processes by command +.TP \fB\-h \-\-help Display a help message and exit .TP diff --git a/htop.c b/htop.c index ec4e6242..8d17de9b 100644 --- a/htop.c +++ b/htop.c @@ -39,6 +39,7 @@ static void printHelpFlag(void) { "Released under the GNU GPLv2.\n\n" "-C --no-color Use a monochrome color scheme\n" "-d --delay=DELAY Set the delay between updates, in tenths of seconds\n" + "-F --filter=FILTER Show only the commands matching the given filter\n" "-h --help Print this help screen\n" "-M --no-mouse Disable the mouse\n" "-p --pid=PID,[,PID,PID...] Show only the given PIDs\n" @@ -58,6 +59,7 @@ static void printHelpFlag(void) { typedef struct CommandLineSettings_ { Hashtable* pidMatchList; + char* commFilter; uid_t userId; int sortKey; int delay; @@ -71,6 +73,7 @@ static CommandLineSettings parseArguments(int argc, char** argv) { CommandLineSettings flags = { .pidMatchList = NULL, + .commFilter = NULL, .userId = -1, // -1 is guaranteed to be an invalid uid_t (see setreuid(2)) .sortKey = 0, .delay = -1, @@ -93,12 +96,13 @@ static CommandLineSettings parseArguments(int argc, char** argv) { {"no-unicode", no_argument, 0, 'U'}, {"tree", no_argument, 0, 't'}, {"pid", required_argument, 0, 'p'}, + {"filter", required_argument, 0, 'F'}, {0,0,0,0} }; int opt, opti=0; /* Parse arguments */ - while ((opt = getopt_long(argc, argv, "hVMCs:td:u::Up:", long_opts, &opti))) { + while ((opt = getopt_long(argc, argv, "hVMCs:td:u::Up:F:", long_opts, &opti))) { if (opt == EOF) break; switch (opt) { case 'h': @@ -179,6 +183,12 @@ static CommandLineSettings parseArguments(int argc, char** argv) { break; } + case 'F': { + assert(optarg); + flags.commFilter = xStrdup(optarg); + + break; + } default: exit(1); } @@ -196,6 +206,23 @@ static void millisleep(unsigned long millisec) { } } +static void setCommFilter(State* state, char** commFilter) { + MainPanel* panel = (MainPanel*)state->panel; + ProcessList* pl = state->pl; + IncSet* inc = panel->inc; + size_t maxlen = sizeof(inc->modes[INC_FILTER].buffer) - 1; + char* buffer = inc->modes[INC_FILTER].buffer; + + strncpy(buffer, *commFilter, maxlen); + buffer[maxlen] = 0; + inc->modes[INC_FILTER].index = strlen(buffer); + inc->filtering = true; + pl->incFilter = IncSet_filter(inc); + + free(*commFilter); + *commFilter = NULL; +} + int main(int argc, char** argv) { char *lc_ctype = getenv("LC_CTYPE"); @@ -257,7 +284,11 @@ int main(int argc, char** argv) { .panel = (Panel*) panel, .header = header, }; + MainPanel_setState(panel, &state); + if (flags.commFilter) { + setCommFilter(&state, &(flags.commFilter)); + } ScreenManager* scr = ScreenManager_new(0, header->height, 0, -1, HORIZONTAL, header, settings, true); ScreenManager_add(scr, (Panel*) panel, -1);