From 037ee1a6cfb35383f34b0fcc70dfc53f9463bd09 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Wed, 19 Jul 2023 18:50:12 +0200 Subject: compose: allow removing headers Allow removing headers from the compose window when edit-headers=false (the default) with :header -d . Signed-off-by: Robin Jarry Acked-by: Koni Marti --- commands/compose/header.go | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) (limited to 'commands/compose') diff --git a/commands/compose/header.go b/commands/compose/header.go index c512e341..59d01952 100644 --- a/commands/compose/header.go +++ b/commands/compose/header.go @@ -1,7 +1,6 @@ package compose import ( - "errors" "fmt" "strings" @@ -30,47 +29,56 @@ func (Header) Aliases() []string { return []string{"header"} } +func (Header) Options() string { + return "fd" +} + 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]) +func (h Header) Execute(aerc *widgets.Aerc, args []string) error { + opts, optind, err := getopt.Getopts(args, h.Options()) + args = args[optind:] + if err == nil && len(args) < 1 { + err = fmt.Errorf("not enough arguments") } - - opts, optind, err := getopt.Getopts(args, "f") if err != nil { - return err - } - - if len(args) < optind+1 { - return errors.New("command parsing failed") + return fmt.Errorf("%w. usage: header [-fd] []", err) } var force bool = false + var remove bool = false for _, opt := range opts { - if opt.Option == 'f' { + switch opt.Option { + case 'f': force = true + case 'd': + remove = true } } composer, _ := aerc.SelectedTabContent().(*widgets.Composer) - args[optind] = strings.TrimRight(args[optind], ":") + name := strings.TrimRight(args[0], ":") + + if remove { + return composer.DelEditor(name) + } - value := strings.Join(args[optind+1:], " ") + value := strings.Join(args[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]) + if headers.Get(name) != "" && value != "" { + return fmt.Errorf( + "Header %s is already set to %q (use -f to overwrite)", + name, headers.Get(name)) } } - return composer.AddEditor(args[optind], value, false) + return composer.AddEditor(name, value, false) } -- cgit