Move ANSI stripping from filters to Go

This commit is contained in:
Drew DeVault 2019-06-07 11:14:40 -04:00
parent e0cadd61a2
commit 0647ea6483
3 changed files with 20 additions and 8 deletions

View file

@ -3,13 +3,11 @@ from colorama import Fore, Style
import sys import sys
import re import re
ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
stat_re = re.compile(r'(| \d+ )(\+*)(\-*)') stat_re = re.compile(r'(| \d+ )(\+*)(\-*)')
lines_re = re.compile(r'@@ (-\d+,\d+ \+\d+,\d+) @@') lines_re = re.compile(r'@@ (-\d+,\d+ \+\d+,\d+) @@')
sys.stdin.reconfigure(encoding='utf-8', errors='ignore') sys.stdin.reconfigure(encoding='utf-8', errors='ignore')
patch = sys.stdin.read().replace("\r\n", "\n") patch = sys.stdin.read().replace("\r\n", "\n")
patch = ansi_escape.sub('', patch)
hit_diff = False hit_diff = False
for line in patch.split("\n"): for line in patch.split("\n"):

View file

@ -5,7 +5,6 @@ import re
# TODO: Wrap text to terminal width? # 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 # TODO: I guess this might vary from MUA to MUA. I've definitely seen localized
# versions in the wild # versions in the wild
quote_prefix_re = re.compile(r"On .*, .* wrote:") 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') sys.stdin.reconfigure(encoding='utf-8', errors='ignore')
mail = sys.stdin.read().replace("\r\n", "\n") mail = sys.stdin.read().replace("\r\n", "\n")
mail = ansi_escape.sub('', mail)
for line in mail.split("\n"): for line in mail.split("\n"):
if quote_re.match(line) or quote_prefix_re.match(line): if quote_re.match(line) or quote_prefix_re.match(line):

View file

@ -1,10 +1,12 @@
package widgets package widgets
import ( import (
"bufio"
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"os/exec" "os/exec"
"regexp"
"strings" "strings"
"github.com/danwakefield/fnmatch" "github.com/danwakefield/fnmatch"
@ -22,6 +24,8 @@ import (
"git.sr.ht/~sircmpwn/aerc/worker/types" "git.sr.ht/~sircmpwn/aerc/worker/types"
) )
var ansi = regexp.MustCompile("^\x1B\\[[0-?]*[ -/]*[@-~]")
type MessageViewer struct { type MessageViewer struct {
ui.Invalidatable ui.Invalidatable
acct *AccountView acct *AccountView
@ -221,9 +225,11 @@ func (mv *MessageViewer) SelectedMessage() *types.MessageInfo {
func (mv *MessageViewer) ToggleHeaders() { func (mv *MessageViewer) ToggleHeaders() {
switcher := mv.switcher 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 { 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() switcher.Invalidate()
} }
@ -468,7 +474,8 @@ func (pv *PartViewer) attemptCopy() {
if pv.showHeaders && pv.msg.RFC822Headers != nil { if pv.showHeaders && pv.msg.RFC822Headers != nil {
fields := pv.msg.RFC822Headers.Fields() fields := pv.msg.RFC822Headers.Fields()
for fields.Next() { 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(field))
} }
pv.sink.Write([]byte{'\n'}) pv.sink.Write([]byte{'\n'})
@ -487,7 +494,16 @@ func (pv *PartViewer) attemptCopy() {
pv.Invalidate() pv.Invalidate()
return return
} }
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) io.Copy(pv.sink, part.Body)
}
pv.sink.Close() pv.sink.Close()
}() }()
} }