reader.go
This file
Path
src/reader.go
115 lines · 2.5 KiB · Go
Last changed by
Minor refactoringsJunegunn Choibefore adoptionaf809c9
History of this fileTake it
The walk stopped after 200 commits: lines older than 12a43b5 point at it.
Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5
lines 1–6why, one step back1package fzf23import (4 "bufio"5 "io"6 "os"Make Reader event notification asynchronousJunegunn Choibefore adoption487c8fe
lines 7–8why, one step back7 "sync/atomic"8 "time"Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5
lines 9–17why, one step back910 "github.com/junegunn/fzf/src/util"11)1213// Reader reads from command or standard input14type Reader struct {15 pusher func([]byte) bool16 eventBox *util.EventBox17 delimNil boolMake Reader event notification asynchronousJunegunn Choibefore adoption487c8fe
line 18why, one step back18 event int32Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5
lines 19–20why, one step back19}20Make Reader event notification asynchronousJunegunn Choibefore adoption487c8fe
lines 21–51why, one step back21// NewReader returns new Reader object22func NewReader(pusher func([]byte) bool, eventBox *util.EventBox, delimNil bool) *Reader {23 return &Reader{pusher, eventBox, delimNil, int32(EvtReady)}24}2526func (r *Reader) startEventPoller() {27 go func() {28 ptr := &r.event29 pollInterval := readerPollIntervalMin30 for {31 if atomic.CompareAndSwapInt32(ptr, int32(EvtReadNew), int32(EvtReady)) {32 r.eventBox.Set(EvtReadNew, true)33 pollInterval = readerPollIntervalMin34 } else if atomic.LoadInt32(ptr) == int32(EvtReadFin) {35 return36 } else {37 pollInterval += readerPollIntervalStep38 if pollInterval > readerPollIntervalMax {39 pollInterval = readerPollIntervalMax40 }41 }42 time.Sleep(pollInterval)43 }44 }()45}4647func (r *Reader) fin(success bool) {48 atomic.StoreInt32(&r.event, int32(EvtReadFin))49 r.eventBox.Set(EvtReadFin, success)50}51Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5
lines 52–53why, one step back52// ReadSource reads data from the default command or from standard input53func (r *Reader) ReadSource() {Make Reader event notification asynchronousJunegunn Choibefore adoption487c8fe
line 54why, one step back54 r.startEventPoller()Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0
line 55why, one step back55 var success boolDisable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5
lines 56–60why, one step back56 if util.IsTty() {57 cmd := os.Getenv("FZF_DEFAULT_COMMAND")58 if len(cmd) == 0 {59 cmd = defaultCommand60 }Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0
line 61why, one step back61 success = r.readFromCommand(cmd)Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5
line 62why, one step back62 } else {Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0
line 63why, one step back63 success = r.readFromStdin()Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5
line 64why, one step back64 }Make Reader event notification asynchronousJunegunn Choibefore adoption487c8fe
line 65why, one step back65 r.fin(success)Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5
lines 66–78why, one step back66}6768func (r *Reader) feed(src io.Reader) {69 delim := byte('\n')70 if r.delimNil {71 delim = '\000'72 }73 reader := bufio.NewReaderSize(src, readerBufferSize)74 for {75 // ReadBytes returns err != nil if and only if the returned data does not76 // end in delim.77 bytea, err := reader.ReadBytes(delim)78 byteaLen := len(bytea)Minor refactoringsJunegunn Choibefore adoptionaf809c9
line 79why, one step back79 if byteaLen > 0 {Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5
lines 80–88why, one step back80 if err == nil {81 // get rid of carriage return if under Windows:82 if util.IsWindows() && byteaLen >= 2 && bytea[byteaLen-2] == byte('\r') {83 bytea = bytea[:byteaLen-2]84 } else {85 bytea = bytea[:byteaLen-1]86 }87 }88 if r.pusher(bytea) {Make Reader event notification asynchronousJunegunn Choibefore adoption487c8fe
line 89why, one step back89 atomic.StoreInt32(&r.event, int32(EvtReadNew))Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5
lines 90–97why, one step back90 }91 }92 if err != nil {93 break94 }95 }96}97Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0
line 98why, one step back98func (r *Reader) readFromStdin() bool {Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5
line 99why, one step back99 r.feed(os.Stdin)Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0
line 100why, one step back100 return trueDisable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5
lines 101–102why, one step back101}102Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0
line 103why, one step back103func (r *Reader) readFromCommand(cmd string) bool {Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5
lines 104–106why, one step back104 listCommand := util.ExecCommand(cmd)105 out, err := listCommand.StdoutPipe()106 if err != nil {Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0
line 107why, one step back107 return falseDisable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5
lines 108–110why, one step back108 }109 err = listCommand.Start()110 if err != nil {Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0
line 111why, one step back111 return falseDisable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5
lines 112–113why, one step back112 }113 r.feed(out)Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0
line 114why, one step back114 return listCommand.Wait() == nilDisable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5
line 115why, one step back115}