diff options
| author | Grail Finder <wohilas@gmail.com> | 2026-02-19 10:14:16 +0300 |
|---|---|---|
| committer | Grail Finder <wohilas@gmail.com> | 2026-02-19 10:14:16 +0300 |
| commit | d3361c13c5cd20e277e8d470a8606cb57d9867bc (patch) | |
| tree | 3d710bc319e336021a207bbe1fc9c91b376a1a0d /helpfuncs.go | |
| parent | 7c1a8b01220083bdaf0b14d81f39949f0385f3f8 (diff) | |
Enha (shell): tabcompletion depth and limit
Diffstat (limited to 'helpfuncs.go')
| -rw-r--r-- | helpfuncs.go | 51 |
1 files changed, 40 insertions, 11 deletions
diff --git a/helpfuncs.go b/helpfuncs.go index 350b6ed..194d5a9 100644 --- a/helpfuncs.go +++ b/helpfuncs.go @@ -9,6 +9,7 @@ import ( "os" "os/exec" "path" + "path/filepath" "slices" "strings" "unicode" @@ -706,23 +707,51 @@ func searchPrev() { // == tab completion == func scanFiles(dir, filter string) []string { + const maxDepth = 3 + const maxFiles = 50 var files []string - entries, err := os.ReadDir(dir) - if err != nil { - return files - } - for _, entry := range entries { - name := entry.Name() - if strings.HasPrefix(name, ".") { - continue + + var scanRecursive func(currentDir string, currentDepth int, relPath string) + scanRecursive = func(currentDir string, currentDepth int, relPath string) { + if len(files) >= maxFiles { + return + } + if currentDepth > maxDepth { + return + } + + entries, err := os.ReadDir(currentDir) + if err != nil { + return } - if filter == "" || strings.HasPrefix(strings.ToLower(name), strings.ToLower(filter)) { + + for _, entry := range entries { + if len(files) >= maxFiles { + return + } + + name := entry.Name() + if strings.HasPrefix(name, ".") { + continue + } + + fullPath := name + if relPath != "" { + fullPath = relPath + "/" + name + } + if entry.IsDir() { - files = append(files, name+"/") + // Recursively scan subdirectories + scanRecursive(filepath.Join(currentDir, name), currentDepth+1, fullPath) } else { - files = append(files, name) + // Check if file matches filter + if filter == "" || strings.HasPrefix(strings.ToLower(fullPath), strings.ToLower(filter)) { + files = append(files, fullPath) + } } } } + + scanRecursive(dir, 0, "") return files } |
