THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

426 lines
8.9KB

  1. #include "cache.h"
  2. #include "config.h"
  3. #include "color.h"
  4. static int git_use_color_default = GIT_COLOR_AUTO;
  5. int color_stdout_is_tty = -1;
  6. /*
  7. * The list of available column colors.
  8. */
  9. const 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. /* Ignore the RESET at the end when giving the size */
  25. const int column_colors_ansi_max = ARRAY_SIZE(column_colors_ansi) - 1;
  26. /* An individual foreground or background color. */
  27. struct color {
  28. enum {
  29. COLOR_UNSPECIFIED = 0,
  30. COLOR_NORMAL,
  31. COLOR_ANSI, /* basic 0-7 ANSI colors */
  32. COLOR_256,
  33. COLOR_RGB
  34. } type;
  35. /* The numeric value for ANSI and 256-color modes */
  36. unsigned char value;
  37. /* 24-bit RGB color values */
  38. unsigned char red, green, blue;
  39. };
  40. /*
  41. * "word" is a buffer of length "len"; does it match the NUL-terminated
  42. * "match" exactly?
  43. */
  44. static int match_word(const char *word, int len, const char *match)
  45. {
  46. return !strncasecmp(word, match, len) && !match[len];
  47. }
  48. static int get_hex_color(const char *in, unsigned char *out)
  49. {
  50. unsigned int val;
  51. val = (hexval(in[0]) << 4) | hexval(in[1]);
  52. if (val & ~0xff)
  53. return -1;
  54. *out = val;
  55. return 0;
  56. }
  57. static int parse_color(struct color *out, const char *name, int len)
  58. {
  59. /* Positions in array must match ANSI color codes */
  60. static const char * const color_names[] = {
  61. "black", "red", "green", "yellow",
  62. "blue", "magenta", "cyan", "white"
  63. };
  64. char *end;
  65. int i;
  66. long val;
  67. /* First try the special word "normal"... */
  68. if (match_word(name, len, "normal")) {
  69. out->type = COLOR_NORMAL;
  70. return 0;
  71. }
  72. /* Try a 24-bit RGB value */
  73. if (len == 7 && name[0] == '#') {
  74. if (!get_hex_color(name + 1, &out->red) &&
  75. !get_hex_color(name + 3, &out->green) &&
  76. !get_hex_color(name + 5, &out->blue)) {
  77. out->type = COLOR_RGB;
  78. return 0;
  79. }
  80. }
  81. /* Then pick from our human-readable color names... */
  82. for (i = 0; i < ARRAY_SIZE(color_names); i++) {
  83. if (match_word(name, len, color_names[i])) {
  84. out->type = COLOR_ANSI;
  85. out->value = i;
  86. return 0;
  87. }
  88. }
  89. /* And finally try a literal 256-color-mode number */
  90. val = strtol(name, &end, 10);
  91. if (end - name == len) {
  92. /*
  93. * Allow "-1" as an alias for "normal", but other negative
  94. * numbers are bogus.
  95. */
  96. if (val < -1)
  97. ; /* fall through to error */
  98. else if (val < 0) {
  99. out->type = COLOR_NORMAL;
  100. return 0;
  101. /* Rewrite low numbers as more-portable standard colors. */
  102. } else if (val < 8) {
  103. out->type = COLOR_ANSI;
  104. out->value = val;
  105. return 0;
  106. } else if (val < 256) {
  107. out->type = COLOR_256;
  108. out->value = val;
  109. return 0;
  110. }
  111. }
  112. return -1;
  113. }
  114. static int parse_attr(const char *name, size_t len)
  115. {
  116. static const struct {
  117. const char *name;
  118. size_t len;
  119. int val, neg;
  120. } attrs[] = {
  121. #define ATTR(x, val, neg) { (x), sizeof(x)-1, (val), (neg) }
  122. ATTR("bold", 1, 22),
  123. ATTR("dim", 2, 22),
  124. ATTR("italic", 3, 23),
  125. ATTR("ul", 4, 24),
  126. ATTR("blink", 5, 25),
  127. ATTR("reverse", 7, 27),
  128. ATTR("strike", 9, 29)
  129. #undef ATTR
  130. };
  131. int negate = 0;
  132. int i;
  133. if (skip_prefix_mem(name, len, "no", &name, &len)) {
  134. skip_prefix_mem(name, len, "-", &name, &len);
  135. negate = 1;
  136. }
  137. for (i = 0; i < ARRAY_SIZE(attrs); i++) {
  138. if (attrs[i].len == len && !memcmp(attrs[i].name, name, len))
  139. return negate ? attrs[i].neg : attrs[i].val;
  140. }
  141. return -1;
  142. }
  143. int color_parse(const char *value, char *dst)
  144. {
  145. return color_parse_mem(value, strlen(value), dst);
  146. }
  147. /*
  148. * Write the ANSI color codes for "c" to "out"; the string should
  149. * already have the ANSI escape code in it. "out" should have enough
  150. * space in it to fit any color.
  151. */
  152. static char *color_output(char *out, int len, const struct color *c, char type)
  153. {
  154. switch (c->type) {
  155. case COLOR_UNSPECIFIED:
  156. case COLOR_NORMAL:
  157. break;
  158. case COLOR_ANSI:
  159. if (len < 2)
  160. BUG("color parsing ran out of space");
  161. *out++ = type;
  162. *out++ = '0' + c->value;
  163. break;
  164. case COLOR_256:
  165. out += xsnprintf(out, len, "%c8;5;%d", type, c->value);
  166. break;
  167. case COLOR_RGB:
  168. out += xsnprintf(out, len, "%c8;2;%d;%d;%d", type,
  169. c->red, c->green, c->blue);
  170. break;
  171. }
  172. return out;
  173. }
  174. static int color_empty(const struct color *c)
  175. {
  176. return c->type <= COLOR_NORMAL;
  177. }
  178. int color_parse_mem(const char *value, int value_len, char *dst)
  179. {
  180. const char *ptr = value;
  181. int len = value_len;
  182. char *end = dst + COLOR_MAXLEN;
  183. unsigned int attr = 0;
  184. struct color fg = { COLOR_UNSPECIFIED };
  185. struct color bg = { COLOR_UNSPECIFIED };
  186. while (len > 0 && isspace(*ptr)) {
  187. ptr++;
  188. len--;
  189. }
  190. if (!len) {
  191. dst[0] = '\0';
  192. return 0;
  193. }
  194. if (!strncasecmp(ptr, "reset", len)) {
  195. xsnprintf(dst, end - dst, GIT_COLOR_RESET);
  196. return 0;
  197. }
  198. /* [fg [bg]] [attr]... */
  199. while (len > 0) {
  200. const char *word = ptr;
  201. struct color c = { COLOR_UNSPECIFIED };
  202. int val, wordlen = 0;
  203. while (len > 0 && !isspace(word[wordlen])) {
  204. wordlen++;
  205. len--;
  206. }
  207. ptr = word + wordlen;
  208. while (len > 0 && isspace(*ptr)) {
  209. ptr++;
  210. len--;
  211. }
  212. if (!parse_color(&c, word, wordlen)) {
  213. if (fg.type == COLOR_UNSPECIFIED) {
  214. fg = c;
  215. continue;
  216. }
  217. if (bg.type == COLOR_UNSPECIFIED) {
  218. bg = c;
  219. continue;
  220. }
  221. goto bad;
  222. }
  223. val = parse_attr(word, wordlen);
  224. if (0 <= val)
  225. attr |= (1 << val);
  226. else
  227. goto bad;
  228. }
  229. #undef OUT
  230. #define OUT(x) do { \
  231. if (dst == end) \
  232. BUG("color parsing ran out of space"); \
  233. *dst++ = (x); \
  234. } while(0)
  235. if (attr || !color_empty(&fg) || !color_empty(&bg)) {
  236. int sep = 0;
  237. int i;
  238. OUT('\033');
  239. OUT('[');
  240. for (i = 0; attr; i++) {
  241. unsigned bit = (1 << i);
  242. if (!(attr & bit))
  243. continue;
  244. attr &= ~bit;
  245. if (sep++)
  246. OUT(';');
  247. dst += xsnprintf(dst, end - dst, "%d", i);
  248. }
  249. if (!color_empty(&fg)) {
  250. if (sep++)
  251. OUT(';');
  252. /* foreground colors are all in the 3x range */
  253. dst = color_output(dst, end - dst, &fg, '3');
  254. }
  255. if (!color_empty(&bg)) {
  256. if (sep++)
  257. OUT(';');
  258. /* background colors are all in the 4x range */
  259. dst = color_output(dst, end - dst, &bg, '4');
  260. }
  261. OUT('m');
  262. }
  263. OUT(0);
  264. return 0;
  265. bad:
  266. return error(_("invalid color value: %.*s"), value_len, value);
  267. #undef OUT
  268. }
  269. int git_config_colorbool(const char *var, const char *value)
  270. {
  271. if (value) {
  272. if (!strcasecmp(value, "never"))
  273. return 0;
  274. if (!strcasecmp(value, "always"))
  275. return 1;
  276. if (!strcasecmp(value, "auto"))
  277. return GIT_COLOR_AUTO;
  278. }
  279. if (!var)
  280. return -1;
  281. /* Missing or explicit false to turn off colorization */
  282. if (!git_config_bool(var, value))
  283. return 0;
  284. /* any normal truth value defaults to 'auto' */
  285. return GIT_COLOR_AUTO;
  286. }
  287. static int check_auto_color(int fd)
  288. {
  289. static int color_stderr_is_tty = -1;
  290. int *is_tty_p = fd == 1 ? &color_stdout_is_tty : &color_stderr_is_tty;
  291. if (*is_tty_p < 0)
  292. *is_tty_p = isatty(fd);
  293. if (*is_tty_p || (fd == 1 && pager_in_use() && pager_use_color)) {
  294. if (!is_terminal_dumb())
  295. return 1;
  296. }
  297. return 0;
  298. }
  299. int want_color_fd(int fd, int var)
  300. {
  301. /*
  302. * NEEDSWORK: This function is sometimes used from multiple threads, and
  303. * we end up using want_auto racily. That "should not matter" since
  304. * we always write the same value, but it's still wrong. This function
  305. * is listed in .tsan-suppressions for the time being.
  306. */
  307. static int want_auto[3] = { -1, -1, -1 };
  308. if (fd < 1 || fd >= ARRAY_SIZE(want_auto))
  309. BUG("file descriptor out of range: %d", fd);
  310. if (var < 0)
  311. var = git_use_color_default;
  312. if (var == GIT_COLOR_AUTO) {
  313. if (want_auto[fd] < 0)
  314. want_auto[fd] = check_auto_color(fd);
  315. return want_auto[fd];
  316. }
  317. return var;
  318. }
  319. int git_color_config(const char *var, const char *value, void *cb)
  320. {
  321. if (!strcmp(var, "color.ui")) {
  322. git_use_color_default = git_config_colorbool(var, value);
  323. return 0;
  324. }
  325. return 0;
  326. }
  327. int git_color_default_config(const char *var, const char *value, void *cb)
  328. {
  329. if (git_color_config(var, value, cb) < 0)
  330. return -1;
  331. return git_default_config(var, value, cb);
  332. }
  333. void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
  334. {
  335. if (*color)
  336. fprintf(fp, "%s", color);
  337. fprintf(fp, "%s", sb->buf);
  338. if (*color)
  339. fprintf(fp, "%s", GIT_COLOR_RESET);
  340. }
  341. static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
  342. va_list args, const char *trail)
  343. {
  344. int r = 0;
  345. if (*color)
  346. r += fprintf(fp, "%s", color);
  347. r += vfprintf(fp, fmt, args);
  348. if (*color)
  349. r += fprintf(fp, "%s", GIT_COLOR_RESET);
  350. if (trail)
  351. r += fprintf(fp, "%s", trail);
  352. return r;
  353. }
  354. int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
  355. {
  356. va_list args;
  357. int r;
  358. va_start(args, fmt);
  359. r = color_vfprintf(fp, color, fmt, args, NULL);
  360. va_end(args);
  361. return r;
  362. }
  363. int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
  364. {
  365. va_list args;
  366. int r;
  367. va_start(args, fmt);
  368. r = color_vfprintf(fp, color, fmt, args, "\n");
  369. va_end(args);
  370. return r;
  371. }
  372. int color_is_nil(const char *c)
  373. {
  374. return !strcmp(c, "NIL");
  375. }