color.con commit Merge branch 'maint' (2f80de9)
   1#include "cache.h"
   2#include "color.h"
   3
   4int git_use_color_default = 0;
   5
   6/*
   7 * The list of available column colors.
   8 */
   9const char *column_colors_ansi[] = {
  10        GIT_COLOR_RED,
  11        GIT_COLOR_GREEN,
  12        GIT_COLOR_YELLOW,
  13        GIT_COLOR_BLUE,
  14        GIT_COLOR_MAGENTA,
  15        GIT_COLOR_CYAN,
  16        GIT_COLOR_BOLD_RED,
  17        GIT_COLOR_BOLD_GREEN,
  18        GIT_COLOR_BOLD_YELLOW,
  19        GIT_COLOR_BOLD_BLUE,
  20        GIT_COLOR_BOLD_MAGENTA,
  21        GIT_COLOR_BOLD_CYAN,
  22        GIT_COLOR_RESET,
  23};
  24
  25/* Ignore the RESET at the end when giving the size */
  26const int column_colors_ansi_max = ARRAY_SIZE(column_colors_ansi) - 1;
  27
  28static int parse_color(const char *name, int len)
  29{
  30        static const char * const color_names[] = {
  31                "normal", "black", "red", "green", "yellow",
  32                "blue", "magenta", "cyan", "white"
  33        };
  34        char *end;
  35        int i;
  36        for (i = 0; i < ARRAY_SIZE(color_names); i++) {
  37                const char *str = color_names[i];
  38                if (!strncasecmp(name, str, len) && !str[len])
  39                        return i - 1;
  40        }
  41        i = strtol(name, &end, 10);
  42        if (end - name == len && i >= -1 && i <= 255)
  43                return i;
  44        return -2;
  45}
  46
  47static int parse_attr(const char *name, int len)
  48{
  49        static const int attr_values[] = { 1, 2, 4, 5, 7 };
  50        static const char * const attr_names[] = {
  51                "bold", "dim", "ul", "blink", "reverse"
  52        };
  53        int i;
  54        for (i = 0; i < ARRAY_SIZE(attr_names); i++) {
  55                const char *str = attr_names[i];
  56                if (!strncasecmp(name, str, len) && !str[len])
  57                        return attr_values[i];
  58        }
  59        return -1;
  60}
  61
  62void color_parse(const char *value, const char *var, char *dst)
  63{
  64        color_parse_mem(value, strlen(value), var, dst);
  65}
  66
  67void color_parse_mem(const char *value, int value_len, const char *var,
  68                char *dst)
  69{
  70        const char *ptr = value;
  71        int len = value_len;
  72        unsigned int attr = 0;
  73        int fg = -2;
  74        int bg = -2;
  75
  76        if (!strncasecmp(value, "reset", len)) {
  77                strcpy(dst, GIT_COLOR_RESET);
  78                return;
  79        }
  80
  81        /* [fg [bg]] [attr]... */
  82        while (len > 0) {
  83                const char *word = ptr;
  84                int val, wordlen = 0;
  85
  86                while (len > 0 && !isspace(word[wordlen])) {
  87                        wordlen++;
  88                        len--;
  89                }
  90
  91                ptr = word + wordlen;
  92                while (len > 0 && isspace(*ptr)) {
  93                        ptr++;
  94                        len--;
  95                }
  96
  97                val = parse_color(word, wordlen);
  98                if (val >= -1) {
  99                        if (fg == -2) {
 100                                fg = val;
 101                                continue;
 102                        }
 103                        if (bg == -2) {
 104                                bg = val;
 105                                continue;
 106                        }
 107                        goto bad;
 108                }
 109                val = parse_attr(word, wordlen);
 110                if (0 <= val)
 111                        attr |= (1 << val);
 112                else
 113                        goto bad;
 114        }
 115
 116        if (attr || fg >= 0 || bg >= 0) {
 117                int sep = 0;
 118                int i;
 119
 120                *dst++ = '\033';
 121                *dst++ = '[';
 122
 123                for (i = 0; attr; i++) {
 124                        unsigned bit = (1 << i);
 125                        if (!(attr & bit))
 126                                continue;
 127                        attr &= ~bit;
 128                        if (sep++)
 129                                *dst++ = ';';
 130                        *dst++ = '0' + i;
 131                }
 132                if (fg >= 0) {
 133                        if (sep++)
 134                                *dst++ = ';';
 135                        if (fg < 8) {
 136                                *dst++ = '3';
 137                                *dst++ = '0' + fg;
 138                        } else {
 139                                dst += sprintf(dst, "38;5;%d", fg);
 140                        }
 141                }
 142                if (bg >= 0) {
 143                        if (sep++)
 144                                *dst++ = ';';
 145                        if (bg < 8) {
 146                                *dst++ = '4';
 147                                *dst++ = '0' + bg;
 148                        } else {
 149                                dst += sprintf(dst, "48;5;%d", bg);
 150                        }
 151                }
 152                *dst++ = 'm';
 153        }
 154        *dst = 0;
 155        return;
 156bad:
 157        die("bad color value '%.*s' for variable '%s'", value_len, value, var);
 158}
 159
 160int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
 161{
 162        if (value) {
 163                if (!strcasecmp(value, "never"))
 164                        return 0;
 165                if (!strcasecmp(value, "always"))
 166                        return 1;
 167                if (!strcasecmp(value, "auto"))
 168                        goto auto_color;
 169        }
 170
 171        if (!var)
 172                return -1;
 173
 174        /* Missing or explicit false to turn off colorization */
 175        if (!git_config_bool(var, value))
 176                return 0;
 177
 178        /* any normal truth value defaults to 'auto' */
 179 auto_color:
 180        if (stdout_is_tty < 0)
 181                stdout_is_tty = isatty(1);
 182        if (stdout_is_tty || (pager_in_use() && pager_use_color)) {
 183                char *term = getenv("TERM");
 184                if (term && strcmp(term, "dumb"))
 185                        return 1;
 186        }
 187        return 0;
 188}
 189
 190int git_color_default_config(const char *var, const char *value, void *cb)
 191{
 192        if (!strcmp(var, "color.ui")) {
 193                git_use_color_default = git_config_colorbool(var, value, -1);
 194                return 0;
 195        }
 196
 197        return git_default_config(var, value, cb);
 198}
 199
 200void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
 201{
 202        if (*color)
 203                fprintf(fp, "%s", color);
 204        fprintf(fp, "%s", sb->buf);
 205        if (*color)
 206                fprintf(fp, "%s", GIT_COLOR_RESET);
 207}
 208
 209static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
 210                va_list args, const char *trail)
 211{
 212        int r = 0;
 213
 214        if (*color)
 215                r += fprintf(fp, "%s", color);
 216        r += vfprintf(fp, fmt, args);
 217        if (*color)
 218                r += fprintf(fp, "%s", GIT_COLOR_RESET);
 219        if (trail)
 220                r += fprintf(fp, "%s", trail);
 221        return r;
 222}
 223
 224
 225
 226int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
 227{
 228        va_list args;
 229        int r;
 230        va_start(args, fmt);
 231        r = color_vfprintf(fp, color, fmt, args, NULL);
 232        va_end(args);
 233        return r;
 234}
 235
 236int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
 237{
 238        va_list args;
 239        int r;
 240        va_start(args, fmt);
 241        r = color_vfprintf(fp, color, fmt, args, "\n");
 242        va_end(args);
 243        return r;
 244}
 245
 246int color_is_nil(const char *c)
 247{
 248        return !strcmp(c, "NIL");
 249}