aerc/commands/compose/header.go
Moritz Poldrack 978d35d356 lint: homogenize operations and minor fixes (gocritic)
Apply GoDoc comment policy (comments for humans should have a space
after the //; machine-readable comments shouldn't)

Use strings.ReplaceAll instead of strings.Replace when appropriate

Remove if/else chains by replacing them with switches

Use short assignment/increment notation

Replace single case switches with if statements

Combine else and if when appropriate

Signed-off-by: Moritz Poldrack <moritz@poldrack.dev>
Acked-by: Robin Jarry <robin@jarry.cc>
2022-08-04 21:58:01 +02:00

78 lines
1.3 KiB
Go

package compose
import (
"errors"
"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(aerc, 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
}
if len(args) < optind+1 {
return errors.New("command parsing failed")
}
var force bool = false
for _, opt := range opts {
if opt.Option == 'f' {
force = true
}
}
composer, _ := aerc.SelectedTabContent().(*widgets.Composer)
args[optind] = strings.TrimRight(args[optind], ":")
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
}