this repo has no description
1package knotserver 2 3import ( 4 "bytes" 5 "io" 6 "log" 7 "net/http" 8 "strings" 9 10 "github.com/icyphox/bild/git" 11) 12 13func (h *Handle) listFiles(files []git.NiceTree, data map[string]any, w http.ResponseWriter) { 14 data["files"] = files 15 data["meta"] = h.c.Meta 16 17 writeJSON(w, data) 18 return 19} 20 21func countLines(r io.Reader) (int, error) { 22 buf := make([]byte, 32*1024) 23 bufLen := 0 24 count := 0 25 nl := []byte{'\n'} 26 27 for { 28 c, err := r.Read(buf) 29 if c > 0 { 30 bufLen += c 31 } 32 count += bytes.Count(buf[:c], nl) 33 34 switch { 35 case err == io.EOF: 36 /* handle last line not having a newline at the end */ 37 if bufLen >= 1 && buf[(bufLen-1)%(32*1024)] != '\n' { 38 count++ 39 } 40 return count, nil 41 case err != nil: 42 return 0, err 43 } 44 } 45} 46 47func (h *Handle) showFile(content string, data map[string]any, w http.ResponseWriter) { 48 lc, err := countLines(strings.NewReader(content)) 49 if err != nil { 50 // Non-fatal, we'll just skip showing line numbers in the template. 51 log.Printf("counting lines: %s", err) 52 } 53 54 lines := make([]int, lc) 55 if lc > 0 { 56 for i := range lines { 57 lines[i] = i + 1 58 } 59 } 60 61 data["linecount"] = lines 62 data["content"] = content 63 data["meta"] = h.c.Meta 64 65 writeJSON(w, data) 66 return 67} 68 69func (h *Handle) showRaw(content string, w http.ResponseWriter) { 70 w.WriteHeader(http.StatusOK) 71 w.Header().Set("Content-Type", "text/plain") 72 w.Write([]byte(content)) 73 return 74}