From 9b9082a10b83eb6ea1b10f688fb4fab8cb4e84a5 Mon Sep 17 00:00:00 2001 From: Joe Lim <50560759+joelim-work@users.noreply.github.com> Date: Tue, 4 Jun 2024 10:09:21 +1000 Subject: [PATCH] Fix image previews containing long lines (#1737) To reproduce, use `chafa --polite on -f symbols` for an image preview. --- nav.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/nav.go b/nav.go index 9937f5a5..ba9b2c3e 100644 --- a/nav.go +++ b/nav.go @@ -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 { + line = append(line, bytes...) + + if !isPrefix { reg.lines = append(reg.lines, string(line)) + line = []byte{} } - - addLine = !isPrefix } }