From dde71c6637905e1707bd1020c93e930f4b0a480b Mon Sep 17 00:00:00 2001 From: Adam Saponara Date: Fri, 30 Oct 2020 21:56:16 -0400 Subject: Highlight new and old processes (#74) --- htop.c | 75 +++++++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 24 deletions(-) (limited to 'htop.c') diff --git a/htop.c b/htop.c index e4d437b9..351f5867 100644 --- a/htop.c +++ b/htop.c @@ -46,17 +46,18 @@ static void printHelpFlag(void) { fputs("htop " VERSION "\n" COPYRIGHT "\n" "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" - "-s --sort-key=COLUMN Sort by COLUMN (try --sort-key=help for a list)\n" - "-t --tree Show the tree view by default\n" - "-u --user[=USERNAME] Show only processes for a given user (or $USER)\n" - "-U --no-unicode Do not use unicode but plain ASCII\n" - "-V --version Print version info\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" + "-s --sort-key=COLUMN Sort by COLUMN (try --sort-key=help for a list)\n" + "-t --tree Show the tree view by default\n" + "-u --user[=USERNAME] Show only processes for a given user (or $USER)\n" + "-U --no-unicode Do not use unicode but plain ASCII\n" + "-V --version Print version info\n" + "-H --highlight-changes[=DELAY] Highlight new and old processes\n" "\n" "Long options may be passed with a single dash.\n\n" "Press F1 inside htop for online help.\n" @@ -76,6 +77,8 @@ typedef struct CommandLineSettings_ { bool enableMouse; bool treeView; bool allowUnicode; + bool highlightChanges; + int highlightDelaySecs; } CommandLineSettings; static CommandLineSettings parseArguments(int argc, char** argv) { @@ -90,28 +93,31 @@ static CommandLineSettings parseArguments(int argc, char** argv) { .enableMouse = true, .treeView = false, .allowUnicode = true, + .highlightChanges = false, + .highlightDelaySecs = -1, }; static struct option long_opts[] = { - {"help", no_argument, 0, 'h'}, - {"version", no_argument, 0, 'V'}, - {"delay", required_argument, 0, 'd'}, - {"sort-key", required_argument, 0, 's'}, - {"user", optional_argument, 0, 'u'}, - {"no-color", no_argument, 0, 'C'}, - {"no-colour", no_argument, 0, 'C'}, - {"no-mouse", no_argument, 0, 'M'}, - {"no-unicode", no_argument, 0, 'U'}, - {"tree", no_argument, 0, 't'}, - {"pid", required_argument, 0, 'p'}, - {"filter", required_argument, 0, 'F'}, + {"help", no_argument, 0, 'h'}, + {"version", no_argument, 0, 'V'}, + {"delay", required_argument, 0, 'd'}, + {"sort-key", required_argument, 0, 's'}, + {"user", optional_argument, 0, 'u'}, + {"no-color", no_argument, 0, 'C'}, + {"no-colour", no_argument, 0, 'C'}, + {"no-mouse", no_argument, 0, 'M'}, + {"no-unicode", no_argument, 0, 'U'}, + {"tree", no_argument, 0, 't'}, + {"pid", required_argument, 0, 'p'}, + {"filter", required_argument, 0, 'F'}, + {"highlight-changes", optional_argument, 0, 'H'}, {0,0,0,0} }; int opt, opti=0; /* Parse arguments */ - while ((opt = getopt_long(argc, argv, "hVMCs:td:u::Up:F:", long_opts, &opti))) { + while ((opt = getopt_long(argc, argv, "hVMCs:td:u::Up:F:H::", long_opts, &opti))) { if (opt == EOF) break; switch (opt) { case 'h': @@ -198,6 +204,23 @@ static CommandLineSettings parseArguments(int argc, char** argv) { break; } + case 'H': { + const char *delay = optarg; + if (!delay && optind < argc && argv[optind] != NULL && + (argv[optind][0] != '\0' && argv[optind][0] != '-')) { + delay = argv[optind++]; + } + if (delay) { + if (sscanf(delay, "%16d", &(flags.highlightDelaySecs)) == 1) { + if (flags.highlightDelaySecs < 1) flags.highlightDelaySecs = 1; + } else { + fprintf(stderr, "Error: invalid highlight delay value \"%s\".\n", delay); + exit(1); + } + } + flags.highlightChanges = true; + break; + } default: exit(1); } @@ -271,6 +294,10 @@ int main(int argc, char** argv) { settings->enableMouse = false; if (flags.treeView) settings->treeView = true; + if (flags.highlightChanges) + settings->highlightChanges = true; + if (flags.highlightDelaySecs != -1) + settings->highlightDelaySecs = flags.highlightDelaySecs; CRT_init(settings->delay, settings->colorScheme, flags.allowUnicode); -- cgit v1.2.3 From a83f515f0fb75a079601be0d2e0e24b9402c9e15 Mon Sep 17 00:00:00 2001 From: Adam Saponara Date: Sat, 31 Oct 2020 20:36:53 -0400 Subject: Address items from review --- htop.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'htop.c') diff --git a/htop.c b/htop.c index 351f5867..48c6f8d0 100644 --- a/htop.c +++ b/htop.c @@ -50,14 +50,14 @@ static void printHelpFlag(void) { "-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 --highlight-changes[=DELAY] Highlight new and old processes\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" "-s --sort-key=COLUMN Sort by COLUMN (try --sort-key=help for a list)\n" "-t --tree Show the tree view by default\n" "-u --user[=USERNAME] Show only processes for a given user (or $USER)\n" "-U --no-unicode Do not use unicode but plain ASCII\n" "-V --version Print version info\n" - "-H --highlight-changes[=DELAY] Highlight new and old processes\n" "\n" "Long options may be passed with a single dash.\n\n" "Press F1 inside htop for online help.\n" @@ -212,7 +212,8 @@ static CommandLineSettings parseArguments(int argc, char** argv) { } if (delay) { if (sscanf(delay, "%16d", &(flags.highlightDelaySecs)) == 1) { - if (flags.highlightDelaySecs < 1) flags.highlightDelaySecs = 1; + if (flags.highlightDelaySecs < 1) + flags.highlightDelaySecs = 1; } else { fprintf(stderr, "Error: invalid highlight delay value \"%s\".\n", delay); exit(1); -- cgit v1.2.3