From 0595acb675ea2819cba07fbeafecceeb31b0cd96 Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Tue, 7 Sep 2021 18:17:52 +0300 Subject: [PATCH] upd --- cmd/get.go | 43 ++++++++++++++--------------- cmd/list.go | 36 +++++++++++++++++++++++- cmd/send.go | 5 ++-- main.go | 79 +++++++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 129 insertions(+), 34 deletions(-) diff --git a/cmd/get.go b/cmd/get.go index 795e701..26ffccd 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -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 } diff --git a/cmd/list.go b/cmd/list.go index eed7b12..21af62f 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -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) } diff --git a/cmd/send.go b/cmd/send.go index 37adc73..dffe2be 100644 --- a/cmd/send.go +++ b/cmd/send.go @@ -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 @@ -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 } diff --git a/main.go b/main.go index 5ffdb47..ead5344 100644 --- a/main.go +++ b/main.go @@ -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") @@ -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": @@ -81,7 +87,6 @@ func main() { if err != nil { log.Fatal(err) } - cmd.Edit(filename, context.session, context.client) } @@ -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")) } }