Skip to content

Commit

Permalink
Add --builtin-filter-dirs
Browse files Browse the repository at this point in the history
* fzf's builtin filesystem walker is faster then overridding via FZF_DEFAULT_COMMAND, especially on Windows
* If this switch is specified, fzf will read directories instead of files if FZF_DEFAULT_COMMAND is not specified and there's no input from tty
  • Loading branch information
kelleyma49 committed Oct 7, 2023
1 parent d8188fc commit 41e3dd3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func Run(opts *Options, version string, revision string) {
reader = NewReader(func(data []byte) bool {
return chunkList.Push(data)
}, eventBox, opts.ReadZero, opts.Filter == nil)
go reader.ReadSource()
go reader.ReadSource(opts.FilterDirs)
}

// Matcher
Expand Down Expand Up @@ -165,7 +165,7 @@ func Run(opts *Options, version string, revision string) {
}
return false
}, eventBox, opts.ReadZero, false)
reader.ReadSource()
reader.ReadSource(opts.FilterDirs)
} else {
eventBox.Unwatch(EvtReadNew)
eventBox.WaitFor(EvtReadFin)
Expand Down
6 changes: 6 additions & 0 deletions src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const usage = `usage: fzf [options]
--tiebreak=CRI[,..] Comma-separated list of sort criteria to apply
when the scores are tied [length|chunk|begin|end|index]
(default: length)
--builtin-filter-dirs Filter directories instead of files when FZF_DEFAULT_COMMAND
is not set and input is not tty
Interface
-m, --multi[=MAX] Enable multi-select with tab/shift-tab
Expand Down Expand Up @@ -336,6 +338,7 @@ type Options struct {
Tabstop int
ListenPort *int
ClearOnExit bool
FilterDirs bool
Version bool
}

Expand Down Expand Up @@ -405,6 +408,7 @@ func defaultOptions() *Options {
BorderLabel: labelOpts{},
PreviewLabel: labelOpts{},
ClearOnExit: true,
FilterDirs: false,
Version: false}
}

Expand Down Expand Up @@ -1821,6 +1825,8 @@ func parseOptions(opts *Options, allArgs []string) {
opts.ClearOnExit = true
case "--no-clear":
opts.ClearOnExit = false
case "--builtin-filter-dirs":
opts.FilterDirs = true
case "--version":
opts.Version = true
case "--":
Expand Down
8 changes: 4 additions & 4 deletions src/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (r *Reader) restart(command string) {
}

// ReadSource reads data from the default command or from standard input
func (r *Reader) ReadSource() {
func (r *Reader) ReadSource(readDirs bool) {
r.startEventPoller()
var success bool
if util.IsTty() {
Expand All @@ -115,7 +115,7 @@ func (r *Reader) ReadSource() {
if defaultCommand != "" {
success = r.readFromCommand(&shell, defaultCommand)
} else {
success = r.readFiles()
success = r.readFilesOrDirs(readDirs)
}
} else {
success = r.readFromCommand(nil, cmd)
Expand Down Expand Up @@ -161,7 +161,7 @@ func (r *Reader) readFromStdin() bool {
return true
}

func (r *Reader) readFiles() bool {
func (r *Reader) readFilesOrDirs(readDirs bool) bool {
r.killed = false
fn := func(path string, mode os.FileInfo) error {
path = filepath.Clean(path)
Expand All @@ -170,7 +170,7 @@ func (r *Reader) readFiles() bool {
if isDir && filepath.Base(path)[0] == '.' {
return filepath.SkipDir
}
if !isDir && r.pusher([]byte(path)) {
if ((isDir && readDirs) || (!isDir && !readDirs)) && r.pusher([]byte(path)) {
atomic.StoreInt32(&r.event, int32(EvtReadNew))
}
}
Expand Down

0 comments on commit 41e3dd3

Please sign in to comment.