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
1package fzf23import (4 "bufio"5 "context"6 "io"7 "os"8 "os/exec"9 "path/filepath"10 "sync"11 "sync/atomic"12 "time"1314 "github.com/charlievieth/fastwalk"15 "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)82 } else {83 os.Stdin.Close()84 }85 r.mutex.Unlock()86}8788func (r *Reader) restart(command string, environ []string) {89 r.event = int32(EvtReady)90 r.startEventPoller()91 success := r.readFromCommand(command, environ)92 r.fin(success)93}9495// ReadSource reads data from the default command or from standard input96func (r *Reader) ReadSource(root string, opts walkerOpts, ignores []string) {97 r.startEventPoller()98 var success bool99 if util.IsTty() {100 cmd := os.Getenv("FZF_DEFAULT_COMMAND")101 if len(cmd) == 0 {102 success = r.readFiles(root, opts, ignores)103 } else {104 // We can't export FZF_* environment variables to the default command105 success = r.readFromCommand(cmd, nil)106 }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}147148func (r *Reader) readFiles(root string, opts walkerOpts, ignores []string) bool {149 r.killed = false150 conf := fastwalk.Config{Follow: opts.follow}151 fn := func(path string, de os.DirEntry, err error) error {152 if err != nil {153 return nil154 }155 path = filepath.Clean(path)156 if path != "." {157 isDir := de.IsDir()158 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 }168 }169 if ((opts.file && !isDir) || (opts.dir && isDir)) && r.pusher([]byte(path)) {170 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 }180 return fastwalk.Walk(&conf, root, fn) == nil181}182183func (r *Reader) readFromCommand(command string, environ []string) bool {184 r.mutex.Lock()185 r.killed = false186 r.command = &command187 r.exec = util.ExecCommand(command, true)188 if environ != nil {189 r.exec.Env = environ190 }191 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}