From b46b2d22824a4feb351f27fff8dce3d6947b190b Mon Sep 17 00:00:00 2001 From: Moritz Poldrack Date: Wed, 15 Mar 2023 22:40:39 +0100 Subject: 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 Signed-off-by: Robin Jarry Tested-by: Bence Ferdinandy --- CHANGELOG.md | 2 +- config/aerc.conf | 4 ++++ config/hooks.go | 1 + doc/aerc-config.5.scd | 8 ++++++++ lib/hooks/aerc-shutdown.go | 22 ++++++++++++++++++++++ main.go | 9 +++++++++ 6 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 lib/hooks/aerc-shutdown.go 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* = __ + 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()), + } +} diff --git a/main.go b/main.go index 9b76d0cc..99557d2b 100644 --- a/main.go +++ b/main.go @@ -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: -- cgit