progress: use term_clear_line()

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>

SZEDER Gábor committed Jun 24, 2019 at 20:13 UTC 5b12e3123b7b70e3875404a4ffe571ca079364fe
2 files changed +14 -20
progress.c
+11 -17
@@ -88,7 +88,6 @@ static void display(struct progress *progress, uint64_t n, const char *done)
88 const char *tp;
89 struct strbuf *counters_sb = &progress->counters_sb;
90 int show_update = 0;
91 - int last_count_len = counters_sb->len;
91
92 if (progress->delay && (!progress_update || --progress->delay))
93 return;
@@ -116,26 +115,21 @@ static void display(struct progress *progress, uint64_t n, const char *done)
115 if (show_update) {
116 if (is_foreground_fd(fileno(stderr)) || done) {
117 const char *eol = done ? done : "\r";
119 - size_t clear_len = counters_sb->len < last_count_len ?
120 - last_count_len - counters_sb->len + 1 :
121 - 0;
122 - size_t progress_line_len = progress->title_len +
123 - counters_sb->len + 2;
124 - int cols = term_columns();
118
119 + term_clear_line();
120 if (progress->split) {
127 - fprintf(stderr, " %s%*s", counters_sb->buf,
128 - (int) clear_len, eol);
129 - } else if (!done && cols < progress_line_len) {
130 - clear_len = progress->title_len + 1 < cols ?
131 - cols - progress->title_len - 1 : 0;
132 - fprintf(stderr, "%s:%*s\n %s%s",
133 - progress->title, (int) clear_len, "",
134 - counters_sb->buf, eol);
121 + fprintf(stderr, " %s%s", counters_sb->buf,
122 + eol);
123 + } else if (!done &&
124 + /* The "+ 2" accounts for the ": ". */
125 + term_columns() < progress->title_len +
126 + counters_sb->len + 2) {
127 + fprintf(stderr, "%s:\n %s%s",
128 + progress->title, counters_sb->buf, eol);
129 progress->split = 1;
130 } else {
137 - fprintf(stderr, "%s: %s%*s", progress->title,
138 - counters_sb->buf, (int) clear_len, eol);
131 + fprintf(stderr, "%s: %s%s", progress->title,
132 + counters_sb->buf, eol);
133 }
134 fflush(stderr);
135 }
t/t5541-http-push-smart.sh
+3 -3
@@ -213,7 +213,7 @@ test_expect_success TTY 'push shows progress when stderr is a tty' '
213 cd "$ROOT_PATH"/test_repo_clone &&
214 test_commit noisy &&
215 test_terminal git push >output 2>&1 &&
216 - test_i18ngrep "^Writing objects" output
216 + test_i18ngrep "Writing objects" output
217 '
218
219 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' '
228 test_commit no-progress &&
229 test_terminal git push --no-progress >output 2>&1 &&
230 test_i18ngrep "^To http" output &&
231 - test_i18ngrep ! "^Writing objects" output
231 + test_i18ngrep ! "Writing objects" output
232 '
233
234 test_expect_success 'push --progress shows progress to non-tty' '
@@ -236,7 +236,7 @@ test_expect_success 'push --progress shows progress to non-tty' '
236 test_commit progress &&
237 git push --progress >output 2>&1 &&
238 test_i18ngrep "^To http" output &&
239 - test_i18ngrep "^Writing objects" output
239 + test_i18ngrep "Writing objects" output
240 '
241
242 test_expect_success 'http push gives sane defaults to reflog' '