align some columns - stagit-gopher - A git gopher frontend. (mirror)
 (HTM) git clone git://bitreich.org/stagit-gopher/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/stagit-gopher/
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit aee984022be470e18b1b085703801ae842b4e24a
 (DIR) parent caa55f2bb419bd2f5327a779fe9afe13f7b4ed38
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Tue, 13 Jun 2017 19:42:35 +0200
       
       align some columns
       
       Diffstat:
         M stagit.c                            |     123 ++++++++++++++++++++++---------
       
       1 file changed, 89 insertions(+), 34 deletions(-)
       ---
 (DIR) diff --git a/stagit.c b/stagit.c
       @@ -248,6 +248,43 @@ xmlencode(FILE *fp, const char *s, size_t len)
                }
        }
        
       +void
       +trim(char *buf, size_t bufsiz, const char *src)
       +{
       +        size_t d = 0, i, len, n = 0, s;
       +
       +        len = strlen(src);
       +        for (s = 0; s < len && d < bufsiz - 1; s++) {
       +                if (src[s] == '\n')
       +                        n = 0;
       +
       +                switch (src[s]) {
       +                case '\t':
       +                        if (d + 8 >= bufsiz - 1)
       +                                goto end;
       +                        for (i = 0; i < 8; i++)
       +                                buf[d++] = ' ';
       +                        break;
       +                case '|':
       +                case '\r':
       +                        buf[d++] = ' ';
       +                        break;
       +                case 't':
       +                        if (!n && src[s] == 't') {
       +                                if (d + 1 >= bufsiz - 1)
       +                                        goto end;
       +                                buf[d++] = src[s];
       +                        }
       +                default:
       +                        buf[d++] = src[s];
       +                        break;
       +                }
       +                n++;
       +        }
       +end:
       +        buf[d] = '\0';
       +}
       +
        /* Escape characters in text in geomyidae .gph format */
        void
        gphtext(FILE *fp, const char *s, size_t len)
       @@ -264,10 +301,10 @@ gphtext(FILE *fp, const char *s, size_t len)
                                n = 1;
                        }
        
       -                if (s[i] == '\t') {
       -                        fputs("        ", fp);
       -                } else {
       -                        fputc(s[i], fp);
       +                switch (s[i]) {
       +                case '\r': break;
       +                case '\t': fputs("        ", fp); break;
       +                default: fputc(s[i], fp);
                        }
                        n++;
                }
       @@ -415,10 +452,11 @@ writeblobgph(FILE *fp, const git_blob *blob)
                                n++;
                                fprintf(fp, nfmt, n, n, n);
                                for (j = prev; s[j] && j <= i; j++) {
       -                                if (s[j] == '\t')
       -                                        fputs("        ", fp);
       -                                else
       -                                        fputc(s[j], fp);
       +                                switch (s[j]) {
       +                                case '\r': break;
       +                                case '\t': fputs("        ", fp); break;
       +                                default: fputc(s[j], fp);
       +                                }
                                }
                                prev = i + 1;
                        }
       @@ -427,10 +465,11 @@ writeblobgph(FILE *fp, const git_blob *blob)
                                n++;
                                fprintf(fp, nfmt, n, n, n);
                                for (j = prev; s[j] && j < len - prev; j++) {
       -                                if (s[j] == '\t')
       -                                        fputs("        ", fp);
       -                                else
       -                                        fputc(s[j], fp);
       +                                switch (s[j]) {
       +                                case '\r': break;
       +                                case '\t': fputs("        ", fp); break;
       +                                default: fputc(s[j], fp);
       +                                }
                                }
                        }
                }
       @@ -567,18 +606,24 @@ printshowfile(FILE *fp, struct commitinfo *ci)
        void
        writelogline(FILE *fp, struct commitinfo *ci)
        {
       +        char buf[1024];
       +
                fputs("[1|", fp);
                if (ci->author)
                        printtimeshort(fp, &(ci->author->when));
                fputs("  ", fp);
       -        if (ci->summary)
       -                gphlink(fp, ci->summary, strlen(ci->summary));
       +        if (ci->summary) {
       +                trim(buf, sizeof(buf), ci->summary);
       +                fprintf(fp, "%-50.50s", buf);
       +        }
                fputs("  ", fp);
       -        if (ci->author)
       -                gphlink(fp, ci->author->name, strlen(ci->author->name));
       -        fprintf(fp, "  %zu", ci->filecount);
       -        fprintf(fp, "  +%zu", ci->addcount);
       -        fprintf(fp, "  -%zu", ci->delcount);
       +        if (ci->author) {
       +                trim(buf, sizeof(buf), ci->author->name);
       +                fprintf(fp, "%-25.25s", buf);
       +        }
       +        fprintf(fp, "  %5zu", ci->filecount);
       +        fprintf(fp, "  %5zu+", ci->addcount);
       +        fprintf(fp, "  %5zu-", ci->delcount);
                fprintf(fp, "|%scommit/%s.gph", relpath, ci->oid);
                fputs("|server|port]\n", fp);
        }
       @@ -796,6 +841,7 @@ writefilestree(FILE *fp, git_tree *tree, const char *path)
                git_off_t filesize;
                const char *entryname;
                char filepath[PATH_MAX], entrypath[PATH_MAX];
       +        char buf[1024];
                size_t count, i;
                int lc, r, ret;
        
       @@ -834,12 +880,12 @@ writefilestree(FILE *fp, git_tree *tree, const char *path)
                                fputs("[1|", fp);
                                fputs(filemode(git_tree_entry_filemode(entry)), fp);
                                fputs("  ", fp);
       -                        gphlink(fp, entrypath, strlen(entrypath));
       -                        fputs("  ", fp);
       +                        trim(buf, sizeof(buf), entrypath);
       +                        fprintf(fp, "%-50.50s  ", buf);
                                if (lc > 0)
       -                                fprintf(fp, "%dL", lc);
       +                                fprintf(fp, "%7dL", lc);
                                else
       -                                fprintf(fp, "%juB", (uintmax_t)filesize);
       +                                fprintf(fp, "%7juB", (uintmax_t)filesize);
                                fprintf(fp, "|%s%s", relpath, filepath);
                                fputs("|server|port]\n", fp);
                                git_object_free(obj);
       @@ -862,7 +908,9 @@ writefiles(FILE *fp, const git_oid *id)
                git_commit *commit = NULL;
                int ret = -1;
        
       -        fputs("Mode  Name  Size\n", fp);
       +        fprintf(fp, "%-10.10s  ", "Mode");
       +        fprintf(fp, "%-50.50s  ", "Name");
       +        fprintf(fp, "%8.8s\n", "Size");
        
                if (!git_commit_lookup(&commit, repo, id) &&
                    !git_commit_tree(&tree, commit))
       @@ -899,8 +947,8 @@ writerefs(FILE *fp)
                git_reference **refs = NULL;
                size_t count, i, j, refcount;
                const char *titles[] = { "Branches", "Tags" };
       -        const char *ids[] = { "branches", "tags" };
                const char *name;
       +        char buf[1024];
        
                if (git_reference_iterator_new(&it, repo))
                        return -1;
       @@ -943,20 +991,24 @@ writerefs(FILE *fp)
        
                                /* print header if it has an entry (first). */
                                if (++count == 1) {
       -                                gphtext(fp, titles[j], strlen(titles[j]));
       -                                fputs("Name  Last commit date  Author\n\n", fp);
       +                                fprintf(fp, "%s\n", titles[j]);
       +                                fprintf(fp, "  %-25.25s", "Name");
       +                                fprintf(fp, "  %-16.16s", "Last commit date");
       +                                fprintf(fp, "  %-25.25s\n", "Author");
                                }
        
                                name = git_reference_shorthand(r);
        
                                fputs("  ", fp);
       -                        xmlencode(fp, name, strlen(name));
       -                        fputs("  ", fp);
       +                        trim(buf, sizeof(buf), name);
       +                        fprintf(fp, "%-25.25s  ", name);
                                if (ci->author)
                                        printtimeshort(fp, &(ci->author->when));
                                fputs("  ", fp);
       -                        if (ci->author)
       -                                xmlencode(fp, ci->author->name, strlen(ci->author->name));
       +                        if (ci->author) {
       +                                trim(buf, sizeof(buf), ci->author->name);
       +                                fprintf(fp, "%-25.25s\n", buf);
       +                        }
                                fputs("\n", fp);
        
                                commitinfo_free(ci);
       @@ -1099,9 +1151,12 @@ main(int argc, char *argv[])
                fp = efopen("log.gph", "w");
                mkdir("commit", 0755);
                writeheader(fp, "Log");
       -        fputs("Date  "
       -              "Commit message  "
       -              "Author  Files  +  -\n", fp);
       +        fprintf(fp, "%-16.16s  ", "Date");
       +        fprintf(fp, "%-50.50s  ", "Commit message");
       +        fprintf(fp, "%-25.25s  ", "Author");
       +        fprintf(fp, "%5.5s  ", "Files");
       +        fprintf(fp, "%6.6s  ", "+");
       +        fprintf(fp, "%6.6s\n", "-");
        
                if (cachefile) {
                        /* read from cache file (does not need to exist) */