commit:     03c2d27602bc94aaa1660da7b084d40a9cf70f5d
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Mon May 10 09:12:43 2021 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Mon May 10 09:12:43 2021 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=03c2d276

main: add global option --color to force colour output

allow override for colouring to be enabled, this reorganises the code a
bit because argument handling is done by the applets, and not by the
main process, e.g. total colour handling can unfortunately not be done
in a single place.

Bug: https://bugs.gentoo.org/769929
Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>

 applets.h | 10 +++++++---
 main.c    | 23 +++++++++--------------
 2 files changed, 16 insertions(+), 17 deletions(-)

diff --git a/applets.h b/applets.h
index 956ab67..8918ff0 100644
--- a/applets.h
+++ b/applets.h
@@ -1,9 +1,10 @@
 /*
- * Copyright 2005-2019 Gentoo Foundation
+ * Copyright 2005-2021 Gentoo Foundation
  * Distributed under the terms of the GNU General Public License v2
  *
  * Copyright 2005-2010 Ned Ludd        - <[email protected]>
  * Copyright 2005-2014 Mike Frysinger  - <[email protected]>
+ * Copyright 2019-     Fabian Groffen  - <[email protected]>
  */
 
 #ifndef _APPLETS_H_
@@ -133,6 +134,7 @@ static const struct applet_t {
        {"verbose",   no_argument, NULL, 'v'}, \
        {"quiet",     no_argument, NULL, 'q'}, \
        {"nocolor",   no_argument, NULL, 'C'}, \
+       {"color",     no_argument, NULL, 0x2}, \
        {"help",      no_argument, NULL, 'h'}, \
        {"version",   no_argument, NULL, 'V'}, \
        {NULL,        no_argument, NULL, 0x0}
@@ -141,6 +143,7 @@ static const struct applet_t {
        "Report full package versions, emit more elaborate output", \
        "Tighter output; suppress warnings", \
        "Don't output color", \
+       "Force color in output", \
        "Print this help and exit", \
        "Print version and exit", \
        NULL
@@ -150,8 +153,9 @@ static const struct applet_t {
        case 'q': setup_quiet(); break; \
        case 'V': version_barf(); break; \
        case 'h': applet ## _usage(EXIT_SUCCESS); break; \
-       case 'C': no_colors(); break; \
-       default: applet ## _usage(EXIT_FAILURE); break;
+       case 'C': color_clear(); setenv("NOCOLOR", "true", 1); break; \
+       case 0x2: color_remap(); unsetenv("NOCOLOR"); break; \
+       default:  applet ## _usage(EXIT_FAILURE); break;
 
 extern char *portarch;
 extern char *portroot;

diff --git a/main.c b/main.c
index ceab587..01b2542 100644
--- a/main.c
+++ b/main.c
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005-2020 Gentoo Foundation
+ * Copyright 2005-2021 Gentoo Foundation
  * Distributed under the terms of the GNU General Public License v2
  *
  * Copyright 2005-2008 Ned Ludd        - <[email protected]>
@@ -70,14 +70,6 @@ init_coredumps(void)
 }
 #endif
 
-void
-no_colors(void)
-{
-       BOLD = NORM = BLUE = DKBLUE = CYAN = GREEN = DKGREEN = \
-                  MAGENTA = RED = YELLOW = BRYELLOW = WHITE = "";
-       setenv("NOCOLOR", "true", 1);
-}
-
 void
 setup_quiet(void)
 {
@@ -1041,10 +1033,13 @@ initialize_portage_env(void)
        if (getenv("PORTAGE_QUIET") != NULL)
                setup_quiet();
 
-       if (nocolor)
-               no_colors();
-       else
+       if (nocolor) {
+               color_clear();
+               setenv("NOCOLOR", "true", 1);
+       } else {
                color_remap();
+               unsetenv("NOCOLOR");
+       }
 }
 
 int main(int argc, char **argv)
@@ -1063,11 +1058,11 @@ int main(int argc, char **argv)
        twidth = 0;
        if (fstat(fileno(stdout), &st) != -1) {
                if (!isatty(fileno(stdout))) {
-                       no_colors();
+                       nocolor = 1;
                } else {
                        if ((getenv("TERM") == NULL) ||
                                        (strcmp(getenv("TERM"), "dumb") == 0))
-                               no_colors();
+                               nocolor = 1;
                        if (ioctl(0, TIOCGWINSZ, &winsz) == 0 && winsz.ws_col > 
0)
                                twidth = (int)winsz.ws_col;
                }

Reply via email to