diff --git a/README.md b/README.md index 5fda0ba..101071f 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,19 @@ running `kks edit` will do the following: - if a session for the directory doesn't exist, `kks` will start a new session and connect to it. +### Default session + +``` +export KKS_DEFAULT_SESSION='mysession' +``` + +When context is not set (`KKS_SESSION` is empty), running `kks edit` will check +for a session defined by `KKS_DEFAULT_SESSION` variable. If the session is +running, `kks` will connect to it instead of starting a new session. + +`kks` will not start the default session if it's not running. You can use the +autostarting mechanism of your desktop to start it with `kak -d -s mysession`. + ## Provided scripts | script | function | diff --git a/cmd/edit.go b/cmd/edit.go index ee53b44..8acc807 100644 --- a/cmd/edit.go +++ b/cmd/edit.go @@ -33,42 +33,36 @@ func (c *EditCmd) Run() error { return err } - _, useGitDirSessions := os.LookupEnv("KKS_USE_GITDIR_SESSIONS") - - gitdirSess := struct { - name string - exists bool - }{"", false} + switch c.session { + case "": + var gitDirName string + _, useGitDirSessions := os.LookupEnv("KKS_USE_GITDIR_SESSIONS") - if useGitDirSessions { - gitOut, err := exec.Command("git", "rev-parse", "--show-toplevel").Output() - if err == nil { - gitdirSess.name = strings.TrimSpace(strings.ReplaceAll(path.Base(string(gitOut)), ".", "-")) - sessions, _ := kak.List() - for _, s := range sessions { - if s.Name == gitdirSess.name { - gitdirSess.exists = true - } - } + if useGitDirSessions { + gitDirName = parseGitToplevel() } - } - switch c.session { - case "": - if gitdirSess.name != "" { - if !gitdirSess.exists { - sessionName, err := kak.Create(gitdirSess.name) + if gitDirName != "" { + if !sessionExists(gitDirName) { + sessionName, err := kak.Create(gitDirName) if err != nil { return err } fmt.Println("git-dir session started:", sessionName) } - if err := kak.Connect(fp.Name, fp.Line, fp.Column, gitdirSess.name); err != nil { + if err := kak.Connect(fp.Name, fp.Line, fp.Column, gitDirName); err != nil { return err } } else { - if err := kak.Run(fp.Name, fp.Line, fp.Column); err != nil { - return err + defaultSession := os.Getenv("KKS_DEFAULT_SESSION") + if defaultSession != "" && sessionExists(defaultSession) { + if err := kak.Connect(fp.Name, fp.Line, fp.Column, defaultSession); err != nil { + return err + } + } else { + if err := kak.Run(fp.Name, fp.Line, fp.Column); err != nil { + return err + } } } default: @@ -97,3 +91,21 @@ func (c *EditCmd) Run() error { return nil } + +func parseGitToplevel() string { + gitOut, err := exec.Command("git", "rev-parse", "--show-toplevel").Output() + if err != nil { + return "" + } + return strings.TrimSpace(strings.ReplaceAll(path.Base(string(gitOut)), ".", "-")) +} + +func sessionExists(name string) bool { + sessions, _ := kak.List() + for _, s := range sessions { + if s.Name == name { + return true + } + } + return false +}