-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbashup.hooks
executable file
·30 lines (25 loc) · 1.1 KB
/
bashup.hooks
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
#!/usr/bin/env bash
hook.exists() { declare -F -- "$1";} >/dev/null
hook.before() { hook.quote-args REPLY "${@:2}"; hook.eval-before "$1" "$REPLY";}
hook.after() { hook.quote-args REPLY "${@:2}"; hook.eval-after "$1" "$REPLY";}
hook.without(){ hook.quote-args REPLY "${@:2}"; hook.eval-without "$1" "$REPLY";}
hook.on() { hook.after "$@";}
hook.around(){
hook.body "$1" ":;"; local old=$REPLY
hook.body "$2" "$3;"; local wrap=$REPLY
eval "$1() ${wrap/#{/{ $3() $old}"
}
hook.quote-args() { printf -v "$1" ' %q' "${@:2}";}
hook.eval-before(){ hook.body "$1" "" "{ $2" "}"; eval "$1() $REPLY";}
hook.eval-after() { hook.body "$1" "" "{" "$2 ;}"; eval "$1() $REPLY";}
hook.eval-without() {
local l=$'\n}' r='^~hooktmp~^'; eval $r$'() {\n'"$2"$'\n}'; hook.body $r "" "" ""
unset -f $r; r=${REPLY%$'\n'}; hook.body "$1"
while [[ $REPLY =~ "$r"\;?' '*$'\n' ]]; do REPLY=${REPLY//"${BASH_REMATCH[0]}"/$'\n'}; done
eval "$1() ${REPLY/#{$l/{ :$l}";
}
hook.body() {
local lf=$'\n' b="}" r
r="$(declare -f -- "$1")" && { r=${r#*{ }; r=${r%\}*}; } || r=$lf$2$lf
REPLY="${3-{}$r${4-$b}"
}