File tree Expand file tree Collapse file tree 4 files changed +75
-17
lines changed
Expand file tree Collapse file tree 4 files changed +75
-17
lines changed Original file line number Diff line number Diff line change 11package session
22
33import (
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
1013func 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11package zoxide
22
33import (
4- "fmt"
5- "os"
64 "os/exec"
7- "path"
85)
96
107func 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- }
You can’t perform that action at this time.
0 commit comments