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