From dc4c36adbfbffd34319ddc007bad437ef802ee72 Mon Sep 17 00:00:00 2001 From: Jeffas Date: Sun, 21 Jul 2019 21:01:51 +0100 Subject: Add new-email trigger This patch sets up the trigger config section of aerc.conf. Each trigger has its own function which is called from the place where it is triggered. Currently only the new-email trigger is implemented. The triggers make use of format strings. For instance, in the new-email trigger this allows the user to select the trigger command and also the information extracted from the command and placed into their command. To actually execute the trigger commands the keypresses are simulated. Further triggers can be implemented in the future. Formatting of the command is moved to a new package. --- config/triggers.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 config/triggers.go (limited to 'config/triggers.go') diff --git a/config/triggers.go b/config/triggers.go new file mode 100644 index 0000000..d31f267 --- /dev/null +++ b/config/triggers.go @@ -0,0 +1,49 @@ +package config + +import ( + "errors" + "fmt" + + "github.com/google/shlex" + + "git.sr.ht/~sircmpwn/aerc/lib/format" + "git.sr.ht/~sircmpwn/aerc/models" +) + +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 err != nil { + return err + } + command = append(command, formattedPart) + } + return trig.ExecuteCommand(command) +} + +func (trig *TriggersConfig) ExecNewEmail(account *AccountConfig, + conf *AercConfig, msg *models.MessageInfo) { + err := trig.ExecTrigger(trig.NewEmail, + func(part string) (string, error) { + formatstr, args, err := format.ParseMessageFormat(part, + conf.Ui.TimestampFormat, account.Name, 0, msg) + if err != nil { + return "", err + } + return fmt.Sprintf(formatstr, args...), nil + }) + if err != nil { + fmt.Printf("Error from the new-email trigger: %s\n", err) + } +} -- cgit v1.2.3