Skip to content

Commit 0c69a13

Browse files
committed
Merge branch 'ls/editor-waiting-message'
Git shows a message to tell the user that it is waiting for the user to finish editing when spawning an editor, in case the editor opens to a hidden window or somewhere obscure and the user gets lost. * ls/editor-waiting-message: launch_editor(): indicate that Git waits for user input refactor "dumb" terminal determination
2 parents bdae4af + abfb04d commit 0c69a13

File tree

7 files changed

+41
-7
lines changed

7 files changed

+41
-7
lines changed

Documentation/config.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,9 @@ advice.*::
354354
ignoredHook::
355355
Advice shown if an hook is ignored because the hook is not
356356
set as executable.
357+
waitingForEditor::
358+
Print a message to the terminal whenever Git is waiting for
359+
editor input from the user.
357360
--
358361

359362
core.fileMode::

advice.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ int advice_object_name_warning = 1;
1818
int advice_rm_hints = 1;
1919
int advice_add_embedded_repo = 1;
2020
int advice_ignored_hook = 1;
21+
int advice_waiting_for_editor = 1;
2122

2223
static struct {
2324
const char *name;
@@ -40,6 +41,7 @@ static struct {
4041
{ "rmhints", &advice_rm_hints },
4142
{ "addembeddedrepo", &advice_add_embedded_repo },
4243
{ "ignoredhook", &advice_ignored_hook },
44+
{ "waitingforeditor", &advice_waiting_for_editor },
4345

4446
/* make this an alias for backward compatibility */
4547
{ "pushnonfastforward", &advice_push_update_rejected }

advice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extern int advice_object_name_warning;
2020
extern int advice_rm_hints;
2121
extern int advice_add_embedded_repo;
2222
extern int advice_ignored_hook;
23+
extern int advice_waiting_for_editor;
2324

2425
int git_default_advice_config(const char *var, const char *value);
2526
__attribute__((format (printf, 1, 2)))

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,7 @@ extern const char *ident_default_name(void);
14911491
extern const char *ident_default_email(void);
14921492
extern const char *git_editor(void);
14931493
extern const char *git_pager(int stdout_is_tty);
1494+
extern int is_terminal_dumb(void);
14941495
extern int git_ident_config(const char *, const char *, void *);
14951496
extern void reset_ident_date(void);
14961497

color.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,7 @@ static int check_auto_color(void)
329329
if (color_stdout_is_tty < 0)
330330
color_stdout_is_tty = isatty(1);
331331
if (color_stdout_is_tty || (pager_in_use() && pager_use_color)) {
332-
char *term = getenv("TERM");
333-
if (term && strcmp(term, "dumb"))
332+
if (!is_terminal_dumb())
334333
return 1;
335334
}
336335
return 0;

editor.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
#define DEFAULT_EDITOR "vi"
88
#endif
99

10+
int is_terminal_dumb(void)
11+
{
12+
const char *terminal = getenv("TERM");
13+
return !terminal || !strcmp(terminal, "dumb");
14+
}
15+
1016
const char *git_editor(void)
1117
{
1218
const char *editor = getenv("GIT_EDITOR");
13-
const char *terminal = getenv("TERM");
14-
int terminal_is_dumb = !terminal || !strcmp(terminal, "dumb");
19+
int terminal_is_dumb = is_terminal_dumb();
1520

1621
if (!editor && editor_program)
1722
editor = editor_program;
@@ -40,6 +45,23 @@ int launch_editor(const char *path, struct strbuf *buffer, const char *const *en
4045
const char *args[] = { editor, real_path(path), NULL };
4146
struct child_process p = CHILD_PROCESS_INIT;
4247
int ret, sig;
48+
int print_waiting_for_editor = advice_waiting_for_editor && isatty(2);
49+
50+
if (print_waiting_for_editor) {
51+
/*
52+
* A dumb terminal cannot erase the line later on. Add a
53+
* newline to separate the hint from subsequent output.
54+
*
55+
* Make sure that our message is separated with a whitespace
56+
* from further cruft that may be written by the editor.
57+
*/
58+
const char term = is_terminal_dumb() ? '\n' : ' ';
59+
60+
fprintf(stderr,
61+
_("hint: Waiting for your editor to close the file...%c"),
62+
term);
63+
fflush(stderr);
64+
}
4365

4466
p.argv = args;
4567
p.env = env;
@@ -58,6 +80,13 @@ int launch_editor(const char *path, struct strbuf *buffer, const char *const *en
5880
if (ret)
5981
return error("There was a problem with the editor '%s'.",
6082
editor);
83+
84+
if (print_waiting_for_editor && !is_terminal_dumb())
85+
/*
86+
* Go back to the beginning and erase the entire line to
87+
* avoid wasting the vertical space.
88+
*/
89+
fputs("\r\033[K", stderr);
6190
}
6291

6392
if (!buffer)

sideband.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@
2020

2121
int recv_sideband(const char *me, int in_stream, int out)
2222
{
23-
const char *term, *suffix;
23+
const char *suffix;
2424
char buf[LARGE_PACKET_MAX + 1];
2525
struct strbuf outbuf = STRBUF_INIT;
2626
int retval = 0;
2727

28-
term = getenv("TERM");
29-
if (isatty(2) && term && strcmp(term, "dumb"))
28+
if (isatty(2) && !is_terminal_dumb())
3029
suffix = ANSI_SUFFIX;
3130
else
3231
suffix = DUMB_SUFFIX;

0 commit comments

Comments
 (0)