2015-01-01 19:49:30 +00:00
|
|
|
package fzf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-02-04 03:31:00 +00:00
|
|
|
"context"
|
2015-01-01 19:49:30 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2019-11-10 02:36:22 +00:00
|
|
|
"os/exec"
|
2023-07-12 04:55:59 +00:00
|
|
|
"path"
|
2020-02-04 03:31:00 +00:00
|
|
|
"path/filepath"
|
2019-11-10 02:36:22 +00:00
|
|
|
"sync"
|
2017-08-15 18:24:23 +00:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
2015-01-12 03:56:17 +00:00
|
|
|
|
|
|
|
"github.com/junegunn/fzf/src/util"
|
2020-02-04 03:31:00 +00:00
|
|
|
"github.com/saracen/walker"
|
2015-01-01 19:49:30 +00:00
|
|
|
)
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// Reader reads from command or standard input
|
2015-01-01 19:49:30 +00:00
|
|
|
type Reader struct {
|
2015-08-02 05:25:57 +00:00
|
|
|
pusher func([]byte) bool
|
2015-01-12 03:56:17 +00:00
|
|
|
eventBox *util.EventBox
|
2015-06-08 06:36:21 +00:00
|
|
|
delimNil bool
|
2017-08-15 18:24:23 +00:00
|
|
|
event int32
|
2019-11-10 02:36:22 +00:00
|
|
|
finChan chan bool
|
|
|
|
mutex sync.Mutex
|
|
|
|
exec *exec.Cmd
|
|
|
|
command *string
|
|
|
|
killed bool
|
2019-11-11 03:53:03 +00:00
|
|
|
wait bool
|
2017-08-15 18:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewReader returns new Reader object
|
2019-11-11 03:53:03 +00:00
|
|
|
func NewReader(pusher func([]byte) bool, eventBox *util.EventBox, delimNil bool, wait bool) *Reader {
|
|
|
|
return &Reader{pusher, eventBox, delimNil, int32(EvtReady), make(chan bool, 1), sync.Mutex{}, nil, nil, false, wait}
|
2017-08-15 18:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) startEventPoller() {
|
|
|
|
go func() {
|
|
|
|
ptr := &r.event
|
|
|
|
pollInterval := readerPollIntervalMin
|
|
|
|
for {
|
|
|
|
if atomic.CompareAndSwapInt32(ptr, int32(EvtReadNew), int32(EvtReady)) {
|
2019-11-10 02:36:22 +00:00
|
|
|
r.eventBox.Set(EvtReadNew, (*string)(nil))
|
2017-08-15 18:24:23 +00:00
|
|
|
pollInterval = readerPollIntervalMin
|
|
|
|
} else if atomic.LoadInt32(ptr) == int32(EvtReadFin) {
|
2019-11-11 03:53:03 +00:00
|
|
|
if r.wait {
|
|
|
|
r.finChan <- true
|
|
|
|
}
|
2017-08-15 18:24:23 +00:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
pollInterval += readerPollIntervalStep
|
|
|
|
if pollInterval > readerPollIntervalMax {
|
|
|
|
pollInterval = readerPollIntervalMax
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(pollInterval)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) fin(success bool) {
|
|
|
|
atomic.StoreInt32(&r.event, int32(EvtReadFin))
|
2019-11-11 03:53:03 +00:00
|
|
|
if r.wait {
|
|
|
|
<-r.finChan
|
|
|
|
}
|
2019-11-10 02:36:22 +00:00
|
|
|
|
|
|
|
r.mutex.Lock()
|
|
|
|
ret := r.command
|
|
|
|
if success || r.killed {
|
|
|
|
ret = nil
|
|
|
|
}
|
|
|
|
r.mutex.Unlock()
|
|
|
|
|
|
|
|
r.eventBox.Set(EvtReadFin, ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) terminate() {
|
|
|
|
r.mutex.Lock()
|
|
|
|
defer func() { r.mutex.Unlock() }()
|
|
|
|
|
|
|
|
r.killed = true
|
|
|
|
if r.exec != nil && r.exec.Process != nil {
|
|
|
|
util.KillCommand(r.exec)
|
2020-02-04 03:31:00 +00:00
|
|
|
} else if defaultCommand != "" {
|
2019-11-10 02:36:22 +00:00
|
|
|
os.Stdin.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) restart(command string) {
|
|
|
|
r.event = int32(EvtReady)
|
|
|
|
r.startEventPoller()
|
|
|
|
success := r.readFromCommand(nil, command)
|
|
|
|
r.fin(success)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// ReadSource reads data from the default command or from standard input
|
2015-01-01 19:49:30 +00:00
|
|
|
func (r *Reader) ReadSource() {
|
2017-08-15 18:24:23 +00:00
|
|
|
r.startEventPoller()
|
2017-06-30 16:13:15 +00:00
|
|
|
var success bool
|
2015-01-12 03:56:17 +00:00
|
|
|
if util.IsTty() {
|
2023-07-12 04:55:59 +00:00
|
|
|
// The default command for *nix requires a shell that supports "pipefail"
|
|
|
|
// https://unix.stackexchange.com/a/654932/62171
|
2019-11-10 02:36:22 +00:00
|
|
|
shell := "bash"
|
2023-07-12 04:55:59 +00:00
|
|
|
currentShell := os.Getenv("SHELL")
|
|
|
|
currentShellName := path.Base(currentShell)
|
|
|
|
for _, shellName := range []string{"bash", "zsh", "ksh", "ash", "hush", "mksh", "yash"} {
|
|
|
|
if currentShellName == shellName {
|
|
|
|
shell = currentShell
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
cmd := os.Getenv("FZF_DEFAULT_COMMAND")
|
|
|
|
if len(cmd) == 0 {
|
2020-02-04 03:31:00 +00:00
|
|
|
if defaultCommand != "" {
|
|
|
|
success = r.readFromCommand(&shell, defaultCommand)
|
|
|
|
} else {
|
|
|
|
success = r.readFiles()
|
|
|
|
}
|
2017-09-28 14:05:02 +00:00
|
|
|
} else {
|
2019-11-10 02:36:22 +00:00
|
|
|
success = r.readFromCommand(nil, cmd)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-06-30 16:13:15 +00:00
|
|
|
success = r.readFromStdin()
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2017-08-15 18:24:23 +00:00
|
|
|
r.fin(success)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) feed(src io.Reader) {
|
2015-06-08 06:36:21 +00:00
|
|
|
delim := byte('\n')
|
|
|
|
if r.delimNil {
|
|
|
|
delim = '\000'
|
|
|
|
}
|
2016-08-15 16:52:24 +00:00
|
|
|
reader := bufio.NewReaderSize(src, readerBufferSize)
|
2015-06-08 06:36:21 +00:00
|
|
|
for {
|
2015-08-02 05:00:18 +00:00
|
|
|
// ReadBytes returns err != nil if and only if the returned data does not
|
|
|
|
// end in delim.
|
|
|
|
bytea, err := reader.ReadBytes(delim)
|
2016-10-24 03:45:45 +00:00
|
|
|
byteaLen := len(bytea)
|
2017-08-25 18:24:42 +00:00
|
|
|
if byteaLen > 0 {
|
2015-06-08 06:36:21 +00:00
|
|
|
if err == nil {
|
2016-10-24 03:45:45 +00:00
|
|
|
// get rid of carriage return if under Windows:
|
2016-11-06 17:15:34 +00:00
|
|
|
if util.IsWindows() && byteaLen >= 2 && bytea[byteaLen-2] == byte('\r') {
|
2016-10-24 03:45:45 +00:00
|
|
|
bytea = bytea[:byteaLen-2]
|
|
|
|
} else {
|
|
|
|
bytea = bytea[:byteaLen-1]
|
|
|
|
}
|
2015-06-02 16:48:02 +00:00
|
|
|
}
|
2015-08-02 05:25:57 +00:00
|
|
|
if r.pusher(bytea) {
|
2017-08-15 18:24:23 +00:00
|
|
|
atomic.StoreInt32(&r.event, int32(EvtReadNew))
|
2015-07-21 18:21:20 +00:00
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2015-06-08 06:36:21 +00:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 16:13:15 +00:00
|
|
|
func (r *Reader) readFromStdin() bool {
|
2015-01-01 19:49:30 +00:00
|
|
|
r.feed(os.Stdin)
|
2017-06-30 16:13:15 +00:00
|
|
|
return true
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 03:31:00 +00:00
|
|
|
func (r *Reader) readFiles() bool {
|
|
|
|
r.killed = false
|
|
|
|
fn := func(path string, mode os.FileInfo) error {
|
|
|
|
path = filepath.Clean(path)
|
|
|
|
if path != "." {
|
2020-03-14 12:43:35 +00:00
|
|
|
isDir := mode.Mode().IsDir()
|
|
|
|
if isDir && filepath.Base(path)[0] == '.' {
|
2020-02-04 03:31:00 +00:00
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
2020-03-14 12:43:35 +00:00
|
|
|
if !isDir && r.pusher([]byte(path)) {
|
2020-02-04 03:31:00 +00:00
|
|
|
atomic.StoreInt32(&r.event, int32(EvtReadNew))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.mutex.Lock()
|
|
|
|
defer r.mutex.Unlock()
|
|
|
|
if r.killed {
|
|
|
|
return context.Canceled
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cb := walker.WithErrorCallback(func(pathname string, err error) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return walker.Walk(".", fn, cb) == nil
|
|
|
|
}
|
|
|
|
|
2019-11-10 02:36:22 +00:00
|
|
|
func (r *Reader) readFromCommand(shell *string, command string) bool {
|
|
|
|
r.mutex.Lock()
|
|
|
|
r.killed = false
|
|
|
|
r.command = &command
|
|
|
|
if shell != nil {
|
|
|
|
r.exec = util.ExecCommandWith(*shell, command, true)
|
|
|
|
} else {
|
|
|
|
r.exec = util.ExecCommand(command, true)
|
|
|
|
}
|
|
|
|
out, err := r.exec.StdoutPipe()
|
2015-01-01 19:49:30 +00:00
|
|
|
if err != nil {
|
2019-11-10 02:36:22 +00:00
|
|
|
r.mutex.Unlock()
|
2017-06-30 16:13:15 +00:00
|
|
|
return false
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2019-11-10 02:36:22 +00:00
|
|
|
err = r.exec.Start()
|
|
|
|
r.mutex.Unlock()
|
2015-01-01 19:49:30 +00:00
|
|
|
if err != nil {
|
2017-06-30 16:13:15 +00:00
|
|
|
return false
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
r.feed(out)
|
2019-11-10 02:36:22 +00:00
|
|
|
return r.exec.Wait() == nil
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|