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 <csmall@enc.com.au>
This commit is contained in:
Jesse Hathaway 2016-07-02 15:45:36 +10:00 committed by Craig Small
parent 672eea2832
commit 94054e7e96
2 changed files with 26 additions and 19 deletions

1
NEWS
View File

@ -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
----------------

44
watch.c
View File

@ -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;
}