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 <bnmvco@gmail.com> Signed-off-by: Slavica Đukić <slawica92@hotmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Slavica Đukić committed
Nov 13, 2019 at 12:41 UTC
1daaebcaa55b43198f3e91135e738fc9e06ec549
1 file changed
+37
-4
add-interactive.c
+37
-4
@@ -1,5 +1,7 @@
1
#include "cache.h"
2
#include "add-interactive.h"
3
+#include "color.h"
4
+#include "config.h"
5
#include "diffcore.h"
6
#include "revision.h"
7
#include "refs.h"
@@ -7,11 +9,40 @@
9
10
struct add_i_state {
11
struct repository *r;
12
+ int use_color;
13
+ char header_color[COLOR_MAXLEN];
14
};
15
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
+
32
static void init_add_i_state(struct add_i_state *s, struct repository *r)
33
{
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);
46
}
47
48
struct list_options {
@@ -20,7 +51,8 @@ struct list_options {
51
void *print_item_data;
52
};
53
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)
56
{
57
int i;
58
@@ -28,7 +60,8 @@ static void list(struct string_list *list, struct list_options *opts)
60
return;
61
62
if (opts->header)
31
- printf("%s\n", opts->header);
63
+ color_fprintf_ln(stdout, s->header_color,
64
+ "%s", opts->header);
65
66
for (i = 0; i < list->nr; i++) {
67
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,
246
if (get_modified_files(s->r, files, ps) < 0)
247
return -1;
248
216
- list(files, opts);
249
+ list(s, files, opts);
250
putchar('\n');
251
252
return 0;