Fix image previews containing long lines (#1737)

To reproduce, use `chafa --polite on -f symbols` for an image preview.
This commit is contained in:
Joe Lim authored and GitHub committed 2024-06-04 10:09:21 +10:00
1 parent aaf5bbb22b
commit 9b9082a10b
1 file changed
+12 -8
+12 -8
View File
@@ -890,25 +890,29 @@ func (nav *nav) preview(path string, win *win) {
// bufio.Scanner can't handle files containing long lines if they exceed the
// size of its internal buffer
addLine := true
line := []byte{}
for len(reg.lines) < win.h {
line, isPrefix, err := reader.ReadLine()
bytes, isPrefix, err := reader.ReadLine()
if err != nil {
if len(line) > 0 {
reg.lines = append(reg.lines, string(line))
}
break
}
for _, r := range line {
if r == 0 {
for _, byte := range bytes {
if byte == 0 {
reg.lines = []string{"\033[7mbinary\033[0m"}
return
}
}
if addLine {
reg.lines = append(reg.lines, string(line))
}
line = append(line, bytes...)
addLine = !isPrefix
if !isPrefix {
reg.lines = append(reg.lines, string(line))
line = []byte{}
}
}
}