aerc/commands/compose/header.go
Robin Jarry 0d645bcebd go.mod: change base git url
I'm not sure what are the implications but it seems required.

Link: https://github.com/golang/go/issues/20883
Signed-off-by: Robin Jarry <robin@jarry.cc>
2021-11-05 10:21:45 +01:00

75 lines
1.2 KiB
Go

package compose
import (
"fmt"
"strings"
"git.sr.ht/~rjarry/aerc/commands"
"git.sr.ht/~rjarry/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)
value := strings.Join(args[optind+1:], " ")
if !force {
headers, err := composer.PrepareHeader()
if err != nil {
return err
}
if headers.Has(args[optind]) && value != "" {
return fmt.Errorf("Header %s already exists", args[optind])
}
}
composer.AddEditor(args[optind], value, false)
return nil
}