Skip to content

Commit a63df21

Browse files
authored
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.
1 parent 9b54759 commit a63df21

File tree

4 files changed

+75
-17
lines changed

4 files changed

+75
-17
lines changed

session/path.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package session
22

33
import (
4+
"fmt"
5+
"os"
46
"path"
57

68
"github.com/joshmedeski/sesh/dir"
79
"github.com/joshmedeski/sesh/tmux"
10+
"github.com/joshmedeski/sesh/zoxide"
811
)
912

1013
func DeterminePath(choice string) (string, error) {
@@ -16,7 +19,15 @@ func DeterminePath(choice string) (string, error) {
1619
if tmux.IsSession(fullPath) {
1720
return fullPath, nil
1821
}
19-
// TODO: if not absolute path, get zoxide results
20-
// TODO: get zoxide result if not path and tmux session doesn't exist
22+
23+
zoxideResult, err := zoxide.Query(fullPath)
24+
if err != nil {
25+
fmt.Println("Couldn't query zoxide", err)
26+
os.Exit(1)
27+
}
28+
if zoxideResult != nil {
29+
return zoxideResult.Path, nil
30+
}
31+
2132
return fullPath, nil
2233
}

zoxide/add.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package zoxide
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"path"
8+
)
9+
10+
func Add(result string) {
11+
if !path.IsAbs(result) {
12+
return
13+
}
14+
cmd := exec.Command("zoxide", "add", result)
15+
_, err := cmd.Output()
16+
if err != nil {
17+
fmt.Println("Error:", err)
18+
os.Exit(1)
19+
}
20+
}

zoxide/query.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package zoxide
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
8+
"github.com/joshmedeski/sesh/convert"
9+
)
10+
11+
func Query(dir string) (*ZoxideResult, error) {
12+
output, err := zoxideCmd([]string{"query", "-s", dir})
13+
if err != nil {
14+
return nil, nil
15+
}
16+
cleanOutput := strings.TrimSpace(string(output))
17+
list := strings.Split(cleanOutput, "\n")
18+
listLen := len(list)
19+
if listLen == 1 && list[0] == "" {
20+
return nil, nil
21+
}
22+
results := make([]*ZoxideResult, 0, listLen)
23+
for _, line := range list {
24+
trimmed := strings.Trim(line, "[]")
25+
trimmed = strings.Trim(trimmed, " ")
26+
fields := strings.SplitN(trimmed, " ", 2)
27+
if len(fields) != 2 {
28+
fmt.Println("Zoxide entry has invalid number of fields (expected 2)")
29+
os.Exit(1)
30+
}
31+
path := fields[1]
32+
results = append(results, &ZoxideResult{
33+
Score: convert.StringToFloat(fields[0]),
34+
Name: convert.PathToPretty(path),
35+
Path: path,
36+
})
37+
}
38+
if len(results) == 0 {
39+
return nil, nil
40+
}
41+
return results[0], nil
42+
}

zoxide/zoxide.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package zoxide
22

33
import (
4-
"fmt"
5-
"os"
64
"os/exec"
7-
"path"
85
)
96

107
func zoxideCmd(args []string) ([]byte, error) {
@@ -19,15 +16,3 @@ func zoxideCmd(args []string) ([]byte, error) {
1916
}
2017
return output, nil
2118
}
22-
23-
func Add(result string) {
24-
if !path.IsAbs(result) {
25-
return
26-
}
27-
cmd := exec.Command("zoxide", "add", result)
28-
_, err := cmd.Output()
29-
if err != nil {
30-
fmt.Println("Error:", err)
31-
os.Exit(1)
32-
}
33-
}

0 commit comments

Comments
 (0)