From 311b78ae82fc8b06f0eefbd54d6290f3a40d5e23 Mon Sep 17 00:00:00 2001 From: mattn Date: Tue, 4 Feb 2020 12:31:00 +0900 Subject: [windows] Use native walker since output of DOS command is not UTF-8 encoded (#1847) Makes scanning 300x faster on Windows --- src/reader.go | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'src/reader.go') diff --git a/src/reader.go b/src/reader.go index b388411b..7ed5cfce 100644 --- a/src/reader.go +++ b/src/reader.go @@ -2,14 +2,17 @@ package fzf import ( "bufio" + "context" "io" "os" "os/exec" + "path/filepath" "sync" "sync/atomic" "time" "github.com/junegunn/fzf/src/util" + "github.com/saracen/walker" ) // Reader reads from command or standard input @@ -78,7 +81,7 @@ func (r *Reader) terminate() { r.killed = true if r.exec != nil && r.exec.Process != nil { util.KillCommand(r.exec) - } else { + } else if defaultCommand != "" { os.Stdin.Close() } } @@ -99,7 +102,11 @@ func (r *Reader) ReadSource() { shell := "bash" cmd := os.Getenv("FZF_DEFAULT_COMMAND") if len(cmd) == 0 { - success = r.readFromCommand(&shell, defaultCommand) + if defaultCommand != "" { + success = r.readFromCommand(&shell, defaultCommand) + } else { + success = r.readFiles() + } } else { success = r.readFromCommand(nil, cmd) } @@ -144,6 +151,31 @@ func (r *Reader) readFromStdin() bool { return true } +func (r *Reader) readFiles() bool { + r.killed = false + fn := func(path string, mode os.FileInfo) error { + path = filepath.Clean(path) + if path != "." { + if mode.Mode().IsDir() && filepath.Base(path)[0] == '.' { + return filepath.SkipDir + } + if r.pusher([]byte(path)) { + atomic.StoreInt32(&r.event, int32(EvtReadNew)) + } + } + r.mutex.Lock() + defer r.mutex.Unlock() + if r.killed { + return context.Canceled + } + return nil + } + cb := walker.WithErrorCallback(func(pathname string, err error) error { + return nil + }) + return walker.Walk(".", fn, cb) == nil +} + func (r *Reader) readFromCommand(shell *string, command string) bool { r.mutex.Lock() r.killed = false -- cgit v1.2.3