From cf0f1df314abe2df0b65e7b2fb03efc262530fc0 Mon Sep 17 00:00:00 2001 From: Thibault Saunier Date: Sat, 23 May 2020 00:38:32 -0400 Subject: [PATCH] flow: Use bat to color diffs when possible Adding a function to check if can output colored logs Part-of: --- validate/gst/validate/gst-validate-utils.c | 22 ++++++++++++++----- validate/gst/validate/gst-validate-utils.h | 1 + validate/plugins/flow/gstvalidateflow.c | 35 ++++++++++++++++++++++++++---- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/validate/gst/validate/gst-validate-utils.c b/validate/gst/validate/gst-validate-utils.c index 4cf76f3..6c491f2 100644 --- a/validate/gst/validate/gst-validate-utils.c +++ b/validate/gst/validate/gst-validate-utils.c @@ -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 (""); } diff --git a/validate/gst/validate/gst-validate-utils.h b/validate/gst/validate/gst-validate-utils.h index b67f910..34cdefe 100644 --- a/validate/gst/validate/gst-validate-utils.h +++ b/validate/gst/validate/gst-validate-utils.h @@ -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 diff --git a/validate/plugins/flow/gstvalidateflow.c b/validate/plugins/flow/gstvalidateflow.c index f0b812c..8782f97 100644 --- a/validate/plugins/flow/gstvalidateflow.c +++ b/validate/plugins/flow/gstvalidateflow.c @@ -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++) { -- 2.7.4