Skip to content

Commit

Permalink
Nested-for loop instead of map lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
junegunn committed Mar 7, 2024
1 parent 3a0ffe5 commit dbf94eb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
20 changes: 10 additions & 10 deletions src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,18 +357,18 @@ type Options struct {
ClearOnExit bool
WalkerOpts walkerOpts
WalkerRoot string
WalkerSkip map[string]struct{}
WalkerSkip []string
Version bool
}

func buildSkipSet(ignore []string) map[string]struct{} {
set := make(map[string]struct{})
for _, path := range ignore {
if len(path) > 0 {
set[path] = struct{}{}
func filterNonEmpty(input []string) []string {
output := make([]string, 0, len(input))
for _, str := range input {
if len(str) > 0 {
output = append(output, str)
}
}
return set
return output
}

func defaultPreviewOpts(command string) previewOpts {
Expand Down Expand Up @@ -441,7 +441,7 @@ func defaultOptions() *Options {
ClearOnExit: true,
WalkerOpts: walkerOpts{file: true, hidden: true, follow: true},
WalkerRoot: ".",
WalkerSkip: buildSkipSet([]string{".git", "node_modules"}),
WalkerSkip: []string{".git", "node_modules"},
Version: false}
}

Expand Down Expand Up @@ -1938,7 +1938,7 @@ func parseOptions(opts *Options, allArgs []string) {
case "--walker-root":
opts.WalkerRoot = nextString(allArgs, &i, "directory required")
case "--walker-skip":
opts.WalkerSkip = buildSkipSet(strings.Split(nextString(allArgs, &i, "directory names to ignore required"), ","))
opts.WalkerSkip = filterNonEmpty(strings.Split(nextString(allArgs, &i, "directory names to ignore required"), ","))
case "--version":
opts.Version = true
case "--":
Expand Down Expand Up @@ -2041,7 +2041,7 @@ func parseOptions(opts *Options, allArgs []string) {
} else if match, value := optString(arg, "--walker-root="); match {
opts.WalkerRoot = value
} else if match, value := optString(arg, "--walker-skip="); match {
opts.WalkerSkip = buildSkipSet(strings.Split(value, ","))
opts.WalkerSkip = filterNonEmpty(strings.Split(value, ","))
} else if match, value := optString(arg, "--hscroll-off="); match {
opts.HscrollOff = atoi(value)
} else if match, value := optString(arg, "--scroll-off="); match {
Expand Down
21 changes: 12 additions & 9 deletions src/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (r *Reader) restart(command string, environ []string) {
}

// ReadSource reads data from the default command or from standard input
func (r *Reader) ReadSource(root string, opts walkerOpts, ignores map[string]struct{}) {
func (r *Reader) ReadSource(root string, opts walkerOpts, ignores []string) {
r.startEventPoller()
var success bool
if util.IsTty() {
Expand Down Expand Up @@ -145,23 +145,26 @@ func (r *Reader) readFromStdin() bool {
return true
}

func (r *Reader) readFiles(root string, opts walkerOpts, ignores map[string]struct{}) bool {
func (r *Reader) readFiles(root string, opts walkerOpts, ignores []string) bool {
r.killed = false
conf := fastwalk.Config{Follow: opts.follow}
contains := func(str string) bool {
_, prs := ignores[str]
return prs
}

fn := func(path string, de os.DirEntry, err error) error {
if err != nil {
return nil
}
path = filepath.Clean(path)
if path != "." {
isDir := de.IsDir()
if isDir && ((!opts.hidden && filepath.Base(path)[0] == '.') || contains(filepath.Base(path))) {
return filepath.SkipDir
if isDir {
base := filepath.Base(path)
if !opts.hidden && base[0] == '.' {
return filepath.SkipDir
}
for _, ignore := range ignores {
if ignore == base {
return filepath.SkipDir
}
}
}
if ((opts.file && !isDir) || (opts.dir && isDir)) && r.pusher([]byte(path)) {
atomic.StoreInt32(&r.event, int32(EvtReadNew))
Expand Down

0 comments on commit dbf94eb

Please sign in to comment.