flow: Use bat to color diffs when possible
authorThibault Saunier <tsaunier@igalia.com>
Sat, 23 May 2020 04:38:32 +0000 (00:38 -0400)
committerThibault Saunier <tsaunier@igalia.com>
Tue, 26 May 2020 23:16:26 +0000 (19:16 -0400)
Adding a function to check if can output colored logs

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-devtools/-/merge_requests/197>

validate/gst/validate/gst-validate-utils.c
validate/gst/validate/gst-validate-utils.h
validate/plugins/flow/gstvalidateflow.c

index 4cf76f3..6c491f2 100644 (file)
@@ -574,6 +574,21 @@ setup_quarks (void)
   debug_quark = g_quark_from_static_string ("__debug__");
 }
 
+gboolean
+gst_validate_has_colored_output (void)
+{
+#if GLIB_CHECK_VERSION(2,50,0)
+  return g_log_writer_supports_color (fileno (stdout));
+#endif
+
+#ifdef G_OS_UNIX
+  if (!isatty (STDOUT_FILENO))
+    return FALSE;
+#elif defined(G_OS_WIN32)
+  return FALSE;
+#endif
+}
+
 /* Parse file that contains a list of GStructures */
 #define GST_STRUCT_LINE_CONTINUATION_CHARS ",{\\["
 static GList *
@@ -594,14 +609,11 @@ _file_get_structures (GFile * file, gchar ** err,
   if (err)
     errstr = g_string_new (NULL);
 
-#if GLIB_CHECK_VERSION(2,50,0)
-  if (g_log_writer_supports_color (fileno (stderr))) {
+  if (gst_validate_has_colored_output ()) {
     red = gst_debug_construct_term_color (GST_DEBUG_FG_RED);
     bold = gst_debug_construct_term_color (GST_DEBUG_BOLD);
     endcolor = "\033[0m";
-  } else
-#endif
-  {
+  } else {
     red = g_strdup ("");
     bold = g_strdup ("");
   }
index b67f910..34cdefe 100644 (file)
@@ -86,5 +86,6 @@ void gst_validate_structure_set_variables_from_struct_file(GstStructure* vars, c
 void gst_validate_set_globals(GstStructure* structure);
 GST_VALIDATE_API
 gboolean gst_validate_fail_on_missing_plugin(void);
+GST_VALIDATE_API gboolean gst_validate_has_colored_output(void);
 
 #endif
index f0b812c..8782f97 100644 (file)
@@ -396,7 +396,36 @@ run_diff (const gchar * expected_file, const gchar * actual_file)
   g_subprocess_communicate_utf8 (process, NULL, NULL, &stdout_text, NULL,
       &error);
   if (!error) {
-    fprintf (stderr, "%s\n", stdout_text);
+    gboolean colored = gst_validate_has_colored_output ();
+    GSubprocess *process;
+    gchar *fname = NULL;
+    gint f = g_file_open_tmp ("XXXXXX.diff", &fname, NULL);
+
+    if (f > 0) {
+      gchar *tmpstdout;
+      g_file_set_contents (fname, stdout_text, -1, NULL);
+      close (f);
+
+      process =
+          g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE, &error, "bat", "-l",
+          "diff", "--paging", "never", "--color", colored ? "always" : "never",
+          fname, NULL);
+
+      g_subprocess_communicate_utf8 (process, NULL, NULL, &tmpstdout, NULL,
+          &error);
+      if (!error) {
+        g_free (stdout_text);
+        stdout_text = tmpstdout;
+      } else {
+        colored = FALSE;
+        GST_DEBUG ("Could not use bat: %s", error->message);
+        g_clear_error (&error);
+      }
+      g_free (fname);
+    }
+
+    fprintf (stderr, "%s%s%s\n",
+        !colored ? "``` diff\n" : "", stdout_text, !colored ? "\n```" : "");
   } else {
     fprintf (stderr, "Cannot show more details, failed to run diff: %s",
         error->message);
@@ -483,9 +512,7 @@ runner_stopping (GstValidateRunner * runner, ValidateFlowOverride * flow)
     g_free (contents);
   }
 
-  gst_validate_printf (flow, "Checking that flow %s matches expected flow %s\n"
-      " $ diff %s %s\n",
-      flow->expectations_file_path, flow->actual_results_file_path,
+  gst_validate_printf (flow, "Checking that flow %s matches expected flow %s\n",
       flow->expectations_file_path, flow->actual_results_file_path);
 
   for (i = 0; lines_expected[i] && lines_actual[i]; i++) {