color.c 6.6 KB

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