From a63df216842e7130ed53320602342ad94061ec91 Mon Sep 17 00:00:00 2001 From: Josh Medeski Date: Tue, 16 Jan 2024 20:10:26 -0600 Subject: [PATCH] feat: query zoxide as fallback on connect (#18) Adds the ability to query zoxide and use the result to generate the session if the connect command's argument is not a path or active session. --- session/path.go | 15 +++++++++++++-- zoxide/add.go | 20 ++++++++++++++++++++ zoxide/query.go | 42 ++++++++++++++++++++++++++++++++++++++++++ zoxide/zoxide.go | 15 --------------- 4 files changed, 75 insertions(+), 17 deletions(-) create mode 100644 zoxide/add.go create mode 100644 zoxide/query.go diff --git a/session/path.go b/session/path.go index cf329db..9a0772a 100644 --- a/session/path.go +++ b/session/path.go @@ -1,10 +1,13 @@ package session import ( + "fmt" + "os" "path" "github.com/joshmedeski/sesh/dir" "github.com/joshmedeski/sesh/tmux" + "github.com/joshmedeski/sesh/zoxide" ) func DeterminePath(choice string) (string, error) { @@ -16,7 +19,15 @@ func DeterminePath(choice string) (string, error) { if tmux.IsSession(fullPath) { return fullPath, nil } - // TODO: if not absolute path, get zoxide results - // TODO: get zoxide result if not path and tmux session doesn't exist + + zoxideResult, err := zoxide.Query(fullPath) + if err != nil { + fmt.Println("Couldn't query zoxide", err) + os.Exit(1) + } + if zoxideResult != nil { + return zoxideResult.Path, nil + } + return fullPath, nil } diff --git a/zoxide/add.go b/zoxide/add.go new file mode 100644 index 0000000..beb36af --- /dev/null +++ b/zoxide/add.go @@ -0,0 +1,20 @@ +package zoxide + +import ( + "fmt" + "os" + "os/exec" + "path" +) + +func Add(result string) { + if !path.IsAbs(result) { + return + } + cmd := exec.Command("zoxide", "add", result) + _, err := cmd.Output() + if err != nil { + fmt.Println("Error:", err) + os.Exit(1) + } +} diff --git a/zoxide/query.go b/zoxide/query.go new file mode 100644 index 0000000..6cda9c8 --- /dev/null +++ b/zoxide/query.go @@ -0,0 +1,42 @@ +package zoxide + +import ( + "fmt" + "os" + "strings" + + "github.com/joshmedeski/sesh/convert" +) + +func Query(dir string) (*ZoxideResult, error) { + output, err := zoxideCmd([]string{"query", "-s", dir}) + if err != nil { + return nil, nil + } + cleanOutput := strings.TrimSpace(string(output)) + list := strings.Split(cleanOutput, "\n") + listLen := len(list) + if listLen == 1 && list[0] == "" { + return nil, nil + } + results := make([]*ZoxideResult, 0, listLen) + for _, line := range list { + trimmed := strings.Trim(line, "[]") + trimmed = strings.Trim(trimmed, " ") + fields := strings.SplitN(trimmed, " ", 2) + if len(fields) != 2 { + fmt.Println("Zoxide entry has invalid number of fields (expected 2)") + os.Exit(1) + } + path := fields[1] + results = append(results, &ZoxideResult{ + Score: convert.StringToFloat(fields[0]), + Name: convert.PathToPretty(path), + Path: path, + }) + } + if len(results) == 0 { + return nil, nil + } + return results[0], nil +} diff --git a/zoxide/zoxide.go b/zoxide/zoxide.go index bb0feb5..3bf6e93 100644 --- a/zoxide/zoxide.go +++ b/zoxide/zoxide.go @@ -1,10 +1,7 @@ package zoxide import ( - "fmt" - "os" "os/exec" - "path" ) func zoxideCmd(args []string) ([]byte, error) { @@ -19,15 +16,3 @@ func zoxideCmd(args []string) ([]byte, error) { } return output, nil } - -func Add(result string) { - if !path.IsAbs(result) { - return - } - cmd := exec.Command("zoxide", "add", result) - _, err := cmd.Output() - if err != nil { - fmt.Println("Error:", err) - os.Exit(1) - } -}