]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/colors: add support for auto, always and never modes
authorKarel Zak <kzak@redhat.com>
Mon, 6 May 2013 17:07:23 +0000 (19:07 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 6 May 2013 17:07:23 +0000 (19:07 +0200)
 ... to implement --color[=<when>]

Signed-off-by: Karel Zak <kzak@redhat.com>
include/colors.h
lib/Makemodule.am
lib/colors.c

index dd77bf6df256eba1f95ad9b3e86b6f4739f58dd3..f9e36fd5751f1be43685b47619f4f38a7b58288e 100644 (file)
 
 #define UL_COLOR_WHITE         "\033[1;37m"
 
+/* --color[=WHEN] */
+enum colortmode {
+       UL_COLORMODE_AUTO = 0,
+       UL_COLORMODE_NEVER,
+       UL_COLORMODE_ALWAYS,
+
+       __UL_NCOLORMODES        /* last */
+};
+
+extern int colormode_from_string(const char *str);
+
 /* Initialize the global variable OUT_IS_TERM */
-extern int colors_init(void);
+extern int colors_init(int mode);
 
 /* Set the color to CLR_SCHEME */
 extern void color_enable(const char *clr_scheme);
index afc2156c72c9bd0f229a4c3e20bff9d7159e0ba9..6c3a1b0f39188148ebcc588b1505604777a37c87 100644 (file)
@@ -45,6 +45,7 @@ check_PROGRAMS += \
        test_at \
        test_blkdev \
        test_canonicalize \
+       test_colors \
        test_fileutils \
        test_ismounted \
        test_mangle \
@@ -89,6 +90,9 @@ test_at_CFLAGS = -DTEST_PROGRAM_AT
 test_strutils_SOURCES = lib/strutils.c
 test_strutils_CFLAGS = -DTEST_PROGRAM
 
+test_colors_SOURCES = lib/colors.c
+test_colors_CFLAGS = -DTEST_PROGRAM
+
 test_randutils_SOURCES = lib/randutils.c
 test_randutils_CFLAGS = -DTEST_PROGRAM
 
index 6af038b318e32cdb7ec3ce9d8ec7f56279f59591..57ecae93dd34ddbd97da3c43366506bc54c8dacd 100644 (file)
@@ -4,14 +4,26 @@
  * This file may be distributed under the terms of the
  * GNU Lesser General Public License.
  */
+#include <c.h>
+#include <assert.h>
 
 #include "colors.h"
 
 static int ul_color_term_ok;
 
-int colors_init(void)
+int colors_init(int mode)
 {
-       ul_color_term_ok = isatty(STDOUT_FILENO);
+       switch (mode) {
+       case UL_COLORMODE_AUTO:
+               ul_color_term_ok = isatty(STDOUT_FILENO);
+               break;
+       case UL_COLORMODE_ALWAYS:
+               ul_color_term_ok = 1;
+               break;
+       case UL_COLORMODE_NEVER:
+       default:
+               ul_color_term_ok = 0;
+       }
        return ul_color_term_ok;
 }
 
@@ -26,3 +38,61 @@ void color_disable(void)
        if (ul_color_term_ok)
                fputs(UL_COLOR_RESET, stdout);
 }
+
+int colormode_from_string(const char *str)
+{
+       size_t i;
+       static const char *modes[] = {
+               [UL_COLORMODE_AUTO]   = "auto",
+               [UL_COLORMODE_NEVER]  = "never",
+               [UL_COLORMODE_ALWAYS] = "always"
+       };
+
+       if (!str || !*str)
+               return -EINVAL;
+
+       assert(ARRAY_SIZE(modes) == __UL_NCOLORMODES);
+
+       for (i = 0; i < ARRAY_SIZE(modes); i++) {
+               if (strcasecmp(str, modes[i]) == 0)
+                       return i;
+       }
+
+       return -EINVAL;
+}
+
+#ifdef TEST_PROGRAM
+# include <getopt.h>
+# include <err.h>
+
+int main(int argc, char *argv[])
+{
+       static const struct option longopts[] = {
+               { "colors", optional_argument, 0, 'c' },
+               { NULL, 0, 0, 0 }
+       };
+       int c, mode = UL_COLORMODE_NEVER;       /* default */
+
+       while ((c = getopt_long(argc, argv, "c::", longopts, NULL)) != -1) {
+               switch (c) {
+               case 'c':
+                       mode = UL_COLORMODE_AUTO;
+                       if (optarg) {
+                               char *p = *optarg == '=' ? optarg + 1 : optarg;
+
+                               mode = colormode_from_string(p);
+                               if (mode < 0)
+                                       errx(EXIT_FAILURE, "'%s' unsupported color mode", p);
+                       }
+                       break;
+               }
+       }
+
+       colors_init(mode);
+       color_enable(UL_COLOR_RED);
+       printf("Hello World!");
+       color_disable();
+       return EXIT_SUCCESS;
+}
+#endif
+