From 2ddaf434213a8db8f28ad1cb54df5ec989b50016 Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Sat, 19 Mar 2022 19:01:24 +0300 Subject: [PATCH] count dirs while reading the parent dir This commit moves directory counting with 'dircounts' option from ui printing to dir reading to be run asynchronously without locking the ui. On the other side, disadvantage of this approach is that if 'dircounts' option is changed at runtime, then directory counts may not be available requiring a manual 'reload'. Indicators for errors are changed to '!' instead of '?' to distinguish them from missing values. --- nav.go | 19 ++++++++++++++++++- ui.go | 21 ++------------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/nav.go b/nav.go index f9d6452..04dabfc 100644 --- a/nav.go +++ b/nav.go @@ -101,12 +101,29 @@ func readdir(path string) ([]*file, error) { // i.e. directories, filenames without extensions ext := filepath.Ext(fpath) + dirCount := -1 + if lstat.IsDir() && gOpts.dircounts { + d, err := os.Open(fpath) + if err != nil { + dirCount = -2 + } + + names, err := d.Readdirnames(1000) + d.Close() + + if names == nil && err != io.EOF { + dirCount = -2 + } else { + dirCount = len(names) + } + } + files = append(files, &file{ FileInfo: lstat, linkState: linkState, linkTarget: linkTarget, path: fpath, - dirCount: -1, + dirCount: dirCount, dirSize: -1, accessTime: at, changeTime: ct, diff --git a/ui.go b/ui.go index f147043..8e2b3aa 100644 --- a/ui.go +++ b/ui.go @@ -3,7 +3,6 @@ package main import ( "bytes" "fmt" - "io" "log" "os" "path/filepath" @@ -287,8 +286,6 @@ func infotimefmt(t time.Time) string { func fileInfo(f *file, d *dir) string { var info string - path := filepath.Join(d.path, f.Name()) - for _, s := range gOpts.info { switch s { case "size": @@ -303,23 +300,9 @@ func fileInfo(f *file, d *dir) string { continue } - if f.dirCount == -1 { - d, err := os.Open(path) - if err != nil { - f.dirCount = -2 - } - - names, err := d.Readdirnames(1000) - d.Close() - - if names == nil && err != io.EOF { - f.dirCount = -2 - } else { - f.dirCount = len(names) - } - } - switch { + case f.dirCount < -1: + info = fmt.Sprintf("%s !", info) case f.dirCount < 0: info = fmt.Sprintf("%s ?", info) case f.dirCount < 1000: