reader.go
This file
Path
src/reader.go
201 lines · 4.2 KiB · Go
Last changed by
[windows] Do not include directories in the listJunegunn Choibefore adoptionc39113e
History of this fileTake it
The walk stopped after 200 commits: lines older than 49d0437 point at it.
[install] Detect MSYS on Windows (#1391)Jan Edmund Lazobefore adoption49d0437
lines 1–4why, one step back1package fzf23import (4 "bufio"[windows] Use native walker since output of DOS command is not UTF-8 encoded (#1847)mattnbefore adoption311b78a
line 5why, one step back5 "context"[install] Detect MSYS on Windows (#1391)Jan Edmund Lazobefore adoption49d0437
lines 6–7why, one step back6 "io"7 "os"Merge branch 'devel'Junegunn Choibefore adoptionb471042
line 8why, one step back8 "os/exec"[windows] Use native walker since output of DOS command is not UTF-8 encoded (#1847)mattnbefore adoption311b78a
line 9why, one step back9 "path/filepath"Merge branch 'devel'Junegunn Choibefore adoptionb471042
line 10why, one step back10 "sync"[install] Detect MSYS on Windows (#1391)Jan Edmund Lazobefore adoption49d0437
lines 11–14why, one step back11 "sync/atomic"12 "time"1314 "github.com/junegunn/fzf/src/util"[windows] Use native walker since output of DOS command is not UTF-8 encoded (#1847)mattnbefore adoption311b78a
line 15why, one step back15 "github.com/saracen/walker"[install] Detect MSYS on Windows (#1391)Jan Edmund Lazobefore adoption49d0437
lines 16–23why, one step back16)1718// Reader reads from command or standard input19type Reader struct {20 pusher func([]byte) bool21 eventBox *util.EventBox22 delimNil bool23 event int32Merge branch 'devel'Junegunn Choibefore adoptionb471042
lines 24–29why, one step back24 finChan chan bool25 mutex sync.Mutex26 exec *exec.Cmd27 command *string28 killed bool29 wait bool[install] Detect MSYS on Windows (#1391)Jan Edmund Lazobefore adoption49d0437
lines 30–32why, one step back30}3132// NewReader returns new Reader objectMerge branch 'devel'Junegunn Choibefore adoptionb471042
lines 33–34why, one step back33func 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}[install] Detect MSYS on Windows (#1391)Jan Edmund Lazobefore adoption49d0437
lines 35–42why, one step back35}3637func (r *Reader) startEventPoller() {38 go func() {39 ptr := &r.event40 pollInterval := readerPollIntervalMin41 for {42 if atomic.CompareAndSwapInt32(ptr, int32(EvtReadNew), int32(EvtReady)) {Merge branch 'devel'Junegunn Choibefore adoptionb471042
line 43why, one step back43 r.eventBox.Set(EvtReadNew, (*string)(nil))[install] Detect MSYS on Windows (#1391)Jan Edmund Lazobefore adoption49d0437
lines 44–45why, one step back44 pollInterval = readerPollIntervalMin45 } else if atomic.LoadInt32(ptr) == int32(EvtReadFin) {Merge branch 'devel'Junegunn Choibefore adoptionb471042
lines 46–48why, one step back46 if r.wait {47 r.finChan <- true48 }[install] Detect MSYS on Windows (#1391)Jan Edmund Lazobefore adoption49d0437
lines 49–62why, one step back49 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))Merge branch 'devel'Junegunn Choibefore adoptionb471042
lines 63–74why, one step back63 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)[install] Detect MSYS on Windows (#1391)Jan Edmund Lazobefore adoption49d0437
lines 75–76why, one step back75}76Merge branch 'devel'Junegunn Choibefore adoptionb471042
lines 77–83why, one step back77func (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)[windows] Use native walker since output of DOS command is not UTF-8 encoded (#1847)mattnbefore adoption311b78a
line 84why, one step back84 } else if defaultCommand != "" {Merge branch 'devel'Junegunn Choibefore adoptionb471042
lines 85–95why, one step back85 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}95[install] Detect MSYS on Windows (#1391)Jan Edmund Lazobefore adoption49d0437
lines 96–100why, one step back96// ReadSource reads data from the default command or from standard input97func (r *Reader) ReadSource() {98 r.startEventPoller()99 var success bool100 if util.IsTty() {Merge branch 'devel'Junegunn Choibefore adoptionb471042
lines 101–102why, one step back101 // The default command for *nix requires bash102 shell := "bash"[install] Detect MSYS on Windows (#1391)Jan Edmund Lazobefore adoption49d0437
lines 103–104why, one step back103 cmd := os.Getenv("FZF_DEFAULT_COMMAND")104 if len(cmd) == 0 {[windows] Use native walker since output of DOS command is not UTF-8 encoded (#1847)mattnbefore adoption311b78a
lines 105–109why, one step back105 if defaultCommand != "" {106 success = r.readFromCommand(&shell, defaultCommand)107 } else {108 success = r.readFiles()109 }[install] Detect MSYS on Windows (#1391)Jan Edmund Lazobefore adoption49d0437
line 110why, one step back110 } else {Merge branch 'devel'Junegunn Choibefore adoptionb471042
line 111why, one step back111 success = r.readFromCommand(nil, cmd)[install] Detect MSYS on Windows (#1391)Jan Edmund Lazobefore adoption49d0437
lines 112–151why, one step back112 }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 true[windows] Use native walker since output of DOS command is not UTF-8 encoded (#1847)mattnbefore adoption311b78a
lines 152–158why, one step back152}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 != "." {[windows] Do not include directories in the listJunegunn Choibefore adoptionc39113e
lines 159–160why, one step back159 isDir := mode.Mode().IsDir()160 if isDir && filepath.Base(path)[0] == '.' {[windows] Use native walker since output of DOS command is not UTF-8 encoded (#1847)mattnbefore adoption311b78a
lines 161–162why, one step back161 return filepath.SkipDir162 }[windows] Do not include directories in the listJunegunn Choibefore adoptionc39113e
line 163why, one step back163 if !isDir && r.pusher([]byte(path)) {[windows] Use native walker since output of DOS command is not UTF-8 encoded (#1847)mattnbefore adoption311b78a
lines 164–177why, one step back164 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) == nil[install] Detect MSYS on Windows (#1391)Jan Edmund Lazobefore adoption49d0437
lines 178–179why, one step back178}179Merge branch 'devel'Junegunn Choibefore adoptionb471042
lines 180–189why, one step back180func (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()[install] Detect MSYS on Windows (#1391)Jan Edmund Lazobefore adoption49d0437
line 190why, one step back190 if err != nil {Merge branch 'devel'Junegunn Choibefore adoptionb471042
line 191why, one step back191 r.mutex.Unlock()[install] Detect MSYS on Windows (#1391)Jan Edmund Lazobefore adoption49d0437
lines 192–193why, one step back192 return false193 }Merge branch 'devel'Junegunn Choibefore adoptionb471042
lines 194–195why, one step back194 err = r.exec.Start()195 r.mutex.Unlock()[install] Detect MSYS on Windows (#1391)Jan Edmund Lazobefore adoption49d0437
lines 196–199why, one step back196 if err != nil {197 return false198 }199 r.feed(out)Merge branch 'devel'Junegunn Choibefore adoptionb471042
line 200why, one step back200 return r.exec.Wait() == nil[install] Detect MSYS on Windows (#1391)Jan Edmund Lazobefore adoption49d0437
line 201why, one step back201}