mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

Blame SOURCES/0143-utils-Move-ansi_-functions-to-header-guestfs-interna.patch

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