Skip to content
Myr

reader.go

This file

Path

src/reader.go

278 lines · 6.1 KiB · Go

Last changed by

Refactor the code to remove global variablesJunegunn Choibefore adoption4bedd33

History of this file

Take it

The walk stopped after 200 commits: lines older than d75bb5c point at it.

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 1–3why, one step back
1package fzf
23import (

Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37

line 4why, one step back
4 "bytes"

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 5–13why, one step back
5 "context"
6 "io"7 "os"8 "os/exec"9 "path/filepath"10 "sync"11 "sync/atomic"12 "time"13

Replace "default find command" with built-in directory traversalJunegunn Choibefore adoption208e556

line 14why, one step back
14 "github.com/charlievieth/fastwalk"

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 15–20why, one step back
15 "github.com/junegunn/fzf/src/util"
16)1718// Reader reads from command or standard input19type Reader struct {20 pusher func([]byte) bool

Add --with-shell for shelling out with different command and flags (#3746)Junegunn Choibefore adoptiona4391ae

line 21why, one step back
21 executor *util.Executor

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 22–33why, one step back
22 eventBox *util.EventBox
23 delimNil bool24 event int3225 finChan chan bool26 mutex sync.Mutex27 exec *exec.Cmd28 command *string29 killed bool30 wait bool31}3233// NewReader returns new Reader object

Add --with-shell for shelling out with different command and flags (#3746)Junegunn Choibefore adoptiona4391ae

lines 34–35why, one step back
34func NewReader(pusher func([]byte) bool, eventBox *util.EventBox, executor *util.Executor, delimNil bool, wait bool) *Reader {
35 return &Reader{pusher, executor, eventBox, delimNil, int32(EvtReady), make(chan bool, 1), sync.Mutex{}, nil, nil, false, wait}

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 36–82why, one step back
36}
3738func (r *Reader) startEventPoller() {39 go func() {40 ptr := &r.event41 pollInterval := readerPollIntervalMin42 for {43 if atomic.CompareAndSwapInt32(ptr, int32(EvtReadNew), int32(EvtReady)) {44 r.eventBox.Set(EvtReadNew, (*string)(nil))45 pollInterval = readerPollIntervalMin46 } else if atomic.LoadInt32(ptr) == int32(EvtReadFin) {47 if r.wait {48 r.finChan <- true49 }50 return51 } else {52 pollInterval += readerPollIntervalStep53 if pollInterval > readerPollIntervalMax {54 pollInterval = readerPollIntervalMax55 }56 }57 time.Sleep(pollInterval)58 }59 }()60}6162func (r *Reader) fin(success bool) {63 atomic.StoreInt32(&r.event, int32(EvtReadFin))64 if r.wait {65 <-r.finChan66 }6768 r.mutex.Lock()69 ret := r.command70 if success || r.killed {71 ret = nil72 }73 r.mutex.Unlock()7475 r.eventBox.Set(EvtReadFin, ret)76}7778func (r *Reader) terminate() {79 r.mutex.Lock()80 r.killed = true81 if r.exec != nil && r.exec.Process != nil {82 util.KillCommand(r.exec)

Replace "default find command" with built-in directory traversalJunegunn Choibefore adoption208e556

line 83why, one step back
83 } else {

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 84–85why, one step back
84 os.Stdin.Close()
85 }

Replace "default find command" with built-in directory traversalJunegunn Choibefore adoption208e556

line 86why, one step back
86 r.mutex.Unlock()

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 87–88why, one step back
87}
88

Refactor the code to remove global variablesJunegunn Choibefore adoption4bedd33

line 89why, one step back
89func (r *Reader) restart(command commandSpec, environ []string) {

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 90–91why, one step back
90 r.event = int32(EvtReady)
91 r.startEventPoller()

Refactor the code to remove global variablesJunegunn Choibefore adoption4bedd33

line 92why, one step back
92 success := r.readFromCommand(command.command, environ)

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

line 93why, one step back
93 r.fin(success)

Refactor the code to remove global variablesJunegunn Choibefore adoption4bedd33

line 94why, one step back
94 removeFiles(command.tempFiles)

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 95–96why, one step back
95}
96

Refactor the code so that fzf can be used as a library (#3769)Junegunn Choibefore adoptione8405f4

lines 97–109why, one step back
97func (r *Reader) readChannel(inputChan chan string) bool {
98 for {99 item, more := <-inputChan100 if !more {101 break102 }103 if r.pusher([]byte(item)) {104 atomic.StoreInt32(&r.event, int32(EvtReadNew))105 }106 }107 return true108}109

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

line 110why, one step back
110// ReadSource reads data from the default command or from standard input

Refactor the code so that fzf can be used as a library (#3769)Junegunn Choibefore adoptione8405f4

line 111why, one step back
111func (r *Reader) ReadSource(inputChan chan string, root string, opts walkerOpts, ignores []string) {

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 112–113why, one step back
112 r.startEventPoller()
113 var success bool

Refactor the code so that fzf can be used as a library (#3769)Junegunn Choibefore adoptione8405f4

lines 114–116why, one step back
114 if inputChan != nil {
115 success = r.readChannel(inputChan)116 } else if util.IsTty() {

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 117–118why, one step back
117 cmd := os.Getenv("FZF_DEFAULT_COMMAND")
118 if len(cmd) == 0 {

Add walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16

line 119why, one step back
119 success = r.readFiles(root, opts, ignores)

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

line 120why, one step back
120 } else {

Export FZF_* variables to 'reload' process as wellJunegunn Choibefore adoption5e6788c

lines 121–122why, one step back
121 // We can't export FZF_* environment variables to the default command
122 success = r.readFromCommand(cmd, nil)

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 123–130why, one step back
123 }
124 } else {125 success = r.readFromStdin()126 }127 r.fin(success)128}129130func (r *Reader) feed(src io.Reader) {

Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37

lines 131–144why, one step back
131 /*
132 readerSlabSize, ae := strconv.Atoi(os.Getenv("SLAB_KB"))133 if ae != nil {134 readerSlabSize = 128 * 1024135 } else {136 readerSlabSize *= 1024137 }138 readerBufferSize, be := strconv.Atoi(os.Getenv("BUF_KB"))139 if be != nil {140 readerBufferSize = 64 * 1024141 } else {142 readerBufferSize *= 1024143 }144 */

Improve ingestion performance (by around 20%)Junegunn Choibefore adoption41b3511

line 145why, one step back
145

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

line 146why, one step back
146 delim := byte('\n')

Do not trim CR on Windows when --read0 is setJunegunn Choibefore adoption68a35e4

line 147why, one step back
147 trimCR := util.IsWindows()

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 148–149why, one step back
148 if r.delimNil {
149 delim = '\000'

Do not trim CR on Windows when --read0 is setJunegunn Choibefore adoption68a35e4

line 150why, one step back
150 trimCR = false

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

line 151why, one step back
151 }

Improve ingestion performance (by around 20%)Junegunn Choibefore adoption41b3511

line 152why, one step back
152

Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37

lines 153–155why, one step back
153 slab := make([]byte, readerSlabSize)
154 leftover := []byte{}155 var err error

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

line 156why, one step back
156 for {

Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37

lines 157–164why, one step back
157 n := 0
158 scope := slab[:util.Min(len(slab), readerBufferSize)]159 for i := 0; i < 100; i++ {160 n, err = src.Read(scope)161 if n > 0 || err != nil {162 break163 }164 }

Improve ingestion performance (by around 20%)Junegunn Choibefore adoption41b3511

line 165why, one step back
165

Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37

line 166why, one step back
166 // We're not making any progress after 100 tries. Stop.

Fix 'reload' not terminating closed standard input streamJunegunn Choibefore adoptionfddbfe7

line 167why, one step back
167 if n == 0 {

Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37

lines 168–169why, one step back
168 break
169 }

Improve ingestion performance (by around 20%)Junegunn Choibefore adoption41b3511

line 170why, one step back
170

Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37

lines 171–178why, one step back
171 buf := slab[:n]
172 slab = slab[n:]173174 for len(buf) > 0 {175 if i := bytes.IndexByte(buf, delim); i >= 0 {176 // Found the delimiter177 slice := buf[:i+1]178 buf = buf[i+1:]

Do not trim CR on Windows when --read0 is setJunegunn Choibefore adoption68a35e4

line 179why, one step back
179 if trimCR && len(slice) >= 2 && slice[len(slice)-2] == byte('\r') {

Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37

lines 180–182why, one step back
180 slice = slice[:len(slice)-2]
181 } else {182 slice = slice[:len(slice)-1]

Improve ingestion performance (by around 20%)Junegunn Choibefore adoption41b3511

line 183why, one step back
183 }

Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37

lines 184–188why, one step back
184 if len(leftover) > 0 {
185 slice = append(leftover, slice...)186 leftover = []byte{}187 }188 if (err == nil || len(slice) > 0) && r.pusher(slice) {

Improve ingestion performance (by around 20%)Junegunn Choibefore adoption41b3511

lines 189–190why, one step back
189 atomic.StoreInt32(&r.event, int32(EvtReadNew))
190 }

Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37

lines 191–192why, one step back
191 } else {
192 // Could not find the delimiter in the buffer

Improve search performance by limiting the search scopeJunegunn Choibefore adoptione86b81b

lines 193–198why, one step back
193 // NOTE: We can further optimize this by keeping track of the cursor
194 // position in the slab so that a straddling item that doesn't go195 // beyond the boundary of a slab doesn't need to be copied to196 // another buffer. However, the performance gain is negligible in197 // practice (< 0.1%) and is not198 // worth the added complexity.

Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37

line 199why, one step back
199 leftover = append(leftover, buf...)

Improve ingestion performance (by around 20%)Junegunn Choibefore adoption41b3511

line 200why, one step back
200 break

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 201–202why, one step back
201 }
202 }

Improve ingestion performance (by around 40%)Junegunn Choibefore adoption5234c37

lines 203–214why, one step back
203
204 if err == io.EOF {205 leftover = append(leftover, buf...)206 break207 }208209 if len(slab) == 0 {210 slab = make([]byte, readerSlabSize)211 }212 }213 if len(leftover) > 0 && r.pusher(leftover) {214 atomic.StoreInt32(&r.event, int32(EvtReadNew))

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 215–222why, one step back
215 }
216}217218func (r *Reader) readFromStdin() bool {219 r.feed(os.Stdin)220 return true221}222

Add walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16

line 223why, one step back
223func (r *Reader) readFiles(root string, opts walkerOpts, ignores []string) bool {

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

line 224why, one step back
224 r.killed = false

Add walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16

line 225why, one step back
225 conf := fastwalk.Config{Follow: opts.follow}

Replace "default find command" with built-in directory traversalJunegunn Choibefore adoption208e556

lines 226–229why, one step back
226 fn := func(path string, de os.DirEntry, err error) error {
227 if err != nil {228 return nil229 }

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 230–231why, one step back
230 path = filepath.Clean(path)
231 if path != "." {

Replace "default find command" with built-in directory traversalJunegunn Choibefore adoption208e556

line 232why, one step back
232 isDir := de.IsDir()

Add walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16

lines 233–242why, one step back
233 if isDir {
234 base := filepath.Base(path)235 if !opts.hidden && base[0] == '.' {236 return filepath.SkipDir237 }238 for _, ignore := range ignores {239 if ignore == base {240 return filepath.SkipDir241 }242 }

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

line 243why, one step back
243 }

Add walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16

line 244why, one step back
244 if ((opts.file && !isDir) || (opts.dir && isDir)) && r.pusher([]byte(path)) {

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 245–254why, one step back
245 atomic.StoreInt32(&r.event, int32(EvtReadNew))
246 }247 }248 r.mutex.Lock()249 defer r.mutex.Unlock()250 if r.killed {251 return context.Canceled252 }253 return nil254 }

Add walker options and replace 'find' with the built-in walker (#3649)Junegunn Choibefore adoptiond282a16

line 255why, one step back
255 return fastwalk.Walk(&conf, root, fn) == nil

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 256–257why, one step back
256}
257

Export FZF_* variables to 'reload' process as wellJunegunn Choibefore adoption5e6788c

line 258why, one step back
258func (r *Reader) readFromCommand(command string, environ []string) bool {

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 259–261why, one step back
259 r.mutex.Lock()
260 r.killed = false261 r.command = &command

Add --with-shell for shelling out with different command and flags (#3746)Junegunn Choibefore adoptiona4391ae

line 262why, one step back
262 r.exec = r.executor.ExecCommand(command, true)

Export FZF_* variables to 'reload' process as wellJunegunn Choibefore adoption5e6788c

lines 263–265why, one step back
263 if environ != nil {
264 r.exec.Env = environ265 }

Deploying to master from @ junegunn/fzf@2671259fdb3094a5eac528f291c1fdadbcf9eda9 🚀junegunnbefore adoptiond75bb5c

lines 266–278why, one step back
266 out, err := r.exec.StdoutPipe()
267 if err != nil {268 r.mutex.Unlock()269 return false270 }271 err = r.exec.Start()272 r.mutex.Unlock()273 if err != nil {274 return false275 }276 r.feed(out)277 return r.exec.Wait() == nil278}