From 086d7e796bc4eb7ab6cf389cdba208155dc0613f Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Wed, 15 Sep 2021 15:46:45 +0300 Subject: [PATCH] ensure session exists when starting new kak sessions --- kak/start.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/kak/start.go b/kak/start.go index cbbaaff..cd92f63 100644 --- a/kak/start.go +++ b/kak/start.go @@ -5,6 +5,7 @@ import ( "math/rand" "os/exec" "strings" + "time" ) func Start(name string) (sessionName string, err error) { @@ -24,9 +25,34 @@ func Start(name string) (sessionName string, err error) { return "", err } + // Ensure session exists before returning + ch := make(chan bool) + go waitForSession(ch, sessionName) + + _ = <-ch + return } +func waitForSession(ch chan bool, name string) error { +out: + for { + sessions, err := Sessions() + if err != nil { + return err + } + for _, s := range sessions { + if s.Name == name { + ch <- true + break out + + } + } + time.Sleep(time.Millisecond * 10) + } + return nil +} + func uniqName() (name string, err error) { s, err := exec.Command("kak", "-l").Output() if err != nil {