web frontend for git (tangled's grandpa)
7
fork

Configure Feed

Select the types of activity you want to include in your feed.

template: line numbers for file view

+41 -1
+34 -1
routes/template.go
··· 1 1 package routes 2 2 3 3 import ( 4 + "bytes" 4 5 "html/template" 6 + "io" 5 7 "log" 6 8 "net/http" 7 9 "path/filepath" 10 + "strings" 8 11 9 12 "icyphox.sh/legit/git" 10 13 ) ··· 40 43 } 41 44 } 42 45 46 + func countLines(r io.Reader) (int, error) { 47 + buf := make([]byte, 32*1024) 48 + count := 0 49 + nl := []byte{'\n'} 50 + 51 + for { 52 + c, err := r.Read(buf) 53 + count += bytes.Count(buf[:c], nl) 54 + 55 + switch { 56 + case err == io.EOF: 57 + return count, nil 58 + case err != nil: 59 + return 0, err 60 + } 61 + } 62 + } 63 + 43 64 func (d *deps) showFile(content string, data map[string]any, w http.ResponseWriter) { 44 65 tpath := filepath.Join(d.c.Template.Dir, "*") 45 66 t := template.Must(template.ParseGlob(tpath)) 46 67 47 - // TODO: Process content here. 68 + lc, err := countLines(strings.NewReader(content)) 69 + if err != nil { 70 + // Non-fatal, we'll just skip showing line numbers in the template. 71 + log.Printf("counting lines: %s", err) 72 + } 73 + 74 + lines := make([]int, lc) 75 + if lc > 0 { 76 + for i := range lines { 77 + lines[i] = i + 1 78 + } 79 + } 48 80 81 + data["linecount"] = lines 49 82 data["content"] = content 50 83 data["meta"] = d.c.Meta 51 84
+5
templates/file.html
··· 9 9 <body> 10 10 {{ template "nav" . }} 11 11 <main> 12 + <pre> 13 + {{ range .linecount }} 14 + <a id="#L{{ . }}" href="#{{ . }}">{{ . }}</a> 15 + {{- end -}} 16 + </pre> 12 17 <pre> 13 18 {{ .content }} 14 19 </pre>
+1
templates/head.html
··· 2 2 <head> 3 3 <meta charset="utf-8"> 4 4 <meta name="viewport" content="width=device-width, initial-scale=1"> 5 + <link rel="stylesheet" href="/static/style.css" type="text/css"> 5 6 <!-- other meta tags here --> 6 7 </head> 7 8 {{ end }}
+1
templates/nav.html
··· 9 9 <li><a href="/{{ .name }}/tree/{{ .ref }}/">tree</a> 10 10 <li><a href="/{{ .name }}/log/{{ .ref }}">log</a> 11 11 {{ end }} 12 + <li><a href="/{{ .name }}/refs">refs</a> 12 13 </ul> 13 14 </nav> 14 15 {{ end }}