diff options
author | Robin Jarry <robin@jarry.cc> | 2023-02-01 23:35:42 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-02-20 14:48:42 +0100 |
commit | aaaa0c184fb43879cc84551983309cad06d665ee (patch) | |
tree | 93bb83d32495f679a70dbe5d3913549c29edf675 /config/triggers.go | |
parent | 420a82a356d53e4b600ba54768f7ed21a43cf85e (diff) | |
download | aerc-aaaa0c184fb43879cc84551983309cad06d665ee.tar.gz |
triggers: use templates instead of % mini language
Since previous commit, all commands now support expanding text/template
markup. Reuse that for the new-email trigger command.
Update commands.ExecuteCommand to take optional *AccountConfig and
*MessageInfo arguments. If these are nil, fallback to using the
currently selected account and message (if any).
Pass the proper *AccountConfig and *MessageInfo objects when firing the
trigger command so that these are used instead of the currently selected
ones.
If new-email contains % placeholders, try to convert them to template
markup reusing the same conversion added in commit 535300cfdbfc
("config: add columns based index format"). Warn the user that they need
to update their configuration file.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Reviewed-by: Moritz Poldrack <moritz@poldrack.dev>
Diffstat (limited to 'config/triggers.go')
-rw-r--r-- | config/triggers.go | 94 |
1 files changed, 37 insertions, 57 deletions
diff --git a/config/triggers.go b/config/triggers.go index 906928ae..c0e70d40 100644 --- a/config/triggers.go +++ b/config/triggers.go @@ -1,82 +1,62 @@ package config import ( - "errors" - "fmt" - "github.com/go-ini/ini" "github.com/google/shlex" "git.sr.ht/~rjarry/aerc/lib/format" "git.sr.ht/~rjarry/aerc/log" - "git.sr.ht/~rjarry/aerc/models" ) type TriggersConfig struct { - NewEmail string `ini:"new-email"` - ExecuteCommand func(command []string) error + NewEmail []string `ini:"-"` } var Triggers = &TriggersConfig{} func parseTriggers(file *ini.File) error { + var cmd string triggers, err := file.GetSection("triggers") if err != nil { goto out } - if err := triggers.MapTo(&Triggers); err != nil { - return err - } -out: - log.Debugf("aerc.conf: [triggers] %#v", Triggers) - return nil -} - -func (trig *TriggersConfig) ExecTrigger(triggerCmd string, - triggerFmt func(string) (string, error), -) error { - if len(triggerCmd) == 0 { - return errors.New("Trigger command empty") - } - triggerCmdParts, err := shlex.Split(triggerCmd) - if err != nil { - return err - } - - var command []string - for _, part := range triggerCmdParts { - formattedPart, err := triggerFmt(part) + if key := triggers.Key("new-email"); key != nil { + cmd = indexFmtRegexp.ReplaceAllStringFunc( + key.String(), + func(s string) string { + runes := []rune(s) + t, _ := indexVerbToTemplate(runes[len(runes)-1]) + return t + }, + ) + Triggers.NewEmail, err = shlex.Split(cmd) if err != nil { return err } - command = append(command, formattedPart) - } - return trig.ExecuteCommand(command) -} - -func (trig *TriggersConfig) ExecNewEmail( - account *AccountConfig, msg *models.MessageInfo, -) { - err := trig.ExecTrigger(trig.NewEmail, - func(part string) (string, error) { - formatstr, args, err := format.ParseMessageFormat( - part, Ui.TimestampFormat, - Ui.ThisDayTimeFormat, - Ui.ThisWeekTimeFormat, - Ui.ThisYearTimeFormat, - Ui.IconAttachment, - format.Ctx{ - FromAddress: format.AddressForHumans(account.From), - AccountName: account.Name, - MsgInfo: msg, - }, - ) - if err != nil { - return "", err - } - return fmt.Sprintf(formatstr, args...), nil - }) - if err != nil { - log.Errorf("failed to run new-email trigger: %v", err) + if cmd != key.String() { + log.Warnf("%s %s", + "The new-email trigger now uses templates instead of %-based placeholders.", + "Backward compatibility will be removed in aerc 0.17.") + Warnings = append(Warnings, Warning{ + Title: "FORMAT CHANGED: [triggers].new-email", + Body: ` +The new-email trigger now uses templates instead of %-based placeholders. + +Your configuration in this instance was automatically converted to: + +[triggers] +new-email = ` + format.ShellQuote(Triggers.NewEmail) + ` + +Your configuration file was not changed. To make this change permanent and to +dismiss this warning on launch, replace the above line into aerc.conf. See +aerc-config(5) for more details. + +The automatic conversion of new-email will be removed in aerc 0.17. +`, + }) + } } +out: + log.Debugf("aerc.conf: [triggers] %#v", Triggers) + return nil } |