diff --git a/main.go b/main.go index 1877358e..af9b6a91 100644 --- a/main.go +++ b/main.go @@ -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) + */ } diff --git a/src/core.go b/src/core.go index 606c54d1..4ade0eb6 100644 --- a/src/core.go +++ b/src/core.go @@ -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) diff --git a/src/options.go b/src/options.go index 6b668d53..b69418aa 100644 --- a/src/options.go +++ b/src/options.go @@ -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 diff --git a/src/reader.go b/src/reader.go index 8fa864e7..6092087e 100644 --- a/src/reader.go +++ b/src/reader.go @@ -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)