Option to set initial filter

Closes #219
This commit is contained in:
laydervus 2020-10-06 16:22:58 -04:00 committed by Daniel Lange
parent 954d6c12f5
commit fbf6424e64
2 changed files with 36 additions and 2 deletions

View File

@ -4,7 +4,7 @@ htop \- interactive process viewer
.SH "SYNOPSIS" .SH "SYNOPSIS"
.LP .LP
.B htop .B htop
.RB [ \-dChpustv ] .RB [ \-dCFhpustv ]
.SH "DESCRIPTION" .SH "DESCRIPTION"
.LP .LP
.B htop .B htop
@ -36,6 +36,9 @@ Start
.B htop .B htop
in monochrome mode in monochrome mode
.TP .TP
\fB\-F \-\-filter=FILTER
Filter processes by command
.TP
\fB\-h \-\-help \fB\-h \-\-help
Display a help message and exit Display a help message and exit
.TP .TP

33
htop.c
View File

@ -39,6 +39,7 @@ static void printHelpFlag(void) {
"Released under the GNU GPLv2.\n\n" "Released under the GNU GPLv2.\n\n"
"-C --no-color Use a monochrome color scheme\n" "-C --no-color Use a monochrome color scheme\n"
"-d --delay=DELAY Set the delay between updates, in tenths of seconds\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" "-h --help Print this help screen\n"
"-M --no-mouse Disable the mouse\n" "-M --no-mouse Disable the mouse\n"
"-p --pid=PID,[,PID,PID...] Show only the given PIDs\n" "-p --pid=PID,[,PID,PID...] Show only the given PIDs\n"
@ -58,6 +59,7 @@ static void printHelpFlag(void) {
typedef struct CommandLineSettings_ { typedef struct CommandLineSettings_ {
Hashtable* pidMatchList; Hashtable* pidMatchList;
char* commFilter;
uid_t userId; uid_t userId;
int sortKey; int sortKey;
int delay; int delay;
@ -71,6 +73,7 @@ static CommandLineSettings parseArguments(int argc, char** argv) {
CommandLineSettings flags = { CommandLineSettings flags = {
.pidMatchList = NULL, .pidMatchList = NULL,
.commFilter = NULL,
.userId = -1, // -1 is guaranteed to be an invalid uid_t (see setreuid(2)) .userId = -1, // -1 is guaranteed to be an invalid uid_t (see setreuid(2))
.sortKey = 0, .sortKey = 0,
.delay = -1, .delay = -1,
@ -93,12 +96,13 @@ static CommandLineSettings parseArguments(int argc, char** argv) {
{"no-unicode", no_argument, 0, 'U'}, {"no-unicode", no_argument, 0, 'U'},
{"tree", no_argument, 0, 't'}, {"tree", no_argument, 0, 't'},
{"pid", required_argument, 0, 'p'}, {"pid", required_argument, 0, 'p'},
{"filter", required_argument, 0, 'F'},
{0,0,0,0} {0,0,0,0}
}; };
int opt, opti=0; int opt, opti=0;
/* Parse arguments */ /* 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; if (opt == EOF) break;
switch (opt) { switch (opt) {
case 'h': case 'h':
@ -179,6 +183,12 @@ static CommandLineSettings parseArguments(int argc, char** argv) {
break; break;
} }
case 'F': {
assert(optarg);
flags.commFilter = xStrdup(optarg);
break;
}
default: default:
exit(1); 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) { int main(int argc, char** argv) {
char *lc_ctype = getenv("LC_CTYPE"); char *lc_ctype = getenv("LC_CTYPE");
@ -257,7 +284,11 @@ int main(int argc, char** argv) {
.panel = (Panel*) panel, .panel = (Panel*) panel,
.header = header, .header = header,
}; };
MainPanel_setState(panel, &state); 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* scr = ScreenManager_new(0, header->height, 0, -1, HORIZONTAL, header, settings, true);
ScreenManager_add(scr, (Panel*) panel, -1); ScreenManager_add(scr, (Panel*) panel, -1);