Monorepo for Tangled

appview/pages: fix line count in blob views

Signed-off-by: oppiliappan <me@oppi.li>

+22 -4
+4 -1
appview/models/repo.go
··· 130 130 131 131 // current display mode 132 132 ShowingRendered bool // currently in rendered mode 133 - ShowingText bool // currently in text/code mode 134 133 135 134 // content type flags 136 135 ContentType BlobContentType ··· 151 150 // no view available, only raw 152 151 return !(b.HasRenderedView || b.HasTextView) 153 152 } 153 + 154 + func (b BlobView) ShowingText() bool { 155 + return !b.ShowingRendered 156 + }
+1 -1
appview/pages/templates/repo/blob.html
··· 35 35 36 36 {{ if .BlobView.ShowingText }} 37 37 <span class="select-none px-1 md:px-2 [&:before]:content-['·']"></span> 38 - <span>{{ .Lines }} lines</span> 38 + <span>{{ .BlobView.Lines }} lines</span> 39 39 {{ end }} 40 40 41 41 {{ if .BlobView.SizeHint }}
+17 -2
appview/repo/blob.go
··· 219 219 if resp.Content != nil { 220 220 bytes, _ := base64.StdEncoding.DecodeString(*resp.Content) 221 221 view.Contents = string(bytes) 222 - view.Lines = strings.Count(view.Contents, "\n") + 1 222 + view.Lines = countLines(view.Contents) 223 223 } 224 224 225 225 case ".mp4", ".webm", ".ogg", ".mov", ".avi": ··· 238 238 239 239 if resp.Content != nil { 240 240 view.Contents = *resp.Content 241 - view.Lines = strings.Count(view.Contents, "\n") + 1 241 + view.Lines = countLines(view.Contents) 242 242 } 243 243 244 244 // with text, we may be dealing with markdown ··· 291 291 } 292 292 return slices.Contains(textualTypes, mimeType) 293 293 } 294 + 295 + // TODO: dedup with strings 296 + func countLines(content string) int { 297 + if content == "" { 298 + return 0 299 + } 300 + 301 + count := strings.Count(content, "\n") 302 + 303 + if !strings.HasSuffix(content, "\n") { 304 + count++ 305 + } 306 + 307 + return count 308 + }