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