mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone
Blob Blame History Raw
From 685108da60b663059570e5a5a4a72c91b0ca4da9 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Wed, 29 Jun 2016 12:49:46 +0100
Subject: [PATCH] utils: Move ansi_* functions to header
 guestfs-internal-frontend.h.

This is just code motion.

(cherry picked from commit d2abfc7b486039efdfb3fa9b910e14911215b85a)
---
 src/guestfs-internal-frontend.h     | 29 ++++++++++++++++++
 utils/boot-analysis/boot-analysis.c | 61 ++++++-------------------------------
 2 files changed, 39 insertions(+), 51 deletions(-)

diff --git a/src/guestfs-internal-frontend.h b/src/guestfs-internal-frontend.h
index cf0eebc..ed9165a 100644
--- a/src/guestfs-internal-frontend.h
+++ b/src/guestfs-internal-frontend.h
@@ -179,4 +179,33 @@ extern void guestfs_int_cleanup_pclose (void *ptr);
    NULL                                                                 \
 )
 
+/* ANSI colours.  These are defined as macros so that we don't have to
+ * define the force_colour global variable in the library.
+ */
+#define ansi_green(fp)                           \
+  do {                                           \
+    if (force_colour || isatty (fileno (fp)))    \
+      fputs ("\033[0;32m", (fp));                \
+  } while (0)
+#define ansi_red(fp)                             \
+  do {                                           \
+    if (force_colour || isatty (fileno (fp)))    \
+      fputs ("\033[1;31m", (fp));                \
+  } while (0)
+#define ansi_blue(fp)                            \
+  do {                                           \
+    if (force_colour || isatty (fileno (fp)))    \
+      fputs ("\033[1;34m", (fp));                \
+  } while (0)
+#define ansi_magenta(fp)                         \
+  do {                                           \
+    if (force_colour || isatty (fileno (fp)))    \
+      fputs ("\033[1;35m", (fp));                \
+  } while (0)
+#define ansi_restore(fp)                         \
+  do {                                           \
+    if (force_colour || isatty (fileno (fp)))    \
+      fputs ("\033[0m", (fp));                   \
+  } while (0)
+
 #endif /* GUESTFS_INTERNAL_FRONTEND_H_ */
diff --git a/utils/boot-analysis/boot-analysis.c b/utils/boot-analysis/boot-analysis.c
index d06d9a1..1b491c4 100644
--- a/utils/boot-analysis/boot-analysis.c
+++ b/utils/boot-analysis/boot-analysis.c
@@ -51,7 +51,7 @@
 #define WARNING_THRESHOLD 1.0
 
 static const char *append = NULL;
-static int force_colour = 0;
+static int force_colour = 0;    /* used by ansi_* macros */
 static int memsize = 0;
 static int smp = 1;
 static int verbose = 0;
@@ -85,11 +85,6 @@ static void print_analysis (void);
 static void print_longest_to_shortest (void);
 static void free_pass_data (void);
 static void free_final_timeline (void);
-static void ansi_green (void);
-static void ansi_red (void);
-static void ansi_blue (void);
-static void ansi_magenta (void);
-static void ansi_restore (void);
 
 static void
 usage (int exitcode)
@@ -1033,15 +1028,15 @@ dump_timeline (void)
 static void
 print_activity (struct activity *activity)
 {
-  if (activity->warning) ansi_red (); else ansi_green ();
+  if (activity->warning) ansi_red (stdout); else ansi_green (stdout);
   assert (activity->magic == ACTIVITY_MAGIC);
   print_escaped_string (activity->name);
-  ansi_restore ();
+  ansi_restore (stdout);
   printf (" %.1fms ±%.1fms ",
           activity->mean / 1000000, activity->sd / 1000000);
-  if (activity->warning) ansi_red (); else ansi_green ();
+  if (activity->warning) ansi_red (stdout); else ansi_green (stdout);
   printf ("(%.1f%%) ", activity->percent);
-  ansi_restore ();
+  ansi_restore (stdout);
 }
 
 static void
@@ -1084,7 +1079,7 @@ print_analysis (void)
     /* Draw a spacer line, but only if last_t -> t is a large jump. */
     if (t - last_t >= 1000000 /* ns */) {
       printf ("          ");
-      ansi_magenta ();
+      ansi_magenta (stdout);
       for (j = 0; j < last_free_column; ++j) {
         if (columns[j] >= 0 &&
             activities[columns[j]].end_t != last_t /* !▼ */)
@@ -1092,7 +1087,7 @@ print_analysis (void)
         else
           printf ("  ");
       }
-      ansi_restore ();
+      ansi_restore (stdout);
       printf ("\n");
     }
 
@@ -1122,10 +1117,10 @@ print_analysis (void)
     }
 
     /* Draw the line. */
-    ansi_blue ();
+    ansi_blue (stdout);
     printf ("%6.1fms: ", t / 1000000);
 
-    ansi_magenta ();
+    ansi_magenta (stdout);
     for (j = 0; j < last_free_column; ++j) {
       if (columns[j] >= 0) {
         if (activities[columns[j]].t == t)
@@ -1138,7 +1133,7 @@ print_analysis (void)
       else
         printf ("  ");
     }
-    ansi_restore ();
+    ansi_restore (stdout);
 
     for (j = 0; j < last_free_column; ++j) {
       if (columns[j] >= 0 && activities[columns[j]].t == t) /* ▲ */
@@ -1215,39 +1210,3 @@ free_final_timeline (void)
     free (activities[i].name);
   free (activities);
 }
-
-/* Colours. */
-static void
-ansi_green (void)
-{
-  if (force_colour || isatty (1))
-    fputs ("\033[0;32m", stdout);
-}
-
-static void
-ansi_red (void)
-{
-  if (force_colour || isatty (1))
-    fputs ("\033[1;31m", stdout);
-}
-
-static void
-ansi_blue (void)
-{
-  if (force_colour || isatty (1))
-    fputs ("\033[1;34m", stdout);
-}
-
-static void
-ansi_magenta (void)
-{
-  if (force_colour || isatty (1))
-    fputs ("\033[1;35m", stdout);
-}
-
-static void
-ansi_restore (void)
-{
-  if (force_colour || isatty (1))
-    fputs ("\033[0m", stdout);
-}
-- 
1.8.3.1