Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
kkga committed Sep 7, 2021
1 parent 6757077 commit 0595acb
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 34 deletions.
43 changes: 20 additions & 23 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,36 @@ package cmd

import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
)

func check(e error) {
if e != nil {
log.Fatal(e)
func Get(getStr, session, client string) ([]string, error) {
f, err := os.CreateTemp("", "kks-tmp")
if err != nil {
return nil, err
}
}

func Get(getStr, session, client string) {
tmpfile, err := ioutil.TempFile("", "kaks-tmp")
check(err)

defer os.Remove(tmpfile.Name())
defer os.Remove(f.Name())
defer f.Close()

Send(fmt.Sprintf("echo -quoting shell -to-file %s %%{ %s }", tmpfile.Name(), getStr), session, client)
sendCmd := fmt.Sprintf("echo -quoting shell -to-file %s %%{ %s }", f.Name(), getStr)

out, err := os.ReadFile(tmpfile.Name())
fmt.Println(string(out))
check(err)

buffers := strings.Split(string(out), " ")
for i, val := range buffers {
buffers[i] = strings.Trim(val, "''")
if err := Send(sendCmd, session, client); err != nil {
return nil, err
}
// TODO: need to wait for Send to finish
time.Sleep(10 * time.Millisecond)

fmt.Print(strings.Join(buffers, "\n"))
out, err := os.ReadFile(f.Name())
if err != nil {
return nil, err
}

if err := tmpfile.Close(); err != nil {
log.Fatal(err)
outStrs := strings.Split(string(out), " ")
for i, val := range outStrs {
outStrs[i] = strings.Trim(val, "''")
}

return outStrs, nil
}
36 changes: 35 additions & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,50 @@
package cmd

import (
// "encoding/json"
"fmt"
"log"
"os/exec"
"strings"
)

type KakSession struct {
name string
clients []string
dir string
}

func List() {
out, err := exec.Command("kak", "-l").Output()
if err != nil {
log.Fatal(err)
}
kakSessions := strings.Split(strings.TrimSpace(string(out)), "\n")

sessions := make([]KakSession, 0)

for _, session := range kakSessions {
s := KakSession{name: session}

clients, err := Get("%val{client_list}", session, "")
if err != nil {
log.Fatal(err)
}
s.clients = clients

dir, err := Get("%sh{pwd}", session, "")
if err != nil {
log.Fatal(err)
}
s.dir = strings.Join(dir, "")

sessions = append(sessions, s)
}

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

fmt.Printf("%s", out)
}
5 changes: 3 additions & 2 deletions cmd/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
)

func Send(kakCommand, session, client string) {
func Send(kakCommand, session, client string) error {
cmd := exec.Command("kak", "-p", session)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand All @@ -23,6 +23,7 @@ func Send(kakCommand, session, client string) {

err := cmd.Run()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return err
}
return nil
}
79 changes: 71 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ func main() {
editCmd := flag.NewFlagSet("edit", flag.ExitOnError)
sendCmd := flag.NewFlagSet("send", flag.ExitOnError)
attachCmd := flag.NewFlagSet("attach", flag.ExitOnError)
getCmd := flag.NewFlagSet("get", flag.ExitOnError)
getValCmd := flag.NewFlagSet("get-val", flag.ExitOnError)
getOptCmd := flag.NewFlagSet("get-opt", flag.ExitOnError)
getRegCmd := flag.NewFlagSet("get-opt", flag.ExitOnError)
getShCmd := flag.NewFlagSet("get-sh", flag.ExitOnError)
// killCmd := flag.NewFlagSet("kill", flag.ExitOnError)

sessionCmds := []*flag.FlagSet{editCmd, sendCmd, getValCmd, getOptCmd, getRegCmd}
sessionCmds := []*flag.FlagSet{editCmd, sendCmd, getCmd, getValCmd, getOptCmd, getRegCmd, getShCmd}
for _, cmd := range sessionCmds {
cmd.StringVar(&session, "s", "", "Kakoune session")
cmd.StringVar(&client, "c", "", "Kakoune client")
Expand All @@ -49,12 +51,16 @@ func main() {
sendCmd.Parse(os.Args[2:])
case "attach", "a":
attachCmd.Parse(os.Args[2:])
case "get-val", "gv":
case "get":
getCmd.Parse(os.Args[2:])
case "get-val":
getValCmd.Parse(os.Args[2:])
case "get-opt", "go":
getOptCmd.Parse(os.Args[2:])
case "get-reg", "gr":
case "get-opt":
getOptCmd.Parse(os.Args[2:])
case "get-reg":
getRegCmd.Parse(os.Args[2:])
case "get-sh":
getShCmd.Parse(os.Args[2:])
case "list", "l", "ls":
cmd.List()
case "env":
Expand All @@ -81,7 +87,6 @@ func main() {
if err != nil {
log.Fatal(err)
}

cmd.Edit(filename, context.session, context.client)
}

Expand All @@ -96,17 +101,75 @@ func main() {
cmd.Send(kakCommand, context.session, context.client)
}

if getCmd.Parsed() {
arg := getCmd.Arg(0)
// kakQuery := fmt.Sprintf("%%val{%s}", arg)

context, err := getContext()
if err != nil {
log.Fatal(err)
}

out, err := cmd.Get(arg, context.session, context.client)
if err != nil {
log.Fatal(err)
}

fmt.Println(strings.Join(out, "\n"))
}

if getValCmd.Parsed() {
arg := getValCmd.Arg(0)
kakVal := fmt.Sprintf("%%val{%s}", arg)
// fmt.Println(kakVal)

context, err := getContext()
if err != nil {
log.Fatal(err)
}

cmd.Get(kakVal, context.session, context.client)
out, err := cmd.Get(kakVal, context.session, context.client)
if err != nil {
log.Fatal(err)
}

fmt.Println(strings.Join(out, "\n"))
}

if getOptCmd.Parsed() {
arg := getOptCmd.Arg(0)
kakOpt := fmt.Sprintf("%%opt{%s}", arg)

context, err := getContext()
if err != nil {
log.Fatal(err)
}
cmd.Get(kakOpt, context.session, context.client)
}

if getRegCmd.Parsed() {
arg := getRegCmd.Arg(0)
kakReg := fmt.Sprintf("%%reg{%s}", arg)

context, err := getContext()
if err != nil {
log.Fatal(err)
}
cmd.Get(kakReg, context.session, context.client)
}

if getShCmd.Parsed() {
args := getShCmd.Args()
kakSh := fmt.Sprintf("%%sh{%s}", strings.Join(args, " "))

context, err := getContext()
if err != nil {
log.Fatal(err)
}
out, err := cmd.Get(kakSh, context.session, context.client)
if err != nil {
log.Fatal(err)
}
fmt.Println(strings.Join(out, "\n"))
}
}

Expand Down

0 comments on commit 0595acb

Please sign in to comment.