Input/Output channels

pull/3769/head
Junegunn Choi 2 weeks ago
parent e7b53df06a
commit 21910968cd
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627

@ -81,4 +81,29 @@ func main() {
os.Stderr.WriteString(err.Error() + "\n")
}
util.Exit(code)
/*
// Example of using fzf in a Go program
inputChan := make(chan string)
outputChan := make(chan string)
go func() {
for _, s := range []string{"a", "b", "c"} {
inputChan <- s
}
close(inputChan)
}()
go func() {
for s := range outputChan {
fmt.Println("Got: " + s)
}
}()
options, err := fzf.ParseOptions(true, []string{"--multi", "--reverse", "--border"})
options.Input = inputChan
options.Output = outputChan
code, err := fzf.Run(options)
if err != nil {
os.Stderr.WriteString(err.Error() + "\n")
}
util.Exit(code)
*/
}

@ -31,6 +31,13 @@ func Run(opts *Options) (int, error) {
defer clearCaches()
defer util.RunAtExitFuncs()
// Output channel given
if opts.Output != nil {
opts.Printer = func(str string) {
opts.Output <- str
}
}
sort := opts.Sort > 0
sortCriteria = opts.Criteria
@ -122,7 +129,7 @@ func Run(opts *Options) (int, error) {
reader = NewReader(func(data []byte) bool {
return chunkList.Push(data)
}, eventBox, executor, opts.ReadZero, opts.Filter == nil)
go reader.ReadSource(opts.WalkerRoot, opts.WalkerOpts, opts.WalkerSkip)
go reader.ReadSource(opts.Input, opts.WalkerRoot, opts.WalkerOpts, opts.WalkerSkip)
}
// Matcher
@ -173,7 +180,7 @@ func Run(opts *Options) (int, error) {
}
return false
}, eventBox, executor, opts.ReadZero, false)
reader.ReadSource(opts.WalkerRoot, opts.WalkerOpts, opts.WalkerSkip)
reader.ReadSource(opts.Input, opts.WalkerRoot, opts.WalkerOpts, opts.WalkerSkip)
} else {
eventBox.Unwatch(EvtReadNew)
eventBox.WaitFor(EvtReadFin)

@ -293,6 +293,8 @@ type walkerOpts struct {
// Options stores the values of command-line options
type Options struct {
Input chan string
Output chan string
Bash bool
Zsh bool
Fish bool

@ -93,11 +93,26 @@ func (r *Reader) restart(command string, environ []string) {
r.fin(success)
}
func (r *Reader) readChannel(inputChan chan string) bool {
for {
item, more := <-inputChan
if !more {
break
}
if r.pusher([]byte(item)) {
atomic.StoreInt32(&r.event, int32(EvtReadNew))
}
}
return true
}
// ReadSource reads data from the default command or from standard input
func (r *Reader) ReadSource(root string, opts walkerOpts, ignores []string) {
func (r *Reader) ReadSource(inputChan chan string, root string, opts walkerOpts, ignores []string) {
r.startEventPoller()
var success bool
if util.IsTty() {
if inputChan != nil {
success = r.readChannel(inputChan)
} else if util.IsTty() {
cmd := os.Getenv("FZF_DEFAULT_COMMAND")
if len(cmd) == 0 {
success = r.readFiles(root, opts, ignores)

Loading…
Cancel
Save