From 44b20f341443cf3daffbe2b092df28df522cad86 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Sun, 16 Apr 2017 20:37:42 +0200 Subject: [PATCH] writeblobhtml: improve file view for text-browsers browsers such as lynx and w3m have some quirks displaying tables and pre elements. links is a bit better with this. --- stagit.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/stagit.c b/stagit.c index 4e081dd..09f3df7 100644 --- a/stagit.c +++ b/stagit.c @@ -378,28 +378,31 @@ writefooter(FILE *fp) int writeblobhtml(FILE *fp, const git_blob *blob) { - off_t i; - size_t n = 0; - const char *nfmt = "%d\n"; + size_t n, i, prev; + const char *nfmt = "%6d "; const char *s = git_blob_rawcontent(blob); git_off_t len = git_blob_rawsize(blob); - fputs("
\n", fp);
+	fputs("
\n", fp);
 
-	if (len) {
-		n++;
-		fprintf(fp, nfmt, n, n, n);
-		for (i = 0; i < len - 1; i++) {
-			if (s[i] == '\n') {
-				n++;
-				fprintf(fp, nfmt, n, n, n);
-			}
+	if (len > 0) {
+		for (i = 0, prev = 0, n = 0; i < (size_t)len; i++) {
+			if (s[i] != '\n')
+				continue;
+			n++;
+			fprintf(fp, nfmt, n, n, n);
+			xmlencode(fp, &s[prev], i - prev + 1);
+			prev = i + 1;
+		}
+		/* trailing data */
+		if ((i - prev) > 0) {
+			n++;
+			fprintf(fp, nfmt, n, n, n);
+			xmlencode(fp, &s[prev], len - prev);
 		}
 	}
 
-	fputs("
\n", fp);
-	xmlencode(fp, s, (size_t)len);
-	fputs("
\n", fp); + fputs("\n", fp); return n; } @@ -486,7 +489,7 @@ printshowfile(FILE *fp, struct commitinfo *ci) fwrite(&linestr[add], 1, del, fp); fputs("\n", fp); } - fprintf(fp, "%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n", + fprintf(fp, "
%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
 		ci->filecount, ci->filecount == 1 ? "" : "s",
 	        ci->addcount,  ci->addcount  == 1 ? "" : "s",
 	        ci->delcount,  ci->delcount  == 1 ? "" : "s");
--
libgit2 1.1.1