]> git.armaanb.net Git - stagit.git/commitdiff
Improve human readable size implementation
authorArmaan Bhojwani <me@armaanb.net>
Tue, 6 Apr 2021 03:22:16 +0000 (23:22 -0400)
committerArmaan Bhojwani <me@armaanb.net>
Tue, 6 Apr 2021 03:29:22 +0000 (23:29 -0400)
src/stagit.c

index d7597f90c2b33d747db2410abc1fd001dffc7378..b5580b8cc772f7bebd6533cd437d0aae98ef6766 100644 (file)
@@ -410,7 +410,7 @@ get_ext(const char *filename)
        return dot + 1;
 }
 
        return dot + 1;
 }
 
-int
+void
 call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
 {
        htmlized = false;
 call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
 {
        htmlized = false;
@@ -420,7 +420,6 @@ call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
        char *html = cmark_markdown_to_html(s, len, CMARK_OPT_DEFAULT);
        if (strcmp(get_ext(filename), "md") == 0) htmlized = true;
 
        char *html = cmark_markdown_to_html(s, len, CMARK_OPT_DEFAULT);
        if (strcmp(get_ext(filename), "md") == 0) htmlized = true;
 
-       int lc;
        if (!htmlized) {
                // Copy STDOUT
                int stdout_copy = dup(1);
        if (!htmlized) {
                // Copy STDOUT
                int stdout_copy = dup(1);
@@ -439,7 +438,6 @@ call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
                // Give code to highlight through STDIN:
                size_t i;
                for (i = 0; *s && i < len; s++, i++) {
                // Give code to highlight through STDIN:
                size_t i;
                for (i = 0; *s && i < len; s++, i++) {
-                       if (*s == '\n') lc++;
                        fprintf(child, "%c", *s);
                }
 
                        fprintf(child, "%c", *s);
                }
 
@@ -452,22 +450,17 @@ call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
        } else {
                fprintf(fp, "%s", html);
        }
        } else {
                fprintf(fp, "%s", html);
        }
-
-       return lc;
 }
 
 }
 
-int
+void
 writeblobhtml(const char *filename, FILE *fp, const git_blob *blob)
 {
 writeblobhtml(const char *filename, FILE *fp, const git_blob *blob)
 {
-       int lc = 0;
        const char *s = git_blob_rawcontent(blob);
        git_off_t len = git_blob_rawsize(blob);
 
        if (len > 0) {
        const char *s = git_blob_rawcontent(blob);
        git_off_t len = git_blob_rawsize(blob);
 
        if (len > 0) {
-               lc = call_chroma(filename, fp, s, len);
+               call_chroma(filename, fp, s, len);
        }
        }
-
-       return lc;
 }
 
 void
 }
 
 void
@@ -805,35 +798,33 @@ writeatom(FILE *fp)
 float
 rounder(float var)
 {
 float
 rounder(float var)
 {
-    float value = (int)(var * 10 + .5);
-    return (float)value / 10;
+    int value = var * 10 + .5;
+    return value / 10.0;
 }
 
 const char *
 convertbytes(int bytes)
 {
 }
 
 const char *
 convertbytes(int bytes)
 {
-       (float)bytes;
-       static char outp[255] = "hi";
+       bytes = (float)bytes;
+       static char outp[25];
        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;
 }
 
        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
+void
 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
 {
        char tmp[PATH_MAX] = "", *d;
        const char *p;
 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
 {
        char tmp[PATH_MAX] = "", *d;
        const char *p;
-       int lc = 0;
        FILE *fp;
 
        if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
                errx(1, "path truncated: '%s'", fpath);
        if (!(d = dirname(tmp)))
                err(1, "dirname");
        FILE *fp;
 
        if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
                errx(1, "path truncated: '%s'", fpath);
        if (!(d = dirname(tmp)))
                err(1, "dirname");
-       if (mkdirp(d))
-               return -1;
+       mkdirp(d);
 
        for (p = fpath, tmp[0] = '\0'; *p; p++) {
                if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
 
        for (p = fpath, tmp[0] = '\0'; *p; p++) {
                if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
@@ -852,7 +843,7 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi
        if (git_blob_is_binary((git_blob *)obj)) {
                fputs("<p>Binary file.</p>\n", fp);
        } else {
        if (git_blob_is_binary((git_blob *)obj)) {
                fputs("<p>Binary file.</p>\n", fp);
        } else {
-               lc = writeblobhtml(filename, fp, (git_blob *)obj);
+               writeblobhtml(filename, fp, (git_blob *)obj);
                if (ferror(fp))
                        err(1, "fwrite");
        }
                if (ferror(fp))
                        err(1, "fwrite");
        }
@@ -861,8 +852,6 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi
        fclose(fp);
 
        relpath = "";
        fclose(fp);
 
        relpath = "";
-
-       return lc;
 }
 
 const char *
 }
 
 const char *
@@ -917,7 +906,7 @@ writefilestree(FILE *fp, git_tree *tree, const char *path)
        const char *entryname;
        char filepath[PATH_MAX], entrypath[PATH_MAX];
        size_t count, i;
        const char *entryname;
        char filepath[PATH_MAX], entrypath[PATH_MAX];
        size_t count, i;
-       int lc, r, ret;
+       int r, ret;
 
        count = git_tree_entrycount(tree);
        for (i = 0; i < count; i++) {
 
        count = git_tree_entrycount(tree);
        for (i = 0; i < count; i++) {
@@ -949,7 +938,7 @@ writefilestree(FILE *fp, git_tree *tree, const char *path)
                        }
 
                        filesize = git_blob_rawsize((git_blob *)obj);
                        }
 
                        filesize = git_blob_rawsize((git_blob *)obj);
-                       lc = writeblob(obj, filepath, entryname, filesize);
+                       writeblob(obj, filepath, entryname, filesize);
 
                        fputs("<tr><td>", fp);
                        fputs(filemode(git_tree_entry_filemode(entry)), fp);
 
                        fputs("<tr><td>", fp);
                        fputs(filemode(git_tree_entry_filemode(entry)), fp);
@@ -958,10 +947,7 @@ writefilestree(FILE *fp, git_tree *tree, const char *path)
                        fputs("\">", fp);
                        xmlencode(fp, entrypath, strlen(entrypath));
                        fputs("</a></td><td class=\"num\" align=\"right\">", fp);
                        fputs("\">", fp);
                        xmlencode(fp, entrypath, strlen(entrypath));
                        fputs("</a></td><td class=\"num\" align=\"right\">", fp);
-                       if (lc > 0)
-                               fprintf(fp, "%dL", lc);
-                       else
-                               fprintf(fp, "%s", convertbytes((int)filesize));
+                       fprintf(fp, "%s", convertbytes((int)filesize));
                        fputs("</td></tr>\n", fp);
                        git_object_free(obj);
                } else if (!git_submodule_lookup(&module, repo, entryname)) {
                        fputs("</td></tr>\n", fp);
                        git_object_free(obj);
                } else if (!git_submodule_lookup(&module, repo, entryname)) {