cgtop: more sensible flushing behavior w/ non-TTY output
authorCharles Duffy <chaduffy@cisco.com>
Wed, 27 May 2015 22:30:11 +0000 (17:30 -0500)
committerCharles Duffy <chaduffy@cisco.com>
Wed, 10 Jun 2015 00:39:16 +0000 (19:39 -0500)
- Explicitly flush stdout before sleep between iterations
- Only clear user keystrokes when output is to TTY
- Add a newline between output batches when output is not to TTY

src/cgtop/cgtop.c

index d04d6f2..3a2842d 100644 (file)
@@ -749,6 +749,10 @@ int main(int argc, char *argv[]) {
                 if (arg_iterations && iteration >= arg_iterations)
                         break;
 
+                if (!on_tty()) /* non-TTY: Empty newline as delimiter between polls */
+                        fputs("\n", stdout);
+                fflush(stdout);
+
                 if (arg_batch) {
                         usleep(last_refresh + arg_delay - t);
                 } else {
@@ -762,8 +766,10 @@ int main(int argc, char *argv[]) {
                         }
                 }
 
-                fputs("\r \r", stdout);
-                fflush(stdout);
+                if (on_tty()) { /* TTY: Clear any user keystroke */
+                        fputs("\r \r", stdout);
+                        fflush(stdout);
+                }
 
                 if (arg_batch)
                         continue;