Debian Codegraph @

commands/compose/header.go

raw
1 package compose
2
3 import (
4 "fmt"
5 "strings"
6
7 "git.sr.ht/~rjarry/aerc/app"
8 "git.sr.ht/~rjarry/aerc/commands"
9 )
10
11 type Header struct {
12 Force bool `opt:"-f" desc:"Overwrite any existing header."`
13 Remove bool `opt:"-d" desc:"Remove the header instead of adding it."`
14 Name string `opt:"name" complete:"CompleteHeaders" desc:"Header name."`
15 Value string `opt:"..." required:"false"`
16 }
17
18 var headers = []string{
19 "From",
20 "To",
21 "Cc",
22 "Bcc",
23 "Subject",
24 "Comments",
25 "Keywords",
26 }
27
28 func init() {
29 commands.Register(Header{})
30 }
31
32 func (Header) Description() string {
33 return "Add or remove the specified email header."
34 }
35
36 func (Header) Context() commands.CommandContext {
37 return commands.COMPOSE_EDIT | commands.COMPOSE_REVIEW
38 }
39
40 func (Header) Aliases() []string {
41 return []string{"header"}
42 }
43
44 func (Header) Options() string {
45 return "fd"
46 }
47
48 func (*Header) CompleteHeaders(arg string) []string {
49 return commands.FilterList(headers, arg, nil)
50 }
51
52 func (h Header) Execute(args []string) error {
53 composer, _ := app.SelectedTabContent().(*app.Composer)
54
55 name := strings.TrimRight(h.Name, ":")
56
57 if h.Remove {
58 return composer.DelEditor(name)
59 }
60
61 if !h.Force {
62 headers, err := composer.PrepareHeader()
63 if err != nil {
64 return err
65 }
66 if headers.Get(name) != "" && h.Value != "" {
67 return fmt.Errorf(
68 "Header %s is already set to %q (use -f to overwrite)",
69 name, headers.Get(name))
70 }
71 }
72
73 return composer.AddEditor(name, h.Value, false)
74 }