From 556f346f96946a54223798685c445ec413a4031e Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Mon, 17 Oct 2022 15:16:09 -0500 Subject: msgviewer: simplify filter and pager command handling Refactor the filtering and paging logic to use several fewer goroutines. Fixes data race condition with the timing of filter -> pager -> terminal, can be found when switching message views fast. Check if filter -> pager process is currently running before calling it to start again. Fixes data race between fetching message body and terminal starting. Both can initiate the copying process, and for long running filters and fast message fetching, it is possible to have fetched a message but still be running the filter when the terminal starts. Move StripAnsi to it's own file in lib/parse, similar to the hyperlinks parser. Signed-off-by: Tim Culverhouse Acked-by: Robin Jarry --- lib/parse/ansi.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 lib/parse/ansi.go (limited to 'lib/parse/ansi.go') diff --git a/lib/parse/ansi.go b/lib/parse/ansi.go new file mode 100644 index 00000000..265ef1f4 --- /dev/null +++ b/lib/parse/ansi.go @@ -0,0 +1,37 @@ +package parse + +import ( + "bufio" + "bytes" + "fmt" + "io" + "os" + "regexp" + + "git.sr.ht/~rjarry/aerc/logging" +) + +var ansi = regexp.MustCompile("\x1B\\[[0-?]*[ -/]*[@-~]") + +// StripAnsi strips ansi escape codes from the reader +func StripAnsi(r io.Reader) io.Reader { + buf := bytes.NewBuffer(nil) + scanner := bufio.NewScanner(r) + scanner.Buffer(nil, 1024*1024*1024) + for scanner.Scan() { + line := scanner.Bytes() + line = ansi.ReplaceAll(line, []byte("")) + _, err := buf.Write(line) + if err != nil { + logging.Warnf("failed write ", err) + } + _, err = buf.Write([]byte("\n")) + if err != nil { + logging.Warnf("failed write ", err) + } + } + if err := scanner.Err(); err != nil { + fmt.Fprintf(os.Stderr, "failed to read line: %v\n", err) + } + return buf +} -- cgit