From a64f213d3fa13fa01e582b6734fe7883ed975dc9 Mon Sep 17 00:00:00 2001 From: Lars Schneider Date: Wed, 29 Nov 2017 15:37:51 +0100 Subject: refactor "dumb" terminal determination Move the code to detect "dumb" terminals into a single location. This avoids duplicating the terminal detection code yet again in a subsequent commit. Signed-off-by: Lars Schneider Signed-off-by: Junio C Hamano diff --git a/cache.h b/cache.h index 6440e2b..0217462 100644 --- a/cache.h +++ b/cache.h @@ -1438,6 +1438,7 @@ extern const char *ident_default_name(void); extern const char *ident_default_email(void); extern const char *git_editor(void); extern const char *git_pager(int stdout_is_tty); +extern int is_terminal_dumb(void); extern int git_ident_config(const char *, const char *, void *); extern void reset_ident_date(void); diff --git a/color.c b/color.c index 9a9261a..d48dd94 100644 --- a/color.c +++ b/color.c @@ -329,8 +329,7 @@ static int check_auto_color(void) if (color_stdout_is_tty < 0) color_stdout_is_tty = isatty(1); if (color_stdout_is_tty || (pager_in_use() && pager_use_color)) { - char *term = getenv("TERM"); - if (term && strcmp(term, "dumb")) + if (!is_terminal_dumb()) return 1; } return 0; diff --git a/editor.c b/editor.c index 7519ede..c65ea69 100644 --- a/editor.c +++ b/editor.c @@ -7,11 +7,16 @@ #define DEFAULT_EDITOR "vi" #endif +int is_terminal_dumb(void) +{ + const char *terminal = getenv("TERM"); + return !terminal || !strcmp(terminal, "dumb"); +} + const char *git_editor(void) { const char *editor = getenv("GIT_EDITOR"); - const char *terminal = getenv("TERM"); - int terminal_is_dumb = !terminal || !strcmp(terminal, "dumb"); + int terminal_is_dumb = is_terminal_dumb(); if (!editor && editor_program) editor = editor_program; diff --git a/sideband.c b/sideband.c index 1e4d684..6d7f943 100644 --- a/sideband.c +++ b/sideband.c @@ -20,13 +20,12 @@ int recv_sideband(const char *me, int in_stream, int out) { - const char *term, *suffix; + const char *suffix; char buf[LARGE_PACKET_MAX + 1]; struct strbuf outbuf = STRBUF_INIT; int retval = 0; - term = getenv("TERM"); - if (isatty(2) && term && strcmp(term, "dumb")) + if (isatty(2) && !is_terminal_dumb()) suffix = ANSI_SUFFIX; else suffix = DUMB_SUFFIX; -- cgit v0.10.2-6-g49f6 From abfb04d0c74cde804c734015ff5868a88c84fb6f Mon Sep 17 00:00:00 2001 From: Lars Schneider Date: Thu, 7 Dec 2017 16:16:41 +0100 Subject: launch_editor(): indicate that Git waits for user input When a graphical GIT_EDITOR is spawned by a Git command that opens and waits for user input (e.g. "git rebase -i"), then the editor window might be obscured by other windows. The user might be left staring at the original Git terminal window without even realizing that s/he needs to interact with another window before Git can proceed. To this user Git appears hanging. Print a message that Git is waiting for editor input in the original terminal and get rid of it when the editor returns, if the terminal supports erasing the last line. Also, make sure that our message is terminated with a whitespace so that any message the editor may show upon starting up will be kept separate from our message. Power users might not want to see this message or their editor might already print such a message (e.g. emacsclient). Allow these users to suppress the message by disabling the "advice.waitingForEditor" config. The standard advise() function is not used here as it would always add a newline which would make deleting the message harder. Helped-by: Junio C Hamano Signed-off-by: Lars Schneider Signed-off-by: Junio C Hamano diff --git a/Documentation/config.txt b/Documentation/config.txt index 9593bfa..6ebc50e 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -351,6 +351,9 @@ advice.*:: addEmbeddedRepo:: Advice on what to do when you've accidentally added one git repo inside of another. + waitingForEditor:: + Print a message to the terminal whenever Git is waiting for + editor input from the user. -- core.fileMode:: diff --git a/advice.c b/advice.c index d81e1cb..af29d23 100644 --- a/advice.c +++ b/advice.c @@ -17,6 +17,7 @@ int advice_set_upstream_failure = 1; int advice_object_name_warning = 1; int advice_rm_hints = 1; int advice_add_embedded_repo = 1; +int advice_waiting_for_editor = 1; static struct { const char *name; @@ -38,6 +39,7 @@ static struct { { "objectnamewarning", &advice_object_name_warning }, { "rmhints", &advice_rm_hints }, { "addembeddedrepo", &advice_add_embedded_repo }, + { "waitingforeditor", &advice_waiting_for_editor }, /* make this an alias for backward compatibility */ { "pushnonfastforward", &advice_push_update_rejected } diff --git a/advice.h b/advice.h index c84a445..f7cbbd3 100644 --- a/advice.h +++ b/advice.h @@ -19,6 +19,7 @@ extern int advice_set_upstream_failure; extern int advice_object_name_warning; extern int advice_rm_hints; extern int advice_add_embedded_repo; +extern int advice_waiting_for_editor; int git_default_advice_config(const char *var, const char *value); __attribute__((format (printf, 1, 2))) diff --git a/editor.c b/editor.c index c65ea69..9a9b4e1 100644 --- a/editor.c +++ b/editor.c @@ -45,6 +45,23 @@ int launch_editor(const char *path, struct strbuf *buffer, const char *const *en const char *args[] = { editor, real_path(path), NULL }; struct child_process p = CHILD_PROCESS_INIT; int ret, sig; + int print_waiting_for_editor = advice_waiting_for_editor && isatty(2); + + if (print_waiting_for_editor) { + /* + * A dumb terminal cannot erase the line later on. Add a + * newline to separate the hint from subsequent output. + * + * Make sure that our message is separated with a whitespace + * from further cruft that may be written by the editor. + */ + const char term = is_terminal_dumb() ? '\n' : ' '; + + fprintf(stderr, + _("hint: Waiting for your editor to close the file...%c"), + term); + fflush(stderr); + } p.argv = args; p.env = env; @@ -63,6 +80,13 @@ int launch_editor(const char *path, struct strbuf *buffer, const char *const *en if (ret) return error("There was a problem with the editor '%s'.", editor); + + if (print_waiting_for_editor && !is_terminal_dumb()) + /* + * Go back to the beginning and erase the entire line to + * avoid wasting the vertical space. + */ + fputs("\r\033[K", stderr); } if (!buffer) -- cgit v0.10.2-6-g49f6