Support configurable header layout in compose widget

This commit is contained in:
Daniel Bridges 2019-07-22 16:29:07 -07:00 committed by Drew DeVault
parent 1b673b5ea7
commit 67fb0938a6
10 changed files with 240 additions and 169 deletions

View file

@ -27,9 +27,9 @@ func (_ Compose) Execute(aerc *widgets.Aerc, args []string) error {
} }
acct := aerc.SelectedAccount() acct := aerc.SelectedAccount()
composer := widgets.NewComposer( composer := widgets.NewComposer(
aerc.Config(), acct.AccountConfig(), acct.Worker()) aerc.Config(), acct.AccountConfig(), acct.Worker(), nil)
tab := aerc.NewTab(composer, "New email") tab := aerc.NewTab(composer, "New email")
composer.OnSubjectChange(func(subject string) { composer.OnHeaderChange("Subject", func(subject string) {
if subject == "" { if subject == "" {
tab.Name = "New email" tab.Name = "New email"
} else { } else {

View file

@ -113,14 +113,15 @@ func (_ reply) Execute(aerc *widgets.Aerc, args []string) error {
} }
} }
defaults := map[string]string{
"To": strings.Join(to, ", "),
"Cc": strings.Join(cc, ", "),
"Subject": subject,
"In-Reply-To": msg.Envelope.MessageId,
}
composer := widgets.NewComposer( composer := widgets.NewComposer(
aerc.Config(), acct.AccountConfig(), acct.Worker()). aerc.Config(), acct.AccountConfig(), acct.Worker(), defaults)
Defaults(map[string]string{
"To": strings.Join(to, ", "),
"Cc": strings.Join(cc, ", "),
"Subject": subject,
"In-Reply-To": msg.Envelope.MessageId,
})
if args[0] == "reply" { if args[0] == "reply" {
composer.FocusTerminal() composer.FocusTerminal()
@ -128,7 +129,7 @@ func (_ reply) Execute(aerc *widgets.Aerc, args []string) error {
addTab := func() { addTab := func() {
tab := aerc.NewTab(composer, subject) tab := aerc.NewTab(composer, subject)
composer.OnSubjectChange(func(subject string) { composer.OnHeaderChange("Subject", func(subject string) {
if subject == "" { if subject == "" {
tab.Name = "New email" tab.Name = "New email"
} else { } else {

View file

@ -83,15 +83,19 @@ func parseUnsubscribeMethods(header string) (methods []*url.URL) {
func unsubscribeMailto(aerc *widgets.Aerc, u *url.URL) error { func unsubscribeMailto(aerc *widgets.Aerc, u *url.URL) error {
widget := aerc.SelectedTab().(widgets.ProvidesMessage) widget := aerc.SelectedTab().(widgets.ProvidesMessage)
acct := widget.SelectedAccount() acct := widget.SelectedAccount()
composer := widgets.NewComposer(aerc.Config(), acct.AccountConfig(), defaults := map[string]string{
acct.Worker())
composer.Defaults(map[string]string{
"To": u.Opaque, "To": u.Opaque,
"Subject": u.Query().Get("subject"), "Subject": u.Query().Get("subject"),
}) }
composer := widgets.NewComposer(
aerc.Config(),
acct.AccountConfig(),
acct.Worker(),
defaults,
)
composer.SetContents(strings.NewReader(u.Query().Get("body"))) composer.SetContents(strings.NewReader(u.Query().Get("body")))
tab := aerc.NewTab(composer, "unsubscribe") tab := aerc.NewTab(composer, "unsubscribe")
composer.OnSubjectChange(func(subject string) { composer.OnHeaderChange("Subject", func(subject string) {
if subject == "" { if subject == "" {
tab.Name = "unsubscribe" tab.Name = "unsubscribe"
} else { } else {

View file

@ -81,6 +81,13 @@ always-show-mime=false
# supports it. Defaults to $EDITOR, or vi. # supports it. Defaults to $EDITOR, or vi.
editor= editor=
#
# Default header fields to display when composing a message. To display
# multiple headers in the same row, separate them with a pipe, e.g. "To|From".
#
# Default: To|From,Subject
header-layout=To|From,Subject
[filters] [filters]
# #
# Filters allow you to pipe an email body through a shell command to render # Filters allow you to pipe an email body through a shell command to render

View file

@ -65,7 +65,8 @@ type BindingConfig struct {
} }
type ComposeConfig struct { type ComposeConfig struct {
Editor string `ini:"editor"` Editor string `ini:"editor"`
HeaderLayout [][]string `ini:"-"`
} }
type FilterConfig struct { type FilterConfig struct {
@ -278,6 +279,12 @@ func (config *AercConfig) LoadConfig(file *ini.File) error {
if err := compose.MapTo(&config.Compose); err != nil { if err := compose.MapTo(&config.Compose); err != nil {
return err return err
} }
for key, val := range compose.KeysHash() {
switch key {
case "header-layout":
config.Compose.HeaderLayout = parseLayout(val)
}
}
} }
if ui, err := file.GetSection("ui"); err == nil { if ui, err := file.GetSection("ui"); err == nil {
if err := ui.MapTo(&config.Ui); err != nil { if err := ui.MapTo(&config.Ui); err != nil {
@ -350,6 +357,13 @@ func LoadConfigFromFile(root *string, sharedir string) (*AercConfig, error) {
{"Subject"}, {"Subject"},
}, },
}, },
Compose: ComposeConfig{
HeaderLayout: [][]string{
{"To", "From"},
{"Subject"},
},
},
} }
// These bindings are not configurable // These bindings are not configurable
config.Bindings.AccountWizard.ExKey = KeyStroke{ config.Bindings.AccountWizard.ExKey = KeyStroke{

View file

@ -151,6 +151,12 @@ These options are configured in the *[compose]* section of aerc.conf.
embedded terminal, though it may also launch a graphical window if the embedded terminal, though it may also launch a graphical window if the
environment supports it. Defaults to *$EDITOR*, or *vi*(1). environment supports it. Defaults to *$EDITOR*, or *vi*(1).
*header-layout*
Defines the default headers to display when composing a message. To display
multiple headers in the same row, separate them with a pipe, e.g. "To|From".
Default: To|From,Subject
## FILTERS ## FILTERS
Filters allow you to pipe an email body through a shell command to render Filters allow you to pipe an email body through a shell command to render

View file

@ -353,7 +353,7 @@ func (aerc *Aerc) Mailto(addr *url.URL) error {
} }
} }
composer := NewComposer(aerc.Config(), composer := NewComposer(aerc.Config(),
acct.AccountConfig(), acct.Worker()).Defaults(defaults) acct.AccountConfig(), acct.Worker(), defaults)
composer.FocusSubject() composer.FocusSubject()
title := "New email" title := "New email"
if subj, ok := defaults["Subject"]; ok { if subj, ok := defaults["Subject"]; ok {
@ -361,7 +361,7 @@ func (aerc *Aerc) Mailto(addr *url.URL) error {
composer.FocusTerminal() composer.FocusTerminal()
} }
tab := aerc.NewTab(composer, title) tab := aerc.NewTab(composer, title)
composer.OnSubjectChange(func(subject string) { composer.OnHeaderChange("Subject", func(subject string) {
if subject == "" { if subject == "" {
tab.Name = "New email" tab.Name = "New email"
} else { } else {

View file

@ -24,11 +24,7 @@ import (
) )
type Composer struct { type Composer struct {
headers struct { editors map[string]*headerEditor
from *headerEditor
subject *headerEditor
to *headerEditor
}
acct *config.AccountConfig acct *config.AccountConfig
config *config.AercConfig config *config.AercConfig
@ -45,77 +41,93 @@ type Composer struct {
focused int focused int
} }
// TODO: Let caller configure headers, initial body (for replies), etc
func NewComposer(conf *config.AercConfig, func NewComposer(conf *config.AercConfig,
acct *config.AccountConfig, worker *types.Worker) *Composer { acct *config.AccountConfig, worker *types.Worker, defaults map[string]string) *Composer {
if defaults == nil {
defaults = make(map[string]string)
}
if from := defaults["From"]; from == "" {
defaults["From"] = acct.From
}
layout, editors, focusable := buildComposeHeader(conf.Compose.HeaderLayout, defaults)
header, headerHeight := layout.grid(
func(header string) ui.Drawable { return editors[header] },
)
grid := ui.NewGrid().Rows([]ui.GridSpec{ grid := ui.NewGrid().Rows([]ui.GridSpec{
{ui.SIZE_EXACT, 3}, {ui.SIZE_EXACT, headerHeight},
{ui.SIZE_WEIGHT, 1}, {ui.SIZE_WEIGHT, 1},
}).Columns([]ui.GridSpec{ }).Columns([]ui.GridSpec{
{ui.SIZE_WEIGHT, 1}, {ui.SIZE_WEIGHT, 1},
}) })
// TODO: let user specify extra headers to edit by default
headers := ui.NewGrid().Rows([]ui.GridSpec{
{ui.SIZE_EXACT, 1}, // To/From
{ui.SIZE_EXACT, 1}, // Subject
{ui.SIZE_EXACT, 1}, // [spacer]
}).Columns([]ui.GridSpec{
{ui.SIZE_WEIGHT, 1},
{ui.SIZE_WEIGHT, 1},
})
to := newHeaderEditor("To", "")
from := newHeaderEditor("From", acct.From)
subject := newHeaderEditor("Subject", "")
headers.AddChild(to).At(0, 0)
headers.AddChild(from).At(0, 1)
headers.AddChild(subject).At(1, 0).Span(1, 2)
headers.AddChild(ui.NewFill(' ')).At(2, 0).Span(1, 2)
email, err := ioutil.TempFile("", "aerc-compose-*.eml") email, err := ioutil.TempFile("", "aerc-compose-*.eml")
if err != nil { if err != nil {
// TODO: handle this better // TODO: handle this better
return nil return nil
} }
grid.AddChild(headers).At(0, 0) grid.AddChild(header).At(0, 0)
c := &Composer{ c := &Composer{
acct: acct, editors: editors,
config: conf, acct: acct,
email: email, config: conf,
grid: grid, defaults: defaults,
worker: worker, email: email,
grid: grid,
worker: worker,
// You have to backtab to get to "From", since you usually don't edit it // You have to backtab to get to "From", since you usually don't edit it
focused: 1, focused: 1,
focusable: []ui.DrawableInteractive{from, to, subject}, focusable: focusable,
} }
c.headers.to = to
c.headers.from = from
c.headers.subject = subject
c.ShowTerminal() c.ShowTerminal()
return c return c
} }
// Sets additional headers to be added to the outgoing email (e.g. In-Reply-To) func buildComposeHeader(layout HeaderLayout, defaults map[string]string) (newLayout HeaderLayout, editors map[string]*headerEditor, focusable []ui.DrawableInteractive) {
func (c *Composer) Defaults(defaults map[string]string) *Composer { editors = make(map[string]*headerEditor)
c.defaults = defaults focusable = make([]ui.DrawableInteractive, 0)
if to, ok := defaults["To"]; ok {
c.headers.to.input.Set(to) for _, row := range layout {
delete(defaults, "To") for _, h := range row {
e := newHeaderEditor(h, "")
editors[h] = e
switch h {
case "From":
// Prepend From to support backtab
focusable = append([]ui.DrawableInteractive{e}, focusable...)
default:
focusable = append(focusable, e)
}
}
} }
if from, ok := defaults["From"]; ok {
c.headers.from.input.Set(from) // Add Cc/Bcc editors to layout if in defaults and not already visible
delete(defaults, "From") for _, h := range []string{"Cc", "Bcc"} {
if val, ok := defaults[h]; ok && val != "" {
if _, ok := editors[h]; !ok {
e := newHeaderEditor(h, "")
editors[h] = e
focusable = append(focusable, e)
layout = append(layout, []string{h})
}
}
} }
if subject, ok := defaults["Subject"]; ok {
c.headers.subject.input.Set(subject) // Set default values for all editors
delete(defaults, "Subject") for key := range editors {
if val, ok := defaults[key]; ok {
editors[key].input.Set(val)
delete(defaults, key)
}
} }
return c return layout, editors, focusable
} }
// Note: this does not reload the editor. You must call this before the first // Note: this does not reload the editor. You must call this before the first
@ -133,7 +145,7 @@ func (c *Composer) FocusTerminal() *Composer {
return c return c
} }
c.focusable[c.focused].Focus(false) c.focusable[c.focused].Focus(false)
c.focused = 3 c.focused = len(c.editors)
c.focusable[c.focused].Focus(true) c.focusable[c.focused].Focus(true)
return c return c
} }
@ -145,10 +157,13 @@ func (c *Composer) FocusSubject() *Composer {
return c return c
} }
func (c *Composer) OnSubjectChange(fn func(subject string)) { // OnHeaderChange registers an OnChange callback for the specified header.
c.headers.subject.OnChange(func() { func (c *Composer) OnHeaderChange(header string, fn func(subject string)) {
fn(c.headers.subject.input.String()) if editor, ok := c.editors[header]; ok {
}) editor.OnChange(func() {
fn(editor.input.String())
})
}
} }
func (c *Composer) Draw(ctx *ui.Context) { func (c *Composer) Draw(ctx *ui.Context) {
@ -209,7 +224,9 @@ func (c *Composer) Worker() *types.Worker {
func (c *Composer) PrepareHeader() (*mail.Header, []string, error) { func (c *Composer) PrepareHeader() (*mail.Header, []string, error) {
// Extract headers from the email, if present // Extract headers from the email, if present
c.email.Seek(0, os.SEEK_SET) if err := c.reloadEmail(); err != nil {
return nil, nil, err
}
var ( var (
rcpts []string rcpts []string
header mail.Header header mail.Header
@ -224,23 +241,62 @@ func (c *Composer) PrepareHeader() (*mail.Header, []string, error) {
// Update headers // Update headers
mhdr := (*message.Header)(&header.Header) mhdr := (*message.Header)(&header.Header)
mhdr.SetText("Message-Id", mail.GenerateMessageID()) mhdr.SetText("Message-Id", mail.GenerateMessageID())
if subject, _ := header.Subject(); subject == "" {
header.SetSubject(c.headers.subject.input.String()) headerKeys := make([]string, 0, len(c.editors))
for key := range c.editors {
headerKeys = append(headerKeys, key)
} }
if date, err := header.Date(); err != nil || date == (time.Time{}) { // Ensure headers which require special processing are included.
header.SetDate(time.Now()) for _, key := range []string{"To", "From", "Cc", "Bcc", "Subject", "Date"} {
} if _, ok := c.editors[key]; !ok {
from := c.headers.from.input.String() headerKeys = append(headerKeys, key)
from_addrs, err := gomail.ParseAddressList(from)
if err != nil {
return nil, nil, errors.Wrapf(err, "ParseAddressList(%s)", from)
} else {
var simon_from []*mail.Address
for _, addr := range from_addrs {
simon_from = append(simon_from, (*mail.Address)(addr))
} }
header.SetAddressList("From", simon_from)
} }
for _, h := range headerKeys {
val := ""
editor, ok := c.editors[h]
if ok {
val = editor.input.String()
} else {
val, _ = mhdr.Text(h)
}
switch h {
case "Subject":
if subject, _ := header.Subject(); subject == "" {
header.SetSubject(val)
}
case "Date":
if date, err := header.Date(); err != nil || date == (time.Time{}) {
header.SetDate(time.Now())
}
case "From", "To", "Cc", "Bcc": // Address headers
if val != "" {
hdrRcpts, err := gomail.ParseAddressList(val)
if err != nil {
return nil, nil, errors.Wrapf(err, "ParseAddressList(%s)", val)
}
edRcpts := make([]*mail.Address, len(hdrRcpts))
for i, addr := range hdrRcpts {
edRcpts[i] = (*mail.Address)(addr)
}
header.SetAddressList(h, edRcpts)
if h != "From" {
for _, addr := range edRcpts {
rcpts = append(rcpts, addr.Address)
}
}
}
default:
// Handle user configured header editors.
if ok && !mhdr.Header.Has(h) {
if val := editor.input.String(); val != "" {
mhdr.SetText(h, val)
}
}
}
}
// Merge in additional headers // Merge in additional headers
txthdr := mhdr.Header txthdr := mhdr.Header
for key, value := range c.defaults { for key, value := range c.defaults {
@ -248,56 +304,14 @@ func (c *Composer) PrepareHeader() (*mail.Header, []string, error) {
mhdr.SetText(key, value) mhdr.SetText(key, value)
} }
} }
if to := c.headers.to.input.String(); to != "" {
// Dammit Simon, this branch is 3x as long as it ought to be because
// your types aren't compatible enough with each other
to_rcpts, err := gomail.ParseAddressList(to)
if err != nil {
return nil, nil, errors.Wrapf(err, "ParseAddressList(%s)", to)
}
ed_rcpts, err := header.AddressList("To")
if err != nil {
return nil, nil, errors.Wrap(err, "AddressList(To)")
}
for _, addr := range to_rcpts {
ed_rcpts = append(ed_rcpts, (*mail.Address)(addr))
}
header.SetAddressList("To", ed_rcpts)
for _, addr := range ed_rcpts {
rcpts = append(rcpts, addr.Address)
}
}
if cc, _ := mhdr.Text("Cc"); cc != "" {
cc_rcpts, err := gomail.ParseAddressList(cc)
if err != nil {
return nil, nil, errors.Wrapf(err, "ParseAddressList(%s)", cc)
}
// TODO: Update when the user inputs Cc's through the UI
for _, addr := range cc_rcpts {
rcpts = append(rcpts, addr.Address)
}
}
if bcc, _ := mhdr.Text("Bcc"); bcc != "" {
bcc_rcpts, err := gomail.ParseAddressList(bcc)
if err != nil {
return nil, nil, errors.Wrapf(err, "ParseAddressList(%s)", bcc)
}
// TODO: Update when the user inputs Bcc's through the UI
for _, addr := range bcc_rcpts {
rcpts = append(rcpts, addr.Address)
}
}
return &header, rcpts, nil return &header, rcpts, nil
} }
func (c *Composer) WriteMessage(header *mail.Header, writer io.Writer) error { func (c *Composer) WriteMessage(header *mail.Header, writer io.Writer) error {
name := c.email.Name() if err := c.reloadEmail(); err != nil {
c.email.Close() return err
file, err := os.Open(name)
if err != nil {
return errors.Wrap(err, "FileOpen")
} }
c.email = file
var body io.Reader var body io.Reader
reader, err := mail.CreateReader(c.email) reader, err := mail.CreateReader(c.email)
if err == nil { if err == nil {
@ -472,6 +486,17 @@ func (c *Composer) NextField() {
c.focusable[c.focused].Focus(true) c.focusable[c.focused].Focus(true)
} }
func (c *Composer) reloadEmail() error {
name := c.email.Name()
c.email.Close()
file, err := os.Open(name)
if err != nil {
return errors.Wrap(err, "ReloadEmail")
}
c.email = file
return nil
}
type headerEditor struct { type headerEditor struct {
name string name string
input *ui.TextInput input *ui.TextInput

41
widgets/headerlayout.go Normal file
View file

@ -0,0 +1,41 @@
package widgets
import (
"git.sr.ht/~sircmpwn/aerc/lib/ui"
"git.sr.ht/~sircmpwn/aerc/models"
)
type HeaderLayout [][]string
// 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 {
// To preserve layout alignment, only hide rows if all columns are empty
for _, col := range row {
if headers.Get(col) != "" {
result = append(result, row)
break
}
}
}
return result
}
// grid builds a ui grid, populating each cell by calling a callback function
// with the current header string.
func (layout HeaderLayout) grid(cb func(string) ui.Drawable) (grid *ui.Grid, height int) {
rowCount := len(layout) + 1 // extra row for spacer
grid = ui.MakeGrid(rowCount, 1, ui.SIZE_EXACT, ui.SIZE_WEIGHT)
for i, cols := range layout {
r := ui.MakeGrid(1, len(cols), ui.SIZE_EXACT, ui.SIZE_WEIGHT)
for j, col := range cols {
r.AddChild(cb(col)).At(0, j)
}
grid.AddChild(r).At(i, 0)
}
grid.AddChild(ui.NewFill(' ')).At(rowCount-1, 0)
return grid, rowCount
}

View file

@ -46,7 +46,16 @@ type PartSwitcher struct {
func NewMessageViewer(acct *AccountView, conf *config.AercConfig, func NewMessageViewer(acct *AccountView, conf *config.AercConfig,
store *lib.MessageStore, msg *models.MessageInfo) *MessageViewer { store *lib.MessageStore, msg *models.MessageInfo) *MessageViewer {
header, headerHeight := createHeader(msg, conf.Viewer.HeaderLayout)
layout := HeaderLayout(conf.Viewer.HeaderLayout).forMessage(msg)
header, headerHeight := layout.grid(
func(header string) ui.Drawable {
return &HeaderView{
Name: header,
Value: fmtHeader(msg, header),
}
},
)
grid := ui.NewGrid().Rows([]ui.GridSpec{ grid := ui.NewGrid().Rows([]ui.GridSpec{
{ui.SIZE_EXACT, headerHeight}, {ui.SIZE_EXACT, headerHeight},
@ -78,42 +87,6 @@ func NewMessageViewer(acct *AccountView, conf *config.AercConfig,
} }
} }
func createHeader(msg *models.MessageInfo, layout [][]string) (grid *ui.Grid, height int) {
presentHeaders := presentHeaders(msg, layout)
rowCount := len(presentHeaders) + 1 // extra row for spacer
grid = ui.MakeGrid(rowCount, 1, ui.SIZE_EXACT, ui.SIZE_WEIGHT)
for i, cols := range presentHeaders {
r := ui.MakeGrid(1, len(cols), ui.SIZE_EXACT, ui.SIZE_WEIGHT)
for j, col := range cols {
r.AddChild(
&HeaderView{
Name: col,
Value: fmtHeader(msg, col),
}).At(0, j)
}
grid.AddChild(r).At(i, 0)
}
grid.AddChild(ui.NewFill(' ')).At(rowCount-1, 0)
return grid, rowCount
}
// presentHeaders returns a filtered header layout, removing rows whose headers
// do not appear in the provided message.
func presentHeaders(msg *models.MessageInfo, layout [][]string) [][]string {
headers := msg.RFC822Headers
result := make([][]string, 0, len(layout))
for _, row := range layout {
// To preserve layout alignment, only hide rows if all columns are empty
for _, col := range row {
if headers.Get(col) != "" {
result = append(result, row)
break
}
}
}
return result
}
func fmtHeader(msg *models.MessageInfo, header string) string { func fmtHeader(msg *models.MessageInfo, header string) string {
switch header { switch header {
case "From": case "From":