Skip to content

Commit

Permalink
initial stub for auto-sessions based on git directory
Browse files Browse the repository at this point in the history
  • Loading branch information
kkga committed Sep 13, 2021
1 parent 945110a commit 7641d40
Showing 1 changed file with 51 additions and 21 deletions.
72 changes: 51 additions & 21 deletions cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package cmd
import (
"flag"
"fmt"
"os/exec"
"path"
"strings"

// "strings"

"github.com/kkga/kks/kak"
)

Expand Down Expand Up @@ -32,32 +36,58 @@ func (c *EditCmd) Run() error {

switch c.session {
case "":
// if no session, just run kak
if err := kak.Run(fp.Name, fp.Line, fp.Column); err != nil {
return err
}
default:
switch c.client {
case "":
// if no client, attach to session with new client
if err := kak.Connect(fp.Name, fp.Line, fp.Column, c.session); err != nil {
return err
}
default:
// if client set, send 'edit [file]' to client
sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("edit -existing %s", fp.Name))
if fp.Line != 0 {
sb.WriteString(fmt.Sprintf(" %d", fp.Line))
gitdir, err := exec.Command("git", "rev-parse", "--show-toplevel").CombinedOutput()
// fmt.Println(path.Base(string(gitdir)))
if err == nil {
sessions, _ := kak.List()
gitdir_session := strings.ReplaceAll(path.Base(string(gitdir)), ".", "")
gitdir_session = strings.TrimSpace(gitdir_session)
for _, s := range sessions {
if s.Name == gitdir_session {
fmt.Println("Connect:", s.Name)
if err := kak.Connect(fp.Name, fp.Line, fp.Column, c.session); err != nil {
return err
}
return nil
}
}
if fp.Column != 0 {
sb.WriteString(fmt.Sprintf(" %d", fp.Column))
fmt.Println("Create:", gitdir_session)
sessionName, err := kak.Create(gitdir_session)
if err != nil {
return err
}

if err := kak.Send(sb.String(), "", c.session, c.client); err != nil {
fmt.Println("session started:", sessionName)
if err := kak.Connect(fp.Name, fp.Line, fp.Column, sessionName); err != nil {
return err
}
}

// if no session, just run kak
// if err := kak.Run(fp.Name, fp.Line, fp.Column); err != nil {
// return err
// }
// default:
// switch c.client {
// case "":
// // if no client, attach to session with new client
// if err := kak.Connect(fp.Name, fp.Line, fp.Column, c.session); err != nil {
// return err
// }
// default:
// // if client set, send 'edit [file]' to client
// sb := strings.Builder{}
// sb.WriteString(fmt.Sprintf("edit -existing %s", fp.Name))
// if fp.Line != 0 {
// sb.WriteString(fmt.Sprintf(" %d", fp.Line))
// }
// if fp.Column != 0 {
// sb.WriteString(fmt.Sprintf(" %d", fp.Column))
// }

// if err := kak.Send(sb.String(), "", c.session, c.client); err != nil {
// return err
// }
// }
}

return nil
Expand Down

0 comments on commit 7641d40

Please sign in to comment.