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 }}