diff options
author | Duc Nguyen <nguyen@emersion.fr> | 2021-11-05 15:24:27 +0100 |
---|---|---|
committer | Hubert Hirtz <hubert@hirtz.pm> | 2021-11-19 11:13:55 +0100 |
commit | 7f70f101bc02d9b7b0ed5afc464d0fb7b1e6fa50 (patch) | |
tree | 6875a8be9d9642b260d18ec62c80e7992fc0e483 /cmd | |
parent | Fix link value, removing debug query string (diff) |
Also write the last buffer on SIGTERM, SIGINT and SIGHUP
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/senpai/main.go | 28 |
1 files 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) + } +} |