-
Notifications
You must be signed in to change notification settings - Fork 0
/
ztupide.zsh
221 lines (189 loc) · 6.38 KB
/
ztupide.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env zsh
# TODO comments
_ztupide_source() {
for f in ${1:h}/**/*.zsh; do
[[ ! -z "${force}" || ( ! "${f}".zwc -nt "${f}" && -r "${f}" && -w "${f:h}" ) ]] && zcompile $f
done
builtin source "${1}"
}
_ztupide_load() {
# TODO: support for .zsh-theme
[ -d "${ZTUPIDE_PLUGIN_PATH}" ] || mkdir "${ZTUPIDE_PLUGIN_PATH}"
local plugin_name="${1:t}"
local plugin_path="${ZTUPIDE_PLUGIN_PATH}"/"${plugin_name}"
[[ "${1}" =~ .+"/".+ && ! -d "${plugin_path}" ]] && git -C "${ZTUPIDE_PLUGIN_PATH}" clone https://github.com/"${1:t2}" --quiet
local plugin_file=("${plugin_path}"/*.plugin.zsh(NY1)) # match first .plugin.zsh found, prevents multiple .plugin.zsh
if [[ -d "${plugin_path}" && "${#plugin_file}" -eq 1 ]]; then
echo "_load_success:${1}:${plugin_file[1]}:${(j/:/)@:2}"
else
rm -rf "${plugin_path}"
echo "_load_fail:${1}"
fi
}
_ztupide_remove() {
[ -z "${1}" ] && echo "plugin remove error: none specified" && exit 1
local plugin_name="${1:t}"
local plugin_path="${ZTUPIDE_PLUGIN_PATH}"/"${plugin_name}"
if [ -d "${plugin_path}" ]; then
if [ -d "${plugin_path}"/.git ]; then
rm -rf "${plugin_path}"
echo "plugin ${plugin_name} removed"
else
read "ans?${plugin_name} is a local plugin. Do you want to remove it (y/N)? "
if [[ "${ans}" =~ ^[Yy]$ ]]; then
rm -rf "${plugin_path}"
echo "plugin ${plugin_name} removed"
fi
fi
else
echo "plugin remove error: ${plugin_name} plugin not found"
exit 1
fi
}
_ztupide_update() {
echo "checking ${1:t} for updates"
git -C ${1} fetch --quiet
local local=$(git -C ${1} rev-parse HEAD)
local base=$(git -C ${1} rev-parse '@{u}')
if [ "${local}" != "${base}" ]; then
git -C ${1} pull origin master --quiet
echo "${1:t} updated"
fi
}
_ztupide_update_all() {
_ztupide_update ${_ztupide_path:h}
local plugin_path
for plugin_path in "${ZTUPIDE_PLUGIN_PATH}"/*(/N); do
if [ -d "${plugin_path}"/.git ]; then
local plugin_file=("${plugin_path}"/*.plugin.zsh(NY1))
if [ "${#plugin_file}" -eq 1 ]; then
_ztupide_update ${plugin_path}
fi
fi
done
echo "plugins updated"
echo "${EPOCHSECONDS}" > ~/.zsh/ztupide/ztupide_last_update
}
_ztupide_autoupdate() {
if [ ! -z ${ZTUPIDE_AUTOUPDATE} ]; then
if [ -f ~/.zsh/ztupide/ztupide_last_update ]; then
local delta=$(cat ~/.zsh/ztupide/ztupide_last_update)
(( delta = ${EPOCHSECONDS} - ${delta} ))
[ ${delta} -gt ${ZTUPIDE_AUTOUPDATE} ] && _ztupide_update_all
else
_ztupide_update_all
fi
fi
}
_ztupide_load_async_handler() {
if read -r -u "${1}" line && [[ "${line}" =~ "_load*" ]]; then
# close FD
exec {1}<&-
# remove handler
zle -F "${1}"
if [[ "${line}" =~ "_load_success:*" ]]; then
local ret=(${(@s/:/)line})
_ztupide_to_source["${ret[2]}"]="${ret[3]}"
for plugin in ${_ztupide_to_load}; do
if [ -z "${_ztupide_to_source["${plugin}"]}" ]; then
return
elif [ "${_ztupide_to_source["${plugin}"]}" = "_fail" ]; then
_ztupide_to_load=(${_ztupide_to_load:1})
else
_ztupide_to_load=(${_ztupide_to_load:1})
_ztupide_source "${_ztupide_to_source["${plugin}"]}"
for ((i = 4; i <= ${#ret}; i++)); do
[ -z "${ret[${i}]}" ] || eval "${ret[${i}]}"
done
fi
done
else
_ztupide_to_source["${${(@s/:/)line}[2]}"]="_fail"
echo "plugin load error: "${${(@s/:/)line}[2]}" is not a valid plugin"
fi
fi
}
_ztupide_load_sync() {
local ret=$(_ztupide_load ${@})
if [[ "${ret}" =~ "_load_success:*" ]]; then
ret=(${(@s/:/)ret})
_ztupide_source "${ret[3]}"
for ((i = 4; i <= ${#ret}; i++)); do
[ -z "${ret[${i}]}" ] || eval "${ret[${i}]}"
done
else
echo "plugin load error: "${${(@s/:/)ret}[2]}" is not a valid plugin"
fi
}
_ztupide_load_async() {
# create async_fd
local async_fd
exec {async_fd}< <(_ztupide_load ${@})
# needed to fix ctrl+c not working in some cases
command true
# zle -F installs input handler on given FD
zle -F "${async_fd}" _ztupide_load_async_handler
}
_ztupide_init() {
_ztupide_path=${1:a}
ZTUPIDE_PLUGIN_PATH=${ZTUPIDE_PLUGIN_PATH:-~/.zsh/plugins}
# zcompile this file
[[ ! "${_ztupide_path}".zwc -nt "${_ztupide_path}" && -r "${_ztupide_path}" && -w "${_ztupide_path:h}" ]] && zcompile ${_ztupide_path}
zmodload zsh/datetime
_ztupide_autoupdate
_ztupide_to_load=()
typeset -gA _ztupide_to_source
compdef _ztupide ztupide
}
ztupide() {
case "${1}" in
load)
if [ "${2}" = "--async" ]; then
[ -z "${3}" ] && echo "plugin load error: none specified" && exit 1
_ztupide_to_load+="${3}"
_ztupide_load_async ${@:3}
else
[ -z "${2}" ] && echo "plugin load error: none specified" && exit 1
_ztupide_load_sync "${@:2}"
fi
;;
remove)
_ztupide_remove "${2}"
;;
update)
_ztupide_update_all
;;
*)
echo "Usage : ztupide OPTION [--async] [PLUGIN]
Options:
load\t\tload PLUGIN (asynchronously if --async used)
remove\t\tremove PLUGIN
update\t\tupdate ztupide and all plugins"
;;
esac
}
# completion function
function _ztupide() {
local line
_arguments -C \
'1: :((load\:"load plugin" remove\:"remove plugin" update\:"update ztupide and all plugins"))' \
'*::arg:->args'
local plugins=(${ZTUPIDE_PLUGIN_PATH}/*(N))
for ((i = 1; i <= ${#plugins}; i++)); do
plugins[${i}]=${plugins[${i}]:t}
done
case ${line[1]} in
load)
_arguments -C "1: :((--async\:'load plugin asynchronously' ${plugins}))" '*::arg:->args'
case ${line[1]} in
--async)
_arguments "1: :(${plugins})"
;;
esac
;;
remove)
_arguments "1: :(${plugins})"
;;
esac;
}
_ztupide_init ${0}