bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Ondrej Oprala <ooprala@redhat.com> |
| 3 | * Copyright (C) 2012-2014 Karel Zak <kzak@redhat.com> |
| 4 | * |
| 5 | * This file may be distributed under the terms of the |
| 6 | * GNU Lesser General Public License. |
| 7 | */ |
| 8 | #ifndef UTIL_LINUX_COLORS_H |
| 9 | #define UTIL_LINUX_COLORS_H |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <unistd.h> |
| 13 | |
| 14 | #define UL_COLOR_RESET "\033[0m" |
| 15 | #define UL_COLOR_BOLD "\033[1m" |
| 16 | #define UL_COLOR_HALFBRIGHT "\033[2m" |
| 17 | #define UL_COLOR_UNDERSCORE "\033[4m" |
| 18 | #define UL_COLOR_BLINK "\033[5m" |
| 19 | #define UL_COLOR_REVERSE "\033[7m" |
| 20 | |
| 21 | /* Standard colors */ |
| 22 | #define UL_COLOR_BLACK "\033[30m" |
| 23 | #define UL_COLOR_RED "\033[31m" |
| 24 | #define UL_COLOR_GREEN "\033[32m" |
| 25 | #define UL_COLOR_BROWN "\033[33m" /* well, brown */ |
| 26 | #define UL_COLOR_BLUE "\033[34m" |
| 27 | #define UL_COLOR_MAGENTA "\033[35m" |
| 28 | #define UL_COLOR_CYAN "\033[36m" |
| 29 | #define UL_COLOR_GRAY "\033[37m" |
| 30 | |
| 31 | /* Bold variants */ |
| 32 | #define UL_COLOR_DARK_GRAY "\033[1;30m" |
| 33 | #define UL_COLOR_BOLD_RED "\033[1;31m" |
| 34 | #define UL_COLOR_BOLD_GREEN "\033[1;32m" |
| 35 | #define UL_COLOR_BOLD_YELLOW "\033[1;33m" |
| 36 | #define UL_COLOR_BOLD_BLUE "\033[1;34m" |
| 37 | #define UL_COLOR_BOLD_MAGENTA "\033[1;35m" |
| 38 | #define UL_COLOR_BOLD_CYAN "\033[1;36m" |
| 39 | |
| 40 | #define UL_COLOR_WHITE "\033[1;37m" |
| 41 | |
| 42 | /* --color[=WHEN] */ |
| 43 | enum colortmode { |
| 44 | UL_COLORMODE_AUTO = 0, |
| 45 | UL_COLORMODE_NEVER, |
| 46 | UL_COLORMODE_ALWAYS, |
| 47 | UL_COLORMODE_UNDEF, |
| 48 | |
| 49 | __UL_NCOLORMODES /* last */ |
| 50 | }; |
| 51 | |
| 52 | extern int colormode_from_string(const char *str); |
| 53 | extern int colormode_or_err(const char *str, const char *errmsg); |
| 54 | |
| 55 | /* Initialize the global variable UL_COLOR_TERM_OK */ |
| 56 | extern int colors_init(int mode, const char *util_name); |
| 57 | |
| 58 | /* Returns 1 or 0 */ |
| 59 | extern int colors_wanted(void); |
| 60 | |
| 61 | /* temporary enable/disable colors */ |
| 62 | extern void colors_off(void); |
| 63 | extern void colors_on(void); |
| 64 | |
| 65 | |
| 66 | /* Set the color */ |
| 67 | extern void color_fenable(const char *seq, FILE *f); |
| 68 | |
| 69 | extern void color_scheme_fenable(const char *name, const char *dflt, FILE *f); |
| 70 | extern const char *color_scheme_get_sequence(const char *name, const char *dflt); |
| 71 | |
| 72 | static inline void color_enable(const char *seq) |
| 73 | { |
| 74 | color_fenable(seq, stdout); |
| 75 | } |
| 76 | |
| 77 | static inline void color_scheme_enable(const char *name, const char *dflt) |
| 78 | { |
| 79 | color_scheme_fenable(name, dflt, stdout); |
| 80 | } |
| 81 | |
| 82 | /* Reset colors to default */ |
| 83 | extern void color_fdisable(FILE *f); |
| 84 | |
| 85 | static inline void color_disable(void) |
| 86 | { |
| 87 | color_fdisable(stdout); |
| 88 | } |
| 89 | |
| 90 | /* converts "red" to UL_COLOR_RED, etc. */ |
| 91 | extern const char *color_sequence_from_colorname(const char *str); |
| 92 | |
| 93 | |
| 94 | #endif /* UTIL_LINUX_COLORS_H */ |