linux/tools/perf/util/color.c
<<
>>
Prefs
   1#include "cache.h"
   2#include "color.h"
   3
   4int perf_use_color_default = -1;
   5
   6static int parse_color(const char *name, int len)
   7{
   8        static const char * const color_names[] = {
   9                "normal", "black", "red", "green", "yellow",
  10                "blue", "magenta", "cyan", "white"
  11        };
  12        char *end;
  13        int i;
  14
  15        for (i = 0; i < (int)ARRAY_SIZE(color_names); i++) {
  16                const char *str = color_names[i];
  17                if (!strncasecmp(name, str, len) && !str[len])
  18                        return i - 1;
  19        }
  20        i = strtol(name, &end, 10);
  21        if (end - name == len && i >= -1 && i <= 255)
  22                return i;
  23        return -2;
  24}
  25
  26static int parse_attr(const char *name, int len)
  27{
  28        static const int attr_values[] = { 1, 2, 4, 5, 7 };
  29        static const char * const attr_names[] = {
  30                "bold", "dim", "ul", "blink", "reverse"
  31        };
  32        unsigned int i;
  33
  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        color_parse_mem(value, strlen(value), var, dst);
  45}
  46
  47void color_parse_mem(const char *value, int value_len, const char *var,
  48                char *dst)
  49{
  50        const char *ptr = value;
  51        int len = value_len;
  52        int attr = -1;
  53        int fg = -2;
  54        int bg = -2;
  55
  56        if (!strncasecmp(value, "reset", len)) {
  57                strcpy(dst, PERF_COLOR_RESET);
  58                return;
  59        }
  60
  61        /* [fg [bg]] [attr] */
  62        while (len > 0) {
  63                const char *word = ptr;
  64                int val, wordlen = 0;
  65
  66                while (len > 0 && !isspace(word[wordlen])) {
  67                        wordlen++;
  68                        len--;
  69                }
  70
  71                ptr = word + wordlen;
  72                while (len > 0 && isspace(*ptr)) {
  73                        ptr++;
  74                        len--;
  75                }
  76
  77                val = parse_color(word, wordlen);
  78                if (val >= -1) {
  79                        if (fg == -2) {
  80                                fg = val;
  81                                continue;
  82                        }
  83                        if (bg == -2) {
  84                                bg = val;
  85                                continue;
  86                        }
  87                        goto bad;
  88                }
  89                val = parse_attr(word, wordlen);
  90                if (val < 0 || attr != -1)
  91                        goto bad;
  92                attr = val;
  93        }
  94
  95        if (attr >= 0 || fg >= 0 || bg >= 0) {
  96                int sep = 0;
  97
  98                *dst++ = '\033';
  99                *dst++ = '[';
 100                if (attr >= 0) {
 101                        *dst++ = '0' + attr;
 102                        sep++;
 103                }
 104                if (fg >= 0) {
 105                        if (sep++)
 106                                *dst++ = ';';
 107                        if (fg < 8) {
 108                                *dst++ = '3';
 109                                *dst++ = '0' + fg;
 110                        } else {
 111                                dst += sprintf(dst, "38;5;%d", fg);
 112                        }
 113                }
 114                if (bg >= 0) {
 115                        if (sep++)
 116                                *dst++ = ';';
 117                        if (bg < 8) {
 118                                *dst++ = '4';
 119                                *dst++ = '0' + bg;
 120                        } else {
 121                                dst += sprintf(dst, "48;5;%d", bg);
 122                        }
 123                }
 124                *dst++ = 'm';
 125        }
 126        *dst = 0;
 127        return;
 128bad:
 129        die("bad color value '%.*s' for variable '%s'", value_len, value, var);
 130}
 131
 132int perf_config_colorbool(const char *var, const char *value, int stdout_is_tty)
 133{
 134        if (value) {
 135                if (!strcasecmp(value, "never"))
 136                        return 0;
 137                if (!strcasecmp(value, "always"))
 138                        return 1;
 139                if (!strcasecmp(value, "auto"))
 140                        goto auto_color;
 141        }
 142
 143        /* Missing or explicit false to turn off colorization */
 144        if (!perf_config_bool(var, value))
 145                return 0;
 146
 147        /* any normal truth value defaults to 'auto' */
 148 auto_color:
 149        if (stdout_is_tty < 0)
 150                stdout_is_tty = isatty(1);
 151        if (stdout_is_tty || (pager_in_use() && pager_use_color)) {
 152                char *term = getenv("TERM");
 153                if (term && strcmp(term, "dumb"))
 154                        return 1;
 155        }
 156        return 0;
 157}
 158
 159int perf_color_default_config(const char *var, const char *value, void *cb)
 160{
 161        if (!strcmp(var, "color.ui")) {
 162                perf_use_color_default = perf_config_colorbool(var, value, -1);
 163                return 0;
 164        }
 165
 166        return perf_default_config(var, value, cb);
 167}
 168
 169static int __color_vfprintf(FILE *fp, const char *color, const char *fmt,
 170                va_list args, const char *trail)
 171{
 172        int r = 0;
 173
 174        /*
 175         * Auto-detect:
 176         */
 177        if (perf_use_color_default < 0) {
 178                if (isatty(1) || pager_in_use())
 179                        perf_use_color_default = 1;
 180                else
 181                        perf_use_color_default = 0;
 182        }
 183
 184        if (perf_use_color_default && *color)
 185                r += fprintf(fp, "%s", color);
 186        r += vfprintf(fp, fmt, args);
 187        if (perf_use_color_default && *color)
 188                r += fprintf(fp, "%s", PERF_COLOR_RESET);
 189        if (trail)
 190                r += fprintf(fp, "%s", trail);
 191        return r;
 192}
 193
 194int color_vfprintf(FILE *fp, const char *color, const char *fmt, va_list args)
 195{
 196        return __color_vfprintf(fp, color, fmt, args, NULL);
 197}
 198
 199
 200int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
 201{
 202        va_list args;
 203        int r;
 204
 205        va_start(args, fmt);
 206        r = color_vfprintf(fp, color, fmt, args);
 207        va_end(args);
 208        return r;
 209}
 210
 211int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
 212{
 213        va_list args;
 214        int r;
 215        va_start(args, fmt);
 216        r = __color_vfprintf(fp, color, fmt, args, "\n");
 217        va_end(args);
 218        return r;
 219}
 220
 221/*
 222 * This function splits the buffer by newlines and colors the lines individually.
 223 *
 224 * Returns 0 on success.
 225 */
 226int color_fwrite_lines(FILE *fp, const char *color,
 227                size_t count, const char *buf)
 228{
 229        if (!*color)
 230                return fwrite(buf, count, 1, fp) != 1;
 231
 232        while (count) {
 233                char *p = memchr(buf, '\n', count);
 234
 235                if (p != buf && (fputs(color, fp) < 0 ||
 236                                fwrite(buf, p ? (size_t)(p - buf) : count, 1, fp) != 1 ||
 237                                fputs(PERF_COLOR_RESET, fp) < 0))
 238                        return -1;
 239                if (!p)
 240                        return 0;
 241                if (fputc('\n', fp) < 0)
 242                        return -1;
 243                count -= p + 1 - buf;
 244                buf = p + 1;
 245        }
 246        return 0;
 247}
 248
 249const char *get_percent_color(double percent)
 250{
 251        const char *color = PERF_COLOR_NORMAL;
 252
 253        /*
 254         * We color high-overhead entries in red, mid-overhead
 255         * entries in green - and keep the low overhead places
 256         * normal:
 257         */
 258        if (percent >= MIN_RED)
 259                color = PERF_COLOR_RED;
 260        else {
 261                if (percent > MIN_GREEN)
 262                        color = PERF_COLOR_GREEN;
 263        }
 264        return color;
 265}
 266
 267int percent_color_fprintf(FILE *fp, const char *fmt, double percent)
 268{
 269        int r;
 270        const char *color;
 271
 272        color = get_percent_color(percent);
 273        r = color_fprintf(fp, color, fmt, percent);
 274
 275        return r;
 276}
 277