commit df75a9ec171a286d8804cdd39e7b208043606b9b
parent d99ab6ed32970d2caf73a52355597524dc8dee86
Author: Armaan Bhojwani <me@armaanb.net>
Date: Mon, 5 Apr 2021 21:56:55 -0400
Express file sizes in human readable format
Diffstat:
1 file changed, 20 insertions(+), 2 deletions(-)
diff --git 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("<p> ", fp);
xmlencode(fp, filename, strlen(filename));
- fprintf(fp, " (%juB)", (uintmax_t)filesize);
+ fprintf(fp, " (%s)", convertbytes((int)filesize));
fputs("</p><hr/>", 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("</td></tr>\n", fp);
git_object_free(obj);
} else if (!git_submodule_lookup(&module, repo, entryname)) {