eliminate redundant renews for sort changes

Mentioned in #92.
This commit is contained in:
Gokcehan 2017-11-18 22:06:30 +03:00
parent d97f4a07b6
commit accb61dad1
2 changed files with 71 additions and 11 deletions

14
eval.go
View File

@ -19,13 +19,13 @@ func (e *setExpr) eval(app *app, args []string) {
gOpts.dircounts = !gOpts.dircounts gOpts.dircounts = !gOpts.dircounts
case "dirfirst": case "dirfirst":
gOpts.dirfirst = true gOpts.dirfirst = true
app.nav.renew(app.nav.height) app.nav.sort()
case "nodirfirst": case "nodirfirst":
gOpts.dirfirst = false gOpts.dirfirst = false
app.nav.renew(app.nav.height) app.nav.sort()
case "dirfirst!": case "dirfirst!":
gOpts.dirfirst = !gOpts.dirfirst gOpts.dirfirst = !gOpts.dirfirst
app.nav.renew(app.nav.height) app.nav.sort()
case "globsearch": case "globsearch":
gOpts.globsearch = true gOpts.globsearch = true
case "noglobsearch": case "noglobsearch":
@ -55,13 +55,13 @@ func (e *setExpr) eval(app *app, args []string) {
gOpts.preview = !gOpts.preview gOpts.preview = !gOpts.preview
case "reverse": case "reverse":
gOpts.reverse = true gOpts.reverse = true
app.nav.renew(app.nav.height) app.nav.sort()
case "noreverse": case "noreverse":
gOpts.reverse = false gOpts.reverse = false
app.nav.renew(app.nav.height) app.nav.sort()
case "reverse!": case "reverse!":
gOpts.reverse = !gOpts.reverse gOpts.reverse = !gOpts.reverse
app.nav.renew(app.nav.height) app.nav.sort()
case "smartcase": case "smartcase":
gOpts.smartcase = true gOpts.smartcase = true
case "nosmartcase": case "nosmartcase":
@ -124,7 +124,7 @@ func (e *setExpr) eval(app *app, args []string) {
return return
} }
gOpts.sortby = e.val gOpts.sortby = e.val
app.nav.renew(app.nav.height) app.nav.sort()
case "timefmt": case "timefmt":
gOpts.timefmt = e.val gOpts.timefmt = e.val
case "ratios": case "ratios":

68
nav.go
View File

@ -140,10 +140,19 @@ type dir struct {
} }
func newDir(path string) *dir { func newDir(path string) *dir {
return &dir{ fi, err := readdir(path)
path: path, if err != nil {
fi: getFilesSorted(path), log.Printf("reading directory: %s", err)
} }
dir := &dir{
path: path,
fi: fi,
}
dir.sort()
return dir
} }
func (dir *dir) renew(height int) { func (dir *dir) renew(height int) {
@ -152,11 +161,56 @@ func (dir *dir) renew(height int) {
name = dir.fi[dir.ind].Name() name = dir.fi[dir.ind].Name()
} }
dir.fi = getFilesSorted(dir.path) fi, err := readdir(dir.path)
if err != nil {
log.Printf("reading directory: %s", err)
}
dir.fi = fi
dir.sort()
dir.load(dir.ind, dir.pos, height, name) dir.load(dir.ind, dir.pos, height, name)
} }
func (dir *dir) sort() {
switch gOpts.sortby {
case "natural":
sortFilesStable(dir.fi, func(i, j int) bool {
return naturalLess(strings.ToLower(dir.fi[i].Name()), strings.ToLower(dir.fi[j].Name()))
})
case "name":
sortFilesStable(dir.fi, func(i, j int) bool {
return strings.ToLower(dir.fi[i].Name()) < strings.ToLower(dir.fi[j].Name())
})
case "size":
sortFilesStable(dir.fi, func(i, j int) bool {
return dir.fi[i].Size() < dir.fi[j].Size()
})
case "time":
sortFilesStable(dir.fi, func(i, j int) bool {
return dir.fi[i].ModTime().Before(dir.fi[j].ModTime())
})
default:
log.Printf("unknown sorting type: %s", gOpts.sortby)
}
if gOpts.reverse {
for i, j := 0, len(dir.fi)-1; i < j; i, j = i+1, j-1 {
dir.fi[i], dir.fi[j] = dir.fi[j], dir.fi[i]
}
}
if gOpts.dirfirst {
sortFilesStable(dir.fi, func(i, j int) bool {
if dir.fi[i].IsDir() == dir.fi[j].IsDir() {
return i < j
}
return dir.fi[i].IsDir()
})
}
}
func (dir *dir) load(ind, pos, height int, name string) { func (dir *dir) load(ind, pos, height int, name string) {
if len(dir.fi) == 0 { if len(dir.fi) == 0 {
dir.ind, dir.pos = 0, 0 dir.ind, dir.pos = 0, 0
@ -252,6 +306,12 @@ func (nav *nav) renew(height int) {
} }
} }
func (nav *nav) sort() {
for _, d := range nav.dirs {
d.sort()
}
}
func (nav *nav) up(dist int) { func (nav *nav) up(dist int) {
dir := nav.currDir() dir := nav.currDir()