add hiddenFiles option (#372)

* add hiddenFiles option

* harden and improve syntax

* refactor name and add to docs
This commit is contained in:
Adam Abrams authored and GitHub committed 2020-06-11 02:52:15 +03:00
1 parent 8933a3eafe
commit 72facea2a0
7 files changed
+43 -7

No files matched your search

+1
View File
@@ -139,6 +139,7 @@ var (
"ratios",
"info",
"shellopts",
"hiddenfiles",
}
)
+1
View File
@@ -134,6 +134,7 @@ The following options can be used to customize the behavior of lf:
ratios string (default '1:2:3')
info string (default '')
shellopts string (default '')
hiddenfiles string (default '.*')
The following variables are exported for shell commands:
+2
View File
@@ -296,6 +296,8 @@ func (e *setExpr) eval(app *app, args []string) {
gOpts.info = toks
case "shellopts":
gOpts.shellopts = strings.Split(e.val, ":")
case "hiddenfiles":
gOpts.hiddenfiles = strings.Split(e.val, ":")
default:
app.ui.echoerrf("unknown option: %s", e.opt)
return
+3 -3
View File
@@ -204,13 +204,13 @@ func (dir *dir) sort() {
// files to the first non-hidden file in the list
if gOpts.sortType.option&hiddenSort == 0 {
sort.SliceStable(dir.files, func(i, j int) bool {
if isHidden(dir.files[i]) && isHidden(dir.files[j]) {
if isHidden(dir.files[i], dir.path) && isHidden(dir.files[j], dir.path) {
return i < j
}
return isHidden(dir.files[i])
return isHidden(dir.files[i], dir.path)
})
for i, f := range dir.files {
if !isHidden(f) {
if !isHidden(f, dir.path) {
dir.files = dir.files[i:]
return
}
+2
View File
@@ -58,6 +58,7 @@ var gOpts struct {
ratios []int
info []string
shellopts []string
hiddenfiles []string
keys map[string]expr
cmdkeys map[string]expr
cmds map[string]expr
@@ -95,6 +96,7 @@ func init() {
gOpts.ratios = []int{1, 2, 3}
gOpts.info = nil
gOpts.shellopts = nil
gOpts.hiddenfiles = []string{".*"}
gOpts.sortType = sortType{naturalSort, dirfirstSort}
gOpts.keys = make(map[string]expr)
+32 -2
View File
@@ -133,8 +133,38 @@ func isExecutable(f os.FileInfo) bool {
return f.Mode()&0111 != 0
}
func isHidden(f os.FileInfo) bool {
return f.Name()[0] == '.'
func isHidden(f os.FileInfo, path string) (hidden bool) {
for _, pattern := range gOpts.hiddenfiles {
if len(pattern) == 0 {
continue
}
matched := matchPattern(strings.TrimPrefix(pattern, "!"), f.Name(), path)
if pattern[0] == '!' && matched {
hidden = false
} else if matched {
hidden = true
}
}
return hidden
}
func matchPattern(pattern, name, path string) bool {
matchStr := name
if pattern[0] == '~' {
pattern = gUser.HomeDir + strings.TrimPrefix(pattern, "~")
}
if pattern[0] == filepath.Separator {
matchStr = filepath.Join(path, name)
}
isMatch, err := filepath.Match(pattern, matchStr)
if err != nil {
log.Printf("pattern %s caused error with %s", pattern, matchStr)
return false
}
return isMatch
}
func exportFiles(f string, fs []string) {
+2 -2
View File
@@ -114,9 +114,9 @@ func isExecutable(f os.FileInfo) bool {
return false
}
func isHidden(f os.FileInfo) bool {
func isHidden(f os.FileInfo, path string) (hidden bool) {
// TODO: implement
return false
return hidden
}
func exportFiles(f string, fs []string) {