Skip to content

Commit

Permalink
Make --walker-root take multiple arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
junegunn committed Nov 24, 2024
1 parent 3f6a7d6 commit cff9d83
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
4 changes: 2 additions & 2 deletions man/man1/fzf.1
Original file line number Diff line number Diff line change
Expand Up @@ -1005,8 +1005,8 @@ Determines the behavior of the built-in directory walker that is used when
.br

.TP
.B "\-\-walker\-root=DIRS"
Comma-separated list of directory names to start the built-in directory walker.
.B "\-\-walker\-root=DIR [...]"
List of directory names to start the built-in directory walker.
The default value is the current working directory.

.TP
Expand Down
26 changes: 22 additions & 4 deletions src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ type Options struct {
Unsafe bool
ClearOnExit bool
WalkerOpts walkerOpts
WalkerRoot string
WalkerRoot []string
WalkerSkip []string
Version bool
Help bool
Expand Down Expand Up @@ -594,7 +594,7 @@ func defaultOptions() *Options {
Unsafe: false,
ClearOnExit: true,
WalkerOpts: walkerOpts{file: true, hidden: true, follow: true},
WalkerRoot: ".",
WalkerRoot: []string{"."},
WalkerSkip: []string{".git", "node_modules"},
Help: false,
Version: false}
Expand Down Expand Up @@ -626,6 +626,23 @@ func optionalNextString(args []string, i *int) (bool, string) {
return false, ""
}

func nextDirs(args []string, i *int) ([]string, error) {
dirs := []string{}
for *i < len(args)-1 {
arg := args[*i+1]
if stat, err := os.Stat(arg); err == nil && stat.IsDir() {
dirs = append(dirs, arg)
*i++
} else {
break
}
}
if len(dirs) == 0 {
return nil, errors.New("no directory specified")
}
return dirs, nil
}

func atoi(str string) (int, error) {
num, err := strconv.Atoi(str)
if err != nil {
Expand Down Expand Up @@ -2487,7 +2504,7 @@ func parseOptions(index *int, opts *Options, allArgs []string) error {
return err
}
case "--walker-root":
if opts.WalkerRoot, err = nextString(allArgs, &i, "directory required"); err != nil {
if opts.WalkerRoot, err = nextDirs(allArgs, &i); err != nil {
return err
}
case "--walker-skip":
Expand Down Expand Up @@ -2685,7 +2702,8 @@ func parseOptions(index *int, opts *Options, allArgs []string) error {
return err
}
} else if match, value := optString(arg, "--walker-root="); match {
opts.WalkerRoot = value
dirs, _ := nextDirs(allArgs, &i)
opts.WalkerRoot = append([]string{value}, dirs...)
} else if match, value := optString(arg, "--walker-skip="); match {
opts.WalkerSkip = filterNonEmpty(strings.Split(value, ","))
} else if match, value := optString(arg, "--hscroll-off="); match {
Expand Down
10 changes: 4 additions & 6 deletions src/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fzf

import (
"bytes"
"strings"
"context"
"io"
"io/fs"
Expand Down Expand Up @@ -121,7 +120,7 @@ func (r *Reader) readChannel(inputChan chan string) bool {
}

// ReadSource reads data from the default command or from standard input
func (r *Reader) ReadSource(inputChan chan string, root string, opts walkerOpts, ignores []string, initCmd string, initEnv []string, readyChan chan bool) {
func (r *Reader) ReadSource(inputChan chan string, roots []string, opts walkerOpts, ignores []string, initCmd string, initEnv []string, readyChan chan bool) {
r.startEventPoller()
var success bool
signalReady := func() {
Expand All @@ -138,7 +137,7 @@ func (r *Reader) ReadSource(inputChan chan string, root string, opts walkerOpts,
cmd := os.Getenv("FZF_DEFAULT_COMMAND")
if len(cmd) == 0 {
signalReady()
success = r.readFiles(root, opts, ignores)
success = r.readFiles(roots, opts, ignores)
} else {
success = r.readFromCommand(cmd, initEnv, signalReady)
}
Expand Down Expand Up @@ -266,7 +265,7 @@ func trimPath(path string) string {
return byteString(bytes)
}

func (r *Reader) readFiles(root string, opts walkerOpts, ignores []string) bool {
func (r *Reader) readFiles(roots []string, opts walkerOpts, ignores []string) bool {
conf := fastwalk.Config{
Follow: opts.follow,
// Use forward slashes when running a Windows binary under WSL or MSYS
Expand Down Expand Up @@ -302,10 +301,9 @@ func (r *Reader) readFiles(root string, opts walkerOpts, ignores []string) bool
}
return nil
}
roots := strings.Split(root, ",")
noerr := true
for _, root := range roots {
noerr = noerr && (fastwalk.Walk(&conf, root, fn) == nil)
noerr = noerr && (fastwalk.Walk(&conf, root, fn) == nil)
}
return noerr
}
Expand Down

0 comments on commit cff9d83

Please sign in to comment.