From 80594403cc5dec2a3c5cad95de2330672163c11f Mon Sep 17 00:00:00 2001 From: Jesse Hathaway Date: Sat, 2 Jul 2016 15:45:36 +1000 Subject: [PATCH] watch: Add hostname to the header watch has the hostname added to the header so you know what device if you have many it is running on. Signed-off-by: Craig Small --- NEWS | 1 + watch.c | 44 +++++++++++++++++++++++++------------------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/NEWS b/NEWS index ef62e0f2..c884b15f 100644 --- a/NEWS +++ b/NEWS @@ -6,6 +6,7 @@ procps-ng-NEXT * tests: Conditionally add prctl Debian #816237 * pidof: check cmd if space in argv0. GitLab #4 * kill: report error if cannot kill process #733172 + * watch: Add hostname to header procps-ng-3.3.11 ---------------- diff --git a/watch.c b/watch.c index e97a4139..e3180ce6 100644 --- a/watch.c +++ b/watch.c @@ -371,40 +371,45 @@ static void output_header(char *restrict command, double interval) { time_t t = time(NULL); char *ts = ctime(&t); - int tsl = strlen(ts); char *header; + char *right_header; + char hostname[HOST_NAME_MAX + 1]; + + gethostname(hostname, sizeof(hostname)); /* - * left justify interval and command, right justify time, + * left justify interval and command, right justify hostname and time, * clipping all to fit window width */ int hlen = asprintf(&header, _("Every %.1fs: "), interval); + int rhlen = asprintf(&right_header, _("%s: %s"), hostname, ts); /* * the rules: - * width < tsl : print nothing - * width < tsl + hlen + 1: print ts - * width = tsl + hlen + 1: print header, ts - * width < tsl + hlen + 4: print header, ..., ts - * width < tsl + hlen + wcommand_columns: print header, - * truncated wcommand, ..., ts - * width > "": print header, wcomand, ts + * width < rhlen : print nothing + * width < rhlen + hlen + 1: print hostname, ts + * width = rhlen + hlen + 1: print header, hostname, ts + * width < rhlen + hlen + 4: print header, ..., hostname, ts + * width < rhlen + hlen + wcommand_columns: print header, + * truncated wcommand, ..., hostname, ts + * width > "": print header, wcomand, hostname, ts * this is slightly different from how it used to be */ - if (width < tsl) { + if (width < rhlen) { free(header); + free(right_header); return; } - if (tsl + hlen + 1 <= width) { + if (rhlen + hlen + 1 <= width) { mvaddstr(0, 0, header); - if (tsl + hlen + 2 <= width) { - if (width < tsl + hlen + 4) { - mvaddstr(0, width - tsl - 4, "... "); + if (rhlen + hlen + 2 <= width) { + if (width < rhlen + hlen + 4) { + mvaddstr(0, width - rhlen - 4, "... "); } else { #ifdef WITH_WATCH8BIT - if (width < tsl + hlen + wcommand_columns) { + if (width < rhlen + hlen + wcommand_columns) { /* print truncated */ - int available = width - tsl - hlen; + int available = width - rhlen - hlen; int in_use = wcommand_columns; int wcomm_len = wcommand_characters; while (available - 4 < in_use) { @@ -412,18 +417,19 @@ static void output_header(char *restrict command, double interval) in_use = wcswidth(wcommand, wcomm_len); } mvaddnwstr(0, hlen, wcommand, wcomm_len); - mvaddstr(0, width - tsl - 4, "... "); + mvaddstr(0, width - rhlen - 4, "... "); } else { mvaddwstr(0, hlen, wcommand); } #else - mvaddnstr(0, hlen, command, width - tsl - hlen); + mvaddnstr(0, hlen, command, width - rhlen - hlen); #endif /* WITH_WATCH8BIT */ } } } - mvaddstr(0, width - tsl + 1, ts); + mvaddstr(0, width - rhlen + 1, right_header); free(header); + free(right_header); return; }