Skip to content

Commit dc37c6b

Browse files
relastleHiroki-Konishi
authored andcommitted
Update: implement general manipulation of resulting left/right buffer (which also supports parametrized expansion)
1 parent f320c7d commit dc37c6b

File tree

5 files changed

+127
-30
lines changed

5 files changed

+127
-30
lines changed

resources/test_setting.json

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,51 @@
11
[
22
{
33
"regexpLeft": "^cd +(?P<path>.*)$",
4-
"command": "\\ls ${PMY_LS_OPTION} -1 <path> | egrep '/$'"
4+
"command": "\\ls ${PMY_LS_OPTION} -1 <path> | egrep '/$'",
5+
"bufferLeft": "[]",
6+
"bufferRight": "[]"
57
},
68
{
79
"regexpLeft": "^python.* ",
8-
"command": "\\find . | egrep py$"
10+
"command": "\\find . | egrep py$",
11+
"bufferLeft": "[]",
12+
"bufferRight": "[]"
913
},
1014
{
1115
"regexpLeft": "^go ",
12-
"command": "\\find . | egrep go$"
16+
"command": "\\find . | egrep go$",
17+
"bufferLeft": "[]",
18+
"bufferRight": "[]"
1319
},
1420
{
1521
"regexpLeft": "^make ",
1622
"regexpRight": "",
17-
"command": "cat Makefile"
23+
"command": "cat Makefile",
24+
"bufferLeft": "[]",
25+
"bufferRight": "[]"
1826
},
1927
{
2028
"regexpLeft": "git +add +",
21-
"command": "git status --short | git status -s | grep -Ev \"^(A|C|D|M|R|T|U|X|B)\" | cut -c4-"
29+
"command": "git status --short | git status -s | grep -Ev \"^(A|C|D|M|R|T|U|X|B)\" | cut -c4-",
30+
"bufferLeft": "[]",
31+
"bufferRight": "[]"
2232
},
2333
{
2434
"regexpLeft": "^export ",
25-
"command": "printenv"
35+
"command": "printenv",
36+
"bufferLeft": "[]",
37+
"bufferRight": "[]"
2638
},
2739
{
28-
"regexpLeft": "ls.for",
29-
"command": "echo 'for file in $(ls); do something; done'"
40+
"regexpLeft": "(?P<cmd>.+)\\.for",
41+
"command": "echo ''",
42+
"bufferLeft": "for x in $(<cmd>); do ",
43+
"bufferRight": "; done"
3044
},
3145
{
3246
"regexpLeft": "^(vi||vim|nvim) ",
33-
"command": "find . -maxdepth 2"
47+
"command": "find . -maxdepth 2",
48+
"bufferLeft": "[]",
49+
"bufferRight": "[]"
3450
}
3551
]

shell/pmy.zsh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ export PMY_CONFIG_PATH="${GOPATH}/src/github.com/relastle/pmy/resources/test_set
55
pmy-widget() {
66
local buffer_left=${LBUFFER}
77
local buffer_right=${RBUFFER}
8-
local cmd=$(pmy --bufferLeft=${buffer_left} --bufferRight=${buffer_right} 2>/dev/null)
9-
if [[ -z $cmd ]] then
8+
local out=$(pmy --bufferLeft=${buffer_left} --bufferRight=${buffer_right} 2>/dev/null)
9+
local lbuffer=$(echo ${out} | awk -F ':::' '{print $1}')
10+
local rbuffer=$(echo ${out} | awk -F ':::' '{print $2}')
11+
local cmd=$(echo ${out} | awk -F ':::' '{print $3}')
12+
if [[ -z $out ]] then
1013
echo "No rule was matched"
1114
else
12-
LBUFFER="${LBUFFER}$(eval ${cmd} | fzf -0 -1)"
15+
LBUFFER="${lbuffer}$(eval ${cmd} | fzf -0 -1)"
16+
RBUFFER="${rbuffer}"
1317
fi
1418
zle reset-prompt
1519
}

src/core.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,28 @@ package pmy
33
import (
44
"fmt"
55
"log"
6+
"os/exec"
67
)
78

9+
// ccheckJq checks whether `jq` command is available
10+
func checkJq() bool {
11+
_, err := exec.LookPath("jq")
12+
if err != nil {
13+
return false
14+
}
15+
return true
16+
}
17+
818
// Run runs the main process of pmy
919
func Run(cfgPath string, bufferLeft string, bufferRight string) {
20+
21+
// Load rules from config file
1022
rules, err := loadAllRules(cfgPath)
1123
if err != nil {
1224
log.Fatal(err)
1325
}
26+
27+
// Fetch rule using LBUFFER and RBUFFER
1428
var fetcher ruleFetcher
1529
fetcher = &ruleFetcherImpl{}
1630
resRules, err := fetcher.fetch(rules, bufferLeft, bufferRight)
@@ -22,6 +36,9 @@ func Run(cfgPath string, bufferLeft string, bufferRight string) {
2236
return
2337
}
2438
rule := resRules[0]
25-
fmt.Print(rule.CommandExpanded)
39+
out := newPmyOutFromRule(&rule)
40+
41+
// Ouput result
42+
fmt.Println(out.serialize())
2643
return
2744
}

src/out.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package pmy
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"strings"
7+
)
8+
9+
const delimiter string = ":::"
10+
11+
// pmyOut represents Output of pmy against zsh routine.
12+
// This struct has strings exported to shell, whose embedded
13+
// variables are all expanded.
14+
type pmyOut struct {
15+
BufferLeft string `json:"bufferLeft"`
16+
BufferRight string `json:"bufferRight"`
17+
Command string `json:"command"`
18+
}
19+
20+
// newPmyOutFromRule create new pmyOut from rule
21+
// which matches query and already has paramMap
22+
func newPmyOutFromRule(rule *pmyRule) pmyOut {
23+
out := pmyOut{}
24+
out.Command = rule.Command
25+
out.BufferLeft = rule.BufferLeft
26+
out.BufferRight = rule.BufferRight
27+
out.expandAll(rule.paramMap)
28+
return out
29+
}
30+
31+
func expand(org string, paramMap map[string]string) string {
32+
res := org
33+
for name, value := range paramMap {
34+
res = strings.ReplaceAll(
35+
res,
36+
fmt.Sprintf("<%v>", name),
37+
value,
38+
)
39+
}
40+
return res
41+
}
42+
43+
func (out *pmyOut) expandAll(paramMap map[string]string) {
44+
out.BufferLeft = expand(out.BufferLeft, paramMap)
45+
out.BufferRight = expand(out.BufferRight, paramMap)
46+
out.Command = expand(out.Command, paramMap)
47+
return
48+
}
49+
50+
func (out *pmyOut) toJSON() string {
51+
bytes, _ := json.Marshal(out)
52+
str := string(bytes)
53+
return str
54+
}
55+
56+
func (out *pmyOut) serialize() string {
57+
return out.BufferLeft + delimiter + out.BufferRight + delimiter + out.Command
58+
}

src/rule.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package pmy
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"io/ioutil"
7-
"log"
86
"os"
97
"regexp"
108
"strings"
@@ -14,12 +12,14 @@ type replaceMap map[string]string
1412

1513
// Rule is a struct representing one rule
1614
type pmyRule struct {
17-
Matcher string `json:"matcher"`
18-
Description string `json:"description"`
19-
RegexpLeft string `json:"regexpLeft"`
20-
RegexpRight string `json:"regexpRight"`
21-
Command string `json:"command"`
22-
CommandExpanded string
15+
Matcher string `json:"matcher"`
16+
Description string `json:"description"`
17+
RegexpLeft string `json:"regexpLeft"`
18+
RegexpRight string `json:"regexpRight"`
19+
Command string `json:"command"`
20+
BufferLeft string `json:"bufferLeft"`
21+
BufferRight string `json:"bufferRight"`
22+
paramMap map[string]string
2323
}
2424

2525
type pmyRules []pmyRule
@@ -56,14 +56,16 @@ func (rule *pmyRule) match(bufferLeft string, bufferRight string) (bool, error)
5656
paramMap[name] = matches[i]
5757
}
5858
}
59-
rule.CommandExpanded = rule.Command
60-
for name, value := range paramMap {
61-
rule.CommandExpanded = strings.ReplaceAll(
62-
rule.CommandExpanded,
63-
fmt.Sprintf("<%v>", name),
64-
value,
65-
)
66-
}
67-
log.Println(paramMap)
59+
rule.BufferLeft = strings.ReplaceAll(
60+
rule.BufferLeft,
61+
"[]",
62+
bufferLeft,
63+
)
64+
rule.BufferRight = strings.ReplaceAll(
65+
rule.BufferRight,
66+
"[]",
67+
bufferRight,
68+
)
69+
rule.paramMap = paramMap
6870
return true, nil
6971
}

0 commit comments

Comments
 (0)