Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added globfilter command #1650

Merged
merged 4 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion diacritics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) + ", " +
Expand Down
5 changes: 5 additions & 0 deletions doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 '.*')
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 '.*')
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions lf.1
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions nav.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -1780,15 +1780,15 @@ 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
}
}
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
Expand All @@ -1801,15 +1801,15 @@ 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
}
}
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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var gOpts struct {
dirpreviews bool
drawbox bool
dupfilefmt string
globfilter bool
Catalyn45 marked this conversation as resolved.
Show resolved Hide resolved
globsearch bool
hidden bool
icons bool
Expand Down Expand Up @@ -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
Expand Down