reader.go
This file
Path
src/reader.go
78 lines · 1.5 KiB · Go
Last changed by
Fix issues in tcell renderer and Windows buildJunegunn Choibefore adoption898d8d9
History of this fileTake it
The walk stopped after 200 commits: lines older than ac549a8 point at it.
[fzf-tmux] Fix bash conditionJunegunn Choibefore adoptionac549a8
lines 1–41why, one step back1package fzf23import (4 "bufio"5 "io"6 "os"78 "github.com/junegunn/fzf/src/util"9)1011// Reader reads from command or standard input12type Reader struct {13 pusher func([]byte) bool14 eventBox *util.EventBox15 delimNil bool16}1718// ReadSource reads data from the default command or from standard input19func (r *Reader) ReadSource() {20 if util.IsTty() {21 cmd := os.Getenv("FZF_DEFAULT_COMMAND")22 if len(cmd) == 0 {23 cmd = defaultCommand24 }25 r.readFromCommand(cmd)26 } else {27 r.readFromStdin()28 }29 r.eventBox.Set(EvtReadFin, nil)30}3132func (r *Reader) feed(src io.Reader) {33 delim := byte('\n')34 if r.delimNil {35 delim = '\000'36 }37 reader := bufio.NewReaderSize(src, readerBufferSize)38 for {39 // ReadBytes returns err != nil if and only if the returned data does not40 // end in delim.41 bytea, err := reader.ReadBytes(delim)Implement tcell-based rendererMichael Kelleybefore adoption26895da
line 42why, one step back42 byteaLen := len(bytea)[fzf-tmux] Fix bash conditionJunegunn Choibefore adoptionac549a8
lines 43–44why, one step back43 if len(bytea) > 0 {44 if err == nil {Implement tcell-based rendererMichael Kelleybefore adoption26895da
line 45why, one step back45 // get rid of carriage return if under Windows:Fix issues in tcell renderer and Windows buildJunegunn Choibefore adoption898d8d9
line 46why, one step back46 if util.IsWindows() && byteaLen >= 2 && bytea[byteaLen-2] == byte('\r') {Implement tcell-based rendererMichael Kelleybefore adoption26895da
lines 47–50why, one step back47 bytea = bytea[:byteaLen-2]48 } else {49 bytea = bytea[:byteaLen-1]50 }[fzf-tmux] Fix bash conditionJunegunn Choibefore adoptionac549a8
lines 51–78why, one step back51 }52 if r.pusher(bytea) {53 r.eventBox.Set(EvtReadNew, nil)54 }55 }56 if err != nil {57 break58 }59 }60}6162func (r *Reader) readFromStdin() {63 r.feed(os.Stdin)64}6566func (r *Reader) readFromCommand(cmd string) {67 listCommand := util.ExecCommand(cmd)68 out, err := listCommand.StdoutPipe()69 if err != nil {70 return71 }72 err = listCommand.Start()73 if err != nil {74 return75 }76 defer listCommand.Wait()77 r.feed(out)78}