From a1e4bac6b2dafd854a5b887a6397836e3aa5b5c2 Mon Sep 17 00:00:00 2001 From: Daniel Schaal Date: Fri, 2 Aug 2013 07:59:02 +0200 Subject: [PATCH] systemd-delta: Only print colors when on a tty --- src/delta/delta.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/delta/delta.c b/src/delta/delta.c index 49c2fc3..8df63c5 100644 --- a/src/delta/delta.c +++ b/src/delta/delta.c @@ -48,6 +48,10 @@ static enum { (SHOW_MASKED | SHOW_EQUIVALENT | SHOW_REDIRECTED | SHOW_OVERRIDDEN | SHOW_EXTENDED) } arg_flags = 0; +static const char *color_output(const char *color) { + return on_tty() ? color : ""; +} + static int equivalent(const char *a, const char *b) { _cleanup_free_ char *x = NULL, *y = NULL; @@ -66,7 +70,7 @@ static int notify_override_masked(const char *top, const char *bottom) { if (!(arg_flags & SHOW_MASKED)) return 0; - printf(ANSI_HIGHLIGHT_RED_ON "[MASKED]" ANSI_HIGHLIGHT_OFF " %s → %s\n", top, bottom); + printf("%s%s%s %s → %s\n", color_output(ANSI_HIGHLIGHT_RED_ON), "[MASKED]", color_output(ANSI_HIGHLIGHT_OFF), top, bottom); return 1; } @@ -74,7 +78,7 @@ static int notify_override_equivalent(const char *top, const char *bottom) { if (!(arg_flags & SHOW_EQUIVALENT)) return 0; - printf(ANSI_HIGHLIGHT_GREEN_ON "[EQUIVALENT]" ANSI_HIGHLIGHT_OFF " %s → %s\n", top, bottom); + printf("%s%s%s %s → %s\n", color_output(ANSI_HIGHLIGHT_GREEN_ON), "[EQUIVALENT]", color_output(ANSI_HIGHLIGHT_OFF), top, bottom); return 1; } @@ -82,7 +86,7 @@ static int notify_override_redirected(const char *top, const char *bottom) { if (!(arg_flags & SHOW_REDIRECTED)) return 0; - printf(ANSI_HIGHLIGHT_ON "[REDIRECTED]" ANSI_HIGHLIGHT_OFF " %s → %s\n", top, bottom); + printf("%s%s%s %s → %s\n", color_output(ANSI_HIGHLIGHT_ON), "[REDIRECTED]", color_output(ANSI_HIGHLIGHT_OFF), top, bottom); return 1; } @@ -90,7 +94,7 @@ static int notify_override_overridden(const char *top, const char *bottom) { if (!(arg_flags & SHOW_OVERRIDDEN)) return 0; - printf(ANSI_HIGHLIGHT_ON "[OVERRIDDEN]" ANSI_HIGHLIGHT_OFF " %s → %s\n", top, bottom); + printf("%s%s%s %s → %s\n", color_output(ANSI_HIGHLIGHT_ON), "[OVERRIDDEN]", color_output(ANSI_HIGHLIGHT_OFF), top, bottom); return 1; } @@ -98,7 +102,7 @@ static int notify_override_extended(const char *top, const char *bottom) { if (!(arg_flags & SHOW_EXTENDED)) return 0; - printf(ANSI_HIGHLIGHT_ON "[EXTENDED]" ANSI_HIGHLIGHT_OFF " %s → %s\n", top, bottom); + printf("%s%s%s %s → %s\n", color_output(ANSI_HIGHLIGHT_ON), "[EXTENDED]", color_output(ANSI_HIGHLIGHT_OFF), top, bottom); return 1; } -- 1.8.4.rc1