-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
9b54759
commit a63df21
Showing
4 changed files
with
75 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters