forked from z-shell/zzcomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzz-deploy-code
31 lines (30 loc) · 1.17 KB
/
zz-deploy-code
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
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
#
# This Zshell function will execute the given code from a Zle context.
# It has an optional delay first argument: "@sleep:<secnods with fractions>".
# If given, then the code will wait in background before being executed, for
# the specified amount of time.
# The limit of the code length is 25 lines and can be easily extended by
# changing the "repeat 25" line
#
# Usage:
# zz-deploy-code "echo Hello world"
# zz-deploy-code "BUFFER[-1]=''"
# zz-deploy-code @sleep:5.5 "BUFFER='The time has passed, sorry for replacing your command line ;)'"
[[ "$1" = <-> && ${#} -eq 1 ]] && {
local alltext text IFS=$'\n' nl=$'\n'
repeat 25; do read -r -u"$1" text; alltext+="${text:+$text$nl}"; done
zle -F "$1"; exec {1}<&-
eval "$alltext"
return 0
}
local THEFD
exec {THEFD} < <(
# The expansion is: if there is @sleep: pfx, then use what is after
# it, otherwise substitute 0
LANG=C sleep $(( 0.005 + ${${${(M)1#@sleep:}:+${1#@sleep:}}:-0} ))
print -r -- ${1:#(@code|@sleep:*)} "${@[2,-1]}"
)
zle -N zz-deploy-code # idempotent
zle -w -F "$THEFD" zz-deploy-code
# vim: ft=zsh sw=2 ts=2 et