~rjarry/aerc

aerc/commands/compose/header.go -rw-r--r-- 1.3 KiB
8e53d330Tim Culverhouse terminal: remove invalidate method 3 days ago
                                                                                
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
67
68
69
70
71
72
73
74
75
76
77
78
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
}