From 54c547fe4d53711365f268f3b34532f3841af857 Mon Sep 17 00:00:00 2001 From: Leon van Kammen Date: Mon, 2 Sep 2024 13:05:47 +0000 Subject: [PATCH] javascript bridge fixes --- .gitignore | 3 +++ com/isoterminal.js | 14 ++++++------- com/isoterminal/mnt/alert | 6 ------ com/isoterminal/mnt/confirm | 4 ---- com/isoterminal/mnt/hook | 11 ---------- com/isoterminal/mnt/jsh | 2 ++ com/isoterminal/mnt/motd | 6 ++++-- com/isoterminal/mnt/profile | 25 +++++++++++++++------- com/isoterminal/mnt/profile.sh | 36 ++++++++++++++++++++++++++++++++ com/isoterminal/mnt/profile.xrsh | 14 ++++++++++--- com/isoterminal/mnt/prompt | 3 --- 11 files changed, 80 insertions(+), 44 deletions(-) create mode 100644 .gitignore delete mode 100755 com/isoterminal/mnt/alert delete mode 100755 com/isoterminal/mnt/confirm delete mode 100755 com/isoterminal/mnt/hook create mode 100644 com/isoterminal/mnt/profile.sh delete mode 100755 com/isoterminal/mnt/prompt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8474985 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +com/isoterminal/mnt/hook.d +com/isoterminal/mnt/console* +com/isoterminal/mnt/dev/* diff --git a/com/isoterminal.js b/com/isoterminal.js index 71f900e..057f67e 100644 --- a/com/isoterminal.js +++ b/com/isoterminal.js @@ -174,12 +174,9 @@ AFRAME.registerComponent('isoterminal', { const files = [ "com/isoterminal/mnt/js", "com/isoterminal/mnt/jsh", - "com/isoterminal/mnt/confirm", - "com/isoterminal/mnt/prompt", - "com/isoterminal/mnt/alert", - "com/isoterminal/mnt/hook", "com/isoterminal/mnt/xrsh", "com/isoterminal/mnt/profile", + "com/isoterminal/mnt/profile.sh", "com/isoterminal/mnt/profile.xrsh", "com/isoterminal/mnt/profile.js", "com/isoterminal/mnt/motd", @@ -282,12 +279,13 @@ AFRAME.registerComponent('isoterminal', { if( typeof document.location[i] == 'string' ) env.push( 'export '+String(i).toUpperCase()+'="'+document.location[i]+'"') } - env.map( (e) => emulator.serial0_send(`echo '${e}' >> /mnt/profile\n`) ) - let boot = `source /mnt/profile; js "$(cat /mnt/profile.js)"` + await emulator.create_file("profile.browser", this.toUint8Array( env.join('\n') ) ) + let boot = `source /mnt/profile ; js "$(cat /mnt/profile.js)"` // exec hash as extra boot cmd if( document.location.hash.length > 1 ){ - boot += ` && cmd='${decodeURI(document.location.hash.substr(1))}' && $cmd` + boot += ` ; cmd='${decodeURI(document.location.hash.substr(1))}' && $cmd` } + console.dir(boot) emulator.serial0_send(boot+"\n") instance.winbox.maximize() emulator.serial_adapter.term.focus() @@ -368,7 +366,7 @@ AFRAME.registerComponent('isoterminal', { if( instance.dom.emulator && instance.dom.emulator.serial_adapter ){ setTimeout( () => { this.autoResize(instance.dom.emulator.serial_adapter.term,instance,-5) - },500) // wait for resize anim + },800) // wait for resize anim } } instance.addEventListener('window.onresize', resize ) diff --git a/com/isoterminal/mnt/alert b/com/isoterminal/mnt/alert deleted file mode 100755 index ed9e5bd..0000000 --- a/com/isoterminal/mnt/alert +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh -title=$1 -shift -msg="$*" -echo "$title $(printf "\033[0m")$msg" -hook alert $title "$msg" diff --git a/com/isoterminal/mnt/confirm b/com/isoterminal/mnt/confirm deleted file mode 100755 index 4045f01..0000000 --- a/com/isoterminal/mnt/confirm +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -read -p "$(printf "\033[0m")[?] $1 [y/n] $(printf "\033[0m")" y -test $y = y && echo true && exit -echo false diff --git a/com/isoterminal/mnt/hook b/com/isoterminal/mnt/hook deleted file mode 100755 index 67c0467..0000000 --- a/com/isoterminal/mnt/hook +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh - -test -z $1 && { echo "usage: hook [args]"; exit 0; } -cmd=$1 -shift - -test -d ~/hook.d/$cmd && { - find ~/hook.d/$cmd/ -type f -executable | while read hook; do - { $hook "$@" || true; } | awk '{ gsub(/\/root\/\//,"",$1); $1 = sprintf("%-40s", $1)} 1' - done -} diff --git a/com/isoterminal/mnt/jsh b/com/isoterminal/mnt/jsh index 7f2427b..c155bbc 100755 --- a/com/isoterminal/mnt/jsh +++ b/com/isoterminal/mnt/jsh @@ -3,6 +3,8 @@ # # 'jsh prompt question answer' executes: js prompt('question','answer') ) +source /mnt/profile.sh + to_js(){ printf "%s(" "$1" shift diff --git a/com/isoterminal/mnt/motd b/com/isoterminal/mnt/motd index 5a17055..8ed4fc0 100644 --- a/com/isoterminal/mnt/motd +++ b/com/isoterminal/mnt/motd @@ -9,6 +9,8 @@ Open, local-first, unix hackable & selfcontained XR apps. - credits: all FOSS devs | @lvk@mastodon.online - copy.sh (v86) | @utopiah@mastodon.pirateparty.be + credits: NLnet | @nlnet@nlnet.nl https://nlnet.nl/project + all FOSS devs | copy.sh (v86) aframe.io (AFRAME) + Leon van Kammen | @lvk@mastodon.online + Fabien Benetou | @utopiah@mastodon.pirateparty.be diff --git a/com/isoterminal/mnt/profile b/com/isoterminal/mnt/profile index 2305535..eab91d3 100644 --- a/com/isoterminal/mnt/profile +++ b/com/isoterminal/mnt/profile @@ -1,19 +1,30 @@ +# source URL data from v86 (file created by v86 during boot) +test -f /mnt/profile.browser && source /mnt/profile.browser + # source xrsh env source /mnt/profile.xrsh +# source shell functions +source /mnt/profile.sh + +# source js functions +js "$(cat ~/.profile.js)" + ## forward not-found commands to javascript (via jsh) command_not_found_handle(){ echo "$1 not found, did you mean $1(...) (javascript?)" - alert '[XRSH TIPS]' - alert 'js console: ' "type 'jsh'" - alert 'js shellfunction:' "type 'alias $1=\"jsh $1\"' to run '$1 yo' as $1('yo')" - alert 'js logging: ' "type 'echo 0 > /dev/browser/console.tty' to disable" - alert 'js capture log: ' "type 'tail -f /dev/browser/console'" - alert 'jsh<->sh hooks: ' "type 'chmod +x ~/hook.d/*/* && alert helloworld'" + echo "" + echo 'TIPS' + echo '----' + echo 'js console: ' "type 'jsh'" + echo 'js shellfunction:' "type 'alias $1=\"jsh $1\"' to run '$1 yo' as $1('yo')" + echo 'js logging: ' "type 'echo 0 > /dev/browser/console.tty' to disable" + echo 'js capture log: ' "type 'tail -f /dev/browser/console'" + echo 'jsh<->sh hooks: ' "type 'chmod +x ~/hook.d/*/* && alert helloworld'" } resize -test $HOSTNAME = localhost || clear +#test $HOSTNAME = localhost || clear cat /mnt/motd export PATH=$PATH:/mnt export PS1="\nxrsh # \033[0m" diff --git a/com/isoterminal/mnt/profile.sh b/com/isoterminal/mnt/profile.sh new file mode 100644 index 0000000..d0fa5d1 --- /dev/null +++ b/com/isoterminal/mnt/profile.sh @@ -0,0 +1,36 @@ +hook(){ + test -z "$1" && { echo "usage: hook [args]"; return 0; } + cmd=$1 + shift + test -d ~/hook.d/$cmd && { + find ~/hook.d/$cmd/ -type f -executable | while read hook; do + { $hook "$@" || true; } | awk '{ gsub(/\/root\/\//,"",$1); $1 = sprintf("%-40s", $1)} 1' + done + } +} + +alert(){ + test -z "$1" && { echo "usage: alert <message>"; return 0; } + title=$1 + test -z "$1" || shift + msg="$*" + printf "%s \033[0m%s\n" "$title" "$msg" + hook alert $title "$msg" +} + +confirm(){ + test -z "$1" && { echo "usage: confirm <question>"; return 0; } + read -p "$(printf "\033[0m")[?] $1 [y/n] $(printf "\033[0m")" y + 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" + read -p "$(printf "\033[0m")[?] $1: $answer $(printf "\033[0m")" answer + test -z "$answer" && answer="$answer_fallback" + echo "$answer" + hook prompt $1 $answer +} diff --git a/com/isoterminal/mnt/profile.xrsh b/com/isoterminal/mnt/profile.xrsh index 3729b68..c69ad19 100644 --- a/com/isoterminal/mnt/profile.xrsh +++ b/com/isoterminal/mnt/profile.xrsh @@ -3,12 +3,20 @@ test -d /dev/browser || { setup_binaries(){ - for bin in /mnt/prompt /mnt/alert /mnt/confirm /mnt/hook /mnt/js* /mnt/v86pipe /mnt/xrsh; do + for bin in /mnt/js* /mnt/v86pipe /mnt/xrsh; do chmod +x $bin ln -s $bin /bin/. done } + setup_links(){ + ln -s /mnt/profile ~/.profile + ln -s /mnt/profile.js ~/.profile.js + ln -s /mnt/profile.browser ~/.profile.browser + ln -s /mnt/profile.sh ~/.profile.sh + ln -s /mnt/motd ~/.motd + } + setup_browser_dev(){ mkdir -p /mnt/dev/browser touch /mnt/dev/browser/js @@ -35,7 +43,7 @@ test -d /dev/browser || { setup_network(){ test -n "$BROWSER" || return 0 - mount -a + #mount -a udhcpc 1>>/var/log/network.log 2>>/var/log/network.log & echo 0 > /proc/sys/kernel/printk } @@ -43,6 +51,6 @@ test -d /dev/browser || { setup_binaries setup_browser_dev setup_hook_dirs + setup_links } - diff --git a/com/isoterminal/mnt/prompt b/com/isoterminal/mnt/prompt deleted file mode 100755 index c57061b..0000000 --- a/com/isoterminal/mnt/prompt +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -read -p "$(printf "\033[0m")[?] $1: $(printf "\033[0m")" answer -echo "$answer"