fix(cd): harden zoxide directory suggestions

This commit is contained in:
2026-08-14 13:11:21 +03:00
parent 0b2221d5e9
commit 11fad73ef5
12 changed files with 436 additions and 136 deletions
+22 -6
View File
@@ -2,6 +2,7 @@ package spec
import (
"maps"
"path/filepath"
"slices"
"strings"
"sync"
@@ -274,15 +275,15 @@ func Lookup(input string) []Suggestion {
}
suggested := g.Cmd
if strings.Contains(suggested, " ") && !strings.HasPrefix(suggested, "\"") {
suggested = "\"" + suggested + "\""
if filepath.IsAbs(g.Cmd) || strings.ContainsAny(g.Cmd, " \t\r\n") {
suggested = quoteSuggestionArgument(g.Cmd)
}
// if the suggestion is a full path that includes
// words already in the command line (multi-word support), we replace
// the entire argument part by using prefix only
// Absolute results replace all typed arguments, avoiding duplication
// for multi-word directory matches.
finalCmd := ""
if len(tokens) > depth+1 && strings.HasPrefix(g.Cmd, tokens[depth]) {
replaceArguments := strings.HasPrefix(g.Cmd, tokens[depth]) || filepath.IsAbs(g.Cmd)
if len(tokens) > depth+1 && replaceArguments {
finalCmd = prefix + " " + suggested
} else if genLinePrefix != "" {
finalCmd = genLinePrefix + " " + suggested
@@ -382,6 +383,21 @@ func Lookup(input string) []Suggestion {
return results
}
func quoteSuggestionArgument(argument string) string {
prefix := ""
value := argument
if strings.HasPrefix(value, "~/") {
prefix = "~/"
value = strings.TrimPrefix(value, prefix)
}
if !strings.ContainsAny(value, " \t\r\n'\"\\$`!;&|<>()*?[]{}#^") {
return argument
}
escaped := strings.ReplaceAll(value, "'", `'\''`)
return prefix + "'" + escaped + "'"
}
func topLevelSuggestions(query string, aliases map[string]string) []Suggestion {
scanExternalCommands()
results, seen := []Suggestion{}, make(map[string]bool)