aboutsummaryrefslogblamecommitdiffstats
path: root/commands/compose/header.go
blob: aaa14e43663e0763fff5fa04e662701102fbd7dd (plain) (tree)
1
2
3
4
5
6
7
8
9
10





                 
                                    
                                         

 





                                                  
 








                       








                                  



                                

                                                         

 
                                              
                                                               
 
                                              
 
                     

                                               
 
                     
                                                        


                                  
                                                             


                                                                                       


                 
                                                       
 
package compose

import (
	"fmt"
	"strings"

	"git.sr.ht/~rjarry/aerc/app"
	"git.sr.ht/~rjarry/aerc/commands"
)

type Header struct {
	Force  bool   `opt:"-f"`
	Remove bool   `opt:"-d"`
	Name   string `opt:"name"`
	Value  string `opt:"..." required:"false"`
}

var headers = []string{
	"From",
	"To",
	"Cc",
	"Bcc",
	"Subject",
	"Comments",
	"Keywords",
}

func init() {
	register(Header{})
}

func (Header) Aliases() []string {
	return []string{"header"}
}

func (Header) Options() string {
	return "fd"
}

func (Header) Complete(args []string) []string {
	return commands.CompletionFromList(headers, args)
}

func (h Header) Execute(args []string) error {
	composer, _ := app.SelectedTabContent().(*app.Composer)

	name := strings.TrimRight(h.Name, ":")

	if h.Remove {
		return composer.DelEditor(name)
	}

	if !h.Force {
		headers, err := composer.PrepareHeader()
		if err != nil {
			return err
		}
		if headers.Get(name) != "" && h.Value != "" {
			return fmt.Errorf(
				"Header %s is already set to %q (use -f to overwrite)",
				name, headers.Get(name))
		}
	}

	return composer.AddEditor(name, h.Value, false)
}