-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzsh-command-note.plugin.zsh
297 lines (248 loc) · 6.12 KB
/
zsh-command-note.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#
# edit session
# open session
# list session
_s_help() {
cat <<EOF
edit
open
list
EOF
}
_s_read_single_record() {
#
# ret_names = {
# "key1": "command comment",
# "key2": "command",
# "key3": "command comment",
# }
# ret_records = {
# "key1-command": "ssh 127.0.0.1",
# "key1-comment": "ssh to local",
# "key2-command": "ssh fdslakjf",
# ....
# }
#
#
if [[ $# == 0 ]];then
echo "BUG _s_read_single_record called with 0 arg"
exit -1
fi
local ret_names=$1
local ret_records=$2
local name=$3
local flag=0
O_IFS=$IFS
IFS=$'\n'
for line in $(cat $_s_config_dir/$name); do
local arr=(${(s.: .)line})
local attr_key=$arr[1]
local attr_val=${arr[@]:1}
local tmp=${attr_val//\\/\\\\\\\\}
tmp=${tmp//\'/\\\'}
eval $ret_names"[$name]"+="'${attr_key} '"
eval $ret_records"[$name-$attr_key]"="$'$tmp'"
done
IFS=$O_IFS
}
_s_read_records() {
for f in $(ls $_s_config_dir); do
_s_read_single_record $1 $2 $f
done
}
_s_convert_record() {
local names_var=$1
local records_var=$2
local output_var=$3
local name=$4
local attr_str=${${(P)names_var}[$name]}
attrs=(${(s: :)attr_str})
for attr in $attrs;do
local key=$name"-"$attr
key=${key// /}
local tmp=${${(P)records_var}[$key]}
tmp=${tmp//\\/\\\\}
tmp=${tmp//\'/\\\'}
eval $output_var"[$attr]="$"'$tmp'"
done
}
_s_print_item() {
local names_var=$1
local records_var=$2
local name=$3
typeset -A dict
_s_convert_record $names_var $records_var dict $name
echo $name":"
for k v in ${(kv)dict};do
if [[ $k != "name" ]]; then
echo " "$k"\t\t->\t"$v
fi
done
}
_s_list_commands() {
typeset -A names
typeset -A records
if [[ $# == 0 ]]; then
_s_read_records names records
for name in ${(k)names};do
_s_print_item names records "$name"
echo
done
else
for name in $@; do
_s_check_record_exist $name || continue
_s_read_single_record names records "$name"
_s_print_item names records "$name"
echo
done
fi
}
_s_check_record_exist() {
local f=$_s_config_dir/$1
if [[ ! -f $f ]]; then
echo "No such session name" $1
return -1
fi
}
_s_edit_session() {
local f=$_s_config_dir/$1
_s_check_record_exist "$1" && $editor $f
}
_s_put_record() {
local input_dict_var=$1
local prefix=${${(P)input_dict_var}[name]}
if [ ! -n "$prefix" ]; then
echo "BUG: no prefix specified"
fi
if [[ "$prefix" != "${prefix// /}" ]];then
echo "name should not contain space"
return -1
fi
local config_file=$_s_config_dir/$prefix
for k v in ${(Pkv)input_dict_var};do
echo $k":" $v >> $config_file
done
}
_s_remove_record() {
echo -n "Removing session"
_s_list_commands $1
_s_check_record_exist "$1" && rm -f $_s_config_dir/$1
}
_s_add_record() {
local cmd
local name
local comment
local prev=$(fc -ln -1)
echo "Previous command is: $prev"
vared -p "Enter command and press, use previous command if empty [ENTER] " -c cmd
vared -p "Enter name and press [ENTER] " -c name
vared -p "Enter comment and press [ENTER] " -c comment
if [[ ! -n ${cmd// /} ]]; then
cmd=$prev
fi
typeset -A a
a[name]=$name
a[comment]=$comment
a[command]=$cmd
_s_put_record a && _s_list_commands $name || echo "Failed to add command"
}
_s_execute() {
local name=$1
local options=${@:2}
typeset -A names
typeset -A records
_s_check_record_exist $name || return -1
_s_read_single_record names records "$name"
typeset -A dict
_s_convert_record names records dict "$name"
echo Executing: ${dict[comment]} ${dict[command]}
local cmd=${dict[command]}
if [[ "$options" == "-nohup" ]]; then
eval "nohup ${cmd} > ${name}.out &"
tail -f ${name}.out
else
eval "${cmd}"
fi
}
_s_main() {
local editor=vim
if [[ -n $EDITOR ]];then
if command -v $EDITOR > /dev/null; then
editor=$EDITOR
fi
fi
if [ ! -d $_s_config_dir ]; then
mkdir -p $_s_config_dir
fi
if (( $# == 0 ));then
_s_list_commands
return $?
fi
local cmd=$1
if [[ $cmd == -* ]]; then
shift
fi
case $cmd in
-list)
_s_list_commands ${(s.,.)1}
;;
-edit)
for i in ${(s.,.)1}; do
_s_edit_session $i
done
;;
-add)
_s_add_record
;;
-remove)
for i in ${(s.,.)1}; do
_s_remove_record $i
done
;;
*)
for i in ${(s.,.)1}; do
_s_execute $i ${@:2}
done
;;
esac
}
_s_completion_main() {
local _s_config_dir=~/.cache/ZshSessionManager
_arguments '-list[list commands]:List Commands:->list' \
'-remove[remove commands]:Remove Command:->list' \
'-edit[edit commands]:Remove Command:->list' \
'-add[add command]:Remove Command:->list'
typeset -A names
typeset -A records
typeset -A dict
typeset -a list
_s_read_records names records
for name in ${(k)names};do
_s_convert_record names records dict $name
local comment=${dict[comment]}
list+="$name""[$comment]"
done
if [[ ${#list} != 0 ]];then
_values -s ',' "description" ${list[@]}
fi
}
_s() {
local _s_config_dir=~/.cache/ZshSessionManager
local sub=$1
shift
case $sub in
__s_completion)
_s_completion_main $@
;;
__s_main)
_s_main $@
;;
esac
}
s() {
_s __s_main $@
}
_s_completion_entry() {
_s __s_completion $@
}
compdef _s_completion_entry s