From: Armaan Bhojwani Date: Tue, 6 Apr 2021 01:56:55 +0000 (-0400) Subject: Express file sizes in human readable format X-Git-Url: https://git.armaanb.net/?p=stagit.git;a=commitdiff_plain;h=df75a9ec171a286d8804cdd39e7b208043606b9b Express file sizes in human readable format --- diff --git a/src/stagit.c b/src/stagit.c index 217021e..d7597f9 100644 --- a/src/stagit.c +++ b/src/stagit.c @@ -802,6 +802,24 @@ writeatom(FILE *fp) return 0; } +float +rounder(float var) +{ + float value = (int)(var * 10 + .5); + return (float)value / 10; +} + +const char * +convertbytes(int bytes) +{ + (float)bytes; + static char outp[255] = "hi"; + if (bytes < 1024) sprintf(outp, "%u %s", bytes, "B"); + else if (bytes < 1048576) sprintf(outp, "%0.1f %s", rounder(bytes/1024.0), "K"); + else sprintf(outp, "%0.1f %s", rounder(bytes/1048576.0), "M"); + return outp; +} + int writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize) { @@ -827,7 +845,7 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi writeheader(fp, filename); fputs("

", fp); xmlencode(fp, filename, strlen(filename)); - fprintf(fp, " (%juB)", (uintmax_t)filesize); + fprintf(fp, " (%s)", convertbytes((int)filesize)); fputs("


", fp); @@ -943,7 +961,7 @@ writefilestree(FILE *fp, git_tree *tree, const char *path) if (lc > 0) fprintf(fp, "%dL", lc); else - fprintf(fp, "%juB", (uintmax_t)filesize); + fprintf(fp, "%s", convertbytes((int)filesize)); fputs("\n", fp); git_object_free(obj); } else if (!git_submodule_lookup(&module, repo, entryname)) {