From d80e684a38c1ef715bd203dd26bd12acd9cb2ee1 Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Sun, 25 Jan 2026 03:37:46 +0000 Subject: [PATCH] appview/pages: fix line count in blob views Signed-off-by: oppiliappan --- appview/models/repo.go | 5 ++++- appview/repo/blob.go | 19 +++++++++++++++++-- appview/pages/templates/repo/blob.html | 2 +- 3 file(s) changed, 22 insertion(s)(+), 4 deletion(s)(-) diff --git a/appview/models/repo.go b/appview/models/repo.go --- a/appview/models/repo.go +++ b/appview/models/repo.go @@ -130,7 +130,6 @@ // current display mode ShowingRendered bool // currently in rendered mode - ShowingText bool // currently in text/code mode // content type flags ContentType BlobContentType @@ -150,4 +149,8 @@ func (b BlobView) IsUnsupported() bool { // no view available, only raw return !(b.HasRenderedView || b.HasTextView) +} + +func (b BlobView) ShowingText() bool { + return !b.ShowingRendered } diff --git a/appview/repo/blob.go b/appview/repo/blob.go --- a/appview/repo/blob.go +++ b/appview/repo/blob.go @@ -219,7 +219,7 @@ if resp.Content != nil { bytes, _ := base64.StdEncoding.DecodeString(*resp.Content) view.Contents = string(bytes) - view.Lines = strings.Count(view.Contents, "\n") + 1 + view.Lines = countLines(view.Contents) } case ".mp4", ".webm", ".ogg", ".mov", ".avi": @@ -238,7 +238,7 @@ if resp.Content != nil { view.Contents = *resp.Content - view.Lines = strings.Count(view.Contents, "\n") + 1 + view.Lines = countLines(view.Contents) } // with text, we may be dealing with markdown @@ -290,4 +290,19 @@ "message/", } return slices.Contains(textualTypes, mimeType) +} + +// TODO: dedup with strings +func countLines(content string) int { + if content == "" { + return 0 + } + + count := strings.Count(content, "\n") + + if !strings.HasSuffix(content, "\n") { + count++ + } + + return count } diff --git a/appview/pages/templates/repo/blob.html b/appview/pages/templates/repo/blob.html --- a/appview/pages/templates/repo/blob.html +++ b/appview/pages/templates/repo/blob.html @@ -35,7 +35,7 @@ {{ if .BlobView.ShowingText }} - {{ .Lines }} lines + {{ .BlobView.Lines }} lines {{ end }} {{ if .BlobView.SizeHint }} -- tangled.sh