From 7c23c27c5957a586ed88d9260bd4356362e0d11d Mon Sep 17 00:00:00 2001 From: Joe Lim <50560759+joelim-work@users.noreply.github.com> Date: Mon, 30 Oct 2023 09:40:02 +1100 Subject: [PATCH] Refactor sixel peek in previewer code (#1477) --- nav.go | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/nav.go b/nav.go index c32fc972..b6a0dcea 100644 --- a/nav.go +++ b/nav.go @@ -2,7 +2,6 @@ package main import ( "bufio" - "bytes" "errors" "fmt" "io" @@ -827,7 +826,7 @@ func (nav *nav) preview(path string, win *win) { reg := ®{loadTime: time.Now(), path: path} defer func() { nav.regChan <- reg }() - var reader io.Reader + var reader *bufio.Reader if len(gOpts.previewer) != 0 { cmd := exec.Command(gOpts.previewer, path, @@ -861,7 +860,7 @@ func (nav *nav) preview(path string, win *win) { } }() defer out.Close() - reader = out + reader = bufio.NewReader(out) } else { f, err := os.Open(path) if err != nil { @@ -870,14 +869,11 @@ func (nav *nav) preview(path string, win *win) { } defer f.Close() - reader = f + reader = bufio.NewReader(f) } - prefix := make([]byte, 2) if gOpts.sixel { - n, err := reader.Read(prefix) - reader = io.MultiReader(bytes.NewReader(prefix[:n]), reader) - + prefix, err := reader.Peek(2) if err == nil && string(prefix) == gSixelBegin { b, err := io.ReadAll(reader) if err != nil { @@ -891,11 +887,9 @@ 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 - bufReader := bufio.NewReader(reader) - addLine := true for len(reg.lines) < win.h { - line, isPrefix, err := bufReader.ReadLine() + line, isPrefix, err := reader.ReadLine() if err != nil { break }