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" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
) )
@@ -121,7 +122,7 @@ func FileGenerator(filters ...string) GeneratorFunc {
} }
continue continue
} }
if dirOnly { if dirOnly {
continue 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 return results
} }
} }
+14 -2
View File
@@ -178,8 +178,20 @@ func Lookup(input string) []Suggestion {
continue continue
} }
finalCmd := g.Cmd suggested := g.Cmd
finalCmd = strings.TrimSpace(linePrefix) + " " + 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) newTokens := tokenize(finalCmd)
if len(newTokens) > 0 { if len(newTokens) > 0 {
+1
View File
@@ -62,6 +62,7 @@ func hasPrefix(s, prefix string) bool {
} }
return true return true
} }
// CI = case insensitive // CI = case insensitive
func HasPrefixCI(s, prefix string) bool { func HasPrefixCI(s, prefix string) bool {
return hasPrefix(s, prefix) return hasPrefix(s, prefix)
+7 -1
View File
@@ -1,6 +1,8 @@
package fs package fs
import ( import (
"strings"
"github.com/versenilvis/iris/commands/core" "github.com/versenilvis/iris/commands/core"
) )
@@ -8,6 +10,10 @@ func init() {
core.Register(&core.Spec{ core.Register(&core.Spec{
Name: "cd", Name: "cd",
Description: "change directory", 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{ core.Register(&core.Spec{
Name: "z", Name: "z",
Description: "jump to directory", Description: "jump to directory",
MaxArgs: 1, MaxArgs: 0,
Generator: zoxideGenerator(), Generator: zoxideGenerator(),
}) })
core.Register(&core.Spec{ core.Register(&core.Spec{
Name: "zi", Name: "zi",
Description: "jump to directory interactively", Description: "jump to directory interactively",
MaxArgs: 1, MaxArgs: 0,
Generator: zoxideGenerator(), Generator: zoxideGenerator(),
}) })
} }
func zoxideGenerator() core.GeneratorFunc { func zoxideGenerator() core.GeneratorFunc {
return func(tokens []string, prefix string, partial string) []core.Suggestion { return func(tokens []string, prefix string, partial string) []core.Suggestion {
fullQuery := strings.Join(tokens[1:], " ")
localSuggestions := core.FileGenerator("/")(tokens, prefix, partial) localSuggestions := core.FileGenerator("/")(tokens, prefix, fullQuery)
var zoxideSuggestions []core.Suggestion var zoxideSuggestions []core.Suggestion
cmd := exec.Command("zoxide", "query", "-l") cmd := exec.Command("zoxide", "query", "-l")
@@ -49,7 +49,7 @@ func zoxideGenerator() core.GeneratorFunc {
home, _ := os.UserHomeDir() home, _ := os.UserHomeDir()
if partial == "" { if fullQuery == "" {
limit := 20 limit := 20
if len(dirs) < limit { if len(dirs) < limit {
limit = len(dirs) limit = len(dirs)
@@ -62,10 +62,9 @@ func zoxideGenerator() core.GeneratorFunc {
Desc: display, Desc: display,
}) })
} }
} else if !strings.Contains(partial, "/") { } else if !strings.Contains(fullQuery, "/") {
searcher := fuzzyvn.NewPlainSearcher(dirs) searcher := fuzzyvn.NewPlainSearcher(dirs)
matches := searcher.SearchWithScores(partial, &fuzzyvn.SearchOptions{Limit: 10}) matches := searcher.SearchWithScores(fullQuery, &fuzzyvn.SearchOptions{Limit: 10})
for _, m := range matches { for _, m := range matches {
path := m.Str path := m.Str
display := strings.Replace(path, home, "~", 1) display := strings.Replace(path, home, "~", 1)