diff options
author | Drew DeVault <sir@cmpwn.com> | 2019-06-07 11:14:40 -0400 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2019-06-07 11:14:50 -0400 |
commit | 0647ea64839df5ceecf3a71f672f05a589fd1409 (patch) | |
tree | 63102e7e0bb0167618bfbcb972948e255ae47170 /widgets | |
parent | e0cadd61a24473e5d41ed3e54f2826b7dd8a8a0e (diff) | |
download | aerc-0647ea64839df5ceecf3a71f672f05a589fd1409.tar.gz |
Move ANSI stripping from filters to Go
Diffstat (limited to 'widgets')
-rw-r--r-- | widgets/msgviewer.go | 24 |
1 files changed, 20 insertions, 4 deletions
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() }() } |