Skip to content

Commit 7b2c09b

Browse files
relastleHiroki-Konishi
authored andcommitted
Update: add input struct
1 parent 3142916 commit 7b2c09b

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

main.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"flag"
5+
"fmt"
56
"os"
67

78
pmy "github.com/relastle/pmy/src"
@@ -27,9 +28,12 @@ func main() {
2728
if !ok {
2829
cfgPath = DefaultPmyConfigPath
2930
}
30-
pmy.Run(
31+
out := pmy.Run(
3132
cfgPath,
32-
bufferLeft,
33-
bufferRight,
33+
pmy.Input{
34+
BufferLeft: bufferLeft,
35+
BufferRight: bufferRight,
36+
},
3437
)
38+
fmt.Println(out)
3539
}

src/core.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package pmy
22

33
import (
4-
"fmt"
54
"log"
65
"os/exec"
76
)
@@ -15,8 +14,14 @@ func checkJq() bool {
1514
return true
1615
}
1716

17+
// Input represents input from zsh
18+
type Input struct {
19+
BufferLeft string
20+
BufferRight string
21+
}
22+
1823
// Run runs the main process of pmy
19-
func Run(cfgPath string, bufferLeft string, bufferRight string) {
24+
func Run(cfgPath string, in Input) string {
2025

2126
// Load rules from config file
2227
rules, err := loadAllRules(cfgPath)
@@ -27,18 +32,20 @@ func Run(cfgPath string, bufferLeft string, bufferRight string) {
2732
// Fetch rule using LBUFFER and RBUFFER
2833
var fetcher ruleFetcher
2934
fetcher = &ruleFetcherImpl{}
30-
resRules, err := fetcher.fetch(rules, bufferLeft, bufferRight)
35+
resRules, err := fetcher.fetch(
36+
rules,
37+
in.BufferLeft,
38+
in.BufferRight,
39+
)
3140
if err != nil {
3241
log.Fatal(err)
3342
}
3443
if len(resRules) == 0 {
35-
fmt.Print("")
36-
return
44+
return ""
3745
}
3846
rule := resRules[0]
3947
out := newPmyOutFromRule(&rule)
4048

4149
// Ouput result
42-
fmt.Println(out.serialize())
43-
return
50+
return out.serialize()
4451
}

src/rule.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type replaceMap map[string]string
1212

1313
// Rule is a struct representing one rule
1414
type pmyRule struct {
15+
Name string `json:"name"`
1516
Matcher string `json:"matcher"`
1617
Description string `json:"description"`
1718
RegexpLeft string `json:"regexpLeft"`

0 commit comments

Comments
 (0)