diff options
author | Moritz Poldrack <git@moritz.sh> | 2023-03-15 22:40:39 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-04-01 01:01:09 +0200 |
commit | b46b2d22824a4feb351f27fff8dce3d6947b190b (patch) | |
tree | 1783a28419b533d0b43aa73073176fa321b7ed0a | |
parent | f10b184eb34634b9642fa83ef0f1cd8d0687a4af (diff) | |
download | aerc-b46b2d22824a4feb351f27fff8dce3d6947b190b.tar.gz |
hooks: add aerc-shutdown
Add a hook to run when aerc shuts down. The environment is supplemented
with the duration aerc was alive for.
References: https://todo.sr.ht/~rjarry/aerc/136
Signed-off-by: Moritz Poldrack <git@moritz.sh>
Signed-off-by: Robin Jarry <robin@jarry.cc>
Tested-by: Bence Ferdinandy <bence@ferdinandy.com>
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | config/aerc.conf | 4 | ||||
-rw-r--r-- | config/hooks.go | 1 | ||||
-rw-r--r-- | doc/aerc-config.5.scd | 8 | ||||
-rw-r--r-- | lib/hooks/aerc-shutdown.go | 22 | ||||
-rw-r--r-- | main.go | 9 |
6 files changed, 45 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ee452df..ffd198b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). `aerc.conf`. - Allow basic shell globbing in `[openers]` MIME types. - Dynamic `msglist_*` styling based on email header values in stylesets. -- Add `mail-received` and `aerc-startup` hooks. +- Add `mail-received`, `aerc-startup`, and `aerc-shutdown` hooks. ### Changed diff --git a/config/aerc.conf b/config/aerc.conf index 110e8459..62d2ba92 100644 --- a/config/aerc.conf +++ b/config/aerc.conf @@ -518,6 +518,10 @@ message/rfc822=colorize # Executed when aerc starts #aerc-startup=aerc :terminal calcurse && aerc :next-tab +# +# Executed when aerc shuts down. +#aerc-shutdown= + [templates] # Templates are used to populate email bodies automatically. # diff --git a/config/hooks.go b/config/hooks.go index 41fc8026..bfdf4fe1 100644 --- a/config/hooks.go +++ b/config/hooks.go @@ -9,6 +9,7 @@ import ( type HooksConfig struct { AercStartup string `ini:"aerc-startup"` + AercShutdown string `ini:"aerc-shutdown"` MailReceived string `ini:"mail-received"` } diff --git a/doc/aerc-config.5.scd b/doc/aerc-config.5.scd index 78267229..4931d021 100644 --- a/doc/aerc-config.5.scd +++ b/doc/aerc-config.5.scd @@ -855,6 +855,14 @@ They are configured in the *[hooks]* section of aerc.conf. *mail-received* = _notify-send "New mail from $AERC_FROM_NAME" "$AERC_SUBJECT"_ +*aerc-shutdown* = _<command>_ + Executed when aerc shuts down. Aerc will wait for the command to finish + before exiting. + + Variables: + + - *AERC_LIFETIME* + # TEMPLATES Template files are used to populate the body of an email. The *:compose*, diff --git a/lib/hooks/aerc-shutdown.go b/lib/hooks/aerc-shutdown.go new file mode 100644 index 00000000..a3f55f73 --- /dev/null +++ b/lib/hooks/aerc-shutdown.go @@ -0,0 +1,22 @@ +package hooks + +import ( + "fmt" + "time" + + "git.sr.ht/~rjarry/aerc/config" +) + +type AercShutdown struct { + Lifetime time.Duration +} + +func (a *AercShutdown) Cmd() string { + return config.Hooks.AercShutdown +} + +func (a *AercShutdown) Env() []string { + return []string{ + fmt.Sprintf("AERC_LIFETIME=%s", a.Lifetime.String()), + } +} @@ -9,6 +9,7 @@ import ( "runtime" "sort" "strings" + "time" "git.sr.ht/~sircmpwn/getopt" "github.com/gdamore/tcell/v2" @@ -251,6 +252,14 @@ func main() { aerc.PushError(msg) } }() + defer func(start time.Time) { + err := hooks.RunHook( + &hooks.AercShutdown{Lifetime: time.Since(start)}, + ) + if err != nil { + log.Errorf("aerc-shutdown hook: %s", err) + } + }(time.Now()) for event := range libui.MsgChannel { switch event := event.(type) { case tcell.Event: |