4 int perf_use_color_default
= -1;
6 static int parse_color(const char *name
, int len
)
8 static const char * const color_names
[] = {
9 "normal", "black", "red", "green", "yellow",
10 "blue", "magenta", "cyan", "white"
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
])
20 i
= strtol(name
, &end
, 10);
21 if (end
- name
== len
&& i
>= -1 && i
<= 255)
26 static int parse_attr(const char *name
, int len
)
28 static const int attr_values
[] = { 1, 2, 4, 5, 7 };
29 static const char * const attr_names
[] = {
30 "bold", "dim", "ul", "blink", "reverse"
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
];
42 void color_parse(const char *value
, const char *var
, char *dst
)
44 color_parse_mem(value
, strlen(value
), var
, dst
);
47 void color_parse_mem(const char *value
, int value_len
, const char *var
,
50 const char *ptr
= value
;
56 if (!strncasecmp(value
, "reset", len
)) {
57 strcpy(dst
, PERF_COLOR_RESET
);
61 /* [fg [bg]] [attr] */
63 const char *word
= ptr
;
66 while (len
> 0 && !isspace(word
[wordlen
])) {
72 while (len
> 0 && isspace(*ptr
)) {
77 val
= parse_color(word
, wordlen
);
89 val
= parse_attr(word
, wordlen
);
90 if (val
< 0 || attr
!= -1)
95 if (attr
>= 0 || fg
>= 0 || bg
>= 0) {
111 dst
+= sprintf(dst
, "38;5;%d", fg
);
121 dst
+= sprintf(dst
, "48;5;%d", bg
);
129 die("bad color value '%.*s' for variable '%s'", value_len
, value
, var
);
132 int perf_config_colorbool(const char *var
, const char *value
, int stdout_is_tty
)
135 if (!strcasecmp(value
, "never"))
137 if (!strcasecmp(value
, "always"))
139 if (!strcasecmp(value
, "auto"))
143 /* Missing or explicit false to turn off colorization */
144 if (!perf_config_bool(var
, value
))
147 /* any normal truth value defaults to 'auto' */
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"))
159 int perf_color_default_config(const char *var
, const char *value
, void *cb
)
161 if (!strcmp(var
, "color.ui")) {
162 perf_use_color_default
= perf_config_colorbool(var
, value
, -1);
166 return perf_default_config(var
, value
, cb
);
169 static int __color_vsnprintf(char *bf
, size_t size
, const char *color
,
170 const char *fmt
, va_list args
, const char *trail
)
177 if (perf_use_color_default
< 0) {
178 if (isatty(1) || pager_in_use())
179 perf_use_color_default
= 1;
181 perf_use_color_default
= 0;
184 if (perf_use_color_default
&& *color
)
185 r
+= snprintf(bf
, size
, "%s", color
);
186 r
+= vsnprintf(bf
+ r
, size
- r
, fmt
, args
);
187 if (perf_use_color_default
&& *color
)
188 r
+= snprintf(bf
+ r
, size
- r
, "%s", PERF_COLOR_RESET
);
190 r
+= snprintf(bf
+ r
, size
- r
, "%s", trail
);
194 static int __color_vfprintf(FILE *fp
, const char *color
, const char *fmt
,
195 va_list args
, const char *trail
)
202 if (perf_use_color_default
< 0) {
203 if (isatty(1) || pager_in_use())
204 perf_use_color_default
= 1;
206 perf_use_color_default
= 0;
209 if (perf_use_color_default
&& *color
)
210 r
+= fprintf(fp
, "%s", color
);
211 r
+= vfprintf(fp
, fmt
, args
);
212 if (perf_use_color_default
&& *color
)
213 r
+= fprintf(fp
, "%s", PERF_COLOR_RESET
);
215 r
+= fprintf(fp
, "%s", trail
);
219 int color_vsnprintf(char *bf
, size_t size
, const char *color
,
220 const char *fmt
, va_list args
)
222 return __color_vsnprintf(bf
, size
, color
, fmt
, args
, NULL
);
225 int color_vfprintf(FILE *fp
, const char *color
, const char *fmt
, va_list args
)
227 return __color_vfprintf(fp
, color
, fmt
, args
, NULL
);
230 int color_snprintf(char *bf
, size_t size
, const char *color
,
231 const char *fmt
, ...)
237 r
= color_vsnprintf(bf
, size
, color
, fmt
, args
);
242 int color_fprintf(FILE *fp
, const char *color
, const char *fmt
, ...)
248 r
= color_vfprintf(fp
, color
, fmt
, args
);
253 int color_fprintf_ln(FILE *fp
, const char *color
, const char *fmt
, ...)
258 r
= __color_vfprintf(fp
, color
, fmt
, args
, "\n");
264 * This function splits the buffer by newlines and colors the lines individually.
266 * Returns 0 on success.
268 int color_fwrite_lines(FILE *fp
, const char *color
,
269 size_t count
, const char *buf
)
272 return fwrite(buf
, count
, 1, fp
) != 1;
275 char *p
= memchr(buf
, '\n', count
);
277 if (p
!= buf
&& (fputs(color
, fp
) < 0 ||
278 fwrite(buf
, p
? (size_t)(p
- buf
) : count
, 1, fp
) != 1 ||
279 fputs(PERF_COLOR_RESET
, fp
) < 0))
283 if (fputc('\n', fp
) < 0)
285 count
-= p
+ 1 - buf
;
291 const char *get_percent_color(double percent
)
293 const char *color
= PERF_COLOR_NORMAL
;
296 * We color high-overhead entries in red, mid-overhead
297 * entries in green - and keep the low overhead places
300 if (percent
>= MIN_RED
)
301 color
= PERF_COLOR_RED
;
303 if (percent
> MIN_GREEN
)
304 color
= PERF_COLOR_GREEN
;
309 int percent_color_fprintf(FILE *fp
, const char *fmt
, double percent
)
314 color
= get_percent_color(percent
);
315 r
= color_fprintf(fp
, color
, fmt
, percent
);
320 int percent_color_snprintf(char *bf
, size_t size
, const char *fmt
, double percent
)
322 const char *color
= get_percent_color(percent
);
323 return color_snprintf(bf
, size
, color
, fmt
, percent
);