Harald Hoyer fe20ad
From 2c8837069cfd00a0a440d229f2086899cf43cd62 Mon Sep 17 00:00:00 2001
Harald Hoyer fe20ad
From: Daniel Schaal <farbing@web.de>
Harald Hoyer fe20ad
Date: Fri, 2 Aug 2013 07:59:02 +0200
Harald Hoyer fe20ad
Subject: [PATCH] systemd-delta: Only print colors when on a tty
Harald Hoyer fe20ad
Harald Hoyer fe20ad
This make systemd-delta follow the behaviour of systemctl
Harald Hoyer fe20ad
and journalctl.
Harald Hoyer fe20ad
Harald Hoyer fe20ad
https://bugs.freedesktop.org/show_bug.cgi?id=67656
Harald Hoyer fe20ad
Harald Hoyer fe20ad
[zj: unify color query methods between those three programs.]
Harald Hoyer fe20ad
---
Harald Hoyer fe20ad
 src/delta/delta.c         | 15 +++++---
Harald Hoyer fe20ad
 src/journal/journalctl.c  |  5 ++-
Harald Hoyer fe20ad
 src/shared/util.h         | 16 +++++++++
Harald Hoyer fe20ad
 src/systemctl/systemctl.c | 88 +++++++++++++++++------------------------------
Harald Hoyer fe20ad
 4 files changed, 60 insertions(+), 64 deletions(-)
Harald Hoyer fe20ad
Harald Hoyer fe20ad
diff --git a/src/delta/delta.c b/src/delta/delta.c
Harald Hoyer fe20ad
index 49c2fc3..b3272d9 100644
Harald Hoyer fe20ad
--- a/src/delta/delta.c
Harald Hoyer fe20ad
+++ b/src/delta/delta.c
Harald Hoyer fe20ad
@@ -66,7 +66,8 @@ static int notify_override_masked(const char *top, const char *bottom) {
Harald Hoyer fe20ad
         if (!(arg_flags & SHOW_MASKED))
Harald Hoyer fe20ad
                 return 0;
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
-        printf(ANSI_HIGHLIGHT_RED_ON "[MASKED]" ANSI_HIGHLIGHT_OFF "     %s → %s\n", top, bottom);
Harald Hoyer fe20ad
+        printf("%s%s%s     %s → %s\n",
Harald Hoyer fe20ad
+               ansi_highlight_red(), "[MASKED]", ansi_highlight_off(), top, bottom);
Harald Hoyer fe20ad
         return 1;
Harald Hoyer fe20ad
 }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
@@ -74,7 +75,8 @@ static int notify_override_equivalent(const char *top, const char *bottom) {
Harald Hoyer fe20ad
         if (!(arg_flags & SHOW_EQUIVALENT))
Harald Hoyer fe20ad
                 return 0;
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
-        printf(ANSI_HIGHLIGHT_GREEN_ON "[EQUIVALENT]" ANSI_HIGHLIGHT_OFF " %s → %s\n", top, bottom);
Harald Hoyer fe20ad
+        printf("%s%s%s %s → %s\n",
Harald Hoyer fe20ad
+               ansi_highlight_green(), "[EQUIVALENT]", ansi_highlight(), top, bottom);
Harald Hoyer fe20ad
         return 1;
Harald Hoyer fe20ad
 }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
@@ -82,7 +84,8 @@ static int notify_override_redirected(const char *top, const char *bottom) {
Harald Hoyer fe20ad
         if (!(arg_flags & SHOW_REDIRECTED))
Harald Hoyer fe20ad
                 return 0;
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
-        printf(ANSI_HIGHLIGHT_ON "[REDIRECTED]" ANSI_HIGHLIGHT_OFF "   %s → %s\n", top, bottom);
Harald Hoyer fe20ad
+        printf("%s%s%s   %s → %s\n",
Harald Hoyer fe20ad
+               ansi_highlight(), "[REDIRECTED]", ansi_highlight_off(), top, bottom);
Harald Hoyer fe20ad
         return 1;
Harald Hoyer fe20ad
 }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
@@ -90,7 +93,8 @@ static int notify_override_overridden(const char *top, const char *bottom) {
Harald Hoyer fe20ad
         if (!(arg_flags & SHOW_OVERRIDDEN))
Harald Hoyer fe20ad
                 return 0;
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
-        printf(ANSI_HIGHLIGHT_ON "[OVERRIDDEN]" ANSI_HIGHLIGHT_OFF " %s → %s\n", top, bottom);
Harald Hoyer fe20ad
+        printf("%s%s%s %s → %s\n",
Harald Hoyer fe20ad
+               ansi_highlight(), "[OVERRIDDEN]", ansi_highlight_off(), top, bottom);
Harald Hoyer fe20ad
         return 1;
Harald Hoyer fe20ad
 }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
@@ -98,7 +102,8 @@ static int notify_override_extended(const char *top, const char *bottom) {
Harald Hoyer fe20ad
         if (!(arg_flags & SHOW_EXTENDED))
Harald Hoyer fe20ad
                return 0;
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
-        printf(ANSI_HIGHLIGHT_ON "[EXTENDED]" ANSI_HIGHLIGHT_OFF "   %s → %s\n", top, bottom);
Harald Hoyer fe20ad
+        printf("%s%s%s   %s → %s\n",
Harald Hoyer fe20ad
+               ansi_highlight(), "[EXTENDED]", ansi_highlight_off(), top, bottom);
Harald Hoyer fe20ad
         return 1;
Harald Hoyer fe20ad
 }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
Harald Hoyer fe20ad
index dde2ed7..feea6bf 100644
Harald Hoyer fe20ad
--- a/src/journal/journalctl.c
Harald Hoyer fe20ad
+++ b/src/journal/journalctl.c
Harald Hoyer fe20ad
@@ -1605,14 +1605,13 @@ int main(int argc, char *argv[]) {
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
                         if (!arg_merge) {
Harald Hoyer fe20ad
                                 sd_id128_t boot_id;
Harald Hoyer fe20ad
-                                const char *color_on = on_tty() ? ANSI_HIGHLIGHT_ON : "",
Harald Hoyer fe20ad
-                                           *color_off = on_tty() ? ANSI_HIGHLIGHT_OFF : "";
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
                                 r = sd_journal_get_monotonic_usec(j, NULL, &boot_id);
Harald Hoyer fe20ad
                                 if (r >= 0) {
Harald Hoyer fe20ad
                                         if (previous_boot_id_valid &&
Harald Hoyer fe20ad
                                             !sd_id128_equal(boot_id, previous_boot_id))
Harald Hoyer fe20ad
-                                                printf("%s-- Reboot --%s\n", color_on, color_off);
Harald Hoyer fe20ad
+                                                printf("%s-- Reboot --%s\n",
Harald Hoyer fe20ad
+                                                       ansi_highlight(), ansi_highlight_off());
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
                                         previous_boot_id = boot_id;
Harald Hoyer fe20ad
                                         previous_boot_id_valid = true;
Harald Hoyer fe20ad
diff --git a/src/shared/util.h b/src/shared/util.h
Harald Hoyer fe20ad
index ac999c6..3be692e 100644
Harald Hoyer fe20ad
--- a/src/shared/util.h
Harald Hoyer fe20ad
+++ b/src/shared/util.h
Harald Hoyer fe20ad
@@ -374,6 +374,22 @@ void columns_lines_cache_reset(int _unused_ signum);
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
 bool on_tty(void);
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
+static inline const char *ansi_highlight(void) {
Harald Hoyer fe20ad
+        return on_tty() ? ANSI_HIGHLIGHT_ON : "";
Harald Hoyer fe20ad
+}
Harald Hoyer fe20ad
+
Harald Hoyer fe20ad
+static inline const char *ansi_highlight_red(void) {
Harald Hoyer fe20ad
+        return on_tty() ? ANSI_HIGHLIGHT_RED_ON : "";
Harald Hoyer fe20ad
+}
Harald Hoyer fe20ad
+
Harald Hoyer fe20ad
+static inline const char *ansi_highlight_green(void) {
Harald Hoyer fe20ad
+        return on_tty() ? ANSI_HIGHLIGHT_GREEN_ON : "";
Harald Hoyer fe20ad
+}
Harald Hoyer fe20ad
+
Harald Hoyer fe20ad
+static inline const char *ansi_highlight_off(void) {
Harald Hoyer fe20ad
+        return on_tty() ? ANSI_HIGHLIGHT_OFF : "";
Harald Hoyer fe20ad
+}
Harald Hoyer fe20ad
+
Harald Hoyer fe20ad
 int running_in_chroot(void);
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
 char *ellipsize(const char *s, size_t length, unsigned percent);
Harald Hoyer fe20ad
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
Harald Hoyer fe20ad
index 9f47b2c..aca033b 100644
Harald Hoyer fe20ad
--- a/src/systemctl/systemctl.c
Harald Hoyer fe20ad
+++ b/src/systemctl/systemctl.c
Harald Hoyer fe20ad
@@ -175,30 +175,6 @@ static void polkit_agent_open_if_enabled(void) {
Harald Hoyer fe20ad
 }
Harald Hoyer fe20ad
 #endif
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
-static const char *ansi_highlight(bool b) {
Harald Hoyer fe20ad
-
Harald Hoyer fe20ad
-        if (!on_tty())
Harald Hoyer fe20ad
-                return "";
Harald Hoyer fe20ad
-
Harald Hoyer fe20ad
-        return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
Harald Hoyer fe20ad
-}
Harald Hoyer fe20ad
-
Harald Hoyer fe20ad
-static const char *ansi_highlight_red(bool b) {
Harald Hoyer fe20ad
-
Harald Hoyer fe20ad
-        if (!on_tty())
Harald Hoyer fe20ad
-                return "";
Harald Hoyer fe20ad
-
Harald Hoyer fe20ad
-        return b ? ANSI_HIGHLIGHT_RED_ON : ANSI_HIGHLIGHT_OFF;
Harald Hoyer fe20ad
-}
Harald Hoyer fe20ad
-
Harald Hoyer fe20ad
-static const char *ansi_highlight_green(bool b) {
Harald Hoyer fe20ad
-
Harald Hoyer fe20ad
-        if (!on_tty())
Harald Hoyer fe20ad
-                return "";
Harald Hoyer fe20ad
-
Harald Hoyer fe20ad
-        return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
Harald Hoyer fe20ad
-}
Harald Hoyer fe20ad
-
Harald Hoyer fe20ad
 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
Harald Hoyer fe20ad
         assert(error);
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
@@ -381,14 +357,14 @@ static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
                 if (streq(u->load_state, "error") ||
Harald Hoyer fe20ad
                     streq(u->load_state, "not-found")) {
Harald Hoyer fe20ad
-                        on_loaded = on = ansi_highlight_red(true);
Harald Hoyer fe20ad
-                        off_loaded = off = ansi_highlight_red(false);
Harald Hoyer fe20ad
+                        on_loaded = on = ansi_highlight_red();
Harald Hoyer fe20ad
+                        off_loaded = off = ansi_highlight_off();
Harald Hoyer fe20ad
                 } else
Harald Hoyer fe20ad
                         on_loaded = off_loaded = "";
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
                 if (streq(u->active_state, "failed")) {
Harald Hoyer fe20ad
-                        on_active = on = ansi_highlight_red(true);
Harald Hoyer fe20ad
-                        off_active = off = ansi_highlight_red(false);
Harald Hoyer fe20ad
+                        on_active = on = ansi_highlight_red();
Harald Hoyer fe20ad
+                        off_active = off = ansi_highlight_off();
Harald Hoyer fe20ad
                 } else
Harald Hoyer fe20ad
                         on_active = off_active = "";
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
@@ -416,11 +392,11 @@ static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
Harald Hoyer fe20ad
                         if (job_count)
Harald Hoyer fe20ad
                                 printf("JOB    = Pending job for the unit.\n");
Harald Hoyer fe20ad
                         puts("");
Harald Hoyer fe20ad
-                        on = ansi_highlight(true);
Harald Hoyer fe20ad
-                        off = ansi_highlight(false);
Harald Hoyer fe20ad
+                        on = ansi_highlight();
Harald Hoyer fe20ad
+                        off = ansi_highlight_off();
Harald Hoyer fe20ad
                 } else {
Harald Hoyer fe20ad
-                        on = ansi_highlight_red(true);
Harald Hoyer fe20ad
-                        off = ansi_highlight_red(false);
Harald Hoyer fe20ad
+                        on = ansi_highlight_red();
Harald Hoyer fe20ad
+                        off = ansi_highlight_off();
Harald Hoyer fe20ad
                 }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
                 if (arg_all)
Harald Hoyer fe20ad
@@ -683,13 +659,13 @@ static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) {
Harald Hoyer fe20ad
                         printf("\n");
Harald Hoyer fe20ad
                 }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
-                on = ansi_highlight(true);
Harald Hoyer fe20ad
-                off = ansi_highlight(false);
Harald Hoyer fe20ad
+                on = ansi_highlight();
Harald Hoyer fe20ad
+                off = ansi_highlight_off();
Harald Hoyer fe20ad
                 if (!arg_no_legend)
Harald Hoyer fe20ad
                         printf("\n");
Harald Hoyer fe20ad
         } else {
Harald Hoyer fe20ad
-                on = ansi_highlight_red(true);
Harald Hoyer fe20ad
-                off = ansi_highlight_red(false);
Harald Hoyer fe20ad
+                on = ansi_highlight_red();
Harald Hoyer fe20ad
+                off = ansi_highlight_off();
Harald Hoyer fe20ad
         }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
         if (!arg_no_legend) {
Harald Hoyer fe20ad
@@ -838,11 +814,11 @@ static void output_unit_file_list(const UnitFileList *units, unsigned c) {
Harald Hoyer fe20ad
                     u->state == UNIT_FILE_MASKED_RUNTIME ||
Harald Hoyer fe20ad
                     u->state == UNIT_FILE_DISABLED ||
Harald Hoyer fe20ad
                     u->state == UNIT_FILE_INVALID) {
Harald Hoyer fe20ad
-                        on  = ansi_highlight_red(true);
Harald Hoyer fe20ad
-                        off = ansi_highlight_red(false);
Harald Hoyer fe20ad
+                        on  = ansi_highlight_red();
Harald Hoyer fe20ad
+                        off = ansi_highlight_off();
Harald Hoyer fe20ad
                 } else if (u->state == UNIT_FILE_ENABLED) {
Harald Hoyer fe20ad
-                        on  = ansi_highlight_green(true);
Harald Hoyer fe20ad
-                        off = ansi_highlight_green(false);
Harald Hoyer fe20ad
+                        on  = ansi_highlight_green();
Harald Hoyer fe20ad
+                        off = ansi_highlight_off();
Harald Hoyer fe20ad
                 } else
Harald Hoyer fe20ad
                         on = off = "";
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
@@ -1250,8 +1226,8 @@ static void list_jobs_print(struct job_info* jobs, size_t n) {
Harald Hoyer fe20ad
         assert(n == 0 || jobs);
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
         if (n == 0) {
Harald Hoyer fe20ad
-                on = ansi_highlight_green(true);
Harald Hoyer fe20ad
-                off = ansi_highlight_green(false);
Harald Hoyer fe20ad
+                on = ansi_highlight_green();
Harald Hoyer fe20ad
+                off = ansi_highlight_off();
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
                 printf("%sNo jobs running.%s\n", on, off);
Harald Hoyer fe20ad
                 return;
Harald Hoyer fe20ad
@@ -1287,8 +1263,8 @@ static void list_jobs_print(struct job_info* jobs, size_t n) {
Harald Hoyer fe20ad
                         _cleanup_free_ char *e = NULL;
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
                         if (streq(j->state, "running")) {
Harald Hoyer fe20ad
-                                on = ansi_highlight(true);
Harald Hoyer fe20ad
-                                off = ansi_highlight(false);
Harald Hoyer fe20ad
+                                on = ansi_highlight();
Harald Hoyer fe20ad
+                                off = ansi_highlight_off();
Harald Hoyer fe20ad
                         } else
Harald Hoyer fe20ad
                                 on = off = "";
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
@@ -1301,8 +1277,8 @@ static void list_jobs_print(struct job_info* jobs, size_t n) {
Harald Hoyer fe20ad
                 }
Harald Hoyer fe20ad
         }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
-        on = ansi_highlight(true);
Harald Hoyer fe20ad
-        off = ansi_highlight(false);
Harald Hoyer fe20ad
+        on = ansi_highlight();
Harald Hoyer fe20ad
+        off = ansi_highlight_off();
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
         if (on_tty())
Harald Hoyer fe20ad
                 printf("\n%s%zu jobs listed%s.\n", on, n, off);
Harald Hoyer fe20ad
@@ -2588,8 +2564,8 @@ static void print_status_info(UnitStatusInfo *i) {
Harald Hoyer fe20ad
                 printf("   Follow: unit currently follows state of %s\n", i->following);
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
         if (streq_ptr(i->load_state, "error")) {
Harald Hoyer fe20ad
-                on = ansi_highlight_red(true);
Harald Hoyer fe20ad
-                off = ansi_highlight_red(false);
Harald Hoyer fe20ad
+                on = ansi_highlight_red();
Harald Hoyer fe20ad
+                off = ansi_highlight_off();
Harald Hoyer fe20ad
         } else
Harald Hoyer fe20ad
                 on = off = "";
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
@@ -2639,11 +2615,11 @@ static void print_status_info(UnitStatusInfo *i) {
Harald Hoyer fe20ad
         ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
         if (streq_ptr(i->active_state, "failed")) {
Harald Hoyer fe20ad
-                on = ansi_highlight_red(true);
Harald Hoyer fe20ad
-                off = ansi_highlight_red(false);
Harald Hoyer fe20ad
+                on = ansi_highlight_red();
Harald Hoyer fe20ad
+                off = ansi_highlight_off();
Harald Hoyer fe20ad
         } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
Harald Hoyer fe20ad
-                on = ansi_highlight_green(true);
Harald Hoyer fe20ad
-                off = ansi_highlight_green(false);
Harald Hoyer fe20ad
+                on = ansi_highlight_green();
Harald Hoyer fe20ad
+                off = ansi_highlight_off();
Harald Hoyer fe20ad
         } else
Harald Hoyer fe20ad
                 on = off = "";
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
@@ -2718,8 +2694,8 @@ static void print_status_info(UnitStatusInfo *i) {
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
                 good = is_clean_exit_lsb(p->code, p->status, NULL);
Harald Hoyer fe20ad
                 if (!good) {
Harald Hoyer fe20ad
-                        on = ansi_highlight_red(true);
Harald Hoyer fe20ad
-                        off = ansi_highlight_red(false);
Harald Hoyer fe20ad
+                        on = ansi_highlight_red();
Harald Hoyer fe20ad
+                        off = ansi_highlight_off();
Harald Hoyer fe20ad
                 } else
Harald Hoyer fe20ad
                         on = off = "";
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
@@ -2838,8 +2814,8 @@ static void print_status_info(UnitStatusInfo *i) {
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
         if (i->need_daemon_reload)
Harald Hoyer fe20ad
                 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %sdaemon-reload' recommended.\n",
Harald Hoyer fe20ad
-                       ansi_highlight_red(true),
Harald Hoyer fe20ad
-                       ansi_highlight_red(false),
Harald Hoyer fe20ad
+                       ansi_highlight_red(),
Harald Hoyer fe20ad
+                       ansi_highlight_off(),
Harald Hoyer fe20ad
                        arg_scope == UNIT_FILE_SYSTEM ? "" : "--user ");
Harald Hoyer fe20ad
 }
Harald Hoyer fe20ad