Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default session #1

Merged
merged 5 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}