Skip to content
Myr

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 file

Take it

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

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 1–4why, one step back
1package fzf
23import (4 "bufio"

[windows] Use native walker since output of DOS command is not UTF-8 encoded (#1847)mattnbefore adoption311b78a

line 5why, one step back
5 "context"

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 6–8why, one step back
6 "io"
7 "os"8 "os/exec"

[windows] Use native walker since output of DOS command is not UTF-8 encoded (#1847)mattnbefore adoption311b78a

line 9why, one step back
9 "path/filepath"

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 10–14why, one step back
10 "sync"
11 "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 back
15 "github.com/saracen/walker"

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 16–83why, one step back
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)

[windows] Use native walker since output of DOS command is not UTF-8 encoded (#1847)mattnbefore adoption311b78a

line 84why, one step back
84 } else if defaultCommand != "" {

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 85–104why, one step back
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 {

[windows] Use native walker since output of DOS command is not UTF-8 encoded (#1847)mattnbefore adoption311b78a

lines 105–109why, one step back
105 if defaultCommand != "" {
106 success = r.readFromCommand(&shell, defaultCommand)107 } else {108 success = r.readFiles()109 }

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 110–151why, one step back
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 true

[windows] Use native walker since output of DOS command is not UTF-8 encoded (#1847)mattnbefore adoption311b78a

lines 152–158why, one step back
152}
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 back
159 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 back
161 return filepath.SkipDir
162 }

[windows] Do not include directories in the listJunegunn Choibefore adoptionc39113e

line 163why, one step back
163 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 back
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) == nil

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 178–201why, one step back
178}
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}