summaryrefslogtreecommitdiff
path: root/progress.c
diff options
context:
space:
mode:
authorSZEDER Gábor <szeder.dev@gmail.com>2019-06-24 18:13:18 (GMT)
committerJunio C Hamano <gitster@pobox.com>2019-06-27 19:58:41 (GMT)
commit5b12e3123b7b70e3875404a4ffe571ca079364fe (patch)
treeeed88958e7b41500d9ad4ff4aaf8da705c1878fd /progress.c
parentd7d90885e0aeb50eb4acc221ee43301b2a43aa3e (diff)
downloadgit-5b12e3123b7b70e3875404a4ffe571ca079364fe.zip
git-5b12e3123b7b70e3875404a4ffe571ca079364fe.tar.gz
git-5b12e3123b7b70e3875404a4ffe571ca079364fe.tar.bz2
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>
Diffstat (limited to 'progress.c')
-rw-r--r--progress.c28
1 files changed, 11 insertions, 17 deletions
diff --git a/progress.c b/progress.c
index a2e8cf6..095dcd0 100644
--- a/progress.c
+++ b/progress.c
@@ -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);
}