-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
peco-cmigemo.go
52 lines (46 loc) · 885 Bytes
/
peco-cmigemo.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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
func main() {
if len(os.Args) != 2 {
os.Exit(1)
}
args := []string{"cmigemo"}
// dict is same location as executable
// for example, you should put this executable into ~/.peco/
p, err := exec.LookPath(os.Args[0])
if err == nil {
p, err = filepath.Abs(p)
if err == nil {
args = append(args, "-d", filepath.Join(filepath.Dir(p), "dict", "migemo-dict"))
}
}
args = append(args, "-w", os.Args[1])
cmd := exec.Command(args[0], args[1:]...)
b, err := cmd.Output()
if err != nil {
os.Exit(1)
}
re, err := regexp.Compile(strings.TrimSpace(string(b)))
if err != nil {
os.Exit(1)
}
buf := bufio.NewReader(os.Stdin)
for {
b, _, err := buf.ReadLine()
if err != nil {
break
}
line := string(b)
if re.MatchString(line) {
fmt.Println(line)
}
}
}