forked from seletskiy/zsh-fuzzy-search-and-edit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.zsh
103 lines (80 loc) · 2.51 KB
/
plugin.zsh
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
if [ -n "$(uname | grep -i freebsd)" ]; then
grep='/usr/local/bin/grep'
if [[ ! -f $grep ]]; then
echo "Install GNU grep: pkg install gnugrep"
return 1
fi
pattern='s/^([^:]+:[^:]+):\s*(.*)/\1: \2/'
elif [ -n "$(uname | grep -i darwin)" ]; then
grep=`which ggrep`
if [[ ! -f $grep ]]; then
echo "Install GNU grep: brew install grep"
return 1
fi
pattern='s/^([^:]+:[^:]+):\s*(.*)/\x1b[35m\1\x1b[0m: \2/'
else
grep='grep'
pattern='s/^([^:]+:[^:]+):\s*(.*)/\x1b[35m\1\x1b[0m: \2/'
fi
function :fuzzy-search-and-edit:get-files() {
local directory="$1"
local fifo="$2"
cd "$directory"
command $grep --line-buffered -r -nEHI '[[:alnum:]]' "." --color=never --exclude-dir=".git" \
| cut -b3- \
| command sed -ru $pattern > "$fifo"
}
function :fuzzy-search-and-edit:get-files-custom() {
local directory="$1"
local fifo="$2"
cd "$directory"
command $grep --line-buffered -r -nEHI '[[:alnum:]]' "." --color=never --exclude-dir=".git" \
--include="*.php" \
--include="*.js" \
--include="*.py" \
--include="*.rb" \
--include="*.liq" \
--include="*.xml" \
--include="*.htm" \
--include="*.css" \
--include="*.ini" \
--include="*.sql" \
--include="*.json" \
--include="*.txt" \
--include="*.conf" \
--include="*.html" \
| cut -b3- \
| command sed -ru $pattern > "$fifo"
}
function :fuzzy-search-and-edit:abort-job() {
local match
read -r match
printf "%s\n" "$match"
async_flush_jobs ":fuzzy-search-and-edit:worker"
}
function fuzzy-search-and-edit() {
local dir="$(mktemp -d /tmp/fuzzy-search-and-edit.XXXXXX)"
local fifo="$dir/fifo"
mkfifo "$fifo"
async_job ":fuzzy-search-and-edit:worker" ":fuzzy-search-and-edit:get-files" "$(pwd)" "$fifo"
local match=$(
fzf +x --ansi -1 < "$fifo" \
| :fuzzy-search-and-edit:abort-job
)
if [ "$match" ]; then
local file
local line
IFS=: read -r file line _ <<< "$match"
"${=EDITOR}" "+$line" "$file" < /dev/tty
fi
rm -r "$dir"
find /tmp -maxdepth 1 -name "fuzzy-search-and-edit.*" -user $USER -type d -mmin +30 -exec rm -rf {} \;
zle -I
}
:fuzzy-search-and-edit:completed() {
:
}
async_start_worker ":fuzzy-search-and-edit:worker"
async_register_callback ":fuzzy-search-and-edit:worker" \
":fuzzy-search-and-edit:completed"
zle -N fuzzy-search-and-edit