Fix preview for files containing long lines (#1447)

* Fix preview for files containing long lines

* Limit lines read to preview window height

* Refactor

* Make comment more clear
This commit is contained in:
Joe Lim authored and GitHub committed 2023-10-22 20:37:40 +02:00
1 parent b4af8f47c8
commit 154d7182c0
1 file changed
+16 -7
+16 -7
View File
@@ -889,20 +889,29 @@ func (nav *nav) preview(path string, win *win) {
}
}
buf := bufio.NewScanner(reader)
// bufio.Scanner can't handle files containing long lines if they exceed the
// size of its internal buffer
bufReader := bufio.NewReader(reader)
for i := 0; i < win.h && buf.Scan(); i++ {
for _, r := range buf.Text() {
addLine := true
for len(reg.lines) < win.h {
line, isPrefix, err := bufReader.ReadLine()
if err != nil {
break
}
for _, r := range line {
if r == 0 {
reg.lines = []string{"\033[7mbinary\033[0m"}
return
}
}
reg.lines = append(reg.lines, buf.Text())
}
if buf.Err() != nil {
log.Printf("loading file: %s", buf.Err())
if addLine {
reg.lines = append(reg.lines, string(line))
}
addLine = !isPrefix
}
}