void draw_progress_bar(const char *prefix, double percentage) {
+ /* We are going output a bunch of small strings that shall appear as a single line to STDERR which is
+ * unbuffered by default. Let's temporarily turn on full buffering, so that this is passed to the tty
+ * as a single buffer, to make things more efficient. */
+ char buffer[LONG_LINE_MAX];
+ setvbuf(stderr, buffer, _IOFBF, sizeof(buffer));
+
fputc('\r', stderr);
if (prefix)
fputs(prefix, stderr);
fputc('\r', stderr);
fflush(stderr);
+
+ /* Disable buffering again */
+ setvbuf(stderr, NULL, _IONBF, 0);
}
void clear_progress_bar(const char *prefix) {
+ char buffer[LONG_LINE_MAX];
+ setvbuf(stderr, buffer, _IOFBF, sizeof(buffer));
+
fputc('\r', stderr);
if (terminal_is_dumb())
fputc('\r', stderr);
fflush(stderr);
+
+ /* Disable buffering again */
+ setvbuf(stderr, NULL, _IONBF, 0);
}