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 <worldhello.net@gmail.com> Comments-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jiang Xin committed
Jun 25, 2013 at 23:53 UTC
7a9b0b802ecf5a09502b46de27c07ff05f55b247
2 files changed
+81
-9
Documentation/config.txt
+9
-8
@@ -876,16 +876,17 @@ The values of these variables may be specified as in color.branch.<slot>.
876
877
color.interactive::
878
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.
883
884
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>.
890
891
color.pager::
892
A boolean to enable/disable colored output when the pager is in
builtin/clean.c
+72
-1
@@ -14,6 +14,7 @@
14
#include "string-list.h"
15
#include "quote.h"
16
#include "column.h"
17
+#include "color.h"
18
19
static int force = -1; /* unset */
20
static int interactive;
@@ -31,16 +32,82 @@ static const char *msg_skip_git_dir = N_("Skipping repository %s\n");
32
static const char *msg_would_skip_git_dir = N_("Would skip repository %s\n");
33
static const char *msg_warn_remove_failed = N_("failed to remove %s");
34
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
+
70
static int git_clean_config(const char *var, const char *value, void *cb)
71
{
72
if (!prefixcmp(var, "column."))
73
return git_column_config(var, value, "clean", &colopts);
74
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
+
92
if (!strcmp(var, "clean.requireforce")) {
93
force = !git_config_bool(var, value);
94
return 0;
95
}
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));
111
}
112
113
static int exclude_cb(const struct option *opt, const char *arg, int unset)
@@ -184,14 +251,18 @@ static void interactive_main_loop(void)
251
252
while (del_list.nr) {
253
putchar('\n');
254
+ clean_print_color(CLEAN_COLOR_HEADER);
255
printf_ln(Q_("Would remove the following item:",
256
"Would remove the following items:",
257
del_list.nr));
258
+ clean_print_color(CLEAN_COLOR_RESET);
259
putchar('\n');
260
261
pretty_print_dels();
262
263
+ clean_print_color(CLEAN_COLOR_PROMPT);
264
printf(_("Remove [y/n]? "));
265
+ clean_print_color(CLEAN_COLOR_RESET);
266
if (strbuf_getline(&confirm, stdin, '\n') != EOF) {
267
strbuf_trim(&confirm);
268
} else {