reader.go
This file
Path
src/reader.go
117 lines · 2.6 KiB · Go
Last changed by
Update FZF_DEFAULT_COMMANDJunegunn Choibefore adoptionee40212
History of this fileTake it
The walk stopped after 200 commits: lines older than 83e9af6 point at it.
Add git revision to --version outputJunegunn Choibefore adoption83e9af6
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"Add git revision to --version outputJunegunn Choibefore adoption83e9af6
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 int32Add git revision to --version outputJunegunn Choibefore adoption83e9af6
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}51Add git revision to --version outputJunegunn Choibefore adoption83e9af6
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 boolAdd git revision to --version outputJunegunn Choibefore adoption83e9af6
lines 56–58why, one step back56 if util.IsTty() {57 cmd := os.Getenv("FZF_DEFAULT_COMMAND")58 if len(cmd) == 0 {Update FZF_DEFAULT_COMMANDJunegunn Choibefore adoptionee40212
lines 59–62why, one step back59 // The default command for *nix requires bash60 success = r.readFromCommand("bash", defaultCommand)61 } else {62 success = r.readFromCommand("sh", cmd)Add git revision to --version outputJunegunn Choibefore adoption83e9af6
lines 63–64why, one step back63 }64 } else {Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0
line 65why, one step back65 success = r.readFromStdin()Add git revision to --version outputJunegunn Choibefore adoption83e9af6
line 66why, one step back66 }Make Reader event notification asynchronousJunegunn Choibefore adoption487c8fe
line 67why, one step back67 r.fin(success)Add git revision to --version outputJunegunn Choibefore adoption83e9af6
lines 68–80why, one step back68}6970func (r *Reader) feed(src io.Reader) {71 delim := byte('\n')72 if r.delimNil {73 delim = '\000'74 }75 reader := bufio.NewReaderSize(src, readerBufferSize)76 for {77 // ReadBytes returns err != nil if and only if the returned data does not78 // end in delim.79 bytea, err := reader.ReadBytes(delim)80 byteaLen := len(bytea)Minor refactoringsJunegunn Choibefore adoptionaf809c9
line 81why, one step back81 if byteaLen > 0 {Add git revision to --version outputJunegunn Choibefore adoption83e9af6
lines 82–90why, one step back82 if err == nil {83 // get rid of carriage return if under Windows:84 if util.IsWindows() && byteaLen >= 2 && bytea[byteaLen-2] == byte('\r') {85 bytea = bytea[:byteaLen-2]86 } else {87 bytea = bytea[:byteaLen-1]88 }89 }90 if r.pusher(bytea) {Make Reader event notification asynchronousJunegunn Choibefore adoption487c8fe
line 91why, one step back91 atomic.StoreInt32(&r.event, int32(EvtReadNew))Add git revision to --version outputJunegunn Choibefore adoption83e9af6
lines 92–99why, one step back92 }93 }94 if err != nil {95 break96 }97 }98}99Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0
line 100why, one step back100func (r *Reader) readFromStdin() bool {Add git revision to --version outputJunegunn Choibefore adoption83e9af6
line 101why, one step back101 r.feed(os.Stdin)Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0
line 102why, one step back102 return trueAdd git revision to --version outputJunegunn Choibefore adoption83e9af6
lines 103–104why, one step back103}104Update FZF_DEFAULT_COMMANDJunegunn Choibefore adoptionee40212
lines 105–106why, one step back105func (r *Reader) readFromCommand(shell string, cmd string) bool {106 listCommand := util.ExecCommandWith(shell, cmd)Add git revision to --version outputJunegunn Choibefore adoption83e9af6
lines 107–108why, one step back107 out, err := listCommand.StdoutPipe()108 if err != nil {Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0
line 109why, one step back109 return falseAdd git revision to --version outputJunegunn Choibefore adoption83e9af6
lines 110–112why, one step back110 }111 err = listCommand.Start()112 if err != nil {Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0
line 113why, one step back113 return falseAdd git revision to --version outputJunegunn Choibefore adoption83e9af6
lines 114–115why, one step back114 }115 r.feed(out)Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0
line 116why, one step back116 return listCommand.Wait() == nilAdd git revision to --version outputJunegunn Choibefore adoption83e9af6
line 117why, one step back117}