avoid renew when hidden option is changed

Mentioned in #92.
This commit is contained in:
Gokcehan 2017-11-30 23:11:37 +03:00
parent ce508d7ed3
commit 2c63096d3b
2 changed files with 34 additions and 14 deletions

View File

@ -33,13 +33,13 @@ func (e *setExpr) eval(app *app, args []string) {
gOpts.globsearch = !gOpts.globsearch gOpts.globsearch = !gOpts.globsearch
case "hidden": case "hidden":
gOpts.hidden = true gOpts.hidden = true
app.nav.renew(app.nav.height) app.nav.sort()
case "nohidden": case "nohidden":
gOpts.hidden = false gOpts.hidden = false
app.nav.renew(app.nav.height) app.nav.sort()
case "hidden!": case "hidden!":
gOpts.hidden = !gOpts.hidden gOpts.hidden = !gOpts.hidden
app.nav.renew(app.nav.height) app.nav.sort()
case "ignorecase": case "ignorecase":
gOpts.ignorecase = true gOpts.ignorecase = true
case "noignorecase": case "noignorecase":

38
nav.go
View File

@ -50,10 +50,6 @@ func readdir(path string) ([]*file, error) {
fi := make([]*file, 0, len(names)) fi := make([]*file, 0, len(names))
for _, filename := range names { for _, filename := range names {
if !gOpts.hidden && filename[0] == '.' {
continue
}
fpath := filepath.Join(path, filename) fpath := filepath.Join(path, filename)
lstat, lerr := os.Lstat(fpath) lstat, lerr := os.Lstat(fpath)
@ -89,10 +85,11 @@ func readdir(path string) ([]*file, error) {
} }
type dir struct { type dir struct {
ind int // which entry is highlighted ind int // index of current entry in fi
pos int // which line in the ui highlighted entry is pos int // position of current entry in ui
path string path string // full path of directory
fi []*file fi []*file // displayed files in directory including or excluding hidden ones
all []*file // all files in directory including hidden ones (same array as fi)
} }
func newDir(path string) *dir { func newDir(path string) *dir {
@ -104,6 +101,7 @@ func newDir(path string) *dir {
return &dir{ return &dir{
path: path, path: path,
fi: fi, fi: fi,
all: fi,
} }
} }
@ -114,9 +112,12 @@ func (dir *dir) renew() {
} }
dir.fi = fi dir.fi = fi
dir.all = fi
} }
func (dir *dir) sort() { func (dir *dir) sort() {
dir.fi = dir.all
switch gOpts.sortby { switch gOpts.sortby {
case "natural": case "natural":
sortFilesStable(dir.fi, func(i, j int) bool { sortFilesStable(dir.fi, func(i, j int) bool {
@ -152,6 +153,25 @@ func (dir *dir) sort() {
return dir.fi[i].IsDir() return dir.fi[i].IsDir()
}) })
} }
// when hidden option is disabled, we move hidden files to the
// beginning of our file list and then set the beginning of displayed
// files to the first non-hidden file in the list
if !gOpts.hidden {
sortFilesStable(dir.fi, func(i, j int) bool {
if dir.fi[i].Name()[0] == '.' && dir.fi[j].Name()[0] == '.' {
return i < j
}
return dir.fi[i].Name()[0] == '.'
})
for i, f := range dir.fi {
if f.Name()[0] != '.' {
dir.fi = dir.fi[i:]
return
}
}
dir.fi = dir.fi[len(dir.fi):]
}
} }
func (dir *dir) name() string { func (dir *dir) name() string {
@ -176,10 +196,10 @@ func (dir *dir) find(name string, height int) {
break break
} }
} }
}
edge := min(gOpts.scrolloff, len(dir.fi)-dir.ind-1) edge := min(gOpts.scrolloff, len(dir.fi)-dir.ind-1)
dir.pos = min(dir.ind, height-edge-1) dir.pos = min(dir.ind, height-edge-1)
}
} }
type nav struct { type nav struct {