]> granicus.if.org Git - procps-ng/commitdiff
watch: Add no linewrap option
authorCraig Small <csmall@dropbear.xyz>
Mon, 19 Oct 2020 11:03:44 +0000 (22:03 +1100)
committerCraig Small <csmall@dropbear.xyz>
Mon, 19 Oct 2020 11:07:56 +0000 (22:07 +1100)
For long lines from a process, watch would wrap them around to the
next. While this default option has it uses, sometimes you want to
just cut those long lines down.

watch has a -w flag which will truncate the lines to the number
of columns. A few simple lines to do this new trick.

I think I caught all the ANSI state correctly but there might be
a chance it bleeds to the next row.

References:
 procps-ng/procps#182

NEWS
watch.1
watch.c

diff --git a/NEWS b/NEWS
index 3e3da4705d15694954918612cf359c5b579cf059..57790af5db187605c4fcc5c86452e210b1d4723f 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -17,6 +17,7 @@ procps-ng NEXT
   * top: fix potential SEGV involving -p switch            merge #114
   * vmstat: Wide mode gives wider proc columns             merge #48
   * watch: Add environment variable for interval           merge #62
+  * watch: Add no linewrap option                          issue #182
 
 procps-ng-3.3.16
 ----------------
diff --git a/watch.1 b/watch.1
index e06e04e83d870410ecf718cdf6bffd9c2e30339f..5c48f9f5522a2f5d6b5fb7c322d81838edb9bf93 100644 (file)
--- a/watch.1
+++ b/watch.1
@@ -1,4 +1,4 @@
-.TH WATCH 1 "2020-06-04" "procps-ng" "User Commands"
+.TH WATCH 1 "2020-10-19" "procps-ng" "User Commands"
 .SH NAME
 watch \- execute a program periodically, showing output fullscreen
 .SH SYNOPSIS
@@ -64,6 +64,9 @@ instead of
 .B sh \-c
 which reduces the need to use extra quoting to get the desired effect.
 .TP
+\fB\-w\fR, \fB\-\-no\-linewrap\fR
+Turn off line wrapping. Long lines will be truncated instead of wrapped to the next line.
+.TP
 \fB\-h\fR, \fB\-\-help\fR
 Display help text and exit.
 .TP
diff --git a/watch.c b/watch.c
index 057243b68ad33dabe5de33af84979ae3dd35ab22..f8165912eed44d45061471a112088ca89a77e848 100644 (file)
--- a/watch.c
+++ b/watch.c
@@ -78,6 +78,7 @@ static int screen_size_changed = 0;
 static int first_screen = 1;
 static int show_title = 2;     /* number of lines used, 2 or 0 */
 static int precise_timekeeping = 0;
+static int line_wrap = 1;
 
 #define min(x,y) ((x) > (y) ? (y) : (x))
 #define MAX_ANSIBUF 100
@@ -98,6 +99,7 @@ static void __attribute__ ((__noreturn__))
        fputs(_("  -n, --interval <secs>  seconds to wait between updates\n"), out);
        fputs(_("  -p, --precise          attempt run command in precise intervals\n"), out);
        fputs(_("  -t, --no-title         turn off header\n"), out);
+       fputs(_("  -w, --no-wrap          turn off line wrapping\n"), out);
        fputs(_("  -x, --exec             pass command to exec instead of \"sh -c\"\n"), out);
        fputs(USAGE_SEPARATOR, out);
        fputs(USAGE_HELP, out);
@@ -450,6 +452,22 @@ static void output_header(char *restrict command, double interval)
        return;
 }
 
+static void find_eol(FILE *p)
+{
+    int c;
+#ifdef WITH_WATCH8BIT
+    do {
+       c = my_getwc(p);
+    } while (c != WEOF
+           && c!= L'\n');
+#else
+    do {
+       c = getc(p);
+    } while (c != EOF
+           && c != '\n');
+#endif /* WITH_WATCH8BIT */
+}
+
 static int run_command(char *restrict command, char **restrict command_argv)
 {
        FILE *p;
@@ -640,6 +658,12 @@ static int run_command(char *restrict command, char **restrict command_argv)
 #endif
                }
                oldeolseen = eolseen;
+               if (!line_wrap) {
+                   reset_ansi();
+                   if (flags & WATCH_COLOR)
+                       attrset(A_NORMAL);
+                   find_eol(p);
+               }
        }
 
        fclose(p);
@@ -693,6 +717,7 @@ int main(int argc, char *argv[])
                {"exec", no_argument, 0, 'x'},
                {"precise", no_argument, 0, 'p'},
                {"no-title", no_argument, 0, 't'},
+               {"no-wrap", no_argument, 0, 'w'},
                {"version", no_argument, 0, 'v'},
                {0, 0, 0, 0}
        };
@@ -710,7 +735,7 @@ int main(int argc, char *argv[])
                interval = strtod_nol_or_err(interval_string, _("Could not parse interval from WATCH_INTERVAL"));
 
        while ((optc =
-               getopt_long(argc, argv, "+bced::ghn:pvtx", longopts, (int *)0))
+               getopt_long(argc, argv, "+bced::ghn:pvtwx", longopts, (int *)0))
               != EOF) {
                switch (optc) {
                case 'b':
@@ -733,6 +758,9 @@ int main(int argc, char *argv[])
                case 't':
                        show_title = 0;
                        break;
+               case 'w':
+                       line_wrap = 0;
+                       break;
                case 'x':
                        flags |= WATCH_EXEC;
                        break;