msgviewer: Add Labels as a virtual header

In order to accommodate for that, the headerlayout needed to be rewritten
to pass the filter criteria back to the msgviewer, instead of just using
the normal headers.
This commit is contained in:
Reto Brunner 2019-12-23 12:51:59 +01:00 committed by Drew DeVault
parent 63391b7dca
commit b360cca977
2 changed files with 26 additions and 6 deletions

View File

@ -7,15 +7,19 @@ import (
type HeaderLayout [][]string
type HeaderLayoutFilter struct {
layout HeaderLayout
keep func(msg *models.MessageInfo, header string) bool // filter criteria
}
// forMessage returns a filtered header layout, removing rows whose headers
// do not appear in the provided message.
func (layout HeaderLayout) forMessage(msg *models.MessageInfo) HeaderLayout {
headers := msg.RFC822Headers
result := make(HeaderLayout, 0, len(layout))
for _, row := range layout {
func (filter HeaderLayoutFilter) forMessage(msg *models.MessageInfo) HeaderLayout {
result := make(HeaderLayout, 0, len(filter.layout))
for _, row := range filter.layout {
// To preserve layout alignment, only hide rows if all columns are empty
for _, col := range row {
if headers.Get(col) != "" {
if filter.keep(msg, col) {
result = append(result, row)
break
}

View File

@ -52,7 +52,16 @@ type PartSwitcher struct {
func NewMessageViewer(acct *AccountView, conf *config.AercConfig,
store *lib.MessageStore, msg *models.MessageInfo) *MessageViewer {
layout := HeaderLayout(conf.Viewer.HeaderLayout).forMessage(msg)
hf := HeaderLayoutFilter{
layout: HeaderLayout(conf.Viewer.HeaderLayout),
keep: func(msg *models.MessageInfo, header string) bool {
if fmtHeader(msg, header, "2") != "" {
return true
}
return false
},
}
layout := hf.forMessage(msg)
header, headerHeight := layout.grid(
func(header string) ui.Drawable {
return &HeaderView{
@ -109,6 +118,8 @@ func fmtHeader(msg *models.MessageInfo, header string, timefmt string) string {
return msg.Envelope.Date.Local().Format(timefmt)
case "Subject":
return msg.Envelope.Subject
case "Labels":
return strings.Join(msg.Labels, ", ")
default:
return msg.RFC822Headers.Get(header)
}
@ -580,6 +591,11 @@ func (pv *PartViewer) attemptCopy() {
"%s: %s\n", fields.Key(), fields.Value())
pv.sink.Write([]byte(field))
}
// virtual header
if len(pv.msg.Labels) != 0 {
labels := fmtHeader(pv.msg, "Labels", "")
pv.sink.Write([]byte(fmt.Sprintf("Labels: %s\n", labels)))
}
pv.sink.Write([]byte{'\n'})
}