Skip to content
Myr

reader.go

This file

Path

src/reader.go

115 lines · 2.5 KiB · Go

Last changed by

Minor refactoringsJunegunn Choibefore adoptionaf809c9

History of this file

Take 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 back
1package fzf
23import (4 "bufio"5 "io"6 "os"

Make Reader event notification asynchronousJunegunn Choibefore adoption487c8fe

lines 7–8why, one step back
7 "sync/atomic"
8 "time"

Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5

lines 9–17why, one step back
9
10 "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 bool

Make Reader event notification asynchronousJunegunn Choibefore adoption487c8fe

line 18why, one step back
18 event int32

Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5

lines 19–20why, one step back
19}
20

Make Reader event notification asynchronousJunegunn Choibefore adoption487c8fe

lines 21–51why, one step back
21// NewReader returns new Reader object
22func 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}51

Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5

lines 52–53why, one step back
52// ReadSource reads data from the default command or from standard input
53func (r *Reader) ReadSource() {

Make Reader event notification asynchronousJunegunn Choibefore adoption487c8fe

line 54why, one step back
54 r.startEventPoller()

Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0

line 55why, one step back
55 var success bool

Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5

lines 56–60why, one step back
56 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 back
61 success = r.readFromCommand(cmd)

Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5

line 62why, one step back
62 } else {

Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0

line 63why, one step back
63 success = r.readFromStdin()

Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5

line 64why, one step back
64 }

Make Reader event notification asynchronousJunegunn Choibefore adoption487c8fe

line 65why, one step back
65 r.fin(success)

Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5

lines 66–78why, one step back
66}
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 back
79 if byteaLen > 0 {

Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5

lines 80–88why, one step back
80 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 back
89 atomic.StoreInt32(&r.event, int32(EvtReadNew))

Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5

lines 90–97why, one step back
90 }
91 }92 if err != nil {93 break94 }95 }96}97

Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0

line 98why, one step back
98func (r *Reader) readFromStdin() bool {

Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5

line 99why, one step back
99 r.feed(os.Stdin)

Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0

line 100why, one step back
100 return true

Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5

lines 101–102why, one step back
101}
102

Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0

line 103why, one step back
103func (r *Reader) readFromCommand(cmd string) bool {

Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5

lines 104–106why, one step back
104 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 back
107 return false

Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5

lines 108–110why, one step back
108 }
109 err = listCommand.Start()110 if err != nil {

Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0

line 111why, one step back
111 return false

Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5

lines 112–113why, one step back
112 }
113 r.feed(out)

Print [ERROR] on info line when the default command failedJunegunn Choibefore adoption07ef2b0

line 114why, one step back
114 return listCommand.Wait() == nil

Disable mouse if failed to query cursor positionJunegunn Choibefore adoption12a43b5

line 115why, one step back
115}