Skip to content

Commit 1b6e2ae

Browse files
relastleHiroki-Konishi
authored andcommitted
Update: implement auto-tag elimination, and implement after command excecution against fzf result
1 parent 438a2dd commit 1b6e2ae

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

shell/pmy.zsh

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env zsh
12
# test environment path variable
23
export PATH="${GOPATH}/src/github.com/relastle/pmy:${PATH}"
34
export PMY_CONFIG_PATH="${GOPATH}/src/github.com/relastle/pmy/resources/pmy_rules_neo.json"
@@ -10,6 +11,7 @@ pmy-widget() {
1011

1112
# get output from pmy
1213
local out="$(pmy --bufferLeft=${buffer_left} --bufferRight=${buffer_right} 2>/dev/null)"
14+
echo $out
1315

1416
if [[ -z $out ]] then
1517
echo "No rule was matched"
@@ -18,7 +20,19 @@ pmy-widget() {
1820

1921
# get result from fzf
2022
local fzf_res=$(echo "${__pmy_out_sources}" | fzf -0 -1)
21-
LBUFFER="${__pmy_out_buffer_left}${fzf_res}"
23+
# get tag
24+
local tag=$(echo ${fzf_res} | awk '{print $1}')
25+
# get rest statement
26+
local fzf_res_rest=$(echo ${fzf_res} | awk '{for(i=2;i<NF;i++){printf("%s%s",$i,OFS=" ")}print $NF}')
27+
# get after command
28+
local after_cmd_variable="__pmy_out_${tag}_after"
29+
local after_cmd=$(eval echo \$$after_cmd_variable)
30+
echo ${tag}
31+
echo ${after_cmd_variable}
32+
echo ${fzf_res_rest}
33+
echo ${after_cmd}
34+
local res=$(echo ${fzf_res_rest} | eval ${after_cmd})
35+
LBUFFER="${__pmy_out_buffer_left}${res}"
2236
RBUFFER="${__pmy_out_buffer_right}"
2337
fi
2438
zle reset-prompt

src/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func (cu *CmdUnit) init() error {
3434
type CmdGroup struct {
3535
Tag string `json:"tag"`
3636
Stmt string `json:"stmt"`
37+
After string `json:"after"`
3738
CmdUnits []CmdUnit `json:"cmdUnits"`
3839
Lines []string `json:"lines"`
3940
}

src/out.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,44 @@ const (
1313
shellBufferRightVariableName = "__pmy_out_buffer_right"
1414
shellCommandVariableName = "__pmy_out_command"
1515
shellSourcesVariableName = "__pmy_out_sources"
16+
shellAfterVariableName = "__pmy_out_%s_after"
1617
)
1718

19+
type afterCmd struct {
20+
tag string
21+
after string
22+
}
23+
1824
// pmyOut represents Output of pmy against zsh routine.
1925
// This struct has strings exported to shell, whose embedded
2026
// variables are all expanded.
2127
type pmyOut struct {
2228
BufferLeft string `json:"bufferLeft"`
2329
BufferRight string `json:"bufferRight"`
2430
Sources string `json:"source"`
31+
afterCmds []*afterCmd
2532
}
2633

2734
// newPmyOutFromRule create new pmyOut from rule
2835
// which matches query and already has paramMap
2936
func newPmyOutFromRule(rule *pmyRule) pmyOut {
3037
out := pmyOut{}
38+
// pass sources
3139
out.Sources, _ = rule.CmdGroups.GetSources()
40+
// pass resulting buffer informaiton
3241
out.BufferLeft = rule.BufferLeft
3342
out.BufferRight = rule.BufferRight
43+
// expand all parameters
3444
out.expandAll(rule.paramMap)
45+
// set after commnad
46+
out.afterCmds = []*afterCmd{}
47+
for _, cg := range rule.CmdGroups {
48+
ac := &afterCmd{
49+
tag: cg.Tag,
50+
after: cg.After,
51+
}
52+
out.afterCmds = append(out.afterCmds, ac)
53+
}
3554
return out
3655
}
3756

@@ -42,6 +61,13 @@ func (out *pmyOut) toShellVariables() string {
4261
res += fmt.Sprintf("%v=$'%v';", shellBufferLeftVariableName, utils.Escape(out.BufferLeft, "'"))
4362
res += fmt.Sprintf("%v=$'%v';", shellBufferRightVariableName, utils.Escape(out.BufferRight, "'"))
4463
res += fmt.Sprintf("%v=$'%v';", shellSourcesVariableName, utils.Escape(out.Sources, "'"))
64+
for _, ac := range out.afterCmds {
65+
res += fmt.Sprintf(
66+
"%v=$'%v';",
67+
fmt.Sprintf(shellAfterVariableName, ac.tag),
68+
utils.Escape(ac.after, "'"),
69+
)
70+
}
4571
return res
4672
}
4773

0 commit comments

Comments
 (0)