Fix display of files in root dir

This commit is contained in:
Kovid Goyal
2025-05-21 22:55:49 +05:30
parent b784dd4fba
commit c1b158781f
2 changed files with 13 additions and 10 deletions

View File

@@ -25,7 +25,7 @@ type State struct {
func (s State) BaseDir() string { return utils.IfElse(s.base_dir == "", default_cwd, s.base_dir) }
func (s State) SelectDirs() bool { return s.select_dirs }
func (s State) Multiselect() bool { return s.multiselect }
func (s State) MaxDepth() int { return max(8, s.max_depth) }
func (s State) MaxDepth() int { return utils.IfElse(s.max_depth < 1, 4, s.max_depth) }
func (s State) String() string { return utils.Repr(s) }
func (s State) SearchText() string { return s.search_text }
func (s State) CurrentDir() string {

View File

@@ -52,15 +52,19 @@ func scan_dir(path, root_dir string) []ResultItem {
if ans, err := os.ReadDir(path); err == nil {
if rel, err := filepath.Rel(root_dir, path); err == nil {
return utils.Map(func(x os.DirEntry) ResultItem {
path := filepath.Join(root_dir, x.Name())
return ResultItem{dir_entry: x, abspath: path, text: filepath.Join(rel, x.Name())}
path := filepath.Join(path, x.Name())
r := filepath.Join(rel, x.Name())
if root_dir == "/" {
r = "/" + r
}
return ResultItem{dir_entry: x, abspath: path, text: r}
}, ans)
}
}
return []ResultItem{}
}
func (sc *ScanCache) fs_scan(root_dir, current_dir string, max_depth int) (ans []ResultItem) {
func (sc *ScanCache) fs_scan(root_dir, current_dir string, max_depth int, seen map[string]bool) (ans []ResultItem) {
var found bool
if ans, found = sc.get_cached_entries(current_dir); !found {
ans = scan_dir(current_dir, root_dir)
@@ -70,8 +74,9 @@ func (sc *ScanCache) fs_scan(root_dir, current_dir string, max_depth int) (ans [
// now recurse into directories
if max_depth > 0 {
for _, x := range ans {
if x.dir_entry.IsDir() {
ans = append(ans, sc.fs_scan(root_dir, x.abspath, max_depth-1)...)
if x.dir_entry.IsDir() && !seen[x.abspath] {
seen[x.abspath] = true
ans = append(ans, sc.fs_scan(root_dir, x.abspath, max_depth-1, seen)...)
}
}
}
@@ -79,10 +84,8 @@ func (sc *ScanCache) fs_scan(root_dir, current_dir string, max_depth int) (ans [
}
func (sc *ScanCache) scan(root_dir, search_text string, max_depth int) (ans []ResultItem) {
if strings.HasPrefix(search_text, "/") {
root_dir = "/"
}
ans = sc.fs_scan(root_dir, root_dir, max_depth)
seen := make(map[string]bool, 1024)
ans = sc.fs_scan(root_dir, root_dir, max_depth, seen)
if search_text == "" {
slices.SortFunc(ans, func(a, b ResultItem) int {
switch a.dir_entry.IsDir() {