about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2019-06-07 11:14:40 -0400
committerDrew DeVault <sir@cmpwn.com>2019-06-07 11:14:50 -0400
commit0647ea64839df5ceecf3a71f672f05a589fd1409 (patch)
tree63102e7e0bb0167618bfbcb972948e255ae47170
parente0cadd61a24473e5d41ed3e54f2826b7dd8a8a0e (diff)
downloadaerc-0647ea64839df5ceecf3a71f672f05a589fd1409.tar.gz
Move ANSI stripping from filters to Go
-rwxr-xr-xcontrib/hldiff.py2
-rwxr-xr-xcontrib/plaintext.py2
-rw-r--r--widgets/msgviewer.go24
3 files changed, 20 insertions, 8 deletions
diff --git a/contrib/hldiff.py b/contrib/hldiff.py
index 5f660fd..f3cfd20 100755
--- a/contrib/hldiff.py
+++ b/contrib/hldiff.py
@@ -3,13 +3,11 @@ from colorama import Fore, Style
 import sys
 import re
 
-ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
 stat_re = re.compile(r'(| \d+ )(\+*)(\-*)')
 lines_re = re.compile(r'@@ (-\d+,\d+ \+\d+,\d+) @@')
 
 sys.stdin.reconfigure(encoding='utf-8', errors='ignore')
 patch = sys.stdin.read().replace("\r\n", "\n")
-patch = ansi_escape.sub('', patch)
 
 hit_diff = False
 for line in patch.split("\n"):
diff --git a/contrib/plaintext.py b/contrib/plaintext.py
index 522055b..d46991a 100755
--- a/contrib/plaintext.py
+++ b/contrib/plaintext.py
@@ -5,7 +5,6 @@ import re
 
 # TODO: Wrap text to terminal width?
 
-ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
 # TODO: I guess this might vary from MUA to MUA. I've definitely seen localized
 # versions in the wild
 quote_prefix_re = re.compile(r"On .*, .* wrote:")
@@ -13,7 +12,6 @@ quote_re = re.compile(r">+")
 
 sys.stdin.reconfigure(encoding='utf-8', errors='ignore')
 mail = sys.stdin.read().replace("\r\n", "\n")
-mail = ansi_escape.sub('', mail)
 
 for line in mail.split("\n"):
     if quote_re.match(line) or quote_prefix_re.match(line):
diff --git a/widgets/msgviewer.go b/widgets/msgviewer.go
index af0880a..6017e50 100644
--- a/widgets/msgviewer.go
+++ b/widgets/msgviewer.go
@@ -1,10 +1,12 @@
 package widgets
 
 import (
+	"bufio"
 	"bytes"
 	"fmt"
 	"io"
 	"os/exec"
+	"regexp"
 	"strings"
 
 	"github.com/danwakefield/fnmatch"
@@ -22,6 +24,8 @@ import (
 	"git.sr.ht/~sircmpwn/aerc/worker/types"
 )
 
+var ansi = regexp.MustCompile("^\x1B\\[[0-?]*[ -/]*[@-~]")
+
 type MessageViewer struct {
 	ui.Invalidatable
 	acct     *AccountView
@@ -221,9 +225,11 @@ func (mv *MessageViewer) SelectedMessage() *types.MessageInfo {
 
 func (mv *MessageViewer) ToggleHeaders() {
 	switcher := mv.switcher
-	err := createSwitcher(switcher, mv.conf, mv.store, mv.msg, !switcher.showHeaders)
+	err := createSwitcher(
+		switcher, mv.conf, mv.store, mv.msg, !switcher.showHeaders)
 	if err != nil {
-		mv.acct.Logger().Printf("warning: error during create switcher - %v", err)
+		mv.acct.Logger().Printf(
+			"warning: error during create switcher - %v", err)
 	}
 	switcher.Invalidate()
 }
@@ -468,7 +474,8 @@ func (pv *PartViewer) attemptCopy() {
 			if pv.showHeaders && pv.msg.RFC822Headers != nil {
 				fields := pv.msg.RFC822Headers.Fields()
 				for fields.Next() {
-					field := fmt.Sprintf("%s: %s\n", fields.Key(), fields.Value())
+					field := fmt.Sprintf(
+						"%s: %s\n", fields.Key(), fields.Value())
 					pv.sink.Write([]byte(field))
 				}
 				pv.sink.Write([]byte{'\n'})
@@ -487,7 +494,16 @@ func (pv *PartViewer) attemptCopy() {
 				pv.Invalidate()
 				return
 			}
-			io.Copy(pv.sink, part.Body)
+			if pv.part.MIMEType == "text" {
+				scanner := bufio.NewScanner(part.Body)
+				for scanner.Scan() {
+					text := scanner.Text()
+					text = ansi.ReplaceAllString(text, "")
+					io.WriteString(pv.sink, text+"\n")
+				}
+			} else {
+				io.Copy(pv.sink, part.Body)
+			}
 			pv.sink.Close()
 		}()
 	}