@matheusbsbs / matheus-bagliano / commits / 1e3d4119d2
Fork 0

Enable custom schemes for column colors in the graph API

Currently, the graph code is hardcoded to use ANSI color escapes for coloring the column characters in the generated graphs. This patch allows a custom scheme of colors to be set at runtime, allowing different types of color escapes to be used. A new function - graph_set_column_colors() - is added to the graph.h API, which allows a custom column_colors array (and column_colors_max value) to replace the builtin ANSI array (and _max value). The new function - if used - must be called before graph_init() is called. Signed-off-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johan Herland committed Jul 13, 2010 at 23:23 UTC 1e3d4119d21df2812584a09a18d35414efbc1336
2 files changed +43 -11
graph.c
+26 -11
@@ -62,7 +62,7 @@ enum graph_state {
62 /*
63 * The list of available column colors.
64 */
65 -static char column_colors[][COLOR_MAXLEN] = {
65 +static const char *column_colors_ansi[] = {
66 GIT_COLOR_RED,
67 GIT_COLOR_GREEN,
68 GIT_COLOR_YELLOW,
@@ -75,23 +75,33 @@ static char column_colors[][COLOR_MAXLEN] = {
75 GIT_COLOR_BOLD_BLUE,
76 GIT_COLOR_BOLD_MAGENTA,
77 GIT_COLOR_BOLD_CYAN,
78 + GIT_COLOR_RESET,
79 };
80
80 -#define COLUMN_COLORS_MAX (ARRAY_SIZE(column_colors))
81 +#define COLUMN_COLORS_ANSI_MAX (ARRAY_SIZE(column_colors_ansi) - 1)
82
82 -static const char *column_get_color_code(const struct column *c)
83 +static const char **column_colors;
84 +static unsigned short column_colors_max;
85 +
86 +void graph_set_column_colors(const char **colors, unsigned short colors_max)
87 +{
88 + column_colors = colors;
89 + column_colors_max = colors_max;
90 +}
91 +
92 +static const char *column_get_color_code(unsigned short color)
93 {
84 - return column_colors[c->color];
94 + return column_colors[color];
95 }
96
97 static void strbuf_write_column(struct strbuf *sb, const struct column *c,
98 char col_char)
99 {
90 - if (c->color < COLUMN_COLORS_MAX)
91 - strbuf_addstr(sb, column_get_color_code(c));
100 + if (c->color < column_colors_max)
101 + strbuf_addstr(sb, column_get_color_code(c->color));
102 strbuf_addch(sb, col_char);
93 - if (c->color < COLUMN_COLORS_MAX)
94 - strbuf_addstr(sb, GIT_COLOR_RESET);
103 + if (c->color < column_colors_max)
104 + strbuf_addstr(sb, column_get_color_code(column_colors_max));
105 }
106
107 struct git_graph {
@@ -215,6 +225,11 @@ static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, void
225 struct git_graph *graph_init(struct rev_info *opt)
226 {
227 struct git_graph *graph = xmalloc(sizeof(struct git_graph));
228 +
229 + if (!column_colors)
230 + graph_set_column_colors(column_colors_ansi,
231 + COLUMN_COLORS_ANSI_MAX);
232 +
233 graph->commit = NULL;
234 graph->revs = opt;
235 graph->num_parents = 0;
@@ -231,7 +246,7 @@ struct git_graph *graph_init(struct rev_info *opt)
246 * always increment it for the first commit we output.
247 * This way we start at 0 for the first commit.
248 */
234 - graph->default_column_color = COLUMN_COLORS_MAX - 1;
249 + graph->default_column_color = column_colors_max - 1;
250
251 /*
252 * Allocate a reasonably large default number of columns
@@ -354,7 +369,7 @@ static struct commit_list *first_interesting_parent(struct git_graph *graph)
369 static unsigned short graph_get_current_column_color(const struct git_graph *graph)
370 {
371 if (!DIFF_OPT_TST(&graph->revs->diffopt, COLOR_DIFF))
357 - return COLUMN_COLORS_MAX;
372 + return column_colors_max;
373 return graph->default_column_color;
374 }
375
@@ -364,7 +379,7 @@ static unsigned short graph_get_current_column_color(const struct git_graph *gra
379 static void graph_increment_column_color(struct git_graph *graph)
380 {
381 graph->default_column_color = (graph->default_column_color + 1) %
367 - COLUMN_COLORS_MAX;
382 + column_colors_max;
383 }
384
385 static unsigned short graph_find_commit_color(const struct git_graph *graph,
graph.h
+17
@@ -4,6 +4,23 @@
4 /* A graph is a pointer to this opaque structure */
5 struct git_graph;
6
7 +/*
8 + * Set up a custom scheme for column colors.
9 + *
10 + * The default column color scheme inserts ANSI color escapes to colorize
11 + * the graph. The various color escapes are stored in an array of strings
12 + * where each entry corresponds to a color, except for the last entry,
13 + * which denotes the escape for resetting the color back to the default.
14 + * When generating the graph, strings from this array are inserted before
15 + * and after the various column characters.
16 + *
17 + * This function allows you to enable a custom array of color escapes.
18 + * The 'colors_max' argument is the index of the last "reset" entry.
19 + *
20 + * This functions must be called BEFORE graph_init() is called.
21 + */
22 +void graph_set_column_colors(const char **colors, unsigned short colors_max);
23 +
24 /*
25 * Create a new struct git_graph.
26 */