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/pages/templates/repo/blob.html | 2 +- appview/repo/blob.go | 19 +++++++++++++++++-- 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 @@ HasRawView bool // can download raw (everything except submodule) // current display mode ShowingRendered bool // currently in rendered mode - ShowingText bool // currently in text/code mode // content type flags ContentType BlobContentType @@ -151,3 +150,7 @@ 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/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 @@ at {{ .Ref }} {{ if .BlobView.ShowingText }} - {{ .Lines }} lines + {{ .BlobView.Lines }} lines {{ end }} {{ if .BlobView.SizeHint }} 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 @@ view.ShowingRendered = queryParams.Get("code") != "true" 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 @@ view.HasTextView = true 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 @@ -291,3 +291,18 @@ "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 +} -- tangled.sh