Skip to content
This repository has been archived by the owner on May 14, 2022. It is now read-only.

Commit

Permalink
count dirs while reading the parent dir
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gokcehan committed Mar 19, 2022
1 parent 5927a50 commit 2ddaf43
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
19 changes: 18 additions & 1 deletion nav.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 2 additions & 19 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"bytes"
"fmt"
"io"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -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":
Expand All @@ -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:
Expand Down

0 comments on commit 2ddaf43

Please sign in to comment.