2024-09-11 14:58:31 +02:00
|
|
|
|
hook(){
|
|
|
|
|
test -z "$1" && { echo "usage: hook <cmd_or_jsfunction> [args]"; return 0; }
|
2024-11-15 13:29:50 +01:00
|
|
|
|
logger "$ hook $*"
|
2024-09-11 14:58:31 +02:00
|
|
|
|
cmd=$1
|
|
|
|
|
shift
|
|
|
|
|
test -d ~/hook.d/$cmd && {
|
2024-11-15 13:29:50 +01:00
|
|
|
|
find -L ~/hook.d/$cmd/ -type f -executable -maxdepth 1 | while read hook; do
|
|
|
|
|
logger " |+ hook $hook $*"
|
2024-09-11 14:58:31 +02:00
|
|
|
|
{ $hook "$@" || true; } | awk '{ gsub(/\/root\/\//,"",$1); $1 = sprintf("%-40s", $1)} 1'
|
|
|
|
|
done
|
2024-11-15 13:29:50 +01:00
|
|
|
|
} &> ~/hook.d/log.txt
|
2024-09-11 14:58:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
alert(){
|
|
|
|
|
test -z "$1" && { echo "usage: alert <title> <message>"; return 0; }
|
|
|
|
|
title=$1
|
|
|
|
|
test -z "$1" || shift
|
|
|
|
|
msg="$*"
|
|
|
|
|
printf "[38;5;165m%s \033[0m%s\n" "$title" "$msg"
|
|
|
|
|
hook alert $title "$msg"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
confirm(){
|
|
|
|
|
test -z "$1" && { echo "usage: confirm <question>"; return 0; }
|
2024-11-15 13:29:50 +01:00
|
|
|
|
if test -n "$BROWSER"; then
|
|
|
|
|
jsh confirm "$1"
|
|
|
|
|
else
|
|
|
|
|
read -p "$(printf "\033[0m")[?] [38;5;165m$1 [y/n] $(printf "\033[0m")" y
|
|
|
|
|
fi
|
2024-09-11 14:58:31 +02:00
|
|
|
|
test $y = y && echo true && return 0
|
|
|
|
|
test $y = y || echo false
|
|
|
|
|
hook confirm $1 $y
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prompt(){
|
|
|
|
|
test -z "$1" && { echo "usage: prompt <question> [answer_default]"; return 0; }
|
|
|
|
|
test -n "$2" && answer="[$2] " && answer_fallback="$2"
|
2024-11-15 13:29:50 +01:00
|
|
|
|
if test -n "$BROWSER"; then
|
|
|
|
|
jsh prompt "$1" "$answer_fallback"
|
|
|
|
|
else
|
|
|
|
|
read -p "$(printf "\033[0m")[?] [38;5;165m$1: $answer $(printf "\033[0m")" answer
|
|
|
|
|
fi
|
2024-09-11 14:58:31 +02:00
|
|
|
|
test -z "$answer" && answer="$answer_fallback"
|
|
|
|
|
echo "$answer"
|
|
|
|
|
hook prompt $1 $answer
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console(){
|
|
|
|
|
js 'return '$1
|
2024-09-23 17:53:59 +02:00
|
|
|
|
printf "\n"
|
2024-09-11 14:58:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-23 17:53:59 +02:00
|
|
|
|
# usage: require <url|file>
|
|
|
|
|
#
|
|
|
|
|
# info: adds javascript/css url/script to DOM
|
2024-09-11 14:58:31 +02:00
|
|
|
|
require(){
|
|
|
|
|
file=$(basename "$1")
|
2024-09-24 16:13:37 +02:00
|
|
|
|
ext="${file/*\./}"
|
|
|
|
|
|
|
|
|
|
case $ext in
|
|
|
|
|
|
|
|
|
|
js|css)
|
|
|
|
|
js '(async () => {
|
|
|
|
|
await AFRAME.utils.require({"'$file'": "'$1'"})
|
|
|
|
|
})(); return ""'
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
|
|
glb|gltf|obj|usdz|fbx)
|
|
|
|
|
require 'https://xrfragment.org/dist/xrfragment.aframe.js'
|
2024-09-24 17:18:59 +02:00
|
|
|
|
sleep 1.5
|
2024-09-24 16:13:37 +02:00
|
|
|
|
echo "<a-entity xrf='$1'></a-entity>" >> /root/index.html
|
|
|
|
|
;;
|
|
|
|
|
|
2024-10-04 17:57:27 +02:00
|
|
|
|
sh)
|
|
|
|
|
js '
|
|
|
|
|
fetch('$1')
|
|
|
|
|
.then( (res) => res.text() )
|
|
|
|
|
.then( (text) => console.log(text) )
|
|
|
|
|
'
|
|
|
|
|
;;
|
|
|
|
|
|
2024-09-24 16:13:37 +02:00
|
|
|
|
*)
|
|
|
|
|
alert $ext "extension is not supported (yet)"
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
|
|
esac
|
|
|
|
|
|
2024-09-11 14:58:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-23 17:53:59 +02:00
|
|
|
|
# usage: man <topic>
|
|
|
|
|
#
|
|
|
|
|
# info: simple manual viewer
|
2024-09-13 18:55:12 +02:00
|
|
|
|
man(){
|
|
|
|
|
test "$1" = xrsh && {
|
|
|
|
|
cat /root/manual.md | less
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-15 11:18:36 +02:00
|
|
|
|
help(){ man xrsh; }
|
2024-09-13 18:55:12 +02:00
|
|
|
|
|
2024-09-23 17:53:59 +02:00
|
|
|
|
# usage: infinite ./runmydaemon
|
|
|
|
|
#
|
|
|
|
|
# info: will run daemon forever (restarts when quit)
|
2024-09-15 11:18:36 +02:00
|
|
|
|
infinite(){
|
|
|
|
|
while sleep 1s; do
|
|
|
|
|
"$@"
|
2024-09-11 14:58:31 +02:00
|
|
|
|
done
|
|
|
|
|
}
|
2024-09-23 17:53:59 +02:00
|
|
|
|
|
|
|
|
|
# usage: wait_for sleep 5s
|
|
|
|
|
#
|
|
|
|
|
# info: will print dots while waiting for process to end
|
|
|
|
|
wait_for(){
|
|
|
|
|
dotting(){ while sleep 1s; do printf "."; done; }
|
|
|
|
|
dotting &
|
|
|
|
|
PID=$!
|
|
|
|
|
"$@"
|
|
|
|
|
kill -9 $PID
|
|
|
|
|
printf "\n"
|
|
|
|
|
}
|