Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various code optimizations, ensure session exist when starting new daemons #2

Merged
merged 14 commits into from
Sep 15, 2021
Merged
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 4 additions & 3 deletions cmd/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ func NewAttachCmd() *AttachCmd {
c := &AttachCmd{Cmd: Cmd{
fs: flag.NewFlagSet("attach", flag.ExitOnError),
alias: []string{"a"},
usageStr: "[options] [file] [+<line>[:<col]]",
shortDesc: "Attach to Kakoune session with a new client.",
usageLine: "[options] [file] [+<line>[:<col]]",
sessionReq: true,
}}
c.fs.StringVar(&c.session, "s", "", "session")
Expand All @@ -22,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(fp.Name, fp.Line, fp.Column, c.session); err != nil {
if err := kak.Connect(c.kakContext, fp); err != nil {
return err
}

Expand Down
16 changes: 9 additions & 7 deletions cmd/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ func NewCatCmd() *CatCmd {
c := &CatCmd{Cmd: Cmd{
fs: flag.NewFlagSet("cat", flag.ExitOnError),
alias: []string{""},
usageStr: "[options]",
shortDesc: "Print contents of a buffer to stdout.",
usageLine: "[options]",
sessionReq: true,
clientReq: true,
}}
Expand All @@ -27,25 +28,26 @@ 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())
if err := kak.Send(sendCmd, c.buffer, c.session, c.client); err != nil {
sendCmd := fmt.Sprintf("write -force %s", tmp.Name())

if err := kak.Send(c.kakContext, sendCmd); err != nil {
return err
}

output := <-ch

fmt.Print(output)

f.Close()
os.Remove(f.Name())
tmp.Close()
os.Remove(tmp.Name())

return nil
}
35 changes: 27 additions & 8 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"os"
"strings"

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

type Runner interface {
Expand All @@ -16,9 +18,10 @@ type Runner interface {
}

type Cmd struct {
fs *flag.FlagSet
alias []string
usageStr string
fs *flag.FlagSet
alias []string
shortDesc string
usageLine string

session string
client string
Expand All @@ -27,6 +30,8 @@ type Cmd struct {
sessionReq bool
clientReq bool
bufferReq bool

kakContext *kak.Context
}

type EnvContext struct {
Expand All @@ -45,26 +50,40 @@ 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
}

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")
}

return nil
}

func (c *Cmd) usage() {
fmt.Printf("usage: kks %s %s\n\n", c.fs.Name(), c.usageStr)
fmt.Println(c.shortDesc)
fmt.Println()

fmt.Println("USAGE")
fmt.Printf(" kks %s %s\n\n", c.fs.Name(), c.usageLine)

if strings.Contains(c.usageStr, "[options]") {
if strings.Contains(c.usageLine, "[options]") {
fmt.Println("OPTIONS")
c.fs.PrintDefaults()
}
Expand Down
60 changes: 35 additions & 25 deletions cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (

func NewEditCmd() *EditCmd {
c := &EditCmd{Cmd: Cmd{
fs: flag.NewFlagSet("edit", flag.ExitOnError),
alias: []string{"e"},
usageStr: "[options] [file] [+<line>[:<col>]]",
fs: flag.NewFlagSet("edit", flag.ExitOnError),
alias: []string{"e"},
shortDesc: "Edit file. In session and client, if set.",
usageLine: "[options] [file] [+<line>[:<col>]]",
}}
// TODO add flag that allows creating new files (removes -existing)
c.fs.StringVar(&c.session, "s", "", "session")
Expand All @@ -28,12 +29,13 @@ 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
}

switch c.session {
switch c.kakContext.Session.Name {

case "":
var gitDirName string
_, useGitDirSessions := os.LookupEnv("KKS_USE_GITDIR_SESSIONS")
Expand All @@ -43,33 +45,51 @@ 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.Start(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 {

kctx := &kak.Context{Session: gitDirSession}

if err := kak.Connect(kctx, fp); 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 {
kctx := &kak.Context{Session: defaultSession}
if err := kak.Connect(kctx, fp); err != nil {
return err
}

} else {
if err := kak.Run(fp.Name, fp.Line, fp.Column); err != nil {
if err := kak.Run(fp); err != nil {
return err
}
}
}

default:
switch c.client {
switch c.kakContext.Client.Name {
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(c.kakContext, fp); err != nil {
return err
}
default:
Expand All @@ -83,7 +103,7 @@ 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(c.kakContext, sb.String()); err != nil {
return err
}
}
Expand All @@ -99,13 +119,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
}
3 changes: 2 additions & 1 deletion cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 3 additions & 2 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,7 +36,7 @@ func (c *GetCmd) Run() error {
return err
}

resp, err := kak.Get(query, c.buffer, c.session, c.client)
resp, err := kak.Get(c.kakContext, query)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
22 changes: 14 additions & 8 deletions cmd/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -23,21 +24,26 @@ 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(c.kakContext, 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 {
sessCtx := &kak.Context{
Session: s,
Client: c.kakContext.Client,
Buffer: c.kakContext.Buffer,
}
if err := kak.Send(sessCtx, sendCmd); err != nil {
return err
}
}
Expand Down
Loading