From 584243d358e4c14ed1daf65271bb4a3fd505d770 Mon Sep 17 00:00:00 2001 From: Derek Stevens Date: Wed, 1 Feb 2023 22:08:04 -0700 Subject: [PATCH] fix line count for files that don't end in a newline --- routes/template.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/routes/template.go b/routes/template.go index d3e0cef..408841a 100644 --- a/routes/template.go +++ b/routes/template.go @@ -45,15 +45,23 @@ func (d *deps) listFiles(files []git.NiceTree, data map[string]any, w http.Respo func countLines(r io.Reader) (int, error) { buf := make([]byte, 32*1024) + bufLen := 0 count := 0 nl := []byte{'\n'} for { c, err := r.Read(buf) + if c > 0 { + bufLen += c + } count += bytes.Count(buf[:c], nl) switch { case err == io.EOF: + /* handle last line not having a newline at the end */ + if bufLen >= 1 && buf[bufLen-1] != '\n' { + count++ + } return count, nil case err != nil: return 0, err