From da4f0c3f2c3eb9b3f705131958177b4eeab986a2 Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Wed, 15 Sep 2021 02:01:36 +0300 Subject: [PATCH 01/14] wip: use types for session, client, buffer --- cmd/attach.go | 4 ++- cmd/cat.go | 7 ++++- cmd/cmd.go | 3 +- cmd/edit.go | 40 ++++++++++++++------------ cmd/get.go | 6 +++- cmd/kill.go | 18 ++++++++---- cmd/list.go | 8 +++--- cmd/new.go | 2 +- cmd/send.go | 16 +++++++---- go.mod | 2 +- go.sum | 3 +- kak/connect.go | 4 +-- kak/get.go | 6 ++-- kak/kak.go | 63 ++++++++++++++++++++++++++++++++++++++++ kak/list.go | 78 ++++++++++++++++++++++++-------------------------- kak/send.go | 14 ++++----- 16 files changed, 180 insertions(+), 94 deletions(-) create mode 100644 kak/kak.go diff --git a/cmd/attach.go b/cmd/attach.go index 084bee9..1efaec5 100644 --- a/cmd/attach.go +++ b/cmd/attach.go @@ -27,7 +27,9 @@ func (c *AttachCmd) Run() error { return err } - if err := kak.Connect(fp.Name, fp.Line, fp.Column, c.session); err != nil { + s := kak.Session{Name: c.session} + + if err := kak.Connect(s, fp.Name, fp.Line, fp.Column); err != nil { return err } diff --git a/cmd/cat.go b/cmd/cat.go index 89ba8b4..9e0ec0a 100644 --- a/cmd/cat.go +++ b/cmd/cat.go @@ -36,7 +36,12 @@ func (c *CatCmd) Run() error { go kak.ReadTmp(f, ch) sendCmd := fmt.Sprintf("write -force %s", f.Name()) - if err := kak.Send(sendCmd, c.buffer, c.session, c.client); err != nil { + + if err := kak.Send( + kak.Session{c.session}, + kak.Client{c.client}, + kak.Buffer{c.buffer}, + sendCmd); err != nil { return err } diff --git a/cmd/cmd.go b/cmd/cmd.go index 5880869..310751b 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -45,7 +45,8 @@ func (c *Cmd) Init(args []string) error { } c.fs.Usage = c.usage - c.session, c.client = env.Session, env.Client + c.session = env.Session + c.client = env.Client if err := c.fs.Parse(args); err != nil { return err diff --git a/cmd/edit.go b/cmd/edit.go index 8acc807..42e2414 100644 --- a/cmd/edit.go +++ b/cmd/edit.go @@ -43,20 +43,29 @@ func (c *EditCmd) Run() error { } if gitDirName != "" { - if !sessionExists(gitDirName) { - sessionName, err := kak.Create(gitDirName) + gitDirSession := kak.Session{Name: gitDirName} + exists, err := gitDirSession.Exists() + if err != nil { + return err + } + if !exists { + sessionName, err := kak.Create(gitDirSession.Name) if err != nil { return err } fmt.Println("git-dir session started:", sessionName) } - if err := kak.Connect(fp.Name, fp.Line, fp.Column, gitDirName); err != nil { + if err := kak.Connect(gitDirSession, fp.Name, fp.Line, fp.Column); err != nil { return err } } else { - defaultSession := os.Getenv("KKS_DEFAULT_SESSION") - if defaultSession != "" && sessionExists(defaultSession) { - if err := kak.Connect(fp.Name, fp.Line, fp.Column, defaultSession); err != nil { + defaultSession := kak.Session{Name: os.Getenv("KKS_DEFAULT_SESSION")} + exists, err := defaultSession.Exists() + if err != nil { + return err + } + if exists { + if err := kak.Connect(defaultSession, fp.Name, fp.Line, fp.Column); err != nil { return err } } else { @@ -66,10 +75,11 @@ func (c *EditCmd) Run() error { } } default: + session := kak.Session{Name: c.session} 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 { + if err := kak.Connect(session, fp.Name, fp.Line, fp.Column); err != nil { return err } default: @@ -83,7 +93,11 @@ func (c *EditCmd) Run() error { sb.WriteString(fmt.Sprintf(" %d", fp.Column)) } - if err := kak.Send(sb.String(), "", c.session, c.client); err != nil { + if err := kak.Send( + kak.Session{c.session}, + kak.Client{c.client}, + kak.Buffer{c.buffer}, + sb.String()); err != nil { return err } } @@ -99,13 +113,3 @@ func parseGitToplevel() string { } 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 -} diff --git a/cmd/get.go b/cmd/get.go index 8c53f80..64ef5d8 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -35,7 +35,11 @@ func (c *GetCmd) Run() error { return err } - resp, err := kak.Get(query, c.buffer, c.session, c.client) + resp, err := kak.Get( + kak.Session{c.session}, + kak.Client{c.client}, + kak.Buffer{c.buffer}, + query) if err != nil { return err } diff --git a/cmd/kill.go b/cmd/kill.go index 724057e..fac6750 100644 --- a/cmd/kill.go +++ b/cmd/kill.go @@ -23,21 +23,29 @@ type KillCmd struct { } func (c *KillCmd) Run() error { - kakCmd := "kill" + sendCmd := "kill" switch c.allSessions { case false: // TODO need to somehow trigger "no session" err - if err := kak.Send(kakCmd, "", c.session, ""); err != nil { + if err := kak.Send( + kak.Session{c.session}, + kak.Client{c.client}, + kak.Buffer{c.buffer}, + sendCmd); err != nil { return err } case true: - sessions, err := kak.List() + sessions, err := kak.Sessions() if err != nil { return err } - for _, sess := range sessions { - if err := kak.Send(kakCmd, "", sess.Name, ""); err != nil { + for _, s := range sessions { + if err := kak.Send( + s, + kak.Client{c.client}, + kak.Buffer{c.buffer}, + sendCmd); err != nil { return err } } diff --git a/cmd/list.go b/cmd/list.go index 05b0ea7..618e337 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -43,11 +43,11 @@ func (c *ListCmd) Run() error { w.Init(os.Stdout, 0, 8, 1, '\t', 0) for _, s := range sessions { - if len(s.Clients) == 0 { - fmt.Fprintf(w, "%s\t: %s\t: %s\n", s.Name, " ", s.Dir) + if len(s.Clients()) == 0 { + fmt.Fprintf(w, "%s\t: %s\t: %s\n", s.Name, " ", s.Dir()) } else { - for _, cl := range s.Clients { - fmt.Fprintf(w, "%s\t: %s\t: %s\n", s.Name, cl, s.Dir) + for _, cl := range s.Clients() { + fmt.Fprintf(w, "%s\t: %s\t: %s\n", s.Name, cl, s.Dir()) } } } diff --git a/cmd/new.go b/cmd/new.go index db9f562..26d47ca 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -25,7 +25,7 @@ type NewCmd struct { func (c *NewCmd) Run() error { c.name = c.fs.Arg(0) - sessions, err := kak.List() + sessions, err := kak.Sessions() for _, s := range sessions { if s.Name == c.name { return errors.New(fmt.Sprintf("session already exists: %s", c.name)) diff --git a/cmd/send.go b/cmd/send.go index 9279f48..d13d75d 100644 --- a/cmd/send.go +++ b/cmd/send.go @@ -27,24 +27,28 @@ type SendCmd struct { func (c *SendCmd) Run() error { // TODO probably need to do some shell escaping here - kakCmd := strings.Join(c.fs.Args(), " ") + sendCmd := strings.Join(c.fs.Args(), " ") switch c.allClients { case true: - sessions, err := kak.List() + sessions, err := kak.Sessions() if err != nil { return err } - for _, sess := range sessions { - for _, cl := range sess.Clients { - if err := kak.Send(kakCmd, "", sess.Name, cl); err != nil { + for _, s := range sessions { + for _, cl := range s.Clients() { + if err := kak.Send(s, cl, kak.Buffer{}, sendCmd); err != nil { return err } } } case false: // TODO: need to trigger "session not set" error - if err := kak.Send(kakCmd, c.buffer, c.session, c.client); err != nil { + if err := kak.Send( + kak.Session{c.session}, + kak.Client{c.client}, + kak.Buffer{c.buffer}, + sendCmd); err != nil { return err } } diff --git a/go.mod b/go.mod index 7037fa9..b84dfdb 100644 --- a/go.mod +++ b/go.mod @@ -7,4 +7,4 @@ require ( github.com/google/go-cmp v0.5.6 ) -require golang.org/x/sys v0.0.0-20210909193231-528a39cd75f3 // indirect +require golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect diff --git a/go.sum b/go.sum index e2e6575..038e82c 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,7 @@ github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWp github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU= github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210909193231-528a39cd75f3 h1:3Ad41xy2WCESpufXwgs7NpDSu+vjxqLt2UFqUV+20bI= -golang.org/x/sys v0.0.0-20210909193231-528a39cd75f3/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/kak/connect.go b/kak/connect.go index f293771..7ff407e 100644 --- a/kak/connect.go +++ b/kak/connect.go @@ -7,14 +7,14 @@ import ( "syscall" ) -func Connect(file string, line int, col int, sess string) error { +func Connect(session Session, file string, line int, col int) error { kakBinary, err := exec.LookPath("kak") if err != nil { return err } kakExecArgs := []string{kakBinary} - kakExecArgs = append(kakExecArgs, "-c", sess) + kakExecArgs = append(kakExecArgs, "-c", session.Name) if file != "" { kakExecArgs = append(kakExecArgs, file) diff --git a/kak/get.go b/kak/get.go index 5bbe9e3..d53d23b 100644 --- a/kak/get.go +++ b/kak/get.go @@ -9,7 +9,7 @@ import ( "github.com/fsnotify/fsnotify" ) -func Get(getStr, buf, session, client string) ([]string, error) { +func Get(session Session, client Client, buffer Buffer, query string) ([]string, error) { // create a tmp file for kak to echo the value f, err := os.CreateTemp("", "kks-tmp") if err != nil { @@ -21,8 +21,8 @@ func Get(getStr, buf, session, client string) ([]string, error) { go ReadTmp(f, ch) // tell kak to echo the requested state - sendCmd := fmt.Sprintf("echo -quoting kakoune -to-file %s %%{ %s }", f.Name(), getStr) - if err := Send(sendCmd, buf, session, client); err != nil { + sendCmd := fmt.Sprintf("echo -quoting kakoune -to-file %s %%{ %s }", f.Name(), query) + if err := Send(session, client, buffer, sendCmd); err != nil { return nil, err } diff --git a/kak/kak.go b/kak/kak.go new file mode 100644 index 0000000..6ce5be3 --- /dev/null +++ b/kak/kak.go @@ -0,0 +1,63 @@ +package kak + +import ( + "os/exec" + "strings" +) + +type Session struct { + Name string + // clients []string + // dir string +} + +type Client struct{ Name string } +type Buffer struct{ Name string } + +func (s *Session) Exists() (bool, error) { + sessions, err := Sessions() + if err != nil { + return false, err + } + + for _, session := range sessions { + if session.Name == s.Name { + return true, nil + } + } + return false, nil +} + +func (s *Session) Clients() (clients []Client) { + cl, err := Get(*s, Client{}, Buffer{}, "%val{client_list}") + if err != nil { + return []Client{} + } + + for _, c := range cl { + clients = append(clients, Client{c}) + } + + return clients +} + +func (s *Session) Dir() string { + dir, err := Get(*s, Client{}, Buffer{}, "%sh{pwd}") + if err != nil { + return "" + } + return dir[0] +} + +func Sessions() (sessions []Session, err error) { + output, err := exec.Command("kak", "-l").Output() + if err != nil { + return nil, err + } + + for _, s := range strings.Split(strings.TrimSpace(string(output)), "\n") { + sessions = append(sessions, Session{Name: s}) + } + + return sessions, nil +} diff --git a/kak/list.go b/kak/list.go index c151bfe..31b7497 100644 --- a/kak/list.go +++ b/kak/list.go @@ -1,44 +1,40 @@ package kak -import ( - "os/exec" - "strings" -) - -type KakSession struct { - Name string `json:"name"` - Clients []string `json:"clients"` - Dir string `json:"dir"` -} - -func List() ([]KakSession, error) { - out, err := exec.Command("kak", "-l").Output() - if err != nil { - return nil, 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}", "", s.Name, "") - if err != nil { - return nil, err - } - if len(clients) > 0 && clients[0] != "" { - s.Clients = clients - } - - dir, err := Get("%sh{pwd}", "", s.Name, "") - if err != nil { - return nil, err - } - s.Dir = strings.Join(dir, "") - - sessions = append(sessions, s) - } - - return sessions, nil +// type session struct { +// Name string `json:"name"` +// Clients []string `json:"clients"` +// Dir string `json:"dir"` +// } + +func List() (sessions []Session, err error) { + return []Session{}, nil + + // TODO probably don't need this func, can just use Sessions() directly + + // s, err := Sessions() + // if err != nil { + // return s, err + // } + + // for _, s := range kakSessions { + // session := session{Name: s} + + // clients, err := Get("%val{client_list}", "", session.Name, "") + // if err != nil { + // return nil, err + // } + // if len(clients) > 0 && clients[0] != "" { + // session.Clients = clients + // } + + // dir, err := Get("%sh{pwd}", "", session.Name, "") + // if err != nil { + // return nil, err + // } + // session.Dir = strings.Join(dir, "") + + // sessions = append(sessions, session) + // } + + // return sessions, nil } diff --git a/kak/send.go b/kak/send.go index 426ec97..48aebf7 100644 --- a/kak/send.go +++ b/kak/send.go @@ -6,8 +6,8 @@ import ( "os/exec" ) -func Send(kakCommand, buf, ses, cl string) error { - cmd := exec.Command("kak", "-p", ses) +func Send(session Session, client Client, buffer Buffer, command string) error { + cmd := exec.Command("kak", "-p", session.Name) // cmd.Stdout = os.Stdout // cmd.Stderr = os.Stderr @@ -16,13 +16,13 @@ func Send(kakCommand, buf, ses, cl string) error { go func() { io.WriteString(stdin, "evaluate-commands") - if buf != "" { - io.WriteString(stdin, fmt.Sprintf(" -buffer %s", buf)) - } else if cl != "" { - io.WriteString(stdin, fmt.Sprintf(" -try-client %s", cl)) + if buffer.Name != "" { + io.WriteString(stdin, fmt.Sprintf(" -buffer %s", buffer.Name)) + } else if client.Name != "" { + io.WriteString(stdin, fmt.Sprintf(" -try-client %s", client.Name)) } - io.WriteString(stdin, fmt.Sprintf(" %s", kakCommand)) + io.WriteString(stdin, fmt.Sprintf(" %s", command)) stdin.Close() }() From 41ed47c4d3971ee7199340fd3c1805f287d569a7 Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Wed, 15 Sep 2021 02:11:20 +0300 Subject: [PATCH 02/14] add KakContext struct --- cmd/cmd.go | 17 +++++++++++++++-- kak/kak.go | 9 +++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 310751b..17d0053 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -6,6 +6,8 @@ import ( "fmt" "os" "strings" + + "github.com/kkga/kks/kak" ) type Runner interface { @@ -27,6 +29,8 @@ type Cmd struct { sessionReq bool clientReq bool bufferReq bool + + kakContext kak.Context } type EnvContext struct { @@ -52,10 +56,19 @@ func (c *Cmd) Init(args []string) error { return err } - if c.sessionReq && c.session == "" { + c.kakContext = kak.Context{ + Session: kak.Session{Name: c.session}, + Client: kak.Client{Name: c.client}, + Buffer: kak.Buffer{Name: c.buffer}, + } + + if c.sessionReq && c.kakContext.Session.Name == "" { return errors.New("no session in context") } - if c.clientReq && c.client == "" { + if c.clientReq && c.kakContext.Client.Name == "" { + return errors.New("no client in context") + } + if c.bufferReq && c.kakContext.Buffer.Name == "" { return errors.New("no client in context") } diff --git a/kak/kak.go b/kak/kak.go index 6ce5be3..6b6b502 100644 --- a/kak/kak.go +++ b/kak/kak.go @@ -5,12 +5,13 @@ import ( "strings" ) -type Session struct { - Name string - // clients []string - // dir string +type Context struct { + Session Session + Client Client + Buffer Buffer } +type Session struct{ Name string } type Client struct{ Name string } type Buffer struct{ Name string } From eebd079e7fcd1834eb157d3ab4fd9d58431097d5 Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Wed, 15 Sep 2021 03:26:55 +0300 Subject: [PATCH 03/14] use new types in commands --- cmd/cat.go | 6 +----- cmd/edit.go | 6 +----- cmd/get.go | 6 +----- cmd/kill.go | 17 +++++++---------- cmd/list.go | 27 ++++++++++++++++++++++++--- cmd/send.go | 12 +++++------- kak/get.go | 4 ++-- kak/kak.go | 6 ++++-- kak/list.go | 40 ---------------------------------------- kak/send.go | 12 ++++++------ 10 files changed, 51 insertions(+), 85 deletions(-) delete mode 100644 kak/list.go diff --git a/cmd/cat.go b/cmd/cat.go index 9e0ec0a..367a114 100644 --- a/cmd/cat.go +++ b/cmd/cat.go @@ -37,11 +37,7 @@ func (c *CatCmd) Run() error { sendCmd := fmt.Sprintf("write -force %s", f.Name()) - if err := kak.Send( - kak.Session{c.session}, - kak.Client{c.client}, - kak.Buffer{c.buffer}, - sendCmd); err != nil { + if err := kak.Send(c.kakContext, sendCmd); err != nil { return err } diff --git a/cmd/edit.go b/cmd/edit.go index 42e2414..55bb3d6 100644 --- a/cmd/edit.go +++ b/cmd/edit.go @@ -93,11 +93,7 @@ func (c *EditCmd) Run() error { sb.WriteString(fmt.Sprintf(" %d", fp.Column)) } - if err := kak.Send( - kak.Session{c.session}, - kak.Client{c.client}, - kak.Buffer{c.buffer}, - sb.String()); err != nil { + if err := kak.Send(c.kakContext, sb.String()); err != nil { return err } } diff --git a/cmd/get.go b/cmd/get.go index 64ef5d8..d00e622 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -35,11 +35,7 @@ func (c *GetCmd) Run() error { return err } - resp, err := kak.Get( - kak.Session{c.session}, - kak.Client{c.client}, - kak.Buffer{c.buffer}, - query) + resp, err := kak.Get(c.kakContext, query) if err != nil { return err } diff --git a/cmd/kill.go b/cmd/kill.go index fac6750..8709ba7 100644 --- a/cmd/kill.go +++ b/cmd/kill.go @@ -28,11 +28,7 @@ func (c *KillCmd) Run() error { switch c.allSessions { case false: // TODO need to somehow trigger "no session" err - if err := kak.Send( - kak.Session{c.session}, - kak.Client{c.client}, - kak.Buffer{c.buffer}, - sendCmd); err != nil { + if err := kak.Send(c.kakContext, sendCmd); err != nil { return err } case true: @@ -41,11 +37,12 @@ func (c *KillCmd) Run() error { return err } for _, s := range sessions { - if err := kak.Send( - s, - kak.Client{c.client}, - kak.Buffer{c.buffer}, - sendCmd); err != nil { + sessCtx := kak.Context{ + Session: s, + Client: c.kakContext.Client, + Buffer: c.kakContext.Buffer, + } + if err := kak.Send(sessCtx, sendCmd); err != nil { return err } } diff --git a/cmd/list.go b/cmd/list.go index 618e337..50da6cc 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -26,28 +26,49 @@ type ListCmd struct { } func (c *ListCmd) Run() error { - sessions, err := kak.List() + kakSessions, err := kak.Sessions() if err != nil { return err } switch c.json { + case true: + type session struct { + Name string `json:"name"` + Clients []string `json:"clients"` + Dir string `json:"dir"` + } + + sessions := []session{} + + for i, s := range kakSessions { + sessions = append(sessions, session{Name: s.Name, Clients: []string{}, Dir: s.Dir()}) + for _, c := range s.Clients() { + if c.Name != "" { + sessions[i].Clients = append(sessions[i].Clients, c.Name) + } + } + } + j, err := json.MarshalIndent(sessions, "", " ") if err != nil { return err } + fmt.Println(string(j)) + case false: w := new(tabwriter.Writer) w.Init(os.Stdout, 0, 8, 1, '\t', 0) - for _, s := range sessions { + for _, s := range kakSessions { if len(s.Clients()) == 0 { fmt.Fprintf(w, "%s\t: %s\t: %s\n", s.Name, " ", s.Dir()) } else { for _, cl := range s.Clients() { - fmt.Fprintf(w, "%s\t: %s\t: %s\n", s.Name, cl, s.Dir()) + clientCtx := kak.Context{Session: s, Client: cl} + fmt.Fprintf(w, "%s\t: %s\t: %s\n", s.Name, clientCtx.Client.Name, s.Dir()) } } } diff --git a/cmd/send.go b/cmd/send.go index d13d75d..77e2952 100644 --- a/cmd/send.go +++ b/cmd/send.go @@ -36,19 +36,17 @@ func (c *SendCmd) Run() error { return err } for _, s := range sessions { - for _, cl := range s.Clients() { - if err := kak.Send(s, cl, kak.Buffer{}, sendCmd); err != nil { + sessionCtx := kak.Context{Session: s} + for _, cl := range sessionCtx.Session.Clients() { + clientCtx := kak.Context{Session: s, Client: cl} + if err := kak.Send(clientCtx, sendCmd); err != nil { return err } } } case false: // TODO: need to trigger "session not set" error - if err := kak.Send( - kak.Session{c.session}, - kak.Client{c.client}, - kak.Buffer{c.buffer}, - sendCmd); err != nil { + if err := kak.Send(c.kakContext, sendCmd); err != nil { return err } } diff --git a/kak/get.go b/kak/get.go index d53d23b..fdbc825 100644 --- a/kak/get.go +++ b/kak/get.go @@ -9,7 +9,7 @@ import ( "github.com/fsnotify/fsnotify" ) -func Get(session Session, client Client, buffer Buffer, query string) ([]string, error) { +func Get(kctx Context, query string) ([]string, error) { // create a tmp file for kak to echo the value f, err := os.CreateTemp("", "kks-tmp") if err != nil { @@ -22,7 +22,7 @@ func Get(session Session, client Client, buffer Buffer, query string) ([]string, // tell kak to echo the requested state sendCmd := fmt.Sprintf("echo -quoting kakoune -to-file %s %%{ %s }", f.Name(), query) - if err := Send(session, client, buffer, sendCmd); err != nil { + if err := Send(kctx, sendCmd); err != nil { return nil, err } diff --git a/kak/kak.go b/kak/kak.go index 6b6b502..fd4afcb 100644 --- a/kak/kak.go +++ b/kak/kak.go @@ -30,7 +30,8 @@ func (s *Session) Exists() (bool, error) { } func (s *Session) Clients() (clients []Client) { - cl, err := Get(*s, Client{}, Buffer{}, "%val{client_list}") + sessCtx := Context{Session: *s} + cl, err := Get(sessCtx, "%val{client_list}") if err != nil { return []Client{} } @@ -43,7 +44,8 @@ func (s *Session) Clients() (clients []Client) { } func (s *Session) Dir() string { - dir, err := Get(*s, Client{}, Buffer{}, "%sh{pwd}") + sessCtx := Context{Session: *s} + dir, err := Get(sessCtx, "%sh{pwd}") if err != nil { return "" } diff --git a/kak/list.go b/kak/list.go deleted file mode 100644 index 31b7497..0000000 --- a/kak/list.go +++ /dev/null @@ -1,40 +0,0 @@ -package kak - -// type session struct { -// Name string `json:"name"` -// Clients []string `json:"clients"` -// Dir string `json:"dir"` -// } - -func List() (sessions []Session, err error) { - return []Session{}, nil - - // TODO probably don't need this func, can just use Sessions() directly - - // s, err := Sessions() - // if err != nil { - // return s, err - // } - - // for _, s := range kakSessions { - // session := session{Name: s} - - // clients, err := Get("%val{client_list}", "", session.Name, "") - // if err != nil { - // return nil, err - // } - // if len(clients) > 0 && clients[0] != "" { - // session.Clients = clients - // } - - // dir, err := Get("%sh{pwd}", "", session.Name, "") - // if err != nil { - // return nil, err - // } - // session.Dir = strings.Join(dir, "") - - // sessions = append(sessions, session) - // } - - // return sessions, nil -} diff --git a/kak/send.go b/kak/send.go index 48aebf7..3bbef44 100644 --- a/kak/send.go +++ b/kak/send.go @@ -6,8 +6,8 @@ import ( "os/exec" ) -func Send(session Session, client Client, buffer Buffer, command string) error { - cmd := exec.Command("kak", "-p", session.Name) +func Send(kctx Context, command string) error { + cmd := exec.Command("kak", "-p", kctx.Session.Name) // cmd.Stdout = os.Stdout // cmd.Stderr = os.Stderr @@ -16,10 +16,10 @@ func Send(session Session, client Client, buffer Buffer, command string) error { go func() { io.WriteString(stdin, "evaluate-commands") - if buffer.Name != "" { - io.WriteString(stdin, fmt.Sprintf(" -buffer %s", buffer.Name)) - } else if client.Name != "" { - io.WriteString(stdin, fmt.Sprintf(" -try-client %s", client.Name)) + if kctx.Buffer.Name != "" { + io.WriteString(stdin, fmt.Sprintf(" -buffer %s", kctx.Buffer.Name)) + } else if kctx.Client.Name != "" { + io.WriteString(stdin, fmt.Sprintf(" -try-client %s", kctx.Client.Name)) } io.WriteString(stdin, fmt.Sprintf(" %s", command)) From ebce505700902b710b9ef44d51a3f669fe12d823 Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Wed, 15 Sep 2021 03:40:15 +0300 Subject: [PATCH 04/14] use new type in connect --- cmd/attach.go | 4 +--- cmd/edit.go | 21 +++++++++++++++------ kak/connect.go | 4 ++-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/cmd/attach.go b/cmd/attach.go index 1efaec5..5ed3c28 100644 --- a/cmd/attach.go +++ b/cmd/attach.go @@ -27,9 +27,7 @@ func (c *AttachCmd) Run() error { return err } - s := kak.Session{Name: c.session} - - if err := kak.Connect(s, fp.Name, fp.Line, fp.Column); err != nil { + if err := kak.Connect(c.kakContext, fp.Name, fp.Line, fp.Column); err != nil { return err } diff --git a/cmd/edit.go b/cmd/edit.go index 55bb3d6..375fcce 100644 --- a/cmd/edit.go +++ b/cmd/edit.go @@ -33,7 +33,8 @@ func (c *EditCmd) Run() error { return err } - switch c.session { + switch c.kakContext.Session.Name { + case "": var gitDirName string _, useGitDirSessions := os.LookupEnv("KKS_USE_GITDIR_SESSIONS") @@ -48,6 +49,7 @@ func (c *EditCmd) Run() error { if err != nil { return err } + if !exists { sessionName, err := kak.Create(gitDirSession.Name) if err != nil { @@ -55,31 +57,38 @@ func (c *EditCmd) Run() error { } fmt.Println("git-dir session started:", sessionName) } - if err := kak.Connect(gitDirSession, fp.Name, fp.Line, fp.Column); err != nil { + + kctx := kak.Context{Session: gitDirSession} + + if err := kak.Connect(kctx, fp.Name, fp.Line, fp.Column); err != nil { return err } + } else { defaultSession := kak.Session{Name: os.Getenv("KKS_DEFAULT_SESSION")} exists, err := defaultSession.Exists() if err != nil { return err } + if exists { - if err := kak.Connect(defaultSession, fp.Name, fp.Line, fp.Column); err != nil { + kctx := kak.Context{Session: defaultSession} + if err := kak.Connect(kctx, fp.Name, fp.Line, fp.Column); err != nil { return err } + } else { if err := kak.Run(fp.Name, fp.Line, fp.Column); err != nil { return err } } } + default: - session := kak.Session{Name: c.session} - switch c.client { + switch c.kakContext.Client.Name { case "": // if no client, attach to session with new client - if err := kak.Connect(session, fp.Name, fp.Line, fp.Column); err != nil { + if err := kak.Connect(c.kakContext, fp.Name, fp.Line, fp.Column); err != nil { return err } default: diff --git a/kak/connect.go b/kak/connect.go index 7ff407e..ec130ad 100644 --- a/kak/connect.go +++ b/kak/connect.go @@ -7,14 +7,14 @@ import ( "syscall" ) -func Connect(session Session, file string, line int, col int) error { +func Connect(kctx Context, file string, line int, col int) error { kakBinary, err := exec.LookPath("kak") if err != nil { return err } kakExecArgs := []string{kakBinary} - kakExecArgs = append(kakExecArgs, "-c", session.Name) + kakExecArgs = append(kakExecArgs, "-c", kctx.Session.Name) if file != "" { kakExecArgs = append(kakExecArgs, file) From 15e6ed9c25c567ff0e37f80d9ba5779ae0a0e704 Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Wed, 15 Sep 2021 11:23:52 +0300 Subject: [PATCH 05/14] add short descriptions to commands --- TODO | 2 +- cmd/attach.go | 3 ++- cmd/cat.go | 3 ++- cmd/cmd.go | 15 ++++++++++----- cmd/edit.go | 7 ++++--- cmd/env.go | 3 ++- cmd/get.go | 3 ++- cmd/init.go | 7 ++++--- cmd/kill.go | 7 ++++--- cmd/list.go | 7 ++++--- cmd/new.go | 7 ++++--- cmd/send.go | 7 ++++--- 12 files changed, 43 insertions(+), 28 deletions(-) diff --git a/TODO b/TODO index b28ee6c..4a50e44 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,6 @@ - [x] don't pass `0` line/col to kak -- [ ] add key bindings to delete buffers in `kks-buffers` +- [x] add key bindings to delete buffers in `kks-buffers` - [ ] add key bindings to create files in `kks-files` - [x] add extra fzf bindings in kks select diff --git a/cmd/attach.go b/cmd/attach.go index 5ed3c28..4381660 100644 --- a/cmd/attach.go +++ b/cmd/attach.go @@ -10,7 +10,8 @@ func NewAttachCmd() *AttachCmd { c := &AttachCmd{Cmd: Cmd{ fs: flag.NewFlagSet("attach", flag.ExitOnError), alias: []string{"a"}, - usageStr: "[options] [file] [+[:[:[:]]", + fs: flag.NewFlagSet("edit", flag.ExitOnError), + alias: []string{"e"}, + shortDesc: "Edit file. In session and client, if set.", + usageLine: "[options] [file] [+[:]]", }} // TODO add flag that allows creating new files (removes -existing) c.fs.StringVar(&c.session, "s", "", "session") diff --git a/cmd/env.go b/cmd/env.go index b3d61eb..fc2d162 100644 --- a/cmd/env.go +++ b/cmd/env.go @@ -10,7 +10,8 @@ func NewEnvCmd() *EnvCmd { c := &EnvCmd{Cmd: Cmd{ fs: flag.NewFlagSet("env", flag.ExitOnError), alias: []string{""}, - usageStr: "[options]", + shortDesc: "Print current Kakoune context set by environment to stdout.", + usageLine: "[options]", sessionReq: true, }} c.fs.BoolVar(&c.json, "json", false, "json output") diff --git a/cmd/get.go b/cmd/get.go index d00e622..723f3c5 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -13,7 +13,8 @@ func NewGetCmd() *GetCmd { c := &GetCmd{Cmd: Cmd{ fs: flag.NewFlagSet("get", flag.ExitOnError), alias: []string{""}, - usageStr: "[options] (<%val{}> | <%opt{}> | <%reg{}> | <%sh{}>)", + shortDesc: "Get states from Kakoune context.", + usageLine: "[options] (<%val{..}> | <%opt{..}> | <%reg{..}> | <%sh{..}>)", sessionReq: true, // TODO maybe actually just use flags for args // or maybe create separate subcommands get-val, etc diff --git a/cmd/init.go b/cmd/init.go index be36ab8..dd5477c 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -11,9 +11,10 @@ var initKak string func NewInitCmd() *InitCmd { c := &InitCmd{Cmd: Cmd{ - fs: flag.NewFlagSet("init", flag.ExitOnError), - alias: []string{""}, - usageStr: "", + fs: flag.NewFlagSet("init", flag.ExitOnError), + alias: []string{""}, + shortDesc: "Print Kakoune command definitions to stdout.", + usageLine: "", }} return c } diff --git a/cmd/kill.go b/cmd/kill.go index 8709ba7..970ceb3 100644 --- a/cmd/kill.go +++ b/cmd/kill.go @@ -8,9 +8,10 @@ import ( func NewKillCmd() *KillCmd { c := &KillCmd{Cmd: Cmd{ - fs: flag.NewFlagSet("kill", flag.ExitOnError), - alias: []string{""}, - usageStr: "[options]", + fs: flag.NewFlagSet("kill", flag.ExitOnError), + alias: []string{""}, + shortDesc: "Terminate Kakoune session.", + usageLine: "[options]", }} c.fs.StringVar(&c.session, "s", "", "session") c.fs.BoolVar(&c.allSessions, "a", false, "all sessions") diff --git a/cmd/list.go b/cmd/list.go index 50da6cc..73f48c9 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -12,9 +12,10 @@ import ( func NewListCmd() *ListCmd { c := &ListCmd{Cmd: Cmd{ - fs: flag.NewFlagSet("list", flag.ExitOnError), - alias: []string{"ls", "l"}, - usageStr: "[options]", + fs: flag.NewFlagSet("list", flag.ExitOnError), + alias: []string{"ls", "l"}, + shortDesc: "List Kakoune sessions and clients.", + usageLine: "[options]", }} c.fs.BoolVar(&c.json, "json", false, "json output") return c diff --git a/cmd/new.go b/cmd/new.go index 26d47ca..37a1bab 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -10,9 +10,10 @@ import ( func NewNewCmd() *NewCmd { c := &NewCmd{Cmd: Cmd{ - fs: flag.NewFlagSet("new", flag.ExitOnError), - alias: []string{"n"}, - usageStr: "[name]", + fs: flag.NewFlagSet("new", flag.ExitOnError), + alias: []string{"n"}, + shortDesc: "Start new headless Kakoune session.", + usageLine: "[]", }} return c } diff --git a/cmd/send.go b/cmd/send.go index 77e2952..312f6a5 100644 --- a/cmd/send.go +++ b/cmd/send.go @@ -9,9 +9,10 @@ import ( func NewSendCmd() *SendCmd { c := &SendCmd{Cmd: Cmd{ - fs: flag.NewFlagSet("send", flag.ExitOnError), - alias: []string{"s"}, - usageStr: "[options] ", + fs: flag.NewFlagSet("send", flag.ExitOnError), + alias: []string{"s"}, + shortDesc: "Send commands to Kakoune context.", + usageLine: "[options] ", }} c.fs.BoolVar(&c.allClients, "a", false, "send to all clients") c.fs.StringVar(&c.session, "s", "", "session") From ecde9b03986e2adde77f6046f56c3bbaa0dcbbc3 Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Wed, 15 Sep 2021 11:43:45 +0300 Subject: [PATCH 06/14] move filepath to kak package --- cmd/attach.go | 4 ++-- cmd/cmd.go | 4 ++-- cmd/edit.go | 12 ++++++------ cmd/kill.go | 2 +- cmd/send.go | 2 +- kak/connect.go | 10 +++++----- {cmd => kak}/filepath.go | 2 +- {cmd => kak}/filepath_test.go | 4 ++-- kak/get.go | 2 +- kak/kak.go | 4 ++-- kak/send.go | 2 +- 11 files changed, 24 insertions(+), 24 deletions(-) rename {cmd => kak}/filepath.go (99%) rename {cmd => kak}/filepath_test.go (93%) diff --git a/cmd/attach.go b/cmd/attach.go index 4381660..5f8691b 100644 --- a/cmd/attach.go +++ b/cmd/attach.go @@ -23,12 +23,12 @@ type AttachCmd struct { } func (c *AttachCmd) Run() error { - fp, err := NewFilepath(c.fs.Args()) + fp, err := kak.NewFilepath(c.fs.Args()) if err != nil { return err } - if err := kak.Connect(c.kakContext, fp.Name, fp.Line, fp.Column); err != nil { + if err := kak.Connect(c.kakContext, fp); err != nil { return err } diff --git a/cmd/cmd.go b/cmd/cmd.go index 2f5d27d..6575b3c 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -31,7 +31,7 @@ type Cmd struct { clientReq bool bufferReq bool - kakContext kak.Context + kakContext *kak.Context } type EnvContext struct { @@ -57,7 +57,7 @@ func (c *Cmd) Init(args []string) error { return err } - c.kakContext = kak.Context{ + c.kakContext = &kak.Context{ Session: kak.Session{Name: c.session}, Client: kak.Client{Name: c.client}, Buffer: kak.Buffer{Name: c.buffer}, diff --git a/cmd/edit.go b/cmd/edit.go index 1f56716..14ded1d 100644 --- a/cmd/edit.go +++ b/cmd/edit.go @@ -29,7 +29,7 @@ type EditCmd struct { } func (c *EditCmd) Run() error { - fp, err := NewFilepath(c.fs.Args()) + fp, err := kak.NewFilepath(c.fs.Args()) if err != nil { return err } @@ -59,9 +59,9 @@ func (c *EditCmd) Run() error { fmt.Println("git-dir session started:", sessionName) } - kctx := kak.Context{Session: gitDirSession} + kctx := &kak.Context{Session: gitDirSession} - if err := kak.Connect(kctx, fp.Name, fp.Line, fp.Column); err != nil { + if err := kak.Connect(kctx, fp); err != nil { return err } @@ -73,8 +73,8 @@ func (c *EditCmd) Run() error { } if exists { - kctx := kak.Context{Session: defaultSession} - if err := kak.Connect(kctx, fp.Name, fp.Line, fp.Column); err != nil { + kctx := &kak.Context{Session: defaultSession} + if err := kak.Connect(kctx, fp); err != nil { return err } @@ -89,7 +89,7 @@ func (c *EditCmd) Run() error { switch c.kakContext.Client.Name { case "": // if no client, attach to session with new client - if err := kak.Connect(c.kakContext, fp.Name, fp.Line, fp.Column); err != nil { + if err := kak.Connect(c.kakContext, fp); err != nil { return err } default: diff --git a/cmd/kill.go b/cmd/kill.go index 970ceb3..876f09b 100644 --- a/cmd/kill.go +++ b/cmd/kill.go @@ -38,7 +38,7 @@ func (c *KillCmd) Run() error { return err } for _, s := range sessions { - sessCtx := kak.Context{ + sessCtx := &kak.Context{ Session: s, Client: c.kakContext.Client, Buffer: c.kakContext.Buffer, diff --git a/cmd/send.go b/cmd/send.go index 312f6a5..2de7c1c 100644 --- a/cmd/send.go +++ b/cmd/send.go @@ -39,7 +39,7 @@ func (c *SendCmd) Run() error { for _, s := range sessions { sessionCtx := kak.Context{Session: s} for _, cl := range sessionCtx.Session.Clients() { - clientCtx := kak.Context{Session: s, Client: cl} + clientCtx := &kak.Context{Session: s, Client: cl} if err := kak.Send(clientCtx, sendCmd); err != nil { return err } diff --git a/kak/connect.go b/kak/connect.go index ec130ad..3184eb2 100644 --- a/kak/connect.go +++ b/kak/connect.go @@ -7,7 +7,7 @@ import ( "syscall" ) -func Connect(kctx Context, file string, line int, col int) error { +func Connect(kctx *Context, fp *Filepath) error { kakBinary, err := exec.LookPath("kak") if err != nil { return err @@ -16,11 +16,11 @@ func Connect(kctx Context, file string, line int, col int) error { kakExecArgs := []string{kakBinary} kakExecArgs = append(kakExecArgs, "-c", kctx.Session.Name) - if file != "" { - kakExecArgs = append(kakExecArgs, file) + if fp.Name != "" { + kakExecArgs = append(kakExecArgs, fp.Name) - if line != 0 { - kakExecArgs = append(kakExecArgs, fmt.Sprintf("+%d:%d", line, col)) + if fp.Line != 0 { + kakExecArgs = append(kakExecArgs, fmt.Sprintf("+%d:%d", fp.Line, fp.Column)) } } diff --git a/cmd/filepath.go b/kak/filepath.go similarity index 99% rename from cmd/filepath.go rename to kak/filepath.go index b8ad79e..b7e0077 100644 --- a/cmd/filepath.go +++ b/kak/filepath.go @@ -1,4 +1,4 @@ -package cmd +package kak import ( "os" diff --git a/cmd/filepath_test.go b/kak/filepath_test.go similarity index 93% rename from cmd/filepath_test.go rename to kak/filepath_test.go index a9fe2d1..b73b85e 100644 --- a/cmd/filepath_test.go +++ b/kak/filepath_test.go @@ -1,4 +1,4 @@ -package cmd +package kak import ( "testing" @@ -13,7 +13,7 @@ func TestNewFilepath(t *testing.T) { }{ { []string{"file"}, - Filepath{Name: "/home/kkga/projects/kks/cmd/file", + Filepath{Name: "/home/kkga/projects/kks/kak/file", Raw: []string{"file"}}, }, { diff --git a/kak/get.go b/kak/get.go index fdbc825..d069098 100644 --- a/kak/get.go +++ b/kak/get.go @@ -9,7 +9,7 @@ import ( "github.com/fsnotify/fsnotify" ) -func Get(kctx Context, query string) ([]string, error) { +func Get(kctx *Context, query string) ([]string, error) { // create a tmp file for kak to echo the value f, err := os.CreateTemp("", "kks-tmp") if err != nil { diff --git a/kak/kak.go b/kak/kak.go index fd4afcb..eaa9e80 100644 --- a/kak/kak.go +++ b/kak/kak.go @@ -30,7 +30,7 @@ func (s *Session) Exists() (bool, error) { } func (s *Session) Clients() (clients []Client) { - sessCtx := Context{Session: *s} + sessCtx := &Context{Session: *s} cl, err := Get(sessCtx, "%val{client_list}") if err != nil { return []Client{} @@ -44,7 +44,7 @@ func (s *Session) Clients() (clients []Client) { } func (s *Session) Dir() string { - sessCtx := Context{Session: *s} + sessCtx := &Context{Session: *s} dir, err := Get(sessCtx, "%sh{pwd}") if err != nil { return "" diff --git a/kak/send.go b/kak/send.go index 3bbef44..0ca021e 100644 --- a/kak/send.go +++ b/kak/send.go @@ -6,7 +6,7 @@ import ( "os/exec" ) -func Send(kctx Context, command string) error { +func Send(kctx *Context, command string) error { cmd := exec.Command("kak", "-p", kctx.Session.Name) // cmd.Stdout = os.Stdout // cmd.Stderr = os.Stderr From c5446ee26acb85c4a60a328e113e62653b77d66b Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Wed, 15 Sep 2021 11:46:42 +0300 Subject: [PATCH 07/14] kak.Create -> kak.Start --- cmd/edit.go | 2 +- cmd/new.go | 2 +- kak/{create.go => start.go} | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename kak/{create.go => start.go} (92%) diff --git a/cmd/edit.go b/cmd/edit.go index 14ded1d..5fcb54f 100644 --- a/cmd/edit.go +++ b/cmd/edit.go @@ -52,7 +52,7 @@ func (c *EditCmd) Run() error { } if !exists { - sessionName, err := kak.Create(gitDirSession.Name) + sessionName, err := kak.Start(gitDirSession.Name) if err != nil { return err } diff --git a/cmd/new.go b/cmd/new.go index 37a1bab..278fe92 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -33,7 +33,7 @@ func (c *NewCmd) Run() error { } } - sessionName, err := kak.Create(c.name) + sessionName, err := kak.Start(c.name) if err != nil { return err } diff --git a/kak/create.go b/kak/start.go similarity index 92% rename from kak/create.go rename to kak/start.go index 0e8c427..cbbaaff 100644 --- a/kak/create.go +++ b/kak/start.go @@ -7,7 +7,7 @@ import ( "strings" ) -func Create(name string) (sessionName string, err error) { +func Start(name string) (sessionName string, err error) { sessionName = name if sessionName == "" { From c2148fe6a4b2c49eccfbdf99c6441c07a6a924d5 Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Wed, 15 Sep 2021 12:31:38 +0300 Subject: [PATCH 08/14] update preview cmd in scripts --- scripts/kks-buffers | 6 ++++-- scripts/kks-files | 6 ++++-- scripts/kks-mru | 4 +++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/scripts/kks-buffers b/scripts/kks-buffers index 89a5495..b99d480 100755 --- a/scripts/kks-buffers +++ b/scripts/kks-buffers @@ -4,11 +4,13 @@ # # requires: # - fzf (https://github.com/junegunn/fzf) -# - highlight (faster than bat) (https://gitlab.com/saalen/highlight) +# - highlight (faster than bat, change to your liking) (https://gitlab.com/saalen/highlight) + +preview_cmd="highlight --line-range=1-100 -t 4 --force -O ansi -" kks get %val[buflist] | grep -F "$*" | - fzf --height 100% --prompt 'buf> ' --preview 'kks cat -b {} | highlight --line-range=1-100 --force -O ansi' \ + fzf --height 100% --prompt 'buf> ' --preview "kks cat -b {} | $preview_cmd" \ --header="[c-x] delete, [c-t] new scratch" \ --bind="ctrl-x:execute-silent(kks send -b {} delete-buffer)+reload(kks get %val[buflist])" \ --bind="ctrl-t:execute-silent(kks send edit -scratch {q})+reload(kks get %val[buflist])" | diff --git a/scripts/kks-files b/scripts/kks-files index 782ee89..3e42ec8 100755 --- a/scripts/kks-files +++ b/scripts/kks-files @@ -5,10 +5,12 @@ # requires: # - fd (https://github.com/sharkdp/fd) # - fzf (https://github.com/junegunn/fzf) -# - highlight (faster than bat) (https://gitlab.com/saalen/highlight) +# - highlight (faster than bat, change to your liking) (https://gitlab.com/saalen/highlight) + +preview_cmd="highlight --line-range=1-100 -t 4 --force -O ansi" fd --type file . "$@" | - fzf --multi --height 100% --prompt 'files> ' --preview 'highlight --line-range=1-100 --force -O ansi {}' | + fzf --multi --height 100% --prompt 'files> ' --preview "$preview_cmd {}" | while read -r file; do kks edit "$file" done diff --git a/scripts/kks-mru b/scripts/kks-mru index 82e9e3f..d1141d0 100755 --- a/scripts/kks-mru +++ b/scripts/kks-mru @@ -15,5 +15,7 @@ # } # } -(fzf --height 100% --prompt 'mru> ' --preview 'highlight --line-range=1-100 --force -O ansi {}' | +preview_cmd="highlight --line-range=1-100 -t 4 --force -O ansi" + +(fzf --height 100% --prompt 'mru> ' --preview "$preview_cmd {}" | xargs -r kks edit) < ~/.cache/kak-mru From 04766f21fb26415b24a2088674414ad69787a9be Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Wed, 15 Sep 2021 12:43:59 +0300 Subject: [PATCH 09/14] move tmp reader to separate file --- kak/get.go | 42 ------------------------------------------ kak/tmp.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 42 deletions(-) create mode 100644 kak/tmp.go diff --git a/kak/get.go b/kak/get.go index d069098..b1affa8 100644 --- a/kak/get.go +++ b/kak/get.go @@ -2,11 +2,8 @@ package kak import ( "fmt" - "log" "os" "strings" - - "github.com/fsnotify/fsnotify" ) func Get(kctx *Context, query string) ([]string, error) { @@ -38,42 +35,3 @@ func Get(kctx *Context, query string) ([]string, error) { f.Close() return outStrs, nil } - -func ReadTmp(f *os.File, c chan string) { - // create a watcher - watcher, err := fsnotify.NewWatcher() - if err != nil { - log.Fatal(err) - } - defer watcher.Close() - - // add file to watch - err = watcher.Add(f.Name()) - if err != nil { - log.Fatal(err) - } - - // while we don't get the value - for { - select { - case event, ok := <-watcher.Events: - if !ok { - return - } - // if file written, read it and send to chan - if event.Op&fsnotify.Write == fsnotify.Write { - dat, err := os.ReadFile(f.Name()) - defer os.Remove(f.Name()) - if err != nil { - log.Fatal(err) - } - c <- string(dat) - } - case err, ok := <-watcher.Errors: - if !ok { - return - } - log.Println("error:", err) - } - } -} diff --git a/kak/tmp.go b/kak/tmp.go new file mode 100644 index 0000000..74dabc2 --- /dev/null +++ b/kak/tmp.go @@ -0,0 +1,47 @@ +package kak + +import ( + "log" + "os" + + "github.com/fsnotify/fsnotify" +) + +func ReadTmp(f *os.File, c chan string) { + // create a watcher + watcher, err := fsnotify.NewWatcher() + if err != nil { + log.Fatal(err) + } + defer watcher.Close() + + // add file to watch + err = watcher.Add(f.Name()) + if err != nil { + log.Fatal(err) + } + + // while we don't get the value + for { + select { + case event, ok := <-watcher.Events: + if !ok { + return + } + // if file written, read it and send to chan + if event.Op&fsnotify.Write == fsnotify.Write { + dat, err := os.ReadFile(f.Name()) + defer os.Remove(f.Name()) + if err != nil { + log.Fatal(err) + } + c <- string(dat) + } + case err, ok := <-watcher.Errors: + if !ok { + return + } + log.Println("error:", err) + } + } +} From bedd2f88223e0b2c27578532950818d84be33c77 Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Wed, 15 Sep 2021 12:44:09 +0300 Subject: [PATCH 10/14] use filepath in run --- cmd/edit.go | 2 +- kak/filepath.go | 8 ++++---- kak/run.go | 10 +++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cmd/edit.go b/cmd/edit.go index 5fcb54f..8b6c40b 100644 --- a/cmd/edit.go +++ b/cmd/edit.go @@ -79,7 +79,7 @@ func (c *EditCmd) Run() error { } } else { - if err := kak.Run(fp.Name, fp.Line, fp.Column); err != nil { + if err := kak.Run(fp); err != nil { return err } } diff --git a/kak/filepath.go b/kak/filepath.go index b7e0077..1e8b3f8 100644 --- a/kak/filepath.go +++ b/kak/filepath.go @@ -9,10 +9,10 @@ import ( ) type Filepath struct { - Name string `json:"name"` - Line int `json:"line"` - Column int `json:"column"` - Raw []string `json:"raw"` + Name string + Line int + Column int + Raw []string } func NewFilepath(args []string) (fp *Filepath, err error) { diff --git a/kak/run.go b/kak/run.go index 917ced9..48aed23 100644 --- a/kak/run.go +++ b/kak/run.go @@ -7,7 +7,7 @@ import ( "syscall" ) -func Run(file string, line int, col int) error { +func Run(fp *Filepath) error { kakBinary, err := exec.LookPath("kak") if err != nil { return err @@ -15,11 +15,11 @@ func Run(file string, line int, col int) error { kakExecArgs := []string{kakBinary} - if file != "" { - kakExecArgs = append(kakExecArgs, file) + if fp.Name != "" { + kakExecArgs = append(kakExecArgs, fp.Name) - if line != 0 { - kakExecArgs = append(kakExecArgs, fmt.Sprintf("+%d:%d", line, col)) + if fp.Line != 0 { + kakExecArgs = append(kakExecArgs, fmt.Sprintf("+%d:%d", fp.Line, fp.Column)) } } From 9ff7269ceb71a8afca898fe51f313999f20cbdc1 Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Wed, 15 Sep 2021 14:39:57 +0300 Subject: [PATCH 11/14] scripts: ensure normal mode before executing keys in kks-lines --- scripts/kks-lines | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/kks-lines b/scripts/kks-lines index 26e56f3..8a7f65d 100755 --- a/scripts/kks-lines +++ b/scripts/kks-lines @@ -9,4 +9,4 @@ kks cat | nl -ba -w4 -s' │ ' | fzf --height 100% --prompt 'lines> ' | awk '{print $1}' | - xargs -r -I % kks send execute-keys %gx + xargs -r -I {} kks send "execute-keys '{}gx'" From 086d7e796bc4eb7ab6cf389cdba208155dc0613f Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Wed, 15 Sep 2021 15:46:45 +0300 Subject: [PATCH 12/14] ensure session exists when starting new kak sessions --- kak/start.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/kak/start.go b/kak/start.go index cbbaaff..cd92f63 100644 --- a/kak/start.go +++ b/kak/start.go @@ -5,6 +5,7 @@ import ( "math/rand" "os/exec" "strings" + "time" ) func Start(name string) (sessionName string, err error) { @@ -24,9 +25,34 @@ func Start(name string) (sessionName string, err error) { return "", err } + // Ensure session exists before returning + ch := make(chan bool) + go waitForSession(ch, sessionName) + + _ = <-ch + return } +func waitForSession(ch chan bool, name string) error { +out: + for { + sessions, err := Sessions() + if err != nil { + return err + } + for _, s := range sessions { + if s.Name == name { + ch <- true + break out + + } + } + time.Sleep(time.Millisecond * 10) + } + return nil +} + func uniqName() (name string, err error) { s, err := exec.Command("kak", "-l").Output() if err != nil { From 31b3e2a8ad9a74b9bfbfe7d7d740b64df0472d55 Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Wed, 15 Sep 2021 15:48:42 +0300 Subject: [PATCH 13/14] close fsnotify watcher and cleanup tmp files immediately after reading --- kak/get.go | 11 +++++++---- kak/tmp.go | 13 ++++++++----- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/kak/get.go b/kak/get.go index b1affa8..f97eb7e 100644 --- a/kak/get.go +++ b/kak/get.go @@ -2,23 +2,24 @@ package kak import ( "fmt" + "io/ioutil" "os" "strings" ) func Get(kctx *Context, query string) ([]string, error) { // create a tmp file for kak to echo the value - f, err := os.CreateTemp("", "kks-tmp") + tmp, err := ioutil.TempFile("", "kks-tmp") if err != nil { return nil, err } // kak will output to file, so we create a chan for reading ch := make(chan string) - go ReadTmp(f, ch) + go ReadTmp(tmp, ch) // tell kak to echo the requested state - sendCmd := fmt.Sprintf("echo -quoting kakoune -to-file %s %%{ %s }", f.Name(), query) + sendCmd := fmt.Sprintf("echo -quoting kakoune -to-file %s %%{ %s }", tmp.Name(), query) if err := Send(kctx, sendCmd); err != nil { return nil, err } @@ -32,6 +33,8 @@ func Get(kctx *Context, query string) ([]string, error) { outStrs[i] = strings.Trim(val, "''") } - f.Close() + tmp.Close() + os.Remove(tmp.Name()) + return outStrs, nil } diff --git a/kak/tmp.go b/kak/tmp.go index 74dabc2..9e36709 100644 --- a/kak/tmp.go +++ b/kak/tmp.go @@ -1,13 +1,14 @@ package kak import ( + "io/ioutil" "log" "os" "github.com/fsnotify/fsnotify" ) -func ReadTmp(f *os.File, c chan string) { +func ReadTmp(tmp *os.File, c chan string) { // create a watcher watcher, err := fsnotify.NewWatcher() if err != nil { @@ -16,7 +17,7 @@ func ReadTmp(f *os.File, c chan string) { defer watcher.Close() // add file to watch - err = watcher.Add(f.Name()) + err = watcher.Add(tmp.Name()) if err != nil { log.Fatal(err) } @@ -28,14 +29,16 @@ func ReadTmp(f *os.File, c chan string) { if !ok { return } - // if file written, read it and send to chan + // if file written, read it, send to chan and close/clean if event.Op&fsnotify.Write == fsnotify.Write { - dat, err := os.ReadFile(f.Name()) - defer os.Remove(f.Name()) + dat, err := ioutil.ReadFile(tmp.Name()) if err != nil { log.Fatal(err) } c <- string(dat) + watcher.Close() + tmp.Close() + os.Remove(tmp.Name()) } case err, ok := <-watcher.Errors: if !ok { From edce0af468900bb433e7a1287602ba2b6cd48483 Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Wed, 15 Sep 2021 16:03:24 +0300 Subject: [PATCH 14/14] use tmp naming in cat --- cmd/cat.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/cat.go b/cmd/cat.go index b331a10..cdffe3f 100644 --- a/cmd/cat.go +++ b/cmd/cat.go @@ -28,15 +28,15 @@ type CatCmd struct { } func (c *CatCmd) Run() error { - f, err := os.CreateTemp("", "kks-tmp") + tmp, err := os.CreateTemp("", "kks-tmp") if err != nil { return err } ch := make(chan string) - go kak.ReadTmp(f, ch) + go kak.ReadTmp(tmp, ch) - sendCmd := fmt.Sprintf("write -force %s", f.Name()) + sendCmd := fmt.Sprintf("write -force %s", tmp.Name()) if err := kak.Send(c.kakContext, sendCmd); err != nil { return err @@ -46,8 +46,8 @@ func (c *CatCmd) Run() error { fmt.Print(output) - f.Close() - os.Remove(f.Name()) + tmp.Close() + os.Remove(tmp.Name()) return nil }