Skip to content

Commit

Permalink
parse attach command
Browse files Browse the repository at this point in the history
  • Loading branch information
kkga committed Sep 7, 2021
1 parent 7e7e7a4 commit 1d43a40
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
22 changes: 11 additions & 11 deletions cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,46 @@ package cmd

import (
"fmt"
"log"
"os"
"os/exec"
"strings"
"syscall"
)

func Edit(filename, session, client string) {
fmt.Println(session, client)
if session != "" && client != "" {
func Edit(filename, session, client string) error {
if filename != "" && session != "" && client != "" && client != "-" {
kakCommand := fmt.Sprintf("edit %s", filename)
Send(kakCommand, session, client)
os.Exit(0)
}

kakBinary, err := exec.LookPath("kak")
if err != nil {
log.Fatal(err)
return err
}

kakExecArgs := []string{"kak"}

sessions, err := exec.Command("kak", "-l").Output()
if err != nil {
log.Fatal(err)
return err
}

kakExecArgs := []string{"kak"}
if session != "" && strings.Contains(string(sessions), session) {
kakExecArgs = append(kakExecArgs, "-c", session)
} else if session != "" {
// TODO: this gets killed if parent shell closes, use setsid?
kakExecArgs = append(kakExecArgs, "-s", session)
}

kakExecArgs = append(kakExecArgs, filename)
if filename != "" {
kakExecArgs = append(kakExecArgs, filename)
}

fmt.Print(kakExecArgs)
fmt.Println("edit", kakExecArgs)

execErr := syscall.Exec(kakBinary, kakExecArgs, os.Environ())
if execErr != nil {
log.Fatal(err)
return err
}
return nil
}
6 changes: 5 additions & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ func List() {

for _, session := range sessions {
for _, client := range session.clients {
fmt.Printf("%s\t%s\t%s\n", session.name, client, session.dir)
if client != "" {
fmt.Printf("%s\t%s\t%s\n", session.name, client, session.dir)
} else {
fmt.Printf("%s\t%s\t%s\n", session.name, "-", session.dir)
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ func main() {
cmd.Edit(filename, context.session, context.client)
}

if attachCmd.Parsed() {
context, err := getContext()
if err != nil {
log.Fatal(err)
}
if err := cmd.Edit("", context.session, context.client); err != nil {
log.Fatal(err)
}
}

if sendCmd.Parsed() {
args := sendCmd.Args()
kakCommand := strings.Join(args, " ")
Expand Down

0 comments on commit 1d43a40

Please sign in to comment.