summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorDuc Nguyen <nguyen@emersion.fr>2021-09-24 16:57:12 +0200
committerHubert Hirtz <51088794+hhirtz@users.noreply.github.com>2021-09-28 16:34:11 +0000
commit0bc7b2394ae57418fa48ec2222e900a51c913ed8 (patch)
tree9f8ecf09b4398b8e90cf9c679f2386894c9f0e8b /cmd
parentFix history not being loaded on channel re-JOIN (diff)
Restore last buffer on start
review emersion review taiite review taiite Use os.WriteFile instead of os.Create
Diffstat (limited to 'cmd')
-rw-r--r--cmd/senpai/main.go39
1 files changed, 37 insertions, 2 deletions
diff --git a/cmd/senpai/main.go b/cmd/senpai/main.go
index 8d56cef..e41d8db 100644
--- a/cmd/senpai/main.go
+++ b/cmd/senpai/main.go
@@ -3,9 +3,11 @@ package main
import (
"flag"
"fmt"
+ "io/ioutil"
"math/rand"
"os"
"path"
+ "strings"
"time"
"git.sr.ht/~taiite/senpai"
@@ -41,11 +43,44 @@ func main() {
cfg.Debug = cfg.Debug || debug
- app, err := senpai.NewApp(cfg)
+ lastBuffer := getLastBuffer()
+
+ app, err := senpai.NewApp(cfg, lastBuffer)
if err != nil {
panic(err)
}
- defer app.Close()
app.Run()
+ app.Close()
+
+ // Write last buffer on close
+ lastBufferPath := getLastBufferPath()
+ err = os.WriteFile(lastBufferPath, []byte(app.CurrentBuffer()), 0666)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "failed to write last buffer at %q: %s\n", lastBufferPath, err)
+ }
+}
+
+func getLastBufferPath() string {
+ cacheDir, err := os.UserCacheDir()
+ if err != nil {
+ panic(err)
+ }
+ cachePath := path.Join(cacheDir, "senpai")
+ err = os.MkdirAll(cachePath, 0755)
+ if err != nil {
+ panic(err)
+ }
+
+ lastBufferPath := path.Join(cachePath, "lastbuffer.txt")
+ return lastBufferPath
+}
+
+func getLastBuffer() string {
+ buf, err := ioutil.ReadFile(getLastBufferPath())
+ if err != nil {
+ return ""
+ }
+
+ return strings.TrimSpace(string(buf))
}