xrsh/xrsh
Leon van Kammen 2991f0b73e
Some checks failed
/ mirror_to_github (push) Failing after 1m42s
/ test (push) Successful in 7s
added xrsh shell-util with remote keyboard support
2025-02-14 12:17:10 +01:00

52 lines
1.2 KiB
Bash
Executable file

#!/bin/sh
trap "stty sane; trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT
PORT=9090
PROCFILE=/tmp/.xrsh
usage(){ echo "Usage: xrsh [--keyboard]"; exit; }
assert_cmds(){
for cmd in $1; do
command -v $cmd &>dev/null || { echo "error: please install $cmd (not found)"; exit 1; }
done
}
keyboard(){
assert_cmds websocat foobar
echo '
<h1>keyboard connector</h1>
<pre>
connect to ws://127.0.0.1:'$PORT'
</pre>
<script>
var ws = new WebSocket("ws://127.0.0.1:'$PORT'/");
ws.onmessage = function(event) {
if( !event.data ) return
event.data.arrayBuffer().then( (buf) => {
decoder = new TextDecoder("utf-8")
c = decoder.decode( new Uint8Array(buf) )
console.log(c)
})
};
</script>
' > /tmp/index.html
echo $$ > $PROCFILE
echo "warn: CTRL-C is forwarded too"
echo "warn: to exit this app run: $0 --kill"
stty raw
websocat -E -b -s $PORT -F /:text/html:/tmp/index.html
}
# launch!
case "$*" in
-h) usage ;;
--kill) pkill -P $(cat $PROCFILE); exit ;;
--help) usage ;;
--keyboard) keyboard ;;
--raw) RAW=1 ;;
esac