-
Notifications
You must be signed in to change notification settings - Fork 1
/
finder.go
216 lines (198 loc) · 4.3 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package finder
import (
"os"
"os/exec"
"strings"
"github.com/b4b4r07/go-finder/source"
"github.com/pkg/errors"
)
// CLI is the command having a command-line interface
type CLI interface {
Run() ([]string, error)
Read(source.Source)
}
// Item is key-value
type Item struct {
Key string
Value interface{}
}
// Items is the collection of Item
type Items []Item
// NewItems creates Items object
func NewItems() Items {
return Items{}
}
// Add addes item to Items
func (i *Items) Add(key string, value interface{}) {
*i = append(*i, Item{
Key: key,
Value: value,
})
}
// Finder is the interface of a filter command
type Finder interface {
CLI
Install(string) error
Select(interface{}) ([]interface{}, error)
}
// Command represents the command
type Command struct {
Name string
Args []string
Path string
Items Items
Source source.Source
}
// Commands represents the command list
type Commands []Command
// DefaultCommands represents the list of default finder commands optimized for quick usage
var DefaultCommands = Commands{
// https://github.com/junegunn/fzf
Command{
Name: "fzf",
Args: []string{"--reverse", "--height=50%", "--ansi", "--multi"},
},
// https://github.com/jhawthorn/fzy
Command{Name: "fzy"},
// https://github.com/peco/peco
Command{Name: "peco"},
// https://github.com/mooz/percol
Command{Name: "percol"},
}
// Lookup lookups the available command
func (c Commands) Lookup() (Command, error) {
for _, command := range c {
path, err := exec.LookPath(command.Name)
if err == nil {
return Command{
Name: command.Name,
Args: command.Args,
Path: path,
Source: source.Stdin(),
}, nil
}
}
return Command{}, errors.New("no available finder command")
}
// Run runs as a command
func (c *Command) Run() ([]string, error) {
shell := os.Getenv("SHELL")
if len(shell) == 0 {
shell = "sh"
}
cmd := exec.Command(shell, "-c", c.Path+" "+strings.Join(c.Args, " "))
cmd.Stderr = os.Stderr
in, _ := cmd.StdinPipe()
errCh := make(chan error, 1)
go func() {
if err := c.Source(in); err != nil {
errCh <- err
return
}
errCh <- nil
in.Close()
}()
err := <-errCh
if err != nil {
return []string{}, err
}
result, _ := cmd.Output()
return trimLastNewline(strings.Split(string(result), "\n")), nil
}
// Select selects the keys in various map
func (c *Command) Select(args interface{}) ([]interface{}, error) {
switch items := args.(type) {
case Items:
var keys []string
for _, item := range items {
keys = append(keys, item.Key)
}
if len(keys) == 0 {
return nil, errors.New("no items")
}
c.Read(source.Slice(keys))
selectedKeys, err := c.Run()
if err != nil {
return nil, err
}
var values []interface{}
for _, key := range selectedKeys {
for _, item := range items {
if item.Key == key {
values = append(values, item.Value)
}
}
}
return values, nil
case []string:
if len(items) == 0 {
return nil, errors.New("no items")
}
c.Read(source.Slice(items))
selectedItems, err := c.Run()
if err != nil {
return nil, err
}
var values []interface{}
for _, item := range selectedItems {
values = append(values, item)
}
return values, nil
default:
return nil, errors.New("Error")
}
}
func trimLastNewline(s []string) []string {
if len(s) == 0 {
return s
}
last := len(s) - 1
if s[last] == "" {
return s[:last]
}
return s
}
// Install does nothing and is implemented to satisfy Finder interface
// This method should be overwritten by each finder command implementation
func (c *Command) Install(path string) error {
return nil
}
// Read sets the data sources
func (c *Command) Read(data source.Source) {
c.Source = data
}
// New creates Finder instance
func New(args ...string) (Finder, error) {
var (
command Command
err error
)
if len(args) == 0 {
command, err = DefaultCommands.Lookup()
if err != nil {
return nil, err
}
} else {
path, err := exec.LookPath(args[0])
if err != nil {
return nil, errors.Wrapf(err, "%s: not found", args[0])
}
command = Command{
Name: args[0],
Args: args[1:],
Path: path,
Items: Items{},
Source: source.Stdin(),
}
}
switch command.Name {
case "fzf":
return Fzf{&command}, nil
case "fzy":
return Fzy{&command}, nil
case "peco":
return Peco{&command}, nil
default:
return &command, nil
}
}