Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1babd32f32
|
||
|
|
669b73dbda
|
||
|
|
11fad73ef5 | ||
|
|
0b2221d5e9
|
@@ -324,6 +324,9 @@ navigate-right = "right" # accept ghost text
|
|||||||
filter-active-branch = true # exclude current branch from suggestions
|
filter-active-branch = true # exclude current branch from suggestions
|
||||||
deduplicate-branches = true # merge same-name local/remote branches
|
deduplicate-branches = true # merge same-name local/remote branches
|
||||||
|
|
||||||
|
[zoxide]
|
||||||
|
extend-cd = false # include zoxide's frecent directories in cd suggestions
|
||||||
|
|
||||||
[history]
|
[history]
|
||||||
scope = "global" # "global", "directory", "workspace", or "hybrid"
|
scope = "global" # "global", "directory", "workspace", or "hybrid"
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package fs
|
|||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/versenilvis/iris/internal/config"
|
||||||
"github.com/versenilvis/iris/spec"
|
"github.com/versenilvis/iris/spec"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -12,6 +13,11 @@ func init() {
|
|||||||
Description: "change directory",
|
Description: "change directory",
|
||||||
MaxArgs: 0,
|
MaxArgs: 0,
|
||||||
Generator: func(tokens []string, prefix string, partial string) []spec.Suggestion {
|
Generator: func(tokens []string, prefix string, partial string) []spec.Suggestion {
|
||||||
|
// Extend cd with zoxide's frecency database while keeping local
|
||||||
|
// filesystem suggestions first.
|
||||||
|
if config.Get().Zoxide.ExtendCd {
|
||||||
|
return ZoxideGenerator()(tokens, prefix, partial)
|
||||||
|
}
|
||||||
fullQuery := strings.Join(tokens[1:], " ")
|
fullQuery := strings.Join(tokens[1:], " ")
|
||||||
return spec.FileGenerator("/")(tokens, prefix, fullQuery)
|
return spec.FileGenerator("/")(tokens, prefix, fullQuery)
|
||||||
},
|
},
|
||||||
|
|||||||
+188
-55
@@ -1,20 +1,36 @@
|
|||||||
// please note that zoxide also shows external suggestions
|
|
||||||
// at the end of the list on command mode
|
|
||||||
// they are the old directories that you have visited
|
|
||||||
// this is a feature, not a bug, and I want to keep it
|
|
||||||
package fs
|
package fs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/versenilvis/fuzzy"
|
"github.com/versenilvis/fuzzy"
|
||||||
"github.com/versenilvis/iris/spec"
|
"github.com/versenilvis/iris/spec"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
zoxideCacheTTL = 2 * time.Second
|
||||||
|
zoxideQueryTimeout = 500 * time.Millisecond
|
||||||
|
zoxideEmptyLimit = 20
|
||||||
|
zoxideSearchLimit = 10
|
||||||
|
)
|
||||||
|
|
||||||
|
type zoxideQueryFunc func(context.Context) ([]byte, error)
|
||||||
|
|
||||||
|
type zoxideSource struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
query zoxideQueryFunc
|
||||||
|
now func() time.Time
|
||||||
|
dirs []string
|
||||||
|
err error
|
||||||
|
fetched time.Time
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
spec.Register(&spec.Spec{
|
spec.Register(&spec.Spec{
|
||||||
Name: "z",
|
Name: "z",
|
||||||
@@ -30,62 +46,179 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newZoxideSource(query zoxideQueryFunc) *zoxideSource {
|
||||||
|
return &zoxideSource{query: query, now: time.Now}
|
||||||
|
}
|
||||||
|
|
||||||
|
var defaultZoxideSource = newZoxideSource(func(ctx context.Context) ([]byte, error) {
|
||||||
|
return exec.CommandContext(ctx, "zoxide", "query", "-l").Output()
|
||||||
|
})
|
||||||
|
|
||||||
|
// directories caches failures too, preventing a missing zoxide binary from
|
||||||
|
// spawning a failed process on every keystroke.
|
||||||
|
func (s *zoxideSource) directories() ([]string, error) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
|
now := s.now()
|
||||||
|
if !s.fetched.IsZero() && now.Before(s.fetched.Add(zoxideCacheTTL)) {
|
||||||
|
return append([]string(nil), s.dirs...), s.err
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), zoxideQueryTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
out, err := s.query(ctx)
|
||||||
|
s.fetched = s.now()
|
||||||
|
s.err = err
|
||||||
|
s.dirs = nil
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for line := range strings.SplitSeq(string(out), "\n") {
|
||||||
|
line = strings.TrimSuffix(line, "\r")
|
||||||
|
if line != "" {
|
||||||
|
s.dirs = append(s.dirs, line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return append([]string(nil), s.dirs...), nil
|
||||||
|
}
|
||||||
|
|
||||||
func ZoxideGenerator() spec.GeneratorFunc {
|
func ZoxideGenerator() spec.GeneratorFunc {
|
||||||
|
return newZoxideGenerator(defaultZoxideSource.directories)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newZoxideGenerator(directorySource func() ([]string, error)) spec.GeneratorFunc {
|
||||||
return func(tokens []string, prefix string, partial string) []spec.Suggestion {
|
return func(tokens []string, prefix string, partial string) []spec.Suggestion {
|
||||||
fullQuery := strings.Join(tokens[1:], " ")
|
fullQuery := strings.Join(tokens[1:], " ")
|
||||||
localSuggestions := spec.FileGenerator("/")(tokens, prefix, fullQuery)
|
local := spec.FileGenerator("/")(tokens, prefix, fullQuery)
|
||||||
|
if strings.ContainsAny(fullQuery, `/\`) {
|
||||||
var zoxideSuggestions []spec.Suggestion
|
return local
|
||||||
cmd := exec.CommandContext(context.Background(), "zoxide", "query", "-l")
|
|
||||||
out, err := cmd.Output()
|
|
||||||
if err == nil {
|
|
||||||
lines := strings.Split(string(bytes.TrimSpace(out)), "\n")
|
|
||||||
var dirs []string
|
|
||||||
for _, line := range lines {
|
|
||||||
line = strings.TrimSpace(line)
|
|
||||||
if line != "" {
|
|
||||||
dirs = append(dirs, line)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
home, _ := os.UserHomeDir()
|
|
||||||
|
|
||||||
if fullQuery == "" {
|
|
||||||
limit := min(len(dirs), 20)
|
|
||||||
for i := range limit {
|
|
||||||
path := dirs[i]
|
|
||||||
display := strings.Replace(path, home, "~", 1)
|
|
||||||
zoxideSuggestions = append(zoxideSuggestions, spec.Suggestion{
|
|
||||||
Cmd: path,
|
|
||||||
Desc: display,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
} else if !strings.Contains(fullQuery, "/") {
|
|
||||||
searcher := fuzzy.NewPlainSearcher(dirs)
|
|
||||||
matches := searcher.SearchWithScores(fullQuery, &fuzzy.SearchOptions{Limit: 10})
|
|
||||||
for _, m := range matches {
|
|
||||||
path := m.Str
|
|
||||||
display := strings.Replace(path, home, "~", 1)
|
|
||||||
zoxideSuggestions = append(zoxideSuggestions, spec.Suggestion{
|
|
||||||
Cmd: path,
|
|
||||||
Desc: display,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var finalResults []spec.Suggestion
|
dirs, err := directorySource()
|
||||||
seen := make(map[string]bool)
|
if err != nil {
|
||||||
|
return local
|
||||||
finalResults = append(finalResults, localSuggestions...)
|
|
||||||
|
|
||||||
for _, s := range zoxideSuggestions {
|
|
||||||
if !seen[s.Cmd] {
|
|
||||||
finalResults = append(finalResults, s)
|
|
||||||
seen[s.Cmd] = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return finalResults
|
external := zoxideSuggestions(dirs, fullQuery)
|
||||||
|
return mergeDirectorySuggestions(local, external, spec.GetCWD())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func zoxideSuggestions(dirs []string, query string) []spec.Suggestion {
|
||||||
|
candidates := dirs
|
||||||
|
limit := zoxideEmptyLimit
|
||||||
|
if query != "" {
|
||||||
|
candidates = matchedZoxideDirectories(dirs, query)
|
||||||
|
limit = zoxideSearchLimit
|
||||||
|
}
|
||||||
|
|
||||||
|
home, _ := os.UserHomeDir()
|
||||||
|
results := make([]spec.Suggestion, 0, min(limit, len(candidates)))
|
||||||
|
seen := make(map[string]struct{}, len(candidates))
|
||||||
|
for _, candidate := range candidates {
|
||||||
|
path := filepath.Clean(candidate)
|
||||||
|
if !filepath.IsAbs(path) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, ok := seen[path]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[path] = struct{}{}
|
||||||
|
|
||||||
|
info, err := os.Stat(path)
|
||||||
|
if err != nil || !info.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
results = append(results, spec.Suggestion{
|
||||||
|
Cmd: path,
|
||||||
|
Desc: displayDirectory(path, home),
|
||||||
|
Priority: 40,
|
||||||
|
})
|
||||||
|
if len(results) == limit {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
func matchedZoxideDirectories(dirs []string, query string) []string {
|
||||||
|
if len(dirs) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
results := make([]string, 0, len(dirs))
|
||||||
|
seen := make(map[string]struct{}, len(dirs))
|
||||||
|
lowerQuery := strings.ToLower(query)
|
||||||
|
|
||||||
|
for _, dir := range dirs {
|
||||||
|
if strings.Contains(strings.ToLower(dir), lowerQuery) {
|
||||||
|
results = append(results, dir)
|
||||||
|
seen[dir] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
matches := fuzzy.NewSearcher(dirs).SearchWithScores(query, &fuzzy.SearchOptions{Limit: len(dirs)})
|
||||||
|
for _, match := range matches {
|
||||||
|
if _, ok := seen[match.Str]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
results = append(results, match.Str)
|
||||||
|
seen[match.Str] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
func mergeDirectorySuggestions(local, external []spec.Suggestion, cwd string) []spec.Suggestion {
|
||||||
|
results := make([]spec.Suggestion, 0, len(local)+len(external))
|
||||||
|
seen := make(map[string]struct{}, len(local)+len(external))
|
||||||
|
|
||||||
|
add := func(suggestion spec.Suggestion) {
|
||||||
|
key := directoryKey(suggestion.Cmd, cwd)
|
||||||
|
if _, ok := seen[key]; ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
seen[key] = struct{}{}
|
||||||
|
results = append(results, suggestion)
|
||||||
|
}
|
||||||
|
for _, suggestion := range local {
|
||||||
|
add(suggestion)
|
||||||
|
}
|
||||||
|
for _, suggestion := range external {
|
||||||
|
add(suggestion)
|
||||||
|
}
|
||||||
|
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
func directoryKey(path, cwd string) string {
|
||||||
|
if path == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if !filepath.IsAbs(path) {
|
||||||
|
path = filepath.Join(cwd, path)
|
||||||
|
}
|
||||||
|
return filepath.Clean(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func displayDirectory(path, home string) string {
|
||||||
|
path = filepath.Clean(path)
|
||||||
|
if home == "" {
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
|
||||||
|
rel, err := filepath.Rel(filepath.Clean(home), path)
|
||||||
|
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
if rel == "." {
|
||||||
|
return "~"
|
||||||
|
}
|
||||||
|
return filepath.Join("~", rel)
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,176 @@
|
|||||||
|
package fs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"slices"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/versenilvis/iris/spec"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestZoxideSourceCachesDirectoriesAndErrors(t *testing.T) {
|
||||||
|
now := time.Date(2026, time.August, 14, 12, 0, 0, 0, time.UTC)
|
||||||
|
calls := 0
|
||||||
|
source := newZoxideSource(func(context.Context) ([]byte, error) {
|
||||||
|
calls++
|
||||||
|
return []byte("/tmp/Project One \n/tmp/Проект\r\n"), nil
|
||||||
|
})
|
||||||
|
source.now = func() time.Time { return now }
|
||||||
|
|
||||||
|
want := []string{"/tmp/Project One ", "/tmp/Проект"}
|
||||||
|
for range 2 {
|
||||||
|
got, err := source.directories()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("directories returned an error: %v", err)
|
||||||
|
}
|
||||||
|
if !slices.Equal(got, want) {
|
||||||
|
t.Fatalf("directories = %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if calls != 1 {
|
||||||
|
t.Fatalf("query calls = %d, want 1", calls)
|
||||||
|
}
|
||||||
|
|
||||||
|
now = now.Add(zoxideCacheTTL)
|
||||||
|
if _, err := source.directories(); err != nil {
|
||||||
|
t.Fatalf("directories after cache expiry returned an error: %v", err)
|
||||||
|
}
|
||||||
|
if calls != 2 {
|
||||||
|
t.Fatalf("query calls after cache expiry = %d, want 2", calls)
|
||||||
|
}
|
||||||
|
|
||||||
|
wantErr := errors.New("zoxide unavailable")
|
||||||
|
errorCalls := 0
|
||||||
|
errorSource := newZoxideSource(func(context.Context) ([]byte, error) {
|
||||||
|
errorCalls++
|
||||||
|
return nil, wantErr
|
||||||
|
})
|
||||||
|
if _, err := errorSource.directories(); !errors.Is(err, wantErr) {
|
||||||
|
t.Fatalf("directories error = %v, want %v", err, wantErr)
|
||||||
|
}
|
||||||
|
if _, err := errorSource.directories(); !errors.Is(err, wantErr) {
|
||||||
|
t.Fatalf("cached directories error = %v, want %v", err, wantErr)
|
||||||
|
}
|
||||||
|
if errorCalls != 1 {
|
||||||
|
t.Fatalf("failed query calls = %d, want 1", errorCalls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestZoxideGeneratorMergesValidDirectories(t *testing.T) {
|
||||||
|
cwd := t.TempDir()
|
||||||
|
local := filepath.Join(cwd, "project")
|
||||||
|
external := filepath.Join(t.TempDir(), "My Проект")
|
||||||
|
missing := filepath.Join(t.TempDir(), "deleted")
|
||||||
|
mustMkdirAll(t, local)
|
||||||
|
mustMkdirAll(t, external)
|
||||||
|
|
||||||
|
previousCWD := spec.GetCWD()
|
||||||
|
spec.SetCWD(cwd)
|
||||||
|
t.Cleanup(func() { spec.SetCWD(previousCWD) })
|
||||||
|
|
||||||
|
gen := newZoxideGenerator(func() ([]string, error) {
|
||||||
|
return []string{local, external, external, missing}, nil
|
||||||
|
})
|
||||||
|
results := gen([]string{"z", ""}, "z ", "")
|
||||||
|
|
||||||
|
if len(results) != 2 {
|
||||||
|
t.Fatalf("suggestion count = %d, want 2: %#v", len(results), results)
|
||||||
|
}
|
||||||
|
if results[0].Cmd != "project/" {
|
||||||
|
t.Fatalf("first suggestion = %q, want local project/", results[0].Cmd)
|
||||||
|
}
|
||||||
|
if results[1].Cmd != external {
|
||||||
|
t.Fatalf("second suggestion = %q, want %q", results[1].Cmd, external)
|
||||||
|
}
|
||||||
|
if results[1].Priority != 40 {
|
||||||
|
t.Fatalf("external priority = %d, want 40", results[1].Priority)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestZoxideGeneratorFiltersAndFallsBack(t *testing.T) {
|
||||||
|
cwd := t.TempDir()
|
||||||
|
nested := filepath.Join(cwd, "nested", "child")
|
||||||
|
mustMkdirAll(t, nested)
|
||||||
|
|
||||||
|
previousCWD := spec.GetCWD()
|
||||||
|
spec.SetCWD(cwd)
|
||||||
|
t.Cleanup(func() { spec.SetCWD(previousCWD) })
|
||||||
|
|
||||||
|
calls := 0
|
||||||
|
gen := newZoxideGenerator(func() ([]string, error) {
|
||||||
|
calls++
|
||||||
|
return nil, errors.New("zoxide unavailable")
|
||||||
|
})
|
||||||
|
|
||||||
|
results := gen([]string{"z", "nested/ch"}, "z ", "nested/ch")
|
||||||
|
if calls != 0 {
|
||||||
|
t.Fatalf("source calls for explicit path = %d, want 0", calls)
|
||||||
|
}
|
||||||
|
if len(results) != 1 || results[0].Cmd != "nested/child/" {
|
||||||
|
t.Fatalf("explicit path suggestions = %#v", results)
|
||||||
|
}
|
||||||
|
|
||||||
|
results = gen([]string{"z", "n"}, "z ", "n")
|
||||||
|
if calls != 1 {
|
||||||
|
t.Fatalf("source calls after name query = %d, want 1", calls)
|
||||||
|
}
|
||||||
|
if len(results) != 1 || results[0].Cmd != "nested/" {
|
||||||
|
t.Fatalf("fallback suggestions = %#v", results)
|
||||||
|
}
|
||||||
|
|
||||||
|
empty := newZoxideGenerator(func() ([]string, error) { return nil, nil })
|
||||||
|
if results := empty([]string{"z", "missing"}, "z ", "missing"); len(results) != 0 {
|
||||||
|
t.Fatalf("empty database suggestions = %#v, want none", results)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestZoxideGeneratorFuzzyMatchesUnicodeAndLookupQuotesSpaces(t *testing.T) {
|
||||||
|
cwd := t.TempDir()
|
||||||
|
target := filepath.Join(t.TempDir(), "My Awesome $Проект")
|
||||||
|
mustMkdirAll(t, target)
|
||||||
|
|
||||||
|
previousCWD := spec.GetCWD()
|
||||||
|
spec.SetCWD(cwd)
|
||||||
|
t.Cleanup(func() { spec.SetCWD(previousCWD) })
|
||||||
|
|
||||||
|
const command = "zoxide-test-command"
|
||||||
|
spec.Register(&spec.Spec{
|
||||||
|
Name: command,
|
||||||
|
MaxArgs: 0,
|
||||||
|
Generator: newZoxideGenerator(func() ([]string, error) { return []string{target}, nil }),
|
||||||
|
})
|
||||||
|
t.Cleanup(func() { delete(spec.Registry, command) })
|
||||||
|
|
||||||
|
results := spec.Lookup(command + " My Awe")
|
||||||
|
want := command + ` '` + target + `'`
|
||||||
|
if len(results) != 1 || results[0].Cmd != want {
|
||||||
|
t.Fatalf("lookup suggestions = %#v, want command %q", results, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDisplayDirectoryOnlyShortensHomePrefix(t *testing.T) {
|
||||||
|
home := filepath.Join(string(os.PathSeparator), "Users", "example")
|
||||||
|
inside := filepath.Join(home, "Developer", "Проект")
|
||||||
|
outside := filepath.Join(string(os.PathSeparator), "tmp", "Users", "example", "data")
|
||||||
|
|
||||||
|
if got := displayDirectory(home, home); got != "~" {
|
||||||
|
t.Fatalf("displayDirectory(home) = %q, want ~", got)
|
||||||
|
}
|
||||||
|
if got := displayDirectory(inside, home); got != filepath.Join("~", "Developer", "Проект") {
|
||||||
|
t.Fatalf("displayDirectory(inside) = %q", got)
|
||||||
|
}
|
||||||
|
if got := displayDirectory(outside, home); got != outside {
|
||||||
|
t.Fatalf("displayDirectory(outside) = %q, want %q", got, outside)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustMkdirAll(t *testing.T, path string) {
|
||||||
|
t.Helper()
|
||||||
|
if err := os.MkdirAll(path, 0o755); err != nil {
|
||||||
|
t.Fatalf("create directory %q: %v", path, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -78,6 +78,11 @@ type KeybindingsConfig struct {
|
|||||||
NavigateRight string `toml:"navigate-right"`
|
NavigateRight string `toml:"navigate-right"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ZoxideConfig struct {
|
||||||
|
// ExtendCd adds zoxide's frecency database to cd completions.
|
||||||
|
ExtendCd bool `toml:"extend-cd"`
|
||||||
|
}
|
||||||
|
|
||||||
type HistoryConfig struct {
|
type HistoryConfig struct {
|
||||||
// Scope controls Atuin filtering for non-empty history searches.
|
// Scope controls Atuin filtering for non-empty history searches.
|
||||||
Scope string `toml:"scope"`
|
Scope string `toml:"scope"`
|
||||||
@@ -133,6 +138,7 @@ type Config struct {
|
|||||||
History HistoryConfig `toml:"history"`
|
History HistoryConfig `toml:"history"`
|
||||||
Updater UpdaterConfig `toml:"updater"`
|
Updater UpdaterConfig `toml:"updater"`
|
||||||
AI AIConfig `toml:"ai"`
|
AI AIConfig `toml:"ai"`
|
||||||
|
Zoxide ZoxideConfig `toml:"zoxide"`
|
||||||
Keybindings KeybindingsConfig `toml:"keybindings"`
|
Keybindings KeybindingsConfig `toml:"keybindings"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,9 @@ func TestDefaultConfigAndState(t *testing.T) {
|
|||||||
if !cfg.Core.CobraProbeEnabled {
|
if !cfg.Core.CobraProbeEnabled {
|
||||||
t.Errorf("expected cobra probing to be enabled by default")
|
t.Errorf("expected cobra probing to be enabled by default")
|
||||||
}
|
}
|
||||||
|
if cfg.Zoxide.ExtendCd {
|
||||||
|
t.Errorf("expected zoxide cd extension to be disabled by default")
|
||||||
|
}
|
||||||
if cfg.History.Scope != "global" {
|
if cfg.History.Scope != "global" {
|
||||||
t.Errorf("expected global history scope, got %q", cfg.History.Scope)
|
t.Errorf("expected global history scope, got %q", cfg.History.Scope)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,6 +50,9 @@ func DefaultConfig() *Config {
|
|||||||
MinIntervalMS: 5000,
|
MinIntervalMS: 5000,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Zoxide: ZoxideConfig{
|
||||||
|
ExtendCd: false,
|
||||||
|
},
|
||||||
Keybindings: KeybindingsConfig{
|
Keybindings: KeybindingsConfig{
|
||||||
ToggleMode: "ctrl+r",
|
ToggleMode: "ctrl+r",
|
||||||
ToggleMenu: "shift+tab",
|
ToggleMenu: "shift+tab",
|
||||||
|
|||||||
@@ -97,6 +97,10 @@ filter-active-branch = true
|
|||||||
# merge remote and local branches with same name
|
# merge remote and local branches with same name
|
||||||
deduplicate-branches = true
|
deduplicate-branches = true
|
||||||
|
|
||||||
|
[zoxide]
|
||||||
|
# include zoxide directories in cd suggestions
|
||||||
|
extend-cd = false
|
||||||
|
|
||||||
[history]
|
[history]
|
||||||
# atuin scope for typed queries: "global", "directory", "workspace", or "hybrid"
|
# atuin scope for typed queries: "global", "directory", "workspace", or "hybrid"
|
||||||
scope = "global"
|
scope = "global"
|
||||||
|
|||||||
@@ -37,6 +37,9 @@ func TestConfigCommands(t *testing.T) {
|
|||||||
if !strings.Contains(string(content), "shell-login = false") {
|
if !strings.Contains(string(content), "shell-login = false") {
|
||||||
t.Error("expected initialized config to include shell-login = false")
|
t.Error("expected initialized config to include shell-login = false")
|
||||||
}
|
}
|
||||||
|
if !strings.Contains(string(content), "extend-cd = false") {
|
||||||
|
t.Error("expected initialized config to include extend-cd = false")
|
||||||
|
}
|
||||||
if !strings.Contains(string(content), "scope = \"global\"") {
|
if !strings.Contains(string(content), "scope = \"global\"") {
|
||||||
t.Error("expected initialized config to include global history scope")
|
t.Error("expected initialized config to include global history scope")
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-3
@@ -35,7 +35,7 @@ fi
|
|||||||
# prompt to complete, and exec'ing here would seize the tty from the real iris
|
# prompt to complete, and exec'ing here would seize the tty from the real iris
|
||||||
if [[ -o interactive ]] && [ -t 0 ] && [ -z "$IRIS_PID" ] && [ -z "$IRIS_RESCUE" ]; then
|
if [[ -o interactive ]] && [ -t 0 ] && [ -z "$IRIS_PID" ] && [ -z "$IRIS_RESCUE" ]; then
|
||||||
export IRIS_ACTIVE_SHELL="zsh"
|
export IRIS_ACTIVE_SHELL="zsh"
|
||||||
exec iris
|
exec "${IRIS_BIN:-iris}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Iris Autocomplete Hook
|
# Iris Autocomplete Hook
|
||||||
@@ -80,7 +80,7 @@ fi
|
|||||||
# prompt to complete, and exec'ing here would seize the tty from the real iris
|
# prompt to complete, and exec'ing here would seize the tty from the real iris
|
||||||
if [[ $- == *i* ]] && [ -t 0 ] && [ -z "$IRIS_PID" ] && [ -z "$IRIS_RESCUE" ]; then
|
if [[ $- == *i* ]] && [ -t 0 ] && [ -z "$IRIS_PID" ] && [ -z "$IRIS_RESCUE" ]; then
|
||||||
export IRIS_ACTIVE_SHELL="bash"
|
export IRIS_ACTIVE_SHELL="bash"
|
||||||
exec iris
|
exec "${IRIS_BIN:-iris}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Iris Autocomplete Hook
|
# Iris Autocomplete Hook
|
||||||
@@ -115,7 +115,11 @@ end
|
|||||||
|
|
||||||
if status is-interactive; and not set -q IRIS_PID; and not set -q IRIS_RESCUE
|
if status is-interactive; and not set -q IRIS_PID; and not set -q IRIS_RESCUE
|
||||||
set -gx IRIS_ACTIVE_SHELL "fish"
|
set -gx IRIS_ACTIVE_SHELL "fish"
|
||||||
exec iris
|
if set -q IRIS_BIN
|
||||||
|
exec "$IRIS_BIN"
|
||||||
|
else
|
||||||
|
exec iris
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Iris Autocomplete Hook
|
# Iris Autocomplete Hook
|
||||||
@@ -282,6 +286,10 @@ filter-active-branch = true
|
|||||||
# merge remote and local branches with same name
|
# merge remote and local branches with same name
|
||||||
deduplicate-branches = true
|
deduplicate-branches = true
|
||||||
|
|
||||||
|
[zoxide]
|
||||||
|
# include zoxide directories in cd suggestions
|
||||||
|
extend-cd = false
|
||||||
|
|
||||||
[history]
|
[history]
|
||||||
# atuin scope for typed queries: "global", "directory", "workspace", or "hybrid"
|
# atuin scope for typed queries: "global", "directory", "workspace", or "hybrid"
|
||||||
scope = "global"
|
scope = "global"
|
||||||
|
|||||||
+16
-1
@@ -41,7 +41,11 @@ func TestInitAutostartRequiresInteractiveShell(t *testing.T) {
|
|||||||
t.Run(shell, func(t *testing.T) {
|
t.Run(shell, func(t *testing.T) {
|
||||||
script := captureInitScript(t, shell)
|
script := captureInitScript(t, shell)
|
||||||
|
|
||||||
head, _, found := strings.Cut(script, "exec iris")
|
autostart := `exec "${IRIS_BIN:-iris}"`
|
||||||
|
if shell == "fish" {
|
||||||
|
autostart = `exec "$IRIS_BIN"`
|
||||||
|
}
|
||||||
|
head, _, found := strings.Cut(script, autostart)
|
||||||
if !found {
|
if !found {
|
||||||
t.Fatalf("%s init script has no autostart", shell)
|
t.Fatalf("%s init script has no autostart", shell)
|
||||||
}
|
}
|
||||||
@@ -57,3 +61,14 @@ func TestInitAutostartRequiresInteractiveShell(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInitAutostartSupportsBinaryOverride(t *testing.T) {
|
||||||
|
for _, shell := range []string{"zsh", "bash", "fish"} {
|
||||||
|
t.Run(shell, func(t *testing.T) {
|
||||||
|
script := captureInitScript(t, shell)
|
||||||
|
if !strings.Contains(script, "IRIS_BIN") {
|
||||||
|
t.Fatalf("%s init script does not support IRIS_BIN", shell)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+22
-6
@@ -2,6 +2,7 @@ package spec
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"maps"
|
"maps"
|
||||||
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -274,15 +275,15 @@ func Lookup(input string) []Suggestion {
|
|||||||
}
|
}
|
||||||
|
|
||||||
suggested := g.Cmd
|
suggested := g.Cmd
|
||||||
if strings.Contains(suggested, " ") && !strings.HasPrefix(suggested, "\"") {
|
if filepath.IsAbs(g.Cmd) || strings.ContainsAny(g.Cmd, " \t\r\n") {
|
||||||
suggested = "\"" + suggested + "\""
|
suggested = quoteSuggestionArgument(g.Cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the suggestion is a full path that includes
|
// Absolute results replace all typed arguments, avoiding duplication
|
||||||
// words already in the command line (multi-word support), we replace
|
// for multi-word directory matches.
|
||||||
// the entire argument part by using prefix only
|
|
||||||
finalCmd := ""
|
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
|
finalCmd = prefix + " " + suggested
|
||||||
} else if genLinePrefix != "" {
|
} else if genLinePrefix != "" {
|
||||||
finalCmd = genLinePrefix + " " + suggested
|
finalCmd = genLinePrefix + " " + suggested
|
||||||
@@ -382,6 +383,21 @@ func Lookup(input string) []Suggestion {
|
|||||||
return results
|
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 {
|
func topLevelSuggestions(query string, aliases map[string]string) []Suggestion {
|
||||||
scanExternalCommands()
|
scanExternalCommands()
|
||||||
results, seen := []Suggestion{}, make(map[string]bool)
|
results, seen := []Suggestion{}, make(map[string]bool)
|
||||||
|
|||||||
+17
-1
@@ -173,6 +173,23 @@ func TestLookup_NvimFileGenerator(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestQuoteSuggestionArgument(t *testing.T) {
|
||||||
|
tests := map[string]string{
|
||||||
|
"/tmp/project": "/tmp/project",
|
||||||
|
"/tmp/Проект": "/tmp/Проект",
|
||||||
|
"/tmp/My Project": `'/tmp/My Project'`,
|
||||||
|
"/tmp/$Project`command`": "'/tmp/$Project`command`'",
|
||||||
|
`/tmp/quote"and'apostrophe`: "'/tmp/quote\"and'\\''apostrophe'",
|
||||||
|
"~/My Project": `~/'My Project'`,
|
||||||
|
}
|
||||||
|
|
||||||
|
for input, want := range tests {
|
||||||
|
if got := quoteSuggestionArgument(input); got != want {
|
||||||
|
t.Errorf("quoteSuggestionArgument(%q) = %q, want %q", input, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestLookup_OptionAndFilePriority(t *testing.T) {
|
func TestLookup_OptionAndFilePriority(t *testing.T) {
|
||||||
ResetRegistry()
|
ResetRegistry()
|
||||||
Register(&Spec{
|
Register(&Spec{
|
||||||
@@ -389,4 +406,3 @@ func TestLookup_RealGitProvider(t *testing.T) {
|
|||||||
t.Errorf("expected 'git recent' in results for real GitProvider, got %v", results)
|
t.Errorf("expected 'git recent' in results for real GitProvider, got %v", results)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,58 +1,47 @@
|
|||||||
package tests
|
package tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/versenilvis/iris/commands/fs"
|
"github.com/versenilvis/iris/commands/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestZoxideGenerator(t *testing.T) {
|
func TestZoxideGenerator(t *testing.T) {
|
||||||
// Setup: Create a mock zoxide binary
|
dataDir := t.TempDir()
|
||||||
tmp := t.TempDir()
|
project := filepath.Join(dataDir, "project1")
|
||||||
mockZoxide := filepath.Join(tmp, "zoxide")
|
docs := filepath.Join(dataDir, "docs")
|
||||||
|
iris := filepath.Join(dataDir, "dev", "iris")
|
||||||
// Script that prints mock directories
|
missing := filepath.Join(dataDir, "deleted")
|
||||||
script := "#!/bin/sh\necho \"/home/verse/project1\n/home/verse/docs\n/home/verse/dev/iris\""
|
for _, path := range []string{project, docs, iris} {
|
||||||
_ = os.WriteFile(mockZoxide, []byte(script), 0755)
|
if err := os.MkdirAll(path, 0o755); err != nil {
|
||||||
|
t.Fatalf("create directory %q: %v", path, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
t.Setenv("PATH", tmp+string(os.PathListSeparator)+os.Getenv("PATH"))
|
binDir := t.TempDir()
|
||||||
|
mockZoxide := filepath.Join(binDir, "zoxide")
|
||||||
|
script := fmt.Sprintf("#!/bin/sh\nprintf '%%s\\n' %q %q %q %q\n", project, docs, iris, missing)
|
||||||
|
if err := os.WriteFile(mockZoxide, []byte(script), 0o755); err != nil {
|
||||||
|
t.Fatalf("create mock zoxide: %v", err)
|
||||||
|
}
|
||||||
|
t.Setenv("PATH", binDir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||||
|
|
||||||
gen := fs.ZoxideGenerator()
|
gen := fs.ZoxideGenerator()
|
||||||
|
results := gen([]string{"z", ""}, "z ", "")
|
||||||
|
if len(results) != 3 {
|
||||||
t.Run("Query returns correct result when partial is empty", func(t *testing.T) {
|
t.Fatalf("suggestion count = %d, want 3: %#v", len(results), results)
|
||||||
results := gen([]string{"z", ""}, "z ", "")
|
}
|
||||||
if len(results) == 0 {
|
for _, result := range results {
|
||||||
t.Errorf("Expected results from zoxide history, got 0")
|
if result.Cmd == missing {
|
||||||
|
t.Fatalf("missing directory was suggested: %#v", result)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
|
results = gen([]string{"z", "iris"}, "z ", "iris")
|
||||||
t.Run("Path replaces home dir with ~", func(t *testing.T) {
|
if len(results) != 1 || results[0].Cmd != iris {
|
||||||
home, _ := os.UserHomeDir()
|
t.Fatalf("fuzzy suggestions = %#v, want %q", results, iris)
|
||||||
results := gen([]string{"z", ""}, "z ", "")
|
}
|
||||||
foundHome := false
|
|
||||||
for _, r := range results {
|
|
||||||
if strings.HasPrefix(r.Desc, "~") {
|
|
||||||
foundHome = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !foundHome && home != "" {
|
|
||||||
t.Logf("Warning: Did not find ~ in descriptions, home is %s", home)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
t.Run("Sort by descending score", func(t *testing.T) {
|
|
||||||
results := gen([]string{"z", "i"}, "z ", "i")
|
|
||||||
if len(results) >= 1 {
|
|
||||||
if results[0].Cmd == "" {
|
|
||||||
t.Errorf("Empty result command")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user