From 5b523880b4b4cd2abd9457b4b09c384af33be14b Mon Sep 17 00:00:00 2001 From: Daniel Bridges Date: Wed, 7 Aug 2019 23:15:59 -0700 Subject: Let user edit arbitrary headers in composer --- commands/account/compose.go | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'commands/account') diff --git a/commands/account/compose.go b/commands/account/compose.go index f615c0b..fa673f0 100644 --- a/commands/account/compose.go +++ b/commands/account/compose.go @@ -1,9 +1,11 @@ package account import ( - "errors" + "regexp" + "strings" "git.sr.ht/~sircmpwn/aerc/widgets" + "git.sr.ht/~sircmpwn/getopt" ) type Compose struct{} @@ -20,10 +22,11 @@ func (_ Compose) Complete(aerc *widgets.Aerc, args []string) []string { return nil } -// TODO: Accept arguments for default headers, message body +// TODO: Accept arguments for message body func (_ Compose) Execute(aerc *widgets.Aerc, args []string) error { - if len(args) != 1 { - return errors.New("Usage: compose") + body, err := buildBody(args) + if err != nil { + return err } acct := aerc.SelectedAccount() composer := widgets.NewComposer( @@ -37,5 +40,30 @@ func (_ Compose) Execute(aerc *widgets.Aerc, args []string) error { } tab.Content.Invalidate() }) + go composer.SetContents(strings.NewReader(body)) return nil } + +func buildBody(args []string) (string, error) { + var body, headers string + opts, _, err := getopt.Getopts(args, "H:") + if err != nil { + return "", err + } + for _, opt := range opts { + switch opt.Option { + case 'H': + if strings.Index(opt.Value, ":") != -1 { + // ensure first colon is followed by a single space + re := regexp.MustCompile(`^(.*?):\s*(.*)`) + headers += re.ReplaceAllString(opt.Value, "$1: $2") + "\n" + } else { + headers += opt.Value + ":\n" + } + } + } + if headers != "" { + body = headers + "\n\n" + } + return body, nil +} -- cgit v1.2.3