reader.go
This file
Path
src/reader.go
203 lines · 4.4 KiB · Go
Last changed by
Add walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16
History of this fileTake it
The walk stopped after 200 commits: lines older than 3a96585 point at it.
[vim] Keep jump list unaffected when calling term_startJunegunn Choibefore adoption3a96585
lines 1–13why, one step back1package fzf23import (4 "bufio"5 "context"6 "io"7 "os"8 "os/exec"9 "path/filepath"10 "sync"11 "sync/atomic"12 "time"13Replace "default find command" with built-in directory traversalJunegunn Choibefore adoption208e556
line 14why, one step back14 "github.com/charlievieth/fastwalk"[vim] Keep jump list unaffected when calling term_startJunegunn Choibefore adoption3a96585
lines 15–81why, one step back15 "github.com/junegunn/fzf/src/util"16)1718// Reader reads from command or standard input19type Reader struct {20 pusher func([]byte) bool21 eventBox *util.EventBox22 delimNil bool23 event int3224 finChan chan bool25 mutex sync.Mutex26 exec *exec.Cmd27 command *string28 killed bool29 wait bool30}3132// NewReader returns new Reader object33func NewReader(pusher func([]byte) bool, eventBox *util.EventBox, delimNil bool, wait bool) *Reader {34 return &Reader{pusher, eventBox, delimNil, int32(EvtReady), make(chan bool, 1), sync.Mutex{}, nil, nil, false, wait}35}3637func (r *Reader) startEventPoller() {38 go func() {39 ptr := &r.event40 pollInterval := readerPollIntervalMin41 for {42 if atomic.CompareAndSwapInt32(ptr, int32(EvtReadNew), int32(EvtReady)) {43 r.eventBox.Set(EvtReadNew, (*string)(nil))44 pollInterval = readerPollIntervalMin45 } else if atomic.LoadInt32(ptr) == int32(EvtReadFin) {46 if r.wait {47 r.finChan <- true48 }49 return50 } else {51 pollInterval += readerPollIntervalStep52 if pollInterval > readerPollIntervalMax {53 pollInterval = readerPollIntervalMax54 }55 }56 time.Sleep(pollInterval)57 }58 }()59}6061func (r *Reader) fin(success bool) {62 atomic.StoreInt32(&r.event, int32(EvtReadFin))63 if r.wait {64 <-r.finChan65 }6667 r.mutex.Lock()68 ret := r.command69 if success || r.killed {70 ret = nil71 }72 r.mutex.Unlock()7374 r.eventBox.Set(EvtReadFin, ret)75}7677func (r *Reader) terminate() {78 r.mutex.Lock()79 r.killed = true80 if r.exec != nil && r.exec.Process != nil {81 util.KillCommand(r.exec)Replace "default find command" with built-in directory traversalJunegunn Choibefore adoption208e556
line 82why, one step back82 } else {[vim] Keep jump list unaffected when calling term_startJunegunn Choibefore adoption3a96585
lines 83–84why, one step back83 os.Stdin.Close()84 }Replace "default find command" with built-in directory traversalJunegunn Choibefore adoption208e556
line 85why, one step back85 r.mutex.Unlock()[vim] Keep jump list unaffected when calling term_startJunegunn Choibefore adoption3a96585
lines 86–87why, one step back86}87Export FZF_* variables to 'reload' process as wellJunegunn Choibefore adoption5e6788c
line 88why, one step back88func (r *Reader) restart(command string, environ []string) {[vim] Keep jump list unaffected when calling term_startJunegunn Choibefore adoption3a96585
lines 89–90why, one step back89 r.event = int32(EvtReady)90 r.startEventPoller()Export FZF_* variables to 'reload' process as wellJunegunn Choibefore adoption5e6788c
line 91why, one step back91 success := r.readFromCommand(command, environ)[vim] Keep jump list unaffected when calling term_startJunegunn Choibefore adoption3a96585
lines 92–95why, one step back92 r.fin(success)93}9495// ReadSource reads data from the default command or from standard inputAdd walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16
line 96why, one step back96func (r *Reader) ReadSource(root string, opts walkerOpts, ignores []string) {[vim] Keep jump list unaffected when calling term_startJunegunn Choibefore adoption3a96585
lines 97–101why, one step back97 r.startEventPoller()98 var success bool99 if util.IsTty() {100 cmd := os.Getenv("FZF_DEFAULT_COMMAND")101 if len(cmd) == 0 {Add walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16
line 102why, one step back102 success = r.readFiles(root, opts, ignores)[vim] Keep jump list unaffected when calling term_startJunegunn Choibefore adoption3a96585
line 103why, one step back103 } else {Export FZF_* variables to 'reload' process as wellJunegunn Choibefore adoption5e6788c
lines 104–105why, one step back104 // We can't export FZF_* environment variables to the default command105 success = r.readFromCommand(cmd, nil)[vim] Keep jump list unaffected when calling term_startJunegunn Choibefore adoption3a96585
lines 106–147why, one step back106 }107 } else {108 success = r.readFromStdin()109 }110 r.fin(success)111}112113func (r *Reader) feed(src io.Reader) {114 delim := byte('\n')115 if r.delimNil {116 delim = '\000'117 }118 reader := bufio.NewReaderSize(src, readerBufferSize)119 for {120 // ReadBytes returns err != nil if and only if the returned data does not121 // end in delim.122 bytea, err := reader.ReadBytes(delim)123 byteaLen := len(bytea)124 if byteaLen > 0 {125 if err == nil {126 // get rid of carriage return if under Windows:127 if util.IsWindows() && byteaLen >= 2 && bytea[byteaLen-2] == byte('\r') {128 bytea = bytea[:byteaLen-2]129 } else {130 bytea = bytea[:byteaLen-1]131 }132 }133 if r.pusher(bytea) {134 atomic.StoreInt32(&r.event, int32(EvtReadNew))135 }136 }137 if err != nil {138 break139 }140 }141}142143func (r *Reader) readFromStdin() bool {144 r.feed(os.Stdin)145 return true146}147Add walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16
line 148why, one step back148func (r *Reader) readFiles(root string, opts walkerOpts, ignores []string) bool {[vim] Keep jump list unaffected when calling term_startJunegunn Choibefore adoption3a96585
line 149why, one step back149 r.killed = falseAdd walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16
line 150why, one step back150 conf := fastwalk.Config{Follow: opts.follow}Replace "default find command" with built-in directory traversalJunegunn Choibefore adoption208e556
lines 151–154why, one step back151 fn := func(path string, de os.DirEntry, err error) error {152 if err != nil {153 return nil154 }[vim] Keep jump list unaffected when calling term_startJunegunn Choibefore adoption3a96585
lines 155–156why, one step back155 path = filepath.Clean(path)156 if path != "." {Replace "default find command" with built-in directory traversalJunegunn Choibefore adoption208e556
line 157why, one step back157 isDir := de.IsDir()Add walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16
lines 158–167why, one step back158 if isDir {159 base := filepath.Base(path)160 if !opts.hidden && base[0] == '.' {161 return filepath.SkipDir162 }163 for _, ignore := range ignores {164 if ignore == base {165 return filepath.SkipDir166 }167 }[vim] Keep jump list unaffected when calling term_startJunegunn Choibefore adoption3a96585
line 168why, one step back168 }Add walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16
line 169why, one step back169 if ((opts.file && !isDir) || (opts.dir && isDir)) && r.pusher([]byte(path)) {[vim] Keep jump list unaffected when calling term_startJunegunn Choibefore adoption3a96585
lines 170–179why, one step back170 atomic.StoreInt32(&r.event, int32(EvtReadNew))171 }172 }173 r.mutex.Lock()174 defer r.mutex.Unlock()175 if r.killed {176 return context.Canceled177 }178 return nil179 }Add walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16
line 180why, one step back180 return fastwalk.Walk(&conf, root, fn) == nil[vim] Keep jump list unaffected when calling term_startJunegunn Choibefore adoption3a96585
lines 181–182why, one step back181}182Export FZF_* variables to 'reload' process as wellJunegunn Choibefore adoption5e6788c
line 183why, one step back183func (r *Reader) readFromCommand(command string, environ []string) bool {[vim] Keep jump list unaffected when calling term_startJunegunn Choibefore adoption3a96585
lines 184–186why, one step back184 r.mutex.Lock()185 r.killed = false186 r.command = &commandCode cleanup: Remove unused argumentJunegunn Choibefore adoption7a72f1a
line 187why, one step back187 r.exec = util.ExecCommand(command, true)Export FZF_* variables to 'reload' process as wellJunegunn Choibefore adoption5e6788c
lines 188–190why, one step back188 if environ != nil {189 r.exec.Env = environ190 }[vim] Keep jump list unaffected when calling term_startJunegunn Choibefore adoption3a96585
lines 191–203why, one step back191 out, err := r.exec.StdoutPipe()192 if err != nil {193 r.mutex.Unlock()194 return false195 }196 err = r.exec.Start()197 r.mutex.Unlock()198 if err != nil {199 return false200 }201 r.feed(out)202 return r.exec.Wait() == nil203}