Skip to content

Commit a7bcf18

Browse files
committed
built-in add -p: only show the applicable parts of the help text
When displaying the only hunk in a file's diff, the prompt already excludes the commands to navigate to the previous/next hunk. Let's also let the `?` command show only the help lines corresponding to the commands that are displayed in the prompt. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 2243232 commit a7bcf18

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

add-patch.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -956,8 +956,10 @@ N_("y - stage this hunk\n"
956956
"n - do not stage this hunk\n"
957957
"q - quit; do not stage this hunk or any of the remaining ones\n"
958958
"a - stage this and all the remaining hunks\n"
959-
"d - do not stage this hunk nor any of the remaining hunks\n"
960-
"j - leave this hunk undecided, see next undecided hunk\n"
959+
"d - do not stage this hunk nor any of the remaining hunks\n");
960+
961+
static const char help_patch_remainder[] =
962+
N_("j - leave this hunk undecided, see next undecided hunk\n"
961963
"J - leave this hunk undecided, see next hunk\n"
962964
"k - leave this hunk undecided, see previous undecided hunk\n"
963965
"K - leave this hunk undecided, see previous hunk\n"
@@ -1190,9 +1192,31 @@ static int patch_update_file(struct add_p_state *s,
11901192
hunk->use = USE_HUNK;
11911193
goto soft_increment;
11921194
}
1193-
} else
1194-
color_fprintf(stdout, s->s.help_color,
1195+
} else {
1196+
const char *p = _(help_patch_remainder), *eol = p;
1197+
1198+
color_fprintf(stdout, s->s.help_color, "%s",
11951199
_(help_patch_text));
1200+
1201+
/*
1202+
* Show only those lines of the remainder that are
1203+
* actually applicable with the current hunk.
1204+
*/
1205+
for (; *p; p = eol + (*eol == '\n')) {
1206+
eol = strchrnul(p, '\n');
1207+
1208+
/*
1209+
* `s->buf` still contains the part of the
1210+
* commands shown in the prompt that are not
1211+
* always available.
1212+
*/
1213+
if (*p != '?' && !strchr(s->buf.buf, *p))
1214+
continue;
1215+
1216+
color_fprintf_ln(stdout, s->s.help_color,
1217+
"%.*s", (int)(eol - p), p);
1218+
}
1219+
}
11961220
}
11971221

11981222
/* Any hunk to be used? */

0 commit comments

Comments
 (0)