Skip to content
Myr

reader.go

This file

Path

src/reader.go

201 lines · 4.2 KiB · Go

Last changed by

Not known.

History of this file

Take it

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

[make] Add loongarch64 support (#2857)znleybefore adoption885cd8f

lines 1–201why, one step back
1package fzf
23import (4 "bufio"5 "context"6 "io"7 "os"8 "os/exec"9 "path/filepath"10 "sync"11 "sync/atomic"12 "time"1314 "github.com/junegunn/fzf/src/util"15 "github.com/saracen/walker"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 defer func() { r.mutex.Unlock() }()8081 r.killed = true82 if r.exec != nil && r.exec.Process != nil {83 util.KillCommand(r.exec)84 } else if defaultCommand != "" {85 os.Stdin.Close()86 }87}8889func (r *Reader) restart(command string) {90 r.event = int32(EvtReady)91 r.startEventPoller()92 success := r.readFromCommand(nil, command)93 r.fin(success)94}9596// ReadSource reads data from the default command or from standard input97func (r *Reader) ReadSource() {98 r.startEventPoller()99 var success bool100 if util.IsTty() {101 // The default command for *nix requires bash102 shell := "bash"103 cmd := os.Getenv("FZF_DEFAULT_COMMAND")104 if len(cmd) == 0 {105 if defaultCommand != "" {106 success = r.readFromCommand(&shell, defaultCommand)107 } else {108 success = r.readFiles()109 }110 } else {111 success = r.readFromCommand(nil, cmd)112 }113 } else {114 success = r.readFromStdin()115 }116 r.fin(success)117}118119func (r *Reader) feed(src io.Reader) {120 delim := byte('\n')121 if r.delimNil {122 delim = '\000'123 }124 reader := bufio.NewReaderSize(src, readerBufferSize)125 for {126 // ReadBytes returns err != nil if and only if the returned data does not127 // end in delim.128 bytea, err := reader.ReadBytes(delim)129 byteaLen := len(bytea)130 if byteaLen > 0 {131 if err == nil {132 // get rid of carriage return if under Windows:133 if util.IsWindows() && byteaLen >= 2 && bytea[byteaLen-2] == byte('\r') {134 bytea = bytea[:byteaLen-2]135 } else {136 bytea = bytea[:byteaLen-1]137 }138 }139 if r.pusher(bytea) {140 atomic.StoreInt32(&r.event, int32(EvtReadNew))141 }142 }143 if err != nil {144 break145 }146 }147}148149func (r *Reader) readFromStdin() bool {150 r.feed(os.Stdin)151 return true152}153154func (r *Reader) readFiles() bool {155 r.killed = false156 fn := func(path string, mode os.FileInfo) error {157 path = filepath.Clean(path)158 if path != "." {159 isDir := mode.Mode().IsDir()160 if isDir && filepath.Base(path)[0] == '.' {161 return filepath.SkipDir162 }163 if !isDir && r.pusher([]byte(path)) {164 atomic.StoreInt32(&r.event, int32(EvtReadNew))165 }166 }167 r.mutex.Lock()168 defer r.mutex.Unlock()169 if r.killed {170 return context.Canceled171 }172 return nil173 }174 cb := walker.WithErrorCallback(func(pathname string, err error) error {175 return nil176 })177 return walker.Walk(".", fn, cb) == nil178}179180func (r *Reader) readFromCommand(shell *string, command string) bool {181 r.mutex.Lock()182 r.killed = false183 r.command = &command184 if shell != nil {185 r.exec = util.ExecCommandWith(*shell, command, true)186 } else {187 r.exec = util.ExecCommand(command, true)188 }189 out, err := r.exec.StdoutPipe()190 if err != nil {191 r.mutex.Unlock()192 return false193 }194 err = r.exec.Start()195 r.mutex.Unlock()196 if err != nil {197 return false198 }199 r.feed(out)200 return r.exec.Wait() == nil201}