Skip to content

Commit

Permalink
feat: query zoxide as fallback on connect (#18)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joshmedeski authored Jan 17, 2024
1 parent 9b54759 commit a63df21
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 17 deletions.
15 changes: 13 additions & 2 deletions session/path.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
}
20 changes: 20 additions & 0 deletions zoxide/add.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
42 changes: 42 additions & 0 deletions zoxide/query.go
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 0 additions & 15 deletions zoxide/zoxide.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package zoxide

import (
"fmt"
"os"
"os/exec"
"path"
)

func zoxideCmd(args []string) ([]byte, error) {
Expand All @@ -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)
}
}

0 comments on commit a63df21

Please sign in to comment.