]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/colors: force to "never" mode on non-terminal output
authorKarel Zak <kzak@redhat.com>
Mon, 4 Feb 2019 13:14:37 +0000 (14:14 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 4 Feb 2019 13:14:37 +0000 (14:14 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/colors.h
lib/colors.c

index 3636beb558337059f4a127040cae4c29a6e50de5..d4ae4e407897cf4f77ad9647c3e6d6ad50922606 100644 (file)
@@ -38,6 +38,9 @@ extern int colors_init(int mode, const char *util_name);
 /* Returns 1 or 0 */
 extern int colors_wanted(void);
 
+/* Returns UL_COLORMODE_* */
+extern int colors_mode(void);
+
 /* temporary enable/disable colors */
 extern void colors_off(void);
 extern void colors_on(void);
index f636ecc4f44b95fbb722744fc6aa7b2bbb7ef512..bea7bd1d77c441b5d3c95b685bacf1132538fc8b 100644 (file)
@@ -653,9 +653,6 @@ static int colors_terminal_is_ready(void)
 {
        int ncolors = -1;
 
-       if (isatty(STDOUT_FILENO) != 1)
-               goto none;
-
 #if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
        {
                int ret;
@@ -692,11 +689,16 @@ int colors_init(int mode, const char *name)
        struct ul_color_ctl *cc = &ul_colors;
 
        cc->utilname = name;
-       cc->mode = mode;
 
        termcolors_init_debug();
 
-       if (mode == UL_COLORMODE_UNDEF && (ready = colors_terminal_is_ready())) {
+       if (mode != UL_COLORMODE_ALWAYS && !isatty(STDOUT_FILENO))
+               cc->mode = UL_COLORMODE_NEVER;
+       else
+               cc->mode = mode;
+
+       if (cc->mode == UL_COLORMODE_UNDEF
+           && (ready = colors_terminal_is_ready())) {
                int rc = colors_read_configuration(cc);
                if (rc)
                        cc->mode = UL_COLORMODE_DEFAULT;
@@ -754,6 +756,14 @@ int colors_wanted(void)
        return ul_colors.has_colors;
 }
 
+/*
+ * Returns mode
+ */
+int colors_mode(void)
+{
+       return ul_colors.mode;
+}
+
 /*
  * Enable @seq color
  */