Skip to content

Commit 7a9b0b8

Browse files
jiangxingitster
authored andcommitted
git-clean: add colors to interactive git-clean
Show header, help, error messages, and prompt in colors for interactive git-clean. Re-use config variables, such as "color.interactive" and "color.interactive.<slot>" for command `git-add--interactive`. Signed-off-by: Jiang Xin <[email protected]> Comments-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1b8fd46 commit 7a9b0b8

File tree

2 files changed

+81
-9
lines changed

2 files changed

+81
-9
lines changed

Documentation/config.txt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -876,16 +876,17 @@ The values of these variables may be specified as in color.branch.<slot>.
876876

877877
color.interactive::
878878
When set to `always`, always use colors for interactive prompts
879-
and displays (such as those used by "git-add --interactive").
880-
When false (or `never`), never. When set to `true` or `auto`, use
881-
colors only when the output is to the terminal. Defaults to false.
879+
and displays (such as those used by "git-add --interactive" and
880+
"git-clean --interactive"). When false (or `never`), never.
881+
When set to `true` or `auto`, use colors only when the output is
882+
to the terminal. Defaults to false.
882883

883884
color.interactive.<slot>::
884-
Use customized color for 'git add --interactive'
885-
output. `<slot>` may be `prompt`, `header`, `help` or `error`, for
886-
four distinct types of normal output from interactive
887-
commands. The values of these variables may be specified as
888-
in color.branch.<slot>.
885+
Use customized color for 'git add --interactive' and 'git clean
886+
--interactive' output. `<slot>` may be `prompt`, `header`, `help`
887+
or `error`, for four distinct types of normal output from
888+
interactive commands. The values of these variables may be
889+
specified as in color.branch.<slot>.
889890

890891
color.pager::
891892
A boolean to enable/disable colored output when the pager is in

builtin/clean.c

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "string-list.h"
1515
#include "quote.h"
1616
#include "column.h"
17+
#include "color.h"
1718

1819
static int force = -1; /* unset */
1920
static int interactive;
@@ -31,16 +32,82 @@ static const char *msg_skip_git_dir = N_("Skipping repository %s\n");
3132
static const char *msg_would_skip_git_dir = N_("Would skip repository %s\n");
3233
static const char *msg_warn_remove_failed = N_("failed to remove %s");
3334

35+
static int clean_use_color = -1;
36+
static char clean_colors[][COLOR_MAXLEN] = {
37+
GIT_COLOR_RESET,
38+
GIT_COLOR_NORMAL, /* PLAIN */
39+
GIT_COLOR_BOLD_BLUE, /* PROMPT */
40+
GIT_COLOR_BOLD, /* HEADER */
41+
GIT_COLOR_BOLD_RED, /* HELP */
42+
GIT_COLOR_BOLD_RED, /* ERROR */
43+
};
44+
enum color_clean {
45+
CLEAN_COLOR_RESET = 0,
46+
CLEAN_COLOR_PLAIN = 1,
47+
CLEAN_COLOR_PROMPT = 2,
48+
CLEAN_COLOR_HEADER = 3,
49+
CLEAN_COLOR_HELP = 4,
50+
CLEAN_COLOR_ERROR = 5,
51+
};
52+
53+
static int parse_clean_color_slot(const char *var)
54+
{
55+
if (!strcasecmp(var, "reset"))
56+
return CLEAN_COLOR_RESET;
57+
if (!strcasecmp(var, "plain"))
58+
return CLEAN_COLOR_PLAIN;
59+
if (!strcasecmp(var, "prompt"))
60+
return CLEAN_COLOR_PROMPT;
61+
if (!strcasecmp(var, "header"))
62+
return CLEAN_COLOR_HEADER;
63+
if (!strcasecmp(var, "help"))
64+
return CLEAN_COLOR_HELP;
65+
if (!strcasecmp(var, "error"))
66+
return CLEAN_COLOR_ERROR;
67+
return -1;
68+
}
69+
3470
static int git_clean_config(const char *var, const char *value, void *cb)
3571
{
3672
if (!prefixcmp(var, "column."))
3773
return git_column_config(var, value, "clean", &colopts);
3874

75+
/* honors the color.interactive* config variables which also
76+
applied in git-add--interactive and git-stash */
77+
if (!strcmp(var, "color.interactive")) {
78+
clean_use_color = git_config_colorbool(var, value);
79+
return 0;
80+
}
81+
if (!prefixcmp(var, "color.interactive.")) {
82+
int slot = parse_clean_color_slot(var +
83+
strlen("color.interactive."));
84+
if (slot < 0)
85+
return 0;
86+
if (!value)
87+
return config_error_nonbool(var);
88+
color_parse(value, var, clean_colors[slot]);
89+
return 0;
90+
}
91+
3992
if (!strcmp(var, "clean.requireforce")) {
4093
force = !git_config_bool(var, value);
4194
return 0;
4295
}
43-
return git_default_config(var, value, cb);
96+
97+
/* inspect the color.ui config variable and others */
98+
return git_color_default_config(var, value, cb);
99+
}
100+
101+
static const char *clean_get_color(enum color_clean ix)
102+
{
103+
if (want_color(clean_use_color))
104+
return clean_colors[ix];
105+
return "";
106+
}
107+
108+
static void clean_print_color(enum color_clean ix)
109+
{
110+
printf("%s", clean_get_color(ix));
44111
}
45112

46113
static int exclude_cb(const struct option *opt, const char *arg, int unset)
@@ -184,14 +251,18 @@ static void interactive_main_loop(void)
184251

185252
while (del_list.nr) {
186253
putchar('\n');
254+
clean_print_color(CLEAN_COLOR_HEADER);
187255
printf_ln(Q_("Would remove the following item:",
188256
"Would remove the following items:",
189257
del_list.nr));
258+
clean_print_color(CLEAN_COLOR_RESET);
190259
putchar('\n');
191260

192261
pretty_print_dels();
193262

263+
clean_print_color(CLEAN_COLOR_PROMPT);
194264
printf(_("Remove [y/n]? "));
265+
clean_print_color(CLEAN_COLOR_RESET);
195266
if (strbuf_getline(&confirm, stdin, '\n') != EOF) {
196267
strbuf_trim(&confirm);
197268
} else {

0 commit comments

Comments
 (0)