mirror of
https://github.com/git/git.git
synced 2025-03-27 18:19:41 +00:00
Merge branch 'jh/graph-next-line'
* jh/graph-next-line: Enable custom schemes for column colors in the graph API Make graph_next_line() available in the graph.h API
This commit is contained in:
commit
d425aa5cac
50
graph.c
50
graph.c
@ -7,17 +7,6 @@
|
|||||||
|
|
||||||
/* Internal API */
|
/* Internal API */
|
||||||
|
|
||||||
/*
|
|
||||||
* Output the next line for a graph.
|
|
||||||
* This formats the next graph line into the specified strbuf. It is not
|
|
||||||
* terminated with a newline.
|
|
||||||
*
|
|
||||||
* Returns 1 if the line includes the current commit, and 0 otherwise.
|
|
||||||
* graph_next_line() will return 1 exactly once for each time
|
|
||||||
* graph_update() is called.
|
|
||||||
*/
|
|
||||||
static int graph_next_line(struct git_graph *graph, struct strbuf *sb);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Output a padding line in the graph.
|
* Output a padding line in the graph.
|
||||||
* This is similar to graph_next_line(). However, it is guaranteed to
|
* This is similar to graph_next_line(). However, it is guaranteed to
|
||||||
@ -73,7 +62,7 @@ enum graph_state {
|
|||||||
/*
|
/*
|
||||||
* The list of available column colors.
|
* The list of available column colors.
|
||||||
*/
|
*/
|
||||||
static char column_colors[][COLOR_MAXLEN] = {
|
static const char *column_colors_ansi[] = {
|
||||||
GIT_COLOR_RED,
|
GIT_COLOR_RED,
|
||||||
GIT_COLOR_GREEN,
|
GIT_COLOR_GREEN,
|
||||||
GIT_COLOR_YELLOW,
|
GIT_COLOR_YELLOW,
|
||||||
@ -86,23 +75,33 @@ static char column_colors[][COLOR_MAXLEN] = {
|
|||||||
GIT_COLOR_BOLD_BLUE,
|
GIT_COLOR_BOLD_BLUE,
|
||||||
GIT_COLOR_BOLD_MAGENTA,
|
GIT_COLOR_BOLD_MAGENTA,
|
||||||
GIT_COLOR_BOLD_CYAN,
|
GIT_COLOR_BOLD_CYAN,
|
||||||
|
GIT_COLOR_RESET,
|
||||||
};
|
};
|
||||||
|
|
||||||
#define COLUMN_COLORS_MAX (ARRAY_SIZE(column_colors))
|
#define COLUMN_COLORS_ANSI_MAX (ARRAY_SIZE(column_colors_ansi) - 1)
|
||||||
|
|
||||||
static const char *column_get_color_code(const struct column *c)
|
static const char **column_colors;
|
||||||
|
static unsigned short column_colors_max;
|
||||||
|
|
||||||
|
void graph_set_column_colors(const char **colors, unsigned short colors_max)
|
||||||
{
|
{
|
||||||
return column_colors[c->color];
|
column_colors = colors;
|
||||||
|
column_colors_max = colors_max;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char *column_get_color_code(unsigned short color)
|
||||||
|
{
|
||||||
|
return column_colors[color];
|
||||||
}
|
}
|
||||||
|
|
||||||
static void strbuf_write_column(struct strbuf *sb, const struct column *c,
|
static void strbuf_write_column(struct strbuf *sb, const struct column *c,
|
||||||
char col_char)
|
char col_char)
|
||||||
{
|
{
|
||||||
if (c->color < COLUMN_COLORS_MAX)
|
if (c->color < column_colors_max)
|
||||||
strbuf_addstr(sb, column_get_color_code(c));
|
strbuf_addstr(sb, column_get_color_code(c->color));
|
||||||
strbuf_addch(sb, col_char);
|
strbuf_addch(sb, col_char);
|
||||||
if (c->color < COLUMN_COLORS_MAX)
|
if (c->color < column_colors_max)
|
||||||
strbuf_addstr(sb, GIT_COLOR_RESET);
|
strbuf_addstr(sb, column_get_color_code(column_colors_max));
|
||||||
}
|
}
|
||||||
|
|
||||||
struct git_graph {
|
struct git_graph {
|
||||||
@ -226,6 +225,11 @@ static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, void
|
|||||||
struct git_graph *graph_init(struct rev_info *opt)
|
struct git_graph *graph_init(struct rev_info *opt)
|
||||||
{
|
{
|
||||||
struct git_graph *graph = xmalloc(sizeof(struct git_graph));
|
struct git_graph *graph = xmalloc(sizeof(struct git_graph));
|
||||||
|
|
||||||
|
if (!column_colors)
|
||||||
|
graph_set_column_colors(column_colors_ansi,
|
||||||
|
COLUMN_COLORS_ANSI_MAX);
|
||||||
|
|
||||||
graph->commit = NULL;
|
graph->commit = NULL;
|
||||||
graph->revs = opt;
|
graph->revs = opt;
|
||||||
graph->num_parents = 0;
|
graph->num_parents = 0;
|
||||||
@ -242,7 +246,7 @@ struct git_graph *graph_init(struct rev_info *opt)
|
|||||||
* always increment it for the first commit we output.
|
* always increment it for the first commit we output.
|
||||||
* This way we start at 0 for the first commit.
|
* This way we start at 0 for the first commit.
|
||||||
*/
|
*/
|
||||||
graph->default_column_color = COLUMN_COLORS_MAX - 1;
|
graph->default_column_color = column_colors_max - 1;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Allocate a reasonably large default number of columns
|
* Allocate a reasonably large default number of columns
|
||||||
@ -365,7 +369,7 @@ static struct commit_list *first_interesting_parent(struct git_graph *graph)
|
|||||||
static unsigned short graph_get_current_column_color(const struct git_graph *graph)
|
static unsigned short graph_get_current_column_color(const struct git_graph *graph)
|
||||||
{
|
{
|
||||||
if (!DIFF_OPT_TST(&graph->revs->diffopt, COLOR_DIFF))
|
if (!DIFF_OPT_TST(&graph->revs->diffopt, COLOR_DIFF))
|
||||||
return COLUMN_COLORS_MAX;
|
return column_colors_max;
|
||||||
return graph->default_column_color;
|
return graph->default_column_color;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -375,7 +379,7 @@ static unsigned short graph_get_current_column_color(const struct git_graph *gra
|
|||||||
static void graph_increment_column_color(struct git_graph *graph)
|
static void graph_increment_column_color(struct git_graph *graph)
|
||||||
{
|
{
|
||||||
graph->default_column_color = (graph->default_column_color + 1) %
|
graph->default_column_color = (graph->default_column_color + 1) %
|
||||||
COLUMN_COLORS_MAX;
|
column_colors_max;
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned short graph_find_commit_color(const struct git_graph *graph,
|
static unsigned short graph_find_commit_color(const struct git_graph *graph,
|
||||||
@ -1143,7 +1147,7 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf
|
|||||||
graph_update_state(graph, GRAPH_PADDING);
|
graph_update_state(graph, GRAPH_PADDING);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int graph_next_line(struct git_graph *graph, struct strbuf *sb)
|
int graph_next_line(struct git_graph *graph, struct strbuf *sb)
|
||||||
{
|
{
|
||||||
switch (graph->state) {
|
switch (graph->state) {
|
||||||
case GRAPH_PADDING:
|
case GRAPH_PADDING:
|
||||||
|
28
graph.h
28
graph.h
@ -4,6 +4,23 @@
|
|||||||
/* A graph is a pointer to this opaque structure */
|
/* A graph is a pointer to this opaque structure */
|
||||||
struct git_graph;
|
struct git_graph;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Set up a custom scheme for column colors.
|
||||||
|
*
|
||||||
|
* The default column color scheme inserts ANSI color escapes to colorize
|
||||||
|
* the graph. The various color escapes are stored in an array of strings
|
||||||
|
* where each entry corresponds to a color, except for the last entry,
|
||||||
|
* which denotes the escape for resetting the color back to the default.
|
||||||
|
* When generating the graph, strings from this array are inserted before
|
||||||
|
* and after the various column characters.
|
||||||
|
*
|
||||||
|
* This function allows you to enable a custom array of color escapes.
|
||||||
|
* The 'colors_max' argument is the index of the last "reset" entry.
|
||||||
|
*
|
||||||
|
* This functions must be called BEFORE graph_init() is called.
|
||||||
|
*/
|
||||||
|
void graph_set_column_colors(const char **colors, unsigned short colors_max);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Create a new struct git_graph.
|
* Create a new struct git_graph.
|
||||||
*/
|
*/
|
||||||
@ -32,6 +49,17 @@ void graph_update(struct git_graph *graph, struct commit *commit);
|
|||||||
*/
|
*/
|
||||||
int graph_is_commit_finished(struct git_graph const *graph);
|
int graph_is_commit_finished(struct git_graph const *graph);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Output the next line for a graph.
|
||||||
|
* This formats the next graph line into the specified strbuf. It is not
|
||||||
|
* terminated with a newline.
|
||||||
|
*
|
||||||
|
* Returns 1 if the line includes the current commit, and 0 otherwise.
|
||||||
|
* graph_next_line() will return 1 exactly once for each time
|
||||||
|
* graph_update() is called.
|
||||||
|
*/
|
||||||
|
int graph_next_line(struct git_graph *graph, struct strbuf *sb);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* graph_show_*: helper functions for printing to stdout
|
* graph_show_*: helper functions for printing to stdout
|
||||||
|
Loading…
x
Reference in New Issue
Block a user