Skip to content

Commit a3149f3

Browse files
relastleHiroki-Konishi
authored andcommitted
Merge branch 'develop' of https://github.com/relastle/pmy into develop
2 parents 878c9c8 + bfc0261 commit a3149f3

File tree

10 files changed

+228
-79
lines changed

10 files changed

+228
-79
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
.PHONY: main
22
main:
3+
# Remove comments from zsh source code
4+
sed '/^[[:blank:]]*#/d;s/#.*//' ./shell/pmy.zsh > ./_shell/_pmy.zsh
5+
# Make statik files
6+
statik -src=./_shell
7+
# build
38
go build
49

510
.PHONY: clean

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ pmy is a highly customizable context-aware shell(zsh)-completion scheme utilizin
1010
[fzf](https://github.com/junegunn/fzf).
1111
I'm fully in love with fzf, and I think [zsh's completion system](http://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-System) is so complicated (I admit it is very powerful), so I developed this system.
1212

13-
## :bulb: Dependency
13+
## Dependency
1414

1515
- [fzf](https://github.com/junegunn/fzf) (You can of course use other fuzzy finder such as [peco](https://github.com/peco/peco) and [fzy](https://github.com/jhawthorn/fzy) instead of fzf.)
1616
- [go](https://github.com/golang/go)
1717
- [color](https://github.com/fatih/color)
1818

1919
- awk (available in almost all environment.)
2020

21-
## :hammer: Installation
21+
## Installation
2222

2323
First, please get pmy(backend system written in Go)
2424
and its dependency tool `taggo` using go get.
@@ -34,12 +34,12 @@ source "${GOPATH:-${HOME}/go}/src/github.com/relastle/pmy/shell/pmy.zsh"
3434

3535
You can also add the line into your ~/.zshrc if you want.
3636

37-
## :black_nib: Basic Usage
37+
## Basic Usage
3838

3939
Pmy can be invoked by <kbd>Ctrl</kbd> + <kbd>Space</kbd>.
4040
If the current left buffer (the part of the buffer that lies to the left of the cursor position) and the right buffer (the right part) match pre-defined rule (described below), fuzzy-finder launches against outputs of the corresponding command.
4141

42-
## :gear: Basic Configuration
42+
## Basic Configuration
4343

4444
### pmy's rule
4545

@@ -150,7 +150,7 @@ If you want to change these values, you should export them in .zshrc before you
150150
source "${GOPATH:-${HOME}/go}/src/github.com/relastle/pmy/shell/pmy.zsh"
151151
```
152152

153-
## :trumpet: Demonstration
153+
## Demonstration
154154

155155
Here, some of examples of pmy's completion are provided as GIF with its rule(json format).
156156
They are just a few examples of all possible pattern-matching based completion, but I think it help you to create new pmy's rule.
@@ -264,7 +264,7 @@ Pmy's completion rule is highly customizable and flexible, you can easily create
264264
}
265265
```
266266

267-
## :memo: [License](LICENSE)
267+
## [License](LICENSE)
268268

269269
The MIT License (MIT)
270270

_shell/_pmy.zsh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
export PATH="${GOPATH:-${HOME}/go}/src/github.com/relastle/pmy:${PATH}"
3+
4+
export PMY_RULE_PATH="${PMY_RULE_PATH:-${GOPATH:-${HOME}/go}/src/github.com/relastle/pmy/rules/pmy_rules.json}"
5+
export PMY_TAG_DELIMITER=${PMY_TAG_DELIMITER:-"\t"}
6+
export PMY_FUZZY_FINDER_DEFAULT_CMD=${PMY_FUZZY_FINDER_DEFAULT_CMD:-"fzf -0 -1 --ansi"}
7+
export PMY_TRIGGER_KEY=${PMY_TRIGGER_KEY:-'^ '}
8+
export PMY_SNIPPET_ROOT="${PMY_SNIPPET_ROOT:-${GOPATH:-${HOME}/go}/src/github.com/relastle/pmy/snippets}"
9+
10+
_pmy_main() {
11+
local buffer_left=${1:-""}
12+
local buffer_right=${2:-""}
13+
local test_flag=${3:-""}
14+
15+
local out="$(pmy --bufferLeft=${buffer_left} --bufferRight=${buffer_right})"
16+
17+
if [[ -z $out ]] ; then
18+
echo "No rule was matched"
19+
__pmy_res_lbuffer=${buffer_left}
20+
__pmy_res_rbuffer=${buffer_right}
21+
else
22+
eval ${out}
23+
24+
local fuzzy_finder_cmd=${__pmy_out_fuzzy_finder_cmd:-${PMY_FUZZY_FINDER_DEFAULT_CMD}}
25+
local fzf_res_tag_included=$(eval ${__pmy_out_command} | eval ${fuzzy_finder_cmd})
26+
if [[ -z ${__pmy_out_tag_all_empty} ]] ; then
27+
local tag="$(echo -n ${fzf_res_tag_included} | awk -F ${PMY_TAG_DELIMITER} 'BEGIN{ORS = ""}{print $1}' | base64)"
28+
tag=${tag//\//a_a}
29+
tag=${tag//+/b_b}
30+
tag=${tag//=/c_c}
31+
local fzf_res=$(echo ${fzf_res_tag_included} | awk -F ${PMY_TAG_DELIMITER} '{for(i=2;i<NF;i++){printf("%s%s",$i,OFS=" ")}print $NF}')
32+
else
33+
local fzf_res=${fzf_res_tag_included}
34+
local tag=""
35+
fi
36+
local after_cmd_variable="__pmy_out_${tag}_after"
37+
local after_cmd=$(eval echo \$$after_cmd_variable)
38+
local res=$(echo ${fzf_res} | eval ${after_cmd})
39+
__pmy_res_lbuffer="${__pmy_out_buffer_left}${res}"
40+
__pmy_res_rbuffer="${__pmy_out_buffer_right}"
41+
fi
42+
43+
if ! [[ -z $test_flag ]] then
44+
echo $__pmy_res_lbuffer
45+
echo $__pmy_res_rbuffer
46+
fi
47+
}
48+
49+
pmy-widget() {
50+
_pmy_main ${LBUFFER} ${RBUFFER}
51+
zle reset-prompt
52+
LBUFFER=${__pmy_res_lbuffer}
53+
RBUFFER=${__pmy_res_rbuffer}
54+
}
55+
56+
zle -N pmy-widget
57+
bindkey ${PMY_TRIGGER_KEY} pmy-widget

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/relastle/pmy
2+
3+
go 1.13
4+
5+
require (
6+
github.com/fatih/color v1.7.0
7+
github.com/rakyll/statik v0.1.6
8+
github.com/relastle/colorflag v0.0.0-20190926071630-d4c845e34e8c
9+
)

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
2+
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
3+
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
4+
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
5+
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
6+
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
7+
github.com/rakyll/statik v0.1.6 h1:uICcfUXpgqtw2VopbIncslhAmE5hwc4g20TEyEENBNs=
8+
github.com/rakyll/statik v0.1.6/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs=
9+
github.com/relastle/colorflag v0.0.0-20190926071630-d4c845e34e8c h1:+rQG0CRZFZw5xK4A9RCLxRcWdROI4DzCU66iWZx/0HE=
10+
github.com/relastle/colorflag v0.0.0-20190926071630-d4c845e34e8c/go.mod h1:zCHW+H3mqOZ35829qC8zau+dnhUgsqKObIApF0m0KdM=
11+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

main.go

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
"io/ioutil"
7+
"log"
8+
9+
"github.com/rakyll/statik/fs"
10+
_ "github.com/relastle/pmy/statik"
611

712
"github.com/relastle/colorflag"
813
pmy "github.com/relastle/pmy/src"
@@ -13,14 +18,7 @@ var (
1318
bufferRight string
1419
)
1520

16-
func main() {
17-
flag.StringVar(&bufferLeft, "bufferLeft", "", "Current left buffer string of zsh prompt")
18-
flag.StringVar(&bufferRight, "bufferRight", "", "Current right buffer string of zsh prompt")
19-
20-
colorflag.Parse([]*flag.FlagSet{})
21-
22-
pmy.SetConfigs()
23-
21+
func mainRoutine() {
2422
outString := pmy.Run(
2523
pmy.PmyRulePath,
2624
pmy.Input{
@@ -30,3 +28,59 @@ func main() {
3028
)
3129
fmt.Println(outString)
3230
}
31+
32+
func initCmdRoutine() {
33+
statikFS, err := fs.New()
34+
if err != nil {
35+
log.Fatal(err)
36+
}
37+
f, err := statikFS.Open("/_pmy.zsh")
38+
if err != nil {
39+
log.Fatal(err)
40+
}
41+
bs, err := ioutil.ReadAll(f)
42+
if err != nil {
43+
log.Fatal(err)
44+
}
45+
fmt.Printf(string(bs))
46+
}
47+
48+
func ruleListCmdRoutine() {
49+
50+
}
51+
52+
func snippetListCmdRoutine() {
53+
}
54+
55+
func main() {
56+
flag.StringVar(&bufferLeft, "bufferLeft", "", "Current left buffer string of zsh prompt")
57+
flag.StringVar(&bufferRight, "bufferRight", "", "Current right buffer string of zsh prompt")
58+
59+
// Subcommand for dumping init zsh script to stdout
60+
initFlagSet := flag.NewFlagSet("init", flag.ExitOnError)
61+
62+
// Subcommandd for listing the all loaded rule json paths
63+
ruleListFlagSet := flag.NewFlagSet("rule-list", flag.ExitOnError)
64+
65+
// Subcommand for listing the all loaded snippet json paths
66+
snippetsListFlagSet := flag.NewFlagSet("snippet-list", flag.ExitOnError)
67+
68+
pmy.SetConfigs()
69+
70+
subCommand := colorflag.Parse([]*flag.FlagSet{
71+
initFlagSet,
72+
ruleListFlagSet,
73+
snippetsListFlagSet,
74+
})
75+
76+
switch subCommand {
77+
case "init":
78+
initCmdRoutine()
79+
case "rule-list":
80+
fmt.Println("rule-list")
81+
case "snippet-list":
82+
fmt.Println("snippet-list")
83+
default:
84+
mainRoutine()
85+
}
86+
}

main_bench_test.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/core_bench_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package pmy
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"log"
8+
"os"
9+
"testing"
10+
)
11+
12+
// DumpDummyRulesJSON dumps arbitrary number of rules into given file path
13+
func DumpDummyRulesJSON(resultPath string, ruleNum int, cmdGroupNum int) error {
14+
pmyRules := pmyRules{}
15+
for i := 0; i < ruleNum; i++ {
16+
cgs := CmdGroups{}
17+
for j := 0; j < cmdGroupNum; j++ {
18+
cg := &CmdGroup{
19+
Tag: fmt.Sprintf("test%v", ruleNum),
20+
Stmt: "find /Users/hkonishi/ -maxdepth 2",
21+
After: "awk '{print $1}'",
22+
}
23+
cgs = append(cgs, cg)
24+
}
25+
rule := &pmyRule{
26+
Name: fmt.Sprintf("test%v", ruleNum),
27+
RegexpLeft: ".*test.*",
28+
RegexpRight: "",
29+
CmdGroups: cgs,
30+
BufferLeft: "[]",
31+
BufferRight: "[]",
32+
}
33+
pmyRules = append(pmyRules, rule)
34+
}
35+
36+
cgsJSON, _ := json.Marshal(pmyRules)
37+
err := ioutil.WriteFile(resultPath, cgsJSON, 0644)
38+
if err != nil {
39+
return err
40+
}
41+
return nil
42+
}
43+
44+
// BenchmarkLoadLargeRules2 calculates time elapsed to load large rule file
45+
// and fetch them all
46+
func BenchmarkLoadLargeRules2(b *testing.B) {
47+
const ruleNum int = 10000
48+
const cmdGroupNum int = 1
49+
filePath := "./test_pmy_rules_large.json"
50+
DumpDummyRulesJSON(filePath, ruleNum, cmdGroupNum)
51+
b.ResetTimer()
52+
out := Run(
53+
filePath,
54+
Input{
55+
BufferLeft: "git abcdef tes abcdef ",
56+
BufferRight: "",
57+
},
58+
)
59+
b.StopTimer()
60+
os.Remove(filePath)
61+
if out != "" {
62+
log.Fatal("output is not empty")
63+
}
64+
return
65+
}

src/rule.go

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

33
import (
44
"encoding/json"
5-
"fmt"
65
"io/ioutil"
76
"os"
87
"regexp"
@@ -40,38 +39,6 @@ func loadAllRules(cfgPath string) (pmyRules, error) {
4039
return rules, nil
4140
}
4241

43-
// DumpDummyRulesJSON dumps arbitrary number of rules into given file path
44-
func DumpDummyRulesJSON(resultPath string, ruleNum int, cmdGroupNum int) error {
45-
pmyRules := pmyRules{}
46-
for i := 0; i < ruleNum; i++ {
47-
cgs := CmdGroups{}
48-
for j := 0; j < cmdGroupNum; j++ {
49-
cg := &CmdGroup{
50-
Tag: fmt.Sprintf("test%v", ruleNum),
51-
Stmt: "find /Users/hkonishi/ -maxdepth 2",
52-
After: "awk '{print $1}'",
53-
}
54-
cgs = append(cgs, cg)
55-
}
56-
rule := &pmyRule{
57-
Name: fmt.Sprintf("test%v", ruleNum),
58-
RegexpLeft: ".*test.*",
59-
RegexpRight: "",
60-
CmdGroups: cgs,
61-
BufferLeft: "[]",
62-
BufferRight: "[]",
63-
}
64-
pmyRules = append(pmyRules, rule)
65-
}
66-
67-
cgsJSON, _ := json.Marshal(pmyRules)
68-
err := ioutil.WriteFile(resultPath, cgsJSON, 0644)
69-
if err != nil {
70-
return err
71-
}
72-
return nil
73-
}
74-
7542
// match check if the query buffers(left and right) satisfies the concerned
7643
// rule. if the rule regexp contains parametrized subgroups, this function expand
7744
// the `Command` to `CommandExpanded`, where all parametrized variables were expanded.

statik/statik.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)