From 41e3dd349aa0a3fd6d2b000367831f28c8c7a79f Mon Sep 17 00:00:00 2001 From: Michael Kelley Date: Sat, 7 Oct 2023 16:06:37 -0700 Subject: [PATCH] Add --builtin-filter-dirs * 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 --- src/core.go | 4 ++-- src/options.go | 6 ++++++ src/reader.go | 8 ++++---- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/core.go b/src/core.go index e21e8c0aa2c..541a2ca1804 100644 --- a/src/core.go +++ b/src/core.go @@ -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 @@ -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) diff --git a/src/options.go b/src/options.go index 3f4b5e92932..d1358902279 100644 --- a/src/options.go +++ b/src/options.go @@ -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 @@ -336,6 +338,7 @@ type Options struct { Tabstop int ListenPort *int ClearOnExit bool + FilterDirs bool Version bool } @@ -405,6 +408,7 @@ func defaultOptions() *Options { BorderLabel: labelOpts{}, PreviewLabel: labelOpts{}, ClearOnExit: true, + FilterDirs: false, Version: false} } @@ -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 "--": diff --git a/src/reader.go b/src/reader.go index 494a2f7203c..0f5b805db20 100644 --- a/src/reader.go +++ b/src/reader.go @@ -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() { @@ -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) @@ -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) @@ -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)) } }