Skip to content

Commit d61cf9d

Browse files
slavicaDjdscho
authored andcommitted
built-in add -i: color the header in the status command
For simplicity, we only implemented the `status` command without colors. This patch starts adding color, matching what the Perl script `git-add--interactive.perl` does. Original-Patch-By: Daniel Ferreira <[email protected]> Signed-off-by: Slavica Đukić <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 4836191 commit d61cf9d

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

add-interactive.c

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,48 @@
11
#include "cache.h"
22
#include "add-interactive.h"
3+
#include "color.h"
4+
#include "config.h"
35
#include "diffcore.h"
46
#include "revision.h"
57
#include "refs.h"
68
#include "string-list.h"
79

810
struct add_i_state {
911
struct repository *r;
12+
int use_color;
13+
char header_color[COLOR_MAXLEN];
1014
};
1115

16+
static void init_color(struct repository *r, struct add_i_state *s,
17+
const char *slot_name, char *dst,
18+
const char *default_color)
19+
{
20+
char *key = xstrfmt("color.interactive.%s", slot_name);
21+
const char *value;
22+
23+
if (!s->use_color)
24+
dst[0] = '\0';
25+
else if (repo_config_get_value(r, key, &value) ||
26+
color_parse(value, dst))
27+
strlcpy(dst, default_color, COLOR_MAXLEN);
28+
29+
free(key);
30+
}
31+
1232
static void init_add_i_state(struct add_i_state *s, struct repository *r)
1333
{
14-
s->r = r;
34+
const char *value;
35+
36+
s->r = r;
37+
38+
if (repo_config_get_value(r, "color.interactive", &value))
39+
s->use_color = -1;
40+
else
41+
s->use_color =
42+
git_config_colorbool("color.interactive", value);
43+
s->use_color = want_color(s->use_color);
44+
45+
init_color(r, s, "header", s->header_color, GIT_COLOR_BOLD);
1546
}
1647

1748
struct list_options {
@@ -20,15 +51,17 @@ struct list_options {
2051
void *print_item_data;
2152
};
2253

23-
static void list(struct string_list *list, struct list_options *opts)
54+
static void list(struct add_i_state *s, struct string_list *list,
55+
struct list_options *opts)
2456
{
2557
int i;
2658

2759
if (!list->nr)
2860
return;
2961

3062
if (opts->header)
31-
printf("%s\n", opts->header);
63+
color_fprintf_ln(stdout, s->header_color,
64+
"%s", opts->header);
3265

3366
for (i = 0; i < list->nr; i++) {
3467
opts->print_item(i, list->items + i, opts->print_item_data);
@@ -213,7 +246,7 @@ static int run_status(struct add_i_state *s, const struct pathspec *ps,
213246
if (get_modified_files(s->r, files, ps) < 0)
214247
return -1;
215248

216-
list(files, opts);
249+
list(s, files, opts);
217250
putchar('\n');
218251

219252
return 0;

0 commit comments

Comments
 (0)