Skip to content

Commit 5b12e31

Browse files
szedergitster
authored andcommitted
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 545dc34 (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 d53ba84 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d7d9088 commit 5b12e31

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

progress.c

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ static void display(struct progress *progress, uint64_t n, const char *done)
8888
const char *tp;
8989
struct strbuf *counters_sb = &progress->counters_sb;
9090
int show_update = 0;
91-
int last_count_len = counters_sb->len;
9291

9392
if (progress->delay && (!progress_update || --progress->delay))
9493
return;
@@ -116,26 +115,21 @@ static void display(struct progress *progress, uint64_t n, const char *done)
116115
if (show_update) {
117116
if (is_foreground_fd(fileno(stderr)) || done) {
118117
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();
125118

119+
term_clear_line();
126120
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);
135129
progress->split = 1;
136130
} 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);
139133
}
140134
fflush(stderr);
141135
}

t/t5541-http-push-smart.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ test_expect_success TTY 'push shows progress when stderr is a tty' '
213213
cd "$ROOT_PATH"/test_repo_clone &&
214214
test_commit noisy &&
215215
test_terminal git push >output 2>&1 &&
216-
test_i18ngrep "^Writing objects" output
216+
test_i18ngrep "Writing objects" output
217217
'
218218

219219
test_expect_success TTY 'push --quiet silences status and progress' '
@@ -228,15 +228,15 @@ test_expect_success TTY 'push --no-progress silences progress but not status' '
228228
test_commit no-progress &&
229229
test_terminal git push --no-progress >output 2>&1 &&
230230
test_i18ngrep "^To http" output &&
231-
test_i18ngrep ! "^Writing objects" output
231+
test_i18ngrep ! "Writing objects" output
232232
'
233233

234234
test_expect_success 'push --progress shows progress to non-tty' '
235235
cd "$ROOT_PATH"/test_repo_clone &&
236236
test_commit progress &&
237237
git push --progress >output 2>&1 &&
238238
test_i18ngrep "^To http" output &&
239-
test_i18ngrep "^Writing objects" output
239+
test_i18ngrep "Writing objects" output
240240
'
241241

242242
test_expect_success 'http push gives sane defaults to reflog' '

0 commit comments

Comments
 (0)