1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#include <inttypes.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "progress.h"
static uint64_t progress_max;
static uint64_t progress_pcent;
static uint64_t progress_n_upd;
static time_t progress_prevsec;
static struct timespec progress_start;
#define PROGRESS_CHARS 50
void progress_init(uint64_t count)
{
unsigned int i;
progress_max = count;
progress_pcent = 0;
progress_n_upd = ULONG_MAX;
progress_prevsec = ULONG_MAX;
printf("\r[");
for (i = 0; i < PROGRESS_CHARS; i++)
printf(" ");
printf("] 0%%");
fflush(stdout);
clock_gettime(CLOCK_MONOTONIC, &progress_start);}
void progress_tick(uint64_t cur)
{
unsigned int i, pos;
struct timespec now;
uint64_t pcent;
double sec;
pcent = (cur * 100) / progress_max;
if (progress_pcent == pcent && cur < progress_n_upd &&
cur < progress_max)
return;
progress_pcent = pcent;
pos = (pcent * PROGRESS_CHARS) / 101;
clock_gettime(CLOCK_MONOTONIC, &now);
printf("\r[");
for (i = 0; i <= pos; i++)
printf("=");
for (; i < PROGRESS_CHARS; i++)
printf(" ");
printf("] %" PRIu64 "%%", pcent);
sec = difftime(now.tv_sec, progress_start.tv_sec);
if (sec >= 5 && pcent > 0) {
uint64_t persec = cur / sec;
uint64_t rem_sec;
if (!persec)
persec = 1;
progress_n_upd = cur + persec;
rem_sec = ((sec * 100) + (pcent / 2)) / pcent - sec;
if (rem_sec > progress_prevsec)
rem_sec = progress_prevsec;
progress_prevsec = rem_sec;
if (rem_sec < 60)
printf(" ETA:%" PRIu64 "s ", rem_sec);
else {
printf(" ETA:%" PRIu64 ":%02" PRIu64 ":%02" PRIu64 " ",
rem_sec / 3600,
(rem_sec / 60) % 60,
rem_sec % 60);
}
}
fflush(stdout);
}
void progress_end(void)
{
printf("\n");
}
|