803fb7
From 9d67a3a2d4fd378ca04726c5eb5f31ee222c50e4 Mon Sep 17 00:00:00 2001
803fb7
From: Jan Synacek <jsynacek@redhat.com>
803fb7
Date: Tue, 19 Jan 2016 10:17:19 +0100
803fb7
Subject: [PATCH] basic/terminal-util: introduce SYSTEMD_COLORS environment
803fb7
 variable
803fb7
803fb7
... to determine if color output should be enabled. If the variable is not set,
803fb7
fall back to using on_tty(). Also, rewrite existing code to use
803fb7
colors_enabled() where appropriate.
803fb7
803fb7
Cherry-picked from: 40c9fe4c0862114dab390c8ed16f78cf056b9140
803fb7
Resolves: #1247963
803fb7
---
803fb7
 man/systemd.xml           |  7 +++++++
803fb7
 src/journal/journalctl.c  |  2 +-
803fb7
 src/login/loginctl.c      |  2 +-
803fb7
 src/machine/machinectl.c  |  2 +-
803fb7
 src/shared/util.c         | 13 +++++++++++++
803fb7
 src/shared/util.h         | 13 +++++++------
803fb7
 src/systemctl/systemctl.c |  2 +-
803fb7
 7 files changed, 31 insertions(+), 10 deletions(-)
803fb7
803fb7
diff --git a/man/systemd.xml b/man/systemd.xml
803fb7
index eb289f03b..30005b1ef 100644
803fb7
--- a/man/systemd.xml
803fb7
+++ b/man/systemd.xml
803fb7
@@ -754,6 +754,13 @@
803fb7
         script runlevel link farms.</para></listitem>
803fb7
       </varlistentry>
803fb7
 
803fb7
+      <varlistentry>
803fb7
+        <term><varname>$SYSTEMD_COLORS</varname></term>
803fb7
+
803fb7
+        <listitem><para>Controls whether colorized output should be generated.
803fb7
+        </para></listitem>
803fb7
+      </varlistentry>
803fb7
+
803fb7
       <varlistentry>
803fb7
         <term><varname>$LISTEN_PID</varname></term>
803fb7
         <term><varname>$LISTEN_FDS</varname></term>
803fb7
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
803fb7
index 8236d0810..7058788ef 100644
803fb7
--- a/src/journal/journalctl.c
803fb7
+++ b/src/journal/journalctl.c
803fb7
@@ -2140,7 +2140,7 @@ int main(int argc, char *argv[]) {
803fb7
                         flags =
803fb7
                                 arg_all * OUTPUT_SHOW_ALL |
803fb7
                                 arg_full * OUTPUT_FULL_WIDTH |
803fb7
-                                on_tty() * OUTPUT_COLOR |
803fb7
+                                colors_enabled() * OUTPUT_COLOR |
803fb7
                                 arg_catalog * OUTPUT_CATALOG |
803fb7
                                 arg_utc * OUTPUT_UTC;
803fb7
 
803fb7
diff --git a/src/login/loginctl.c b/src/login/loginctl.c
803fb7
index 6c8a59e7c..8e3bfbea8 100644
803fb7
--- a/src/login/loginctl.c
803fb7
+++ b/src/login/loginctl.c
803fb7
@@ -83,7 +83,7 @@ static OutputFlags get_output_flags(void) {
803fb7
                 arg_all * OUTPUT_SHOW_ALL |
803fb7
                 arg_full * OUTPUT_FULL_WIDTH |
803fb7
                 (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
803fb7
-                on_tty() * OUTPUT_COLOR;
803fb7
+                colors_enabled() * OUTPUT_COLOR;
803fb7
 }
803fb7
 
803fb7
 static int list_sessions(int argc, char *argv[], void *userdata) {
803fb7
diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c
803fb7
index f1910709d..ef1214a66 100644
803fb7
--- a/src/machine/machinectl.c
803fb7
+++ b/src/machine/machinectl.c
803fb7
@@ -105,7 +105,7 @@ static OutputFlags get_output_flags(void) {
803fb7
                 arg_all * OUTPUT_SHOW_ALL |
803fb7
                 arg_full * OUTPUT_FULL_WIDTH |
803fb7
                 (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
803fb7
-                on_tty() * OUTPUT_COLOR |
803fb7
+                colors_enabled() * OUTPUT_COLOR |
803fb7
                 !arg_quiet * OUTPUT_WARN_CUTOFF;
803fb7
 }
803fb7
 
803fb7
diff --git a/src/shared/util.c b/src/shared/util.c
803fb7
index 50925888d..dc5185269 100644
803fb7
--- a/src/shared/util.c
803fb7
+++ b/src/shared/util.c
803fb7
@@ -8146,3 +8146,16 @@ char *shell_maybe_quote(const char *s) {
803fb7
 
803fb7
         return r;
803fb7
 }
803fb7
+
803fb7
+bool colors_enabled(void) {
803fb7
+        const char *colors;
803fb7
+
803fb7
+        colors = getenv("SYSTEMD_COLORS");
803fb7
+        if (!colors) {
803fb7
+                if (streq_ptr(getenv("TERM"), "dumb"))
803fb7
+                        return false;
803fb7
+                return on_tty();
803fb7
+        }
803fb7
+
803fb7
+        return parse_boolean(colors) != 0;
803fb7
+}
803fb7
diff --git a/src/shared/util.h b/src/shared/util.h
803fb7
index 7ecfd8571..b4a4a491f 100644
803fb7
--- a/src/shared/util.h
803fb7
+++ b/src/shared/util.h
803fb7
@@ -485,29 +485,30 @@ unsigned lines(void);
803fb7
 void columns_lines_cache_reset(int _unused_ signum);
803fb7
 
803fb7
 bool on_tty(void);
803fb7
+bool colors_enabled(void);
803fb7
 
803fb7
 static inline const char *ansi_highlight(void) {
803fb7
-        return on_tty() ? ANSI_HIGHLIGHT_ON : "";
803fb7
+        return colors_enabled() ? ANSI_HIGHLIGHT_ON : "";
803fb7
 }
803fb7
 
803fb7
 static inline const char *ansi_highlight_red(void) {
803fb7
-        return on_tty() ? ANSI_HIGHLIGHT_RED_ON : "";
803fb7
+        return colors_enabled() ? ANSI_HIGHLIGHT_RED_ON : "";
803fb7
 }
803fb7
 
803fb7
 static inline const char *ansi_highlight_green(void) {
803fb7
-        return on_tty() ? ANSI_HIGHLIGHT_GREEN_ON : "";
803fb7
+        return colors_enabled() ? ANSI_HIGHLIGHT_GREEN_ON : "";
803fb7
 }
803fb7
 
803fb7
 static inline const char *ansi_highlight_yellow(void) {
803fb7
-        return on_tty() ? ANSI_HIGHLIGHT_YELLOW_ON : "";
803fb7
+        return colors_enabled() ? ANSI_HIGHLIGHT_YELLOW_ON : "";
803fb7
 }
803fb7
 
803fb7
 static inline const char *ansi_highlight_blue(void) {
803fb7
-        return on_tty() ? ANSI_HIGHLIGHT_BLUE_ON : "";
803fb7
+        return colors_enabled() ? ANSI_HIGHLIGHT_BLUE_ON : "";
803fb7
 }
803fb7
 
803fb7
 static inline const char *ansi_highlight_off(void) {
803fb7
-        return on_tty() ? ANSI_HIGHLIGHT_OFF : "";
803fb7
+        return colors_enabled() ? ANSI_HIGHLIGHT_OFF : "";
803fb7
 }
803fb7
 
803fb7
 int files_same(const char *filea, const char *fileb);
803fb7
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
803fb7
index 89d0b3b39..5d3a85fd9 100644
803fb7
--- a/src/systemctl/systemctl.c
803fb7
+++ b/src/systemctl/systemctl.c
803fb7
@@ -197,7 +197,7 @@ static OutputFlags get_output_flags(void) {
803fb7
                 arg_all * OUTPUT_SHOW_ALL |
803fb7
                 arg_full * OUTPUT_FULL_WIDTH |
803fb7
                 (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
803fb7
-                on_tty() * OUTPUT_COLOR |
803fb7
+                colors_enabled() * OUTPUT_COLOR |
803fb7
                 !arg_quiet * OUTPUT_WARN_CUTOFF;
803fb7
 }
803fb7