From 7f70f101bc02d9b7b0ed5afc464d0fb7b1e6fa50 Mon Sep 17 00:00:00 2001 From: Duc Nguyen Date: Fri, 5 Nov 2021 15:24:27 +0100 Subject: Also write the last buffer on SIGTERM, SIGINT and SIGHUP --- cmd/senpai/main.go | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/cmd/senpai/main.go b/cmd/senpai/main.go index dc2493e..46ad1bb 100644 --- a/cmd/senpai/main.go +++ b/cmd/senpai/main.go @@ -6,8 +6,10 @@ import ( "io/ioutil" "math/rand" "os" + "os/signal" "path" "strings" + "syscall" "time" "git.sr.ht/~taiite/senpai" @@ -51,16 +53,17 @@ func main() { lastNetID, lastBuffer := getLastBuffer() app.SwitchToBuffer(lastNetID, lastBuffer) + sigCh := make(chan os.Signal, 1) + signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) + + go func() { + <-sigCh + app.Close() + }() + app.Run() app.Close() - - // Write last buffer on close - lastBufferPath := getLastBufferPath() - lastNetID, lastBuffer = app.CurrentBuffer() - err = os.WriteFile(lastBufferPath, []byte(fmt.Sprintf("%s %s", lastNetID, lastBuffer)), 0666) - if err != nil { - fmt.Fprintf(os.Stderr, "failed to write last buffer at %q: %s\n", lastBufferPath, err) - } + writeLastBuffer(app) } func getLastBufferPath() string { @@ -91,3 +94,12 @@ func getLastBuffer() (netID, buffer string) { return fields[0], fields[1] } + +func writeLastBuffer(app *senpai.App) { + lastBufferPath := getLastBufferPath() + lastNetID, lastBuffer := app.CurrentBuffer() + err := os.WriteFile(lastBufferPath, []byte(fmt.Sprintf("%s %s", lastNetID, lastBuffer)), 0666) + if err != nil { + fmt.Fprintf(os.Stderr, "failed to write last buffer at %q: %s\n", lastBufferPath, err) + } +} -- cgit v1.2.3