progress: use term_clear_line()
authorSZEDER Gábor <szeder.dev@gmail.com>
Mon, 24 Jun 2019 18:13:18 +0000 (20:13 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 27 Jun 2019 19:58:41 +0000 (12:58 -0700)
To make sure that the previously displayed progress line is completely
covered up when the new line is shorter, commit 545dc345eb (progress:
break too long progress bar lines, 2019-04-12) added a bunch of
calculations to figure out how many characters it needs to overwrite
with spaces.

Use the just introduced term_clear_line() helper function to, well,
clear the last line, making all these calculations unnecessary, and
thus simplifying the code considerably.

Three tests in 't5541-http-push-smart.sh' 'grep' for specific text
shown in the progress lines at the beginning of the line, but now
those lines begin either with the ANSI escape sequence or with the
terminal width worth of space characters clearing the line. Relax the
'grep' patterns to match anywhere on the line. Note that only two of
these three tests fail without relaxing their 'grep' pattern, but the
third looks for the absence of the pattern, so it still succeeds, but
without the adjustment would potentially hide future regressions.

Note also that with this change we no longer need the length of the
previously displayed progress line, so the strbuf added to 'struct
progress' in d53ba841d4 (progress: assemble percentage and counters in
a strbuf before printing, 2019-04-05) is not strictly necessary
anymore. We still keep it, though, as it avoids allocating and
releasing a strbuf each time the progress is updated.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
progress.c
t/t5541-http-push-smart.sh
index a2e8cf64a8d1aa2f94c745bbb5098b9c771f3a05..095dcd0ddff2132a4dcfd59486ee4a5ddfa4684f 100644 (file)
@@ -88,7 +88,6 @@ static void display(struct progress *progress, uint64_t n, const char *done)
        const char *tp;
        struct strbuf *counters_sb = &progress->counters_sb;
        int show_update = 0;
-       int last_count_len = counters_sb->len;
 
        if (progress->delay && (!progress_update || --progress->delay))
                return;
@@ -116,26 +115,21 @@ static void display(struct progress *progress, uint64_t n, const char *done)
        if (show_update) {
                if (is_foreground_fd(fileno(stderr)) || done) {
                        const char *eol = done ? done : "\r";
-                       size_t clear_len = counters_sb->len < last_count_len ?
-                                       last_count_len - counters_sb->len + 1 :
-                                       0;
-                       size_t progress_line_len = progress->title_len +
-                                               counters_sb->len + 2;
-                       int cols = term_columns();
 
+                       term_clear_line();
                        if (progress->split) {
-                               fprintf(stderr, "  %s%*s", counters_sb->buf,
-                                       (int) clear_len, eol);
-                       } else if (!done && cols < progress_line_len) {
-                               clear_len = progress->title_len + 1 < cols ?
-                                           cols - progress->title_len - 1 : 0;
-                               fprintf(stderr, "%s:%*s\n  %s%s",
-                                       progress->title, (int) clear_len, "",
-                                       counters_sb->buf, eol);
+                               fprintf(stderr, "  %s%s", counters_sb->buf,
+                                       eol);
+                       } else if (!done &&
+                                  /* The "+ 2" accounts for the ": ". */
+                                  term_columns() < progress->title_len +
+                                                   counters_sb->len + 2) {
+                               fprintf(stderr, "%s:\n  %s%s",
+                                       progress->title, counters_sb->buf, eol);
                                progress->split = 1;
                        } else {
-                               fprintf(stderr, "%s: %s%*s", progress->title,
-                                       counters_sb->buf, (int) clear_len, eol);
+                               fprintf(stderr, "%s: %s%s", progress->title,
+                                       counters_sb->buf, eol);
                        }
                        fflush(stderr);
                }
index 8ef8763e063cf30da8af4a80079458cf8e18c56d..2e4802e2063b87bc6c36d2e728195036e47d8edc 100755 (executable)
@@ -213,7 +213,7 @@ test_expect_success TTY 'push shows progress when stderr is a tty' '
        cd "$ROOT_PATH"/test_repo_clone &&
        test_commit noisy &&
        test_terminal git push >output 2>&1 &&
-       test_i18ngrep "^Writing objects" output
+       test_i18ngrep "Writing objects" output
 '
 
 test_expect_success TTY 'push --quiet silences status and progress' '
@@ -228,7 +228,7 @@ test_expect_success TTY 'push --no-progress silences progress but not status' '
        test_commit no-progress &&
        test_terminal git push --no-progress >output 2>&1 &&
        test_i18ngrep "^To http" output &&
-       test_i18ngrep ! "^Writing objects" output
+       test_i18ngrep ! "Writing objects" output
 '
 
 test_expect_success 'push --progress shows progress to non-tty' '
@@ -236,7 +236,7 @@ test_expect_success 'push --progress shows progress to non-tty' '
        test_commit progress &&
        git push --progress >output 2>&1 &&
        test_i18ngrep "^To http" output &&
-       test_i18ngrep "^Writing objects" output
+       test_i18ngrep "Writing objects" output
 '
 
 test_expect_success 'http push gives sane defaults to reflog' '