~taiite/public-inbox

[PATCH senpai] Implement saving last open buffers and reopening on next startup

Details
Message ID
<f602464387da3cd94bf910ccf5e89690@riseup.net>
DKIM signature
missing
Download raw message
NOTICE: This builds on top of
https://lists.sr.ht/~taiite/public-inbox/%3C6d1ece52fe13c76727af16f1bcdf804e%40riseup.net%3E

---
 app.go             | 16 +++++++++++++++-
 cmd/senpai/main.go | 32 ++++++++++++++++++++++++++++++++
 ui/ui.go           |  9 +++++++++
 3 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/app.go b/app.go
index 6dc0da1..057e9ab 100644
--- a/app.go
+++ b/app.go
@@ -196,6 +196,10 @@ func (app *App) CurrentBuffer() (netID, buffer
string) {
        return app.win.CurrentBuffer()
 }
 
+func (app *App) Buffers() []string {
+       return app.win.Buffers()
+}
+
 func (app *App) LastMessageTime() time.Time {
        return app.lastMessageTime
 }
@@ -742,7 +746,17 @@ func (app *App) handleIRCEvent(netID string, ev
interface{}) {
                for _, channel := range app.cfg.Channels {
                        // TODO: group JOIN messages
                        // TODO: support autojoining channels with keys
-                       s.Join(channel, "")
+                       if channel[0] == '#' {
+                               s.Join(channel, "")
+                       } else {
+                               i, added := app.win.AddBuffer(netID, "",
channel)
+                               app.win.JumpBufferIndex(i)
+                               if added {
+                                       s.MonitorAdd(channel)
+                                       s.ReadGet(channel)
+                                      
s.NewHistoryRequest(channel).WithLimit(200).Before(time.Now())
+                               }
+                       }
                }
                s.NewHistoryRequest("").
                        WithLimit(1000).
diff --git a/cmd/senpai/main.go b/cmd/senpai/main.go
index b84c7c5..c11a461 100644
--- a/cmd/senpai/main.go
+++ b/cmd/senpai/main.go
@@ -45,6 +45,8 @@ func main() {
        }
 
        cfg.Debug = cfg.Debug || debug
+       buffers := getBuffers(configPathHash)
+       cfg.Channels = append(cfg.Channels, buffers...)
 
        app, err := senpai.NewApp(cfg)
        if err != nil {
@@ -64,9 +66,12 @@ func main() {
        }()
 
        app.Run()
+
        app.Close()
+
        writeLastBuffer(configPathHash, app)
        writeLastStamp(configPathHash, app)
+       writeBuffers(configPathHash, app)
 }
 
 func configPathHash(configPath string) string {
@@ -146,3 +151,30 @@ func writeLastStamp(configPathHash string, app
*senpai.App) {
                fmt.Fprintf(os.Stderr, "failed to write last stamp at
%q: %s\n", lastStampPath, err)
        }
 }
+
+func buffersPath(configPathHash string) string {
+       var filename = fmt.Sprintf("buffers-%s.txt", configPathHash)
+       return path.Join(cachePath(), filename)
+}
+
+func getBuffers(configPathHash string) []string {
+       buf, err := ioutil.ReadFile(buffersPath(configPathHash))
+       if err != nil {
+               return []string{}
+       }
+
+       buffers := strings.Split(strings.TrimSpace(string(buf)), " ")
+       return buffers
+}
+
+func writeBuffers(configPathHash string, app *senpai.App) {
+       buffersPath := buffersPath(configPathHash)
+       buffers := app.Buffers()
+
+       var buffersString = strings.Join(buffers, " ")
+
+       err := os.WriteFile(buffersPath, []byte(buffersString), 0666)
+       if err != nil {
+               fmt.Fprintf(os.Stderr, "failed to write buffers at %q:
%s\n", buffersPath, err)
+       }
+}
diff --git a/ui/ui.go b/ui/ui.go
index 859f32a..ce135c4 100644
--- a/ui/ui.go
+++ b/ui/ui.go
@@ -140,6 +140,15 @@ func (ui *UI) GoToBufferNo(i int) {
        }
 }
 
+func (ui *UI) Buffers() []string {
+       var buffers = []string{}
+       for _, b := range ui.bs.list {
+               buffers = append(buffers, b.title)
+
+       }
+       return buffers
+}
+
 func (ui *UI) ShowBufferNumbers(enable bool) {
        ui.bs.ShowBufferNumbers(enable)
 }
-- 
2.39.2
Reply to thread Export thread (mbox)