-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfinder.go
54 lines (41 loc) · 952 Bytes
/
finder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
fzf "github.com/junegunn/fzf/src"
)
type channelWriter struct {
ch chan string
}
func (cw *channelWriter) Write(p []byte) (n int, err error) {
str := string(p) // Convert bytes to string
cw.ch <- str // Send to channel
return len(p), nil
}
func runFinder(inputChan chan string, opts []string) ([]string, int, error) {
options, err := fzf.ParseOptions(false, opts)
if err != nil {
return nil, 2, fmt.Errorf("fzf error: %w", err)
}
outputChan := make(chan string)
resultChan := make(chan struct {
code int
err error
}, 1)
options.Input = inputChan
options.Output = outputChan
go func() {
code, runErr := fzf.Run(options)
close(outputChan)
resultChan <- struct {
code int
err error
}{code, runErr}
close(resultChan)
}()
var lines []string
for line := range outputChan {
lines = append(lines, line)
}
result := <-resultChan
return lines, result.code, result.err
}