Skip to content

Commit

Permalink
[progress]update progress.c and add ssize function
Browse files Browse the repository at this point in the history
  • Loading branch information
jianjunjiang committed Aug 5, 2021
1 parent 902978e commit 237a72a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
2 changes: 0 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,7 @@ int main(int argc, char * argv[])
}
}
else
{
usage();
}
if(ctx.hdl)
libusb_close(ctx.hdl);
libusb_exit(NULL);
Expand Down
24 changes: 20 additions & 4 deletions progress.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include <progress.h>

static inline double gettime(void)
static double gettime(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + (double)tv.tv_usec / 1000000.0;
}

static inline const char * format_eta(double remaining)
static const char * format_eta(double remaining)
{
static char result[6] = "";
int seconds = remaining + 0.5;
Expand All @@ -19,6 +19,20 @@ static inline const char * format_eta(double remaining)
return "--:--";
}

static char * ssize(char * buf, double size)
{
const char * unit[] = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
int count = 0;

while((size > 1000) && (count < 8))
{
size /= 1000;
count++;
}
sprintf(buf, "%5.3f %s", size, unit[count]);
return buf;
}

void progress_start(struct progress_t * p, size_t total)
{
if(p && (total > 0))
Expand All @@ -31,6 +45,8 @@ void progress_start(struct progress_t * p, size_t total)

void progress_update(struct progress_t * p, size_t bytes)
{
char buf1[32], buf2[32];

if(p)
{
p->done += bytes;
Expand All @@ -44,9 +60,9 @@ void progress_update(struct progress_t * p, size_t bytes)
for(i = pos; i < 48; i++)
putchar(' ');
if(p->done < p->total)
printf("]%6.1f kB/s, ETA %s ", speed / 1000.0, format_eta(eta));
printf("] %s/s, ETA %s", ssize(buf1, speed), format_eta(eta));
else
printf("] %5.0f kB, %6.1f kB/s", p->done / 1000.0, speed / 1000.0);
printf("] %s, %s/s ", ssize(buf1, p->done), ssize(buf2, speed));
fflush(stdout);
}
}
Expand Down

0 comments on commit 237a72a

Please sign in to comment.