Skip to content
Myr

reader.go

This file

Path

src/reader.go

117 lines · 2.6 KiB · Go

Last changed by

Kill running preview process after 500ms when focus has changedJunegunn Choibefore adoption0d748a0

History of this file

Take it

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

[fish] Accept starting dir for <M-c> key binding (#944)John Nguyenbefore adoption70cfa6a

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"

[fish] Accept starting dir for <M-c> key binding (#944)John Nguyenbefore adoption70cfa6a

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

[fish] Accept starting dir for <M-c> key binding (#944)John Nguyenbefore adoption70cfa6a

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

[fish] Accept starting dir for <M-c> key binding (#944)John Nguyenbefore adoption70cfa6a

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

[fish] Accept starting dir for <M-c> key binding (#944)John Nguyenbefore adoption70cfa6a

lines 56–58why, one step back
56 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 back
59 // The default command for *nix requires bash
60 success = r.readFromCommand("bash", defaultCommand)61 } else {62 success = r.readFromCommand("sh", cmd)

[fish] Accept starting dir for <M-c> key binding (#944)John Nguyenbefore adoption70cfa6a

lines 63–64why, one step back
63 }
64 } else {

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

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

[fish] Accept starting dir for <M-c> key binding (#944)John Nguyenbefore adoption70cfa6a

line 66why, one step back
66 }

Make Reader event notification asynchronousJunegunn Choibefore adoption487c8fe

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

[fish] Accept starting dir for <M-c> key binding (#944)John Nguyenbefore adoption70cfa6a

lines 68–80why, one step back
68}
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 back
81 if byteaLen > 0 {

[fish] Accept starting dir for <M-c> key binding (#944)John Nguyenbefore adoption70cfa6a

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

[fish] Accept starting dir for <M-c> key binding (#944)John Nguyenbefore adoption70cfa6a

lines 92–99why, one step back
92 }
93 }94 if err != nil {95 break96 }97 }98}99

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

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

[fish] Accept starting dir for <M-c> key binding (#944)John Nguyenbefore adoption70cfa6a

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

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

line 102why, one step back
102 return true

[fish] Accept starting dir for <M-c> key binding (#944)John Nguyenbefore adoption70cfa6a

lines 103–104why, one step back
103}
104

Update FZF_DEFAULT_COMMANDJunegunn Choibefore adoptionee40212

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

Kill running preview process after 500ms when focus has changedJunegunn Choibefore adoption0d748a0

line 106why, one step back
106 listCommand := util.ExecCommandWith(shell, cmd, false)

[fish] Accept starting dir for <M-c> key binding (#944)John Nguyenbefore adoption70cfa6a

lines 107–108why, one step back
107 out, err := listCommand.StdoutPipe()
108 if err != nil {

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

line 109why, one step back
109 return false

[fish] Accept starting dir for <M-c> key binding (#944)John Nguyenbefore adoption70cfa6a

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

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

line 113why, one step back
113 return false

[fish] Accept starting dir for <M-c> key binding (#944)John Nguyenbefore adoption70cfa6a

lines 114–115why, one step back
114 }
115 r.feed(out)

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

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

[fish] Accept starting dir for <M-c> key binding (#944)John Nguyenbefore adoption70cfa6a

line 117why, one step back
117}