--- OLD/progress.c Thu Jan 1 00:00:00 1970 +++ NEW/progress.c Thu Jan 1 00:00:00 1970 @@ -2,10 +2,12 @@ * Simple text-based progress display module for GIT * * Copyright (c) 2007 by Nicolas Pitre + * Copyright (c) 2019 by Mouse * - * This code is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. + * This code is not free software; it is licensed under the terms of + * the GNU General Public License, version 2, as published by the Free + * Software Foundation, which impose significant restrictions on what + * you are permitted to do with it. */ #include "git-compat-util.h" @@ -37,6 +39,39 @@ static volatile sig_atomic_t progress_update; +void progress_line(int done, const char *fmt, ...) +{ + va_list ap; + char *s; + int l; + static int lastl = 0; + static char *sufsp = 0; + static char *sufbs = 0; + static int sufa = -1; + int sufl; + const char *end; + + va_start(ap,fmt); + l = vasprintf(&s,fmt,ap); + va_end(ap); + while ((l > 0) && (s[l-1] == ' ')) l --; + sufl = (lastl > l) ? lastl - l : 0; + if (sufl > sufa) + { sufa = sufl ? : 1; + free(sufsp); + free(sufbs); + sufsp = malloc(sufa); + memset(sufsp,' ',sufa); + sufbs = malloc(sufa); + memset(sufbs,'\b',sufa); + } + end = done ? "\n" : "\r"; + fprintf(stderr,"%.*s%.*s%.*s%s",l,s,sufl,sufsp,sufl,sufbs,end); + fflush(stderr); + lastl = done ? 0 : l; + free(s); +} + static void progress_interval(int signum) { progress_update = 1; @@ -71,7 +106,7 @@ static int display(struct progress *progress, unsigned n, const char *done) { - const char *eol, *tp; + const char *tp; if (progress->delay) { if (!progress_update || --progress->delay) @@ -90,20 +125,19 @@ progress->last_value = n; tp = (progress->throughput) ? progress->throughput->display : ""; - eol = done ? done : " \r"; if (progress->total) { unsigned percent = n * 100 / progress->total; if (percent != progress->last_percent || progress_update) { progress->last_percent = percent; - fprintf(stderr, "%s: %3u%% (%u/%u)%s%s", + progress_line(!!done, "%s: %3u%% (%u/%u)%s%s", progress->title, percent, n, - progress->total, tp, eol); + progress->total, tp, done?:""); fflush(stderr); progress_update = 0; return 1; } } else if (progress_update) { - fprintf(stderr, "%s: %u%s%s", progress->title, n, tp, eol); + progress_line(!!done, "%s: %u%s%s", progress->title, n, tp, done?:""); fflush(stderr); progress_update = 0; return 1; @@ -117,23 +151,23 @@ { int l = sizeof(tp->display); if (total > 1 << 30) { - l -= snprintf(tp->display, l, ", %u.%2.2u GiB", + l -= snprintf(tp->display, l, ", %u.%2.2u GB", (int)(total >> 30), (int)(total & ((1 << 30) - 1)) / 10737419); } else if (total > 1 << 20) { int x = total + 5243; /* for rounding */ - l -= snprintf(tp->display, l, ", %u.%2.2u MiB", + l -= snprintf(tp->display, l, ", %u.%2.2u MB", x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20); } else if (total > 1 << 10) { int x = total + 5; /* for rounding */ - l -= snprintf(tp->display, l, ", %u.%2.2u KiB", + l -= snprintf(tp->display, l, ", %u.%2.2u KB", x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10); } else { l -= snprintf(tp->display, l, ", %u bytes", (int)total); } if (rate) snprintf(tp->display + sizeof(tp->display) - l, l, - " | %u KiB/s", rate); + " | %u KB/s", rate); } void display_throughput(struct progress *progress, off_t total) @@ -159,7 +193,7 @@ tp->curr_total = total; /* - * We have x = bytes and y = microsecs. We want z = KiB/s: + * We have x = bytes and y = microsecs. We want z = KB/s: * * z = (x / 1024) / (y / 1000000) * z = x / y * 1000000 / 1024 @@ -252,7 +286,7 @@ throughput_string(tp, tp->curr_total, rate); } progress_update = 1; - sprintf(bufp, ", %s.\n", msg); + sprintf(bufp, ", %s.", msg); display(progress, progress->last_value, bufp); if (buf != bufp) free(bufp);