Skip to content

Commit

Permalink
Merge pull request #1 from kkga/default-session
Browse files Browse the repository at this point in the history
Default session
  • Loading branch information
kkga authored Sep 14, 2021
2 parents 687387f + 45b98a9 commit eb12d46
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 25 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
62 changes: 37 additions & 25 deletions cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
}

0 comments on commit eb12d46

Please sign in to comment.