aerc/commands/compose/header.go
Reto Brunner 20ec2c8eeb compose: use a proper header instead of a string map
Prior to this commit, the composer was based on a map[string]string.
While this approach was very versatile, it lead to a constant encoding / decoding
of addresses and other headers.

This commit switches to a different model, where the composer is based on a header.
Commands which want to interact with it can simply set some defaults they would
like to have. Users can overwrite them however they like.

In order to get access to the functions generating / getting the msgid go-message
was upgraded.
2020-11-14 15:40:13 +01:00

73 lines
1.2 KiB
Go

package compose
import (
"fmt"
"strings"
"git.sr.ht/~sircmpwn/aerc/commands"
"git.sr.ht/~sircmpwn/aerc/widgets"
"git.sr.ht/~sircmpwn/getopt"
)
type Header struct{}
var (
headers = []string{
"From",
"To",
"Cc",
"Bcc",
"Subject",
"Comments",
"Keywords",
}
)
func init() {
register(Header{})
}
func (Header) Aliases() []string {
return []string{"header"}
}
func (Header) Complete(aerc *widgets.Aerc, args []string) []string {
return commands.CompletionFromList(headers, args)
}
func (Header) Execute(aerc *widgets.Aerc, args []string) error {
if len(args) < 2 {
return fmt.Errorf("Usage: %s [-f] field [value]", args[0])
}
opts, optind, err := getopt.Getopts(args, "f")
if err != nil {
return err
}
var (
force bool = false
)
for _, opt := range opts {
switch opt.Option {
case 'f':
force = true
}
}
composer, _ := aerc.SelectedTab().(*widgets.Composer)
if !force {
headers, err := composer.PrepareHeader()
if err != nil {
return err
}
if headers.Has(args[optind]) {
return fmt.Errorf("Header %s already exists", args[optind])
}
}
composer.AddEditor(args[optind], strings.Join(args[optind+1:], " "), false)
return nil
}