Skip to content
Snippets Groups Projects
reader.go 2.57 KiB
Newer Older
Junegunn Choi's avatar
Junegunn Choi committed
package fzf

import (
	"bufio"
	"io"
	"os"
	"sync/atomic"
	"time"
Junegunn Choi's avatar
Junegunn Choi committed

	"github.com/junegunn/fzf/src/util"
Junegunn Choi's avatar
Junegunn Choi committed
)

Junegunn Choi's avatar
Junegunn Choi committed
// Reader reads from command or standard input
Junegunn Choi's avatar
Junegunn Choi committed
type Reader struct {
	pusher   func([]byte) bool
Junegunn Choi's avatar
Junegunn Choi committed
	eventBox *util.EventBox
	event    int32
}

// NewReader returns new Reader object
func NewReader(pusher func([]byte) bool, eventBox *util.EventBox, delimNil bool) *Reader {
	return &Reader{pusher, eventBox, delimNil, int32(EvtReady)}
}

func (r *Reader) startEventPoller() {
	go func() {
		ptr := &r.event
		pollInterval := readerPollIntervalMin
		for {
			if atomic.CompareAndSwapInt32(ptr, int32(EvtReadNew), int32(EvtReady)) {
				r.eventBox.Set(EvtReadNew, true)
				pollInterval = readerPollIntervalMin
			} else if atomic.LoadInt32(ptr) == int32(EvtReadFin) {
				return
			} else {
				pollInterval += readerPollIntervalStep
				if pollInterval > readerPollIntervalMax {
					pollInterval = readerPollIntervalMax
				}
			}
			time.Sleep(pollInterval)
		}
	}()
}

func (r *Reader) fin(success bool) {
	atomic.StoreInt32(&r.event, int32(EvtReadFin))
	r.eventBox.Set(EvtReadFin, success)
Junegunn Choi's avatar
Junegunn Choi committed
}

Junegunn Choi's avatar
Junegunn Choi committed
// ReadSource reads data from the default command or from standard input
Junegunn Choi's avatar
Junegunn Choi committed
func (r *Reader) ReadSource() {
	r.startEventPoller()
Junegunn Choi's avatar
Junegunn Choi committed
	if util.IsTty() {
Junegunn Choi's avatar
Junegunn Choi committed
		cmd := os.Getenv("FZF_DEFAULT_COMMAND")
		if len(cmd) == 0 {
			// The default command for *nix requires bash
			success = r.readFromCommand("bash", defaultCommand)
		} else {
			success = r.readFromCommand("sh", cmd)
Junegunn Choi's avatar
Junegunn Choi committed
		}
	} else {
		success = r.readFromStdin()
Junegunn Choi's avatar
Junegunn Choi committed
	}
Junegunn Choi's avatar
Junegunn Choi committed
}

func (r *Reader) feed(src io.Reader) {
	delim := byte('\n')
	if r.delimNil {
		delim = '\000'
	}
	reader := bufio.NewReaderSize(src, readerBufferSize)
		// ReadBytes returns err != nil if and only if the returned data does not
		// end in delim.
		bytea, err := reader.ReadBytes(delim)
		byteaLen := len(bytea)
Junegunn Choi's avatar
Junegunn Choi committed
		if byteaLen > 0 {
				// get rid of carriage return if under Windows:
				if util.IsWindows() && byteaLen >= 2 && bytea[byteaLen-2] == byte('\r') {
					bytea = bytea[:byteaLen-2]
				} else {
					bytea = bytea[:byteaLen-1]
				}
				atomic.StoreInt32(&r.event, int32(EvtReadNew))
Junegunn Choi's avatar
Junegunn Choi committed
		}
func (r *Reader) readFromStdin() bool {
Junegunn Choi's avatar
Junegunn Choi committed
	r.feed(os.Stdin)
func (r *Reader) readFromCommand(shell string, cmd string) bool {
	listCommand := util.ExecCommandWith(shell, cmd)
Junegunn Choi's avatar
Junegunn Choi committed
	out, err := listCommand.StdoutPipe()
	if err != nil {
Junegunn Choi's avatar
Junegunn Choi committed
	}
	err = listCommand.Start()
	if err != nil {
Junegunn Choi's avatar
Junegunn Choi committed
	}
	r.feed(out)
	return listCommand.Wait() == nil
Junegunn Choi's avatar
Junegunn Choi committed
}