]>
Commit | Line | Data |
---|---|---|
dfa68ad1 OO |
1 | /* |
2 | * Copyright (C) 2012 Ondrej Oprala <ooprala@redhat.com> | |
3 | * | |
4 | * This file may be distributed under the terms of the | |
5 | * GNU Lesser General Public License. | |
6 | */ | |
a10c0434 KZ |
7 | #include <c.h> |
8 | #include <assert.h> | |
dfa68ad1 OO |
9 | |
10 | #include "colors.h" | |
11 | ||
12 | static int ul_color_term_ok; | |
13 | ||
a10c0434 | 14 | int colors_init(int mode) |
dfa68ad1 | 15 | { |
a10c0434 KZ |
16 | switch (mode) { |
17 | case UL_COLORMODE_AUTO: | |
18 | ul_color_term_ok = isatty(STDOUT_FILENO); | |
19 | break; | |
20 | case UL_COLORMODE_ALWAYS: | |
21 | ul_color_term_ok = 1; | |
22 | break; | |
23 | case UL_COLORMODE_NEVER: | |
24 | default: | |
25 | ul_color_term_ok = 0; | |
26 | } | |
dfa68ad1 OO |
27 | return ul_color_term_ok; |
28 | } | |
29 | ||
80a1712f KZ |
30 | int colors_wanted(void) |
31 | { | |
32 | return ul_color_term_ok; | |
33 | } | |
34 | ||
35 | void color_fenable(const char *color_scheme, FILE *f) | |
dfa68ad1 | 36 | { |
5aaee63c | 37 | if (ul_color_term_ok && color_scheme) |
80a1712f | 38 | fputs(color_scheme, f); |
dfa68ad1 OO |
39 | } |
40 | ||
80a1712f | 41 | void color_fdisable(FILE *f) |
dfa68ad1 OO |
42 | { |
43 | if (ul_color_term_ok) | |
80a1712f | 44 | fputs(UL_COLOR_RESET, f); |
dfa68ad1 | 45 | } |
a10c0434 KZ |
46 | |
47 | int colormode_from_string(const char *str) | |
48 | { | |
49 | size_t i; | |
50 | static const char *modes[] = { | |
51 | [UL_COLORMODE_AUTO] = "auto", | |
52 | [UL_COLORMODE_NEVER] = "never", | |
53 | [UL_COLORMODE_ALWAYS] = "always" | |
54 | }; | |
55 | ||
56 | if (!str || !*str) | |
57 | return -EINVAL; | |
58 | ||
59 | assert(ARRAY_SIZE(modes) == __UL_NCOLORMODES); | |
60 | ||
61 | for (i = 0; i < ARRAY_SIZE(modes); i++) { | |
62 | if (strcasecmp(str, modes[i]) == 0) | |
63 | return i; | |
64 | } | |
65 | ||
66 | return -EINVAL; | |
67 | } | |
68 | ||
b7faf991 KZ |
69 | int colormode_or_err(const char *str, const char *errmsg) |
70 | { | |
71 | const char *p = str && *str == '=' ? str + 1 : str; | |
72 | int colormode; | |
73 | ||
74 | colormode = colormode_from_string(p); | |
75 | if (colormode < 0) | |
76 | errx(EXIT_FAILURE, "%s: '%s'", errmsg, p); | |
77 | ||
78 | return colormode; | |
79 | } | |
80 | ||
81 | ||
a10c0434 KZ |
82 | #ifdef TEST_PROGRAM |
83 | # include <getopt.h> | |
a10c0434 KZ |
84 | int main(int argc, char *argv[]) |
85 | { | |
86 | static const struct option longopts[] = { | |
87 | { "colors", optional_argument, 0, 'c' }, | |
88 | { NULL, 0, 0, 0 } | |
89 | }; | |
90 | int c, mode = UL_COLORMODE_NEVER; /* default */ | |
91 | ||
92 | while ((c = getopt_long(argc, argv, "c::", longopts, NULL)) != -1) { | |
93 | switch (c) { | |
94 | case 'c': | |
95 | mode = UL_COLORMODE_AUTO; | |
b7faf991 KZ |
96 | if (optarg) |
97 | mode = colormode_or_err(optarg, "unsupported color mode"); | |
a10c0434 KZ |
98 | break; |
99 | } | |
100 | } | |
101 | ||
102 | colors_init(mode); | |
103 | color_enable(UL_COLOR_RED); | |
104 | printf("Hello World!"); | |
105 | color_disable(); | |
106 | return EXIT_SUCCESS; | |
107 | } | |
108 | #endif | |
109 |