From cd1096b2820d64d5fcb5da20613d5af290e42929 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SZEDER=20G=C3=A1bor?= Date: Mon, 24 Jun 2019 20:13:16 +0200 Subject: pager: add a helper function to clear the last line in the terminal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There are a couple of places where we want to clear the last line on the terminal, e.g. when a progress bar line is overwritten by a shorter line, then the end of that progress line would remain visible, unless we cover it up. In 'progress.c' we did this by always appending a fixed number of space characters to the next line (even if it was not shorter than the previous), but as it turned out that fixed number was not quite large enough, see the fix in 9f1fd84e15 (progress: clear previous progress update dynamically, 2019-04-12). From then on we've been keeping track of the length of the last displayed progress line and appending the appropriate number of space characters to the next line, if necessary, but, alas, this approach turned out to be error prone, see the fix in 1aed1a5f25 (progress: avoid empty line when breaking the progress line, 2019-05-19). The next patch in this series is about to fix a case where we don't clear the last line, and on occasion do end up with such garbage at the end of the line. It would be great if we could do that without the need to deal with that without meticulously computing the necessary number of space characters. So add a helper function to clear the last line on the terminal using an ANSI escape sequence, which has the advantage to clear the whole line no matter how wide it is, even after the terminal width changed. Such an escape sequence is not available on dumb terminals, though, so in that case fall back to simply print a whole terminal width (as reported by term_columns()) worth of space characters. In 'editor.c' launch_specified_editor() already used this ANSI escape sequence, so replace it with a call to this function. Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano diff --git a/cache.h b/cache.h index b4bb2e2..5b2cd32 100644 --- a/cache.h +++ b/cache.h @@ -1759,6 +1759,7 @@ void setup_pager(void); int pager_in_use(void); extern int pager_use_color; int term_columns(void); +void term_clear_line(void); int decimal_width(uintmax_t); int check_pager_config(const char *cmd); void prepare_pager_args(struct child_process *, const char *pager); diff --git a/editor.c b/editor.c index 7154767..f079abb 100644 --- a/editor.c +++ b/editor.c @@ -96,10 +96,10 @@ static int launch_specified_editor(const char *editor, const char *path, if (print_waiting_for_editor && !is_terminal_dumb()) /* - * Go back to the beginning and erase the entire line to - * avoid wasting the vertical space. + * Erase the entire line to avoid wasting the + * vertical space. */ - fputs("\r\033[K", stderr); + term_clear_line(); } if (!buffer) diff --git a/pager.c b/pager.c index 4168460..41446d4 100644 --- a/pager.c +++ b/pager.c @@ -178,6 +178,26 @@ int term_columns(void) } /* + * Clear the entire line, leave cursor in first column. + */ +void term_clear_line(void) +{ + if (is_terminal_dumb()) + /* + * Fall back to print a terminal width worth of space + * characters (hoping that the terminal is still as wide + * as it was upon the first call to term_columns()). + */ + fprintf(stderr, "\r%*s\r", term_columns(), ""); + else + /* + * On non-dumb terminals use an escape sequence to clear + * the whole line, no matter how wide the terminal. + */ + fputs("\r\033[K", stderr); +} + +/* * How many columns do we need to show this number in decimal? */ int decimal_width(uintmax_t number) -- cgit v0.10.2-6-g49f6