add hiddenFiles option (#372)
* add hiddenFiles option * harden and improve syntax * refactor name and add to docs
This commit is contained in:
1 parent
8933a3eafe
commit
72facea2a0
7 files changed
+43
-7
No files matched your search
@@ -139,6 +139,7 @@ var (
|
|||||||
"ratios",
|
"ratios",
|
||||||
"info",
|
"info",
|
||||||
"shellopts",
|
"shellopts",
|
||||||
|
"hiddenfiles",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -134,6 +134,7 @@ The following options can be used to customize the behavior of lf:
|
|||||||
ratios string (default '1:2:3')
|
ratios string (default '1:2:3')
|
||||||
info string (default '')
|
info string (default '')
|
||||||
shellopts string (default '')
|
shellopts string (default '')
|
||||||
|
hiddenfiles string (default '.*')
|
||||||
|
|
||||||
The following variables are exported for shell commands:
|
The following variables are exported for shell commands:
|
||||||
|
|
||||||
|
|||||||
@@ -296,6 +296,8 @@ func (e *setExpr) eval(app *app, args []string) {
|
|||||||
gOpts.info = toks
|
gOpts.info = toks
|
||||||
case "shellopts":
|
case "shellopts":
|
||||||
gOpts.shellopts = strings.Split(e.val, ":")
|
gOpts.shellopts = strings.Split(e.val, ":")
|
||||||
|
case "hiddenfiles":
|
||||||
|
gOpts.hiddenfiles = strings.Split(e.val, ":")
|
||||||
default:
|
default:
|
||||||
app.ui.echoerrf("unknown option: %s", e.opt)
|
app.ui.echoerrf("unknown option: %s", e.opt)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -204,13 +204,13 @@ func (dir *dir) sort() {
|
|||||||
// files to the first non-hidden file in the list
|
// files to the first non-hidden file in the list
|
||||||
if gOpts.sortType.option&hiddenSort == 0 {
|
if gOpts.sortType.option&hiddenSort == 0 {
|
||||||
sort.SliceStable(dir.files, func(i, j int) bool {
|
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 i < j
|
||||||
}
|
}
|
||||||
return isHidden(dir.files[i])
|
return isHidden(dir.files[i], dir.path)
|
||||||
})
|
})
|
||||||
for i, f := range dir.files {
|
for i, f := range dir.files {
|
||||||
if !isHidden(f) {
|
if !isHidden(f, dir.path) {
|
||||||
dir.files = dir.files[i:]
|
dir.files = dir.files[i:]
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ var gOpts struct {
|
|||||||
ratios []int
|
ratios []int
|
||||||
info []string
|
info []string
|
||||||
shellopts []string
|
shellopts []string
|
||||||
|
hiddenfiles []string
|
||||||
keys map[string]expr
|
keys map[string]expr
|
||||||
cmdkeys map[string]expr
|
cmdkeys map[string]expr
|
||||||
cmds map[string]expr
|
cmds map[string]expr
|
||||||
@@ -95,6 +96,7 @@ func init() {
|
|||||||
gOpts.ratios = []int{1, 2, 3}
|
gOpts.ratios = []int{1, 2, 3}
|
||||||
gOpts.info = nil
|
gOpts.info = nil
|
||||||
gOpts.shellopts = nil
|
gOpts.shellopts = nil
|
||||||
|
gOpts.hiddenfiles = []string{".*"}
|
||||||
gOpts.sortType = sortType{naturalSort, dirfirstSort}
|
gOpts.sortType = sortType{naturalSort, dirfirstSort}
|
||||||
|
|
||||||
gOpts.keys = make(map[string]expr)
|
gOpts.keys = make(map[string]expr)
|
||||||
|
|||||||
@@ -133,8 +133,38 @@ func isExecutable(f os.FileInfo) bool {
|
|||||||
return f.Mode()&0111 != 0
|
return f.Mode()&0111 != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func isHidden(f os.FileInfo) bool {
|
func isHidden(f os.FileInfo, path string) (hidden bool) {
|
||||||
return f.Name()[0] == '.'
|
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) {
|
func exportFiles(f string, fs []string) {
|
||||||
|
|||||||
+2
-2
@@ -114,9 +114,9 @@ func isExecutable(f os.FileInfo) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func isHidden(f os.FileInfo) bool {
|
func isHidden(f os.FileInfo, path string) (hidden bool) {
|
||||||
// TODO: implement
|
// TODO: implement
|
||||||
return false
|
return hidden
|
||||||
}
|
}
|
||||||
|
|
||||||
func exportFiles(f string, fs []string) {
|
func exportFiles(f string, fs []string) {
|
||||||
|
|||||||
Reference in new issue
Block a user