Added globfilter command (#1650)

* Added globfilter command

* Kept the alphabetical order and applied other sugestions

* Used pandoc to update docs
This commit is contained in:
Catalin Ancutei authored and GitHub committed 2024-04-07 15:36:17 +02:00
1 parent 5b108037a3
commit f507b618c9
7 files changed
+30 -10

No files matched your search

+1 -1
View File
@@ -61,7 +61,7 @@ func runSearch(t *testing.T, ignorecase, smartcase, ignorediacritics, smartdiacr
gOpts.smartcase = smartcase
gOpts.ignoredia = ignorediacritics
gOpts.smartdia = smartdiacritics
matched, _ := searchMatch(base, pattern)
matched, _ := searchMatch(base, pattern, false)
if matched != expected {
t.Errorf("False search for" +
" ignorecase = " + strconv.FormatBool(gOpts.ignorecase) + ", " +
+5
View File
@@ -155,6 +155,7 @@ The following options can be used to customize the behavior of lf:
errorfmt string (default "\033[7;31;47m")
filesep string (default "\n")
findlen int (default 1)
globfilter bool (default false)
globsearch bool (default false)
hidden bool (default false)
hiddenfiles []string (default '.*')
@@ -749,6 +750,10 @@ File separator used in environment variables `fs` and `fx`.
Number of characters prompted for the find command.
When this value is set to 0, find command prompts until there is only a single match left.
## globfilter (bool) (default false)
Patterns are treated as globs for the filter command, see `globsearch` for more details.
## globsearch (bool) (default false)
When this option is enabled, search command patterns are considered as globs, otherwise, they are literals.
+6
View File
@@ -145,6 +145,7 @@ The following options can be used to customize the behavior of lf:
errorfmt string (default "\033[7;31;47m")
filesep string (default "\n")
findlen int (default 1)
globfilter bool (default false)
globsearch bool (default false)
hidden bool (default false)
hiddenfiles []string (default '.*')
@@ -797,6 +798,11 @@ findlen (int) (default 1)
Number of characters prompted for the find command. When this value is
set to 0, find command prompts until there is only a single match left.
globfilter (bool) (default false)
Patterns are treated as globs for the filter command, see globsearch for
more details.
globsearch (bool) (default false)
When this option is enabled, search command patterns are considered as
+4 -2
View File
@@ -96,14 +96,16 @@ func (e *setExpr) eval(app *app, args []string) {
}
app.ui.loadFile(app, true)
}
case "globsearch", "noglobsearch", "globsearch!":
err = applyBoolOpt(&gOpts.globsearch, e)
case "globfilter", "noglobfilter", "globfilter!":
err = applyBoolOpt(&gOpts.globfilter, e)
if err == nil {
app.nav.sort()
app.nav.position()
app.ui.sort()
app.ui.loadFile(app, true)
}
case "globsearch", "noglobsearch", "globsearch!":
err = applyBoolOpt(&gOpts.globsearch, e)
case "hidden", "nohidden", "hidden!":
err = applyBoolOpt(&gOpts.hidden, e)
if err == nil {
+5
View File
@@ -163,6 +163,7 @@ dupfilefmt string (default \[aq]%f.\[ti]%n\[ti]\[aq])
errorfmt string (default \[dq]\[rs]033[7;31;47m\[dq])
filesep string (default \[dq]\[rs]n\[dq])
findlen int (default 1)
globfilter bool (default false)
globsearch bool (default false)
hidden bool (default false)
hiddenfiles []string (default \[aq].*\[aq])
@@ -815,6 +816,10 @@ File separator used in environment variables \f[C]fs\f[R] and
Number of characters prompted for the find command.
When this value is set to 0, find command prompts until there is only a
single match left.
.SS globfilter (bool) (default false)
.PP
Patterns are treated as globs for the filter command, see
\f[C]globsearch\f[R] for more details.
.SS globsearch (bool) (default false)
.PP
When this option is enabled, search command patterns are considered as
+7 -7
View File
@@ -1756,7 +1756,7 @@ func (nav *nav) findPrev() (bool, bool) {
return false, false
}
func searchMatch(name, pattern string) (matched bool, err error) {
func searchMatch(name, pattern string, glob bool) (matched bool, err error) {
if gOpts.ignorecase {
lpattern := strings.ToLower(pattern)
if !gOpts.smartcase || lpattern == pattern {
@@ -1771,7 +1771,7 @@ func searchMatch(name, pattern string) (matched bool, err error) {
name = removeDiacritics(name)
}
}
if gOpts.globsearch {
if glob {
return filepath.Match(pattern, name)
}
return strings.Contains(name, pattern), nil
@@ -1780,7 +1780,7 @@ func searchMatch(name, pattern string) (matched bool, err error) {
func (nav *nav) searchNext() (bool, error) {
dir := nav.currDir()
for i := dir.ind + 1; i < len(dir.files); i++ {
if matched, err := searchMatch(dir.files[i].Name(), nav.search); err != nil {
if matched, err := searchMatch(dir.files[i].Name(), nav.search, gOpts.globsearch); err != nil {
return false, err
} else if matched {
return nav.down(i - dir.ind), nil
@@ -1788,7 +1788,7 @@ func (nav *nav) searchNext() (bool, error) {
}
if gOpts.wrapscan {
for i := 0; i < dir.ind; i++ {
if matched, err := searchMatch(dir.files[i].Name(), nav.search); err != nil {
if matched, err := searchMatch(dir.files[i].Name(), nav.search, gOpts.globsearch); err != nil {
return false, err
} else if matched {
return nav.up(dir.ind - i), nil
@@ -1801,7 +1801,7 @@ func (nav *nav) searchNext() (bool, error) {
func (nav *nav) searchPrev() (bool, error) {
dir := nav.currDir()
for i := dir.ind - 1; i >= 0; i-- {
if matched, err := searchMatch(dir.files[i].Name(), nav.search); err != nil {
if matched, err := searchMatch(dir.files[i].Name(), nav.search, gOpts.globsearch); err != nil {
return false, err
} else if matched {
return nav.up(dir.ind - i), nil
@@ -1809,7 +1809,7 @@ func (nav *nav) searchPrev() (bool, error) {
}
if gOpts.wrapscan {
for i := len(dir.files) - 1; i > dir.ind; i-- {
if matched, err := searchMatch(dir.files[i].Name(), nav.search); err != nil {
if matched, err := searchMatch(dir.files[i].Name(), nav.search, gOpts.globsearch); err != nil {
return false, err
} else if matched {
return nav.down(i - dir.ind), nil
@@ -1821,7 +1821,7 @@ func (nav *nav) searchPrev() (bool, error) {
func isFiltered(f os.FileInfo, filter []string) bool {
for _, pattern := range filter {
matched, err := searchMatch(f.Name(), strings.TrimPrefix(pattern, "!"))
matched, err := searchMatch(f.Name(), strings.TrimPrefix(pattern, "!"), gOpts.globfilter)
if err != nil {
log.Printf("Filter Error: %s", err)
return false
+2
View File
@@ -46,6 +46,7 @@ var gOpts struct {
dirpreviews bool
drawbox bool
dupfilefmt string
globfilter bool
globsearch bool
hidden bool
icons bool
@@ -191,6 +192,7 @@ func init() {
gOpts.cursorpreviewfmt = "\033[4m"
gOpts.cutfmt = "\033[7;31m"
gOpts.hidecursorinactive = false
gOpts.globfilter = false
gOpts.globsearch = false
gOpts.hidden = false
gOpts.icons = false