diff options
author | Robin Jarry <robin@jarry.cc> | 2023-01-29 15:30:34 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-02-02 21:48:29 +0100 |
commit | 05f218a6122c91c79d131731d65844cf3fa881eb (patch) | |
tree | 7581f0bfbebe1ba90b2a7104bc5e851c8f99247c | |
parent | 17ce7c762cb10ce4272de8ddd45367ba94360456 (diff) | |
download | aerc-05f218a6122c91c79d131731d65844cf3fa881eb.tar.gz |
viewer: use textproto.WriteHeader
When [ui].show-headers is true, use textproto.WriteHeader instead of
manually writing the header values. This allows displaying the original
header buffer with on-the-wire format (and with lines wrapped).
Signed-off-by: Robin Jarry <robin@jarry.cc>
Tested-by: Bence Ferdinandy <bence@ferdinandy.com>
-rw-r--r-- | widgets/msgviewer.go | 29 |
1 files changed, 13 insertions, 16 deletions
diff --git a/widgets/msgviewer.go b/widgets/msgviewer.go index d2fc0d80..0375dda9 100644 --- a/widgets/msgviewer.go +++ b/widgets/msgviewer.go @@ -1,6 +1,7 @@ package widgets import ( + "bytes" "errors" "fmt" "io" @@ -10,6 +11,7 @@ import ( "sync/atomic" "github.com/danwakefield/fnmatch" + "github.com/emersion/go-message/textproto" "github.com/gdamore/tcell/v2" "github.com/google/shlex" "github.com/mattn/go-runewidth" @@ -728,30 +730,25 @@ func (pv *PartViewer) writeMailHeaders() { defer file.Close() } - fields := info.RFC822Headers.Fields() - for fields.Next() { - var value string - var err error - if value, err = fields.Text(); err != nil { - // better than nothing, use the non decoded version - value = fields.Value() - } - field := fmt.Sprintf( - "%s: %s\n", fields.Key(), value) - _, err = file.Write([]byte(field)) - if err != nil { - log.Errorf("failed to write headers: %v", err) - } + var buf bytes.Buffer + err := textproto.WriteHeader(&buf, info.RFC822Headers.Header.Header) + if err != nil { + log.Errorf("failed to format headers: %v", err) } + _, err = file.Write(bytes.TrimRight(buf.Bytes(), "\r\n")) + if err != nil { + log.Errorf("failed to write headers: %v", err) + } + // virtual header if len(info.Labels) != 0 { labels := fmtHeader(info, "Labels", "", "", "", "") - _, err := file.Write([]byte(fmt.Sprintf("Labels: %s\n", labels))) + _, err := file.Write([]byte(fmt.Sprintf("\r\nLabels: %s", labels))) if err != nil { log.Errorf("failed to write to labels: %v", err) } } - _, err := file.Write([]byte{'\n'}) + _, err = file.Write([]byte{'\r', '\n', '\r', '\n'}) if err != nil { log.Errorf("failed to write empty line: %v", err) } |