fix(zoxide/cd): support folder contains space

This commit is contained in:
verse91
2026-05-09 22:54:27 +07:00
parent 5f4a11edf0
commit 3c5a0580d1
5 changed files with 35 additions and 12 deletions
+6 -1
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
)
@@ -121,7 +122,7 @@ func FileGenerator(filters ...string) GeneratorFunc {
}
continue
}
if dirOnly {
continue
}
@@ -139,6 +140,10 @@ func FileGenerator(filters ...string) GeneratorFunc {
})
}
sort.Slice(results, func(i, j int) bool {
return strings.ToLower(results[i].Cmd) < strings.ToLower(results[j].Cmd)
})
return results
}
}
+14 -2
View File
@@ -178,8 +178,20 @@ func Lookup(input string) []Suggestion {
continue
}
finalCmd := g.Cmd
finalCmd = strings.TrimSpace(linePrefix) + " " + g.Cmd
suggested := g.Cmd
if strings.Contains(suggested, " ") && !strings.HasPrefix(suggested, "\"") {
suggested = "\"" + suggested + "\""
}
// 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
finalCmd := ""
if len(tokens) > depth+1 && strings.HasPrefix(g.Cmd, tokens[depth]) {
finalCmd = prefix + " " + suggested
} else {
finalCmd = strings.TrimSpace(linePrefix) + " " + suggested
}
newTokens := tokenize(finalCmd)
if len(newTokens) > 0 {
+1
View File
@@ -62,6 +62,7 @@ func hasPrefix(s, prefix string) bool {
}
return true
}
// CI = case insensitive
func HasPrefixCI(s, prefix string) bool {
return hasPrefix(s, prefix)
+7 -1
View File
@@ -1,6 +1,8 @@
package fs
import (
"strings"
"github.com/versenilvis/iris/commands/core"
)
@@ -8,6 +10,10 @@ func init() {
core.Register(&core.Spec{
Name: "cd",
Description: "change directory",
Generator: core.FileGenerator("/"), // Directory only
MaxArgs: 0,
Generator: func(tokens []string, prefix string, partial string) []core.Suggestion {
fullQuery := strings.Join(tokens[1:], " ")
return core.FileGenerator("/")(tokens, prefix, fullQuery)
},
})
}
+7 -8
View File
@@ -18,21 +18,21 @@ func init() {
core.Register(&core.Spec{
Name: "z",
Description: "jump to directory",
MaxArgs: 1,
MaxArgs: 0,
Generator: zoxideGenerator(),
})
core.Register(&core.Spec{
Name: "zi",
Description: "jump to directory interactively",
MaxArgs: 1,
MaxArgs: 0,
Generator: zoxideGenerator(),
})
}
func zoxideGenerator() core.GeneratorFunc {
return func(tokens []string, prefix string, partial string) []core.Suggestion {
localSuggestions := core.FileGenerator("/")(tokens, prefix, partial)
fullQuery := strings.Join(tokens[1:], " ")
localSuggestions := core.FileGenerator("/")(tokens, prefix, fullQuery)
var zoxideSuggestions []core.Suggestion
cmd := exec.Command("zoxide", "query", "-l")
@@ -49,7 +49,7 @@ func zoxideGenerator() core.GeneratorFunc {
home, _ := os.UserHomeDir()
if partial == "" {
if fullQuery == "" {
limit := 20
if len(dirs) < limit {
limit = len(dirs)
@@ -62,10 +62,9 @@ func zoxideGenerator() core.GeneratorFunc {
Desc: display,
})
}
} else if !strings.Contains(partial, "/") {
} else if !strings.Contains(fullQuery, "/") {
searcher := fuzzyvn.NewPlainSearcher(dirs)
matches := searcher.SearchWithScores(partial, &fuzzyvn.SearchOptions{Limit: 10})
matches := searcher.SearchWithScores(fullQuery, &fuzzyvn.SearchOptions{Limit: 10})
for _, m := range matches {
path := m.Str
display := strings.Replace(path, home, "~", 1)