Skip to content

Commit 83d92a9

Browse files
committed
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 Djukic <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 8cafc6a commit 83d92a9

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

add-interactive.c

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,51 @@
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

9+
struct add_i_state {
10+
struct repository *r;
11+
int use_color;
12+
char header_color[COLOR_MAXLEN];
13+
};
14+
15+
static void init_color(struct repository *r, struct add_i_state *s,
16+
const char *slot_name, char *dst,
17+
const char *default_color)
18+
{
19+
char *key = xstrfmt("color.interactive.%s", slot_name);
20+
const char *value;
21+
22+
if (!s->use_color)
23+
dst[0] = '\0';
24+
else if (repo_config_get_value(r, key, &value) ||
25+
color_parse(value, dst))
26+
strlcpy(dst, default_color, COLOR_MAXLEN);
27+
28+
free(key);
29+
}
30+
31+
static int init_add_i_state(struct repository *r, struct add_i_state *s)
32+
{
33+
const char *value;
34+
35+
s->r = r;
36+
37+
if (repo_config_get_value(r, "color.interactive", &value))
38+
s->use_color = -1;
39+
else
40+
s->use_color =
41+
git_config_colorbool("color.interactive", value);
42+
s->use_color = want_color(s->use_color);
43+
44+
init_color(r, s, "header", s->header_color, GIT_COLOR_BOLD);
45+
46+
return 0;
47+
}
48+
749
struct item {
850
const char *name;
951
};
@@ -14,15 +56,17 @@ struct list_options {
1456
void *print_item_data;
1557
};
1658

17-
static void list(struct item **list, size_t nr, struct list_options *opts)
59+
static void list(struct item **list, size_t nr,
60+
struct add_i_state *s, struct list_options *opts)
1861
{
1962
int i;
2063

2164
if (!nr)
2265
return;
2366

2467
if (opts->header)
25-
printf("%s\n", opts->header);
68+
color_fprintf_ln(stdout, s->header_color,
69+
"%s", opts->header);
2670

2771
for (i = 0; i < nr; i++) {
2872
opts->print_item(i, list[i], opts->print_item_data);
@@ -226,23 +270,24 @@ static void print_file_item(int i, struct item *item,
226270
printf(" %2d: %s", i + 1, d->buf.buf);
227271
}
228272

229-
static int run_status(struct repository *r, const struct pathspec *ps,
273+
static int run_status(struct add_i_state *s, const struct pathspec *ps,
230274
struct file_list *files, struct list_options *opts)
231275
{
232276
reset_file_list(files);
233277

234-
if (get_modified_files(r, files, ps) < 0)
278+
if (get_modified_files(s->r, files, ps) < 0)
235279
return -1;
236280

237281
if (files->nr)
238-
list((struct item **)files->file, files->nr, opts);
282+
list((struct item **)files->file, files->nr, s, opts);
239283
putchar('\n');
240284

241285
return 0;
242286
}
243287

244288
int run_add_i(struct repository *r, const struct pathspec *ps)
245289
{
290+
struct add_i_state s = { NULL };
246291
struct print_file_item_data print_file_item_data = {
247292
"%12s %12s %s", STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
248293
};
@@ -253,13 +298,16 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
253298
struct file_list files = { NULL };
254299
int res = 0;
255300

301+
if (init_add_i_state(r, &s))
302+
return error("could not parse `add -i` config");
303+
256304
strbuf_addstr(&header, " ");
257305
strbuf_addf(&header, print_file_item_data.modified_fmt,
258306
_("staged"), _("unstaged"), _("path"));
259307
opts.header = header.buf;
260308

261309
repo_refresh_and_write_index(r, REFRESH_QUIET, 1);
262-
if (run_status(r, ps, &files, &opts) < 0)
310+
if (run_status(&s, ps, &files, &opts) < 0)
263311
res = -1;
264312

265313
release_file_list(&files);

0 commit comments

Comments
 (0)