]> git.armaanb.net Git - stagit.git/commitdiff
Express file sizes in human readable format
authorArmaan Bhojwani <me@armaanb.net>
Tue, 6 Apr 2021 01:56:55 +0000 (21:56 -0400)
committerArmaan Bhojwani <me@armaanb.net>
Tue, 6 Apr 2021 01:56:55 +0000 (21:56 -0400)
src/stagit.c

index 217021ee896c6a2124693b829175f65c69315ea1..d7597f90c2b33d747db2410abc1fd001dffc7378 100644 (file)
@@ -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)) {