color.con commit Merge branch 'pb/gitweb' (01ed107)
   1#include "cache.h"
   2#include "color.h"
   3
   4#define COLOR_RESET "\033[m"
   5
   6int git_use_color_default = 0;
   7
   8static int parse_color(const char *name, int len)
   9{
  10        static const char * const color_names[] = {
  11                "normal", "black", "red", "green", "yellow",
  12                "blue", "magenta", "cyan", "white"
  13        };
  14        char *end;
  15        int i;
  16        for (i = 0; i < ARRAY_SIZE(color_names); i++) {
  17                const char *str = color_names[i];
  18                if (!strncasecmp(name, str, len) && !str[len])
  19                        return i - 1;
  20        }
  21        i = strtol(name, &end, 10);
  22        if (end - name == len && i >= -1 && i <= 255)
  23                return i;
  24        return -2;
  25}
  26
  27static int parse_attr(const char *name, int len)
  28{
  29        static const int attr_values[] = { 1, 2, 4, 5, 7 };
  30        static const char * const attr_names[] = {
  31                "bold", "dim", "ul", "blink", "reverse"
  32        };
  33        int i;
  34        for (i = 0; i < ARRAY_SIZE(attr_names); i++) {
  35                const char *str = attr_names[i];
  36                if (!strncasecmp(name, str, len) && !str[len])
  37                        return attr_values[i];
  38        }
  39        return -1;
  40}
  41
  42void color_parse(const char *value, const char *var, char *dst)
  43{
  44        const char *ptr = value;
  45        int attr = -1;
  46        int fg = -2;
  47        int bg = -2;
  48
  49        if (!strcasecmp(value, "reset")) {
  50                strcpy(dst, "\033[m");
  51                return;
  52        }
  53
  54        /* [fg [bg]] [attr] */
  55        while (*ptr) {
  56                const char *word = ptr;
  57                int val, len = 0;
  58
  59                while (word[len] && !isspace(word[len]))
  60                        len++;
  61
  62                ptr = word + len;
  63                while (*ptr && isspace(*ptr))
  64                        ptr++;
  65
  66                val = parse_color(word, len);
  67                if (val >= -1) {
  68                        if (fg == -2) {
  69                                fg = val;
  70                                continue;
  71                        }
  72                        if (bg == -2) {
  73                                bg = val;
  74                                continue;
  75                        }
  76                        goto bad;
  77                }
  78                val = parse_attr(word, len);
  79                if (val < 0 || attr != -1)
  80                        goto bad;
  81                attr = val;
  82        }
  83
  84        if (attr >= 0 || fg >= 0 || bg >= 0) {
  85                int sep = 0;
  86
  87                *dst++ = '\033';
  88                *dst++ = '[';
  89                if (attr >= 0) {
  90                        *dst++ = '0' + attr;
  91                        sep++;
  92                }
  93                if (fg >= 0) {
  94                        if (sep++)
  95                                *dst++ = ';';
  96                        if (fg < 8) {
  97                                *dst++ = '3';
  98                                *dst++ = '0' + fg;
  99                        } else {
 100                                dst += sprintf(dst, "38;5;%d", fg);
 101                        }
 102                }
 103                if (bg >= 0) {
 104                        if (sep++)
 105                                *dst++ = ';';
 106                        if (bg < 8) {
 107                                *dst++ = '4';
 108                                *dst++ = '0' + bg;
 109                        } else {
 110                                dst += sprintf(dst, "48;5;%d", bg);
 111                        }
 112                }
 113                *dst++ = 'm';
 114        }
 115        *dst = 0;
 116        return;
 117bad:
 118        die("bad config value '%s' for variable '%s'", value, var);
 119}
 120
 121int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
 122{
 123        if (value) {
 124                if (!strcasecmp(value, "never"))
 125                        return 0;
 126                if (!strcasecmp(value, "always"))
 127                        return 1;
 128                if (!strcasecmp(value, "auto"))
 129                        goto auto_color;
 130        }
 131
 132        /* Missing or explicit false to turn off colorization */
 133        if (!git_config_bool(var, value))
 134                return 0;
 135
 136        /* any normal truth value defaults to 'auto' */
 137 auto_color:
 138        if (stdout_is_tty < 0)
 139                stdout_is_tty = isatty(1);
 140        if (stdout_is_tty || (pager_in_use() && pager_use_color)) {
 141                char *term = getenv("TERM");
 142                if (term && strcmp(term, "dumb"))
 143                        return 1;
 144        }
 145        return 0;
 146}
 147
 148int git_color_default_config(const char *var, const char *value, void *cb)
 149{
 150        if (!strcmp(var, "color.ui")) {
 151                git_use_color_default = git_config_colorbool(var, value, -1);
 152                return 0;
 153        }
 154
 155        return git_default_config(var, value, cb);
 156}
 157
 158static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
 159                va_list args, const char *trail)
 160{
 161        int r = 0;
 162
 163        if (*color)
 164                r += fprintf(fp, "%s", color);
 165        r += vfprintf(fp, fmt, args);
 166        if (*color)
 167                r += fprintf(fp, "%s", COLOR_RESET);
 168        if (trail)
 169                r += fprintf(fp, "%s", trail);
 170        return r;
 171}
 172
 173
 174
 175int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
 176{
 177        va_list args;
 178        int r;
 179        va_start(args, fmt);
 180        r = color_vfprintf(fp, color, fmt, args, NULL);
 181        va_end(args);
 182        return r;
 183}
 184
 185int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
 186{
 187        va_list args;
 188        int r;
 189        va_start(args, fmt);
 190        r = color_vfprintf(fp, color, fmt, args, "\n");
 191        va_end(args);
 192        return r;
 193}