aboutsummaryrefslogtreecommitdiffstats
path: root/commands/compose/header.go
blob: 3283d4e3f73df76f8a66c7675ebe4ec6ab8d0164 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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" complete:"CompleteHeaders"`
	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) CompleteHeaders(arg string) []string {
	return commands.FilterList(headers, arg, commands.QuoteSpace)
}

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)
}