diff --git a/com/isoterminal.js b/com/isoterminal.js index bd9e385..0f310a0 100644 --- a/com/isoterminal.js +++ b/com/isoterminal.js @@ -82,7 +82,30 @@ AFRAME.registerComponent('isoterminal', { //var term = new Terminal() //term.open(dom) //term.write('Hello from \x1B[1;3;31mxterm.js\x1B[0m $ ')`` + if( typeof Terminal == undefined ) throw 'xterm terminal not loaded' + // monkeypatch Xterm (which V86 initializes) so we can add our own constructor args + window._Terminal = window.Terminal + window.Terminal = function(opts){ + const term = new window._Terminal({ ...opts, + cursorBlink:true, + onSelectionChange: function(e){ + debugger + } + }) + term.onSelectionChange( () => { + document.execCommand('copy') + term.select(0, 0, 0) + instance.setStatus('copied to clipboard') + }) + return term + } + + instance.setStatus = (msg) => { + const w = instance.winbox + w.titleBak = w.titleBak || w.title + instance.winbox.setTitle( `${w.titleBak} [${msg}]` ) + } var emulator = window.emulator = dom.emulator = new V86({ wasm_path: "com/isoterminal/v86.wasm", @@ -157,7 +180,9 @@ AFRAME.registerComponent('isoterminal', { "com/isoterminal/mnt/confirm", "com/isoterminal/mnt/prompt", "com/isoterminal/mnt/alert", + "com/isoterminal/mnt/hook", "com/isoterminal/mnt/profile", + "com/isoterminal/mnt/profile.js", "com/isoterminal/mnt/motd", ] diff --git a/com/isoterminal/mnt/alert b/com/isoterminal/mnt/alert index 530b6c8..b071a04 100755 --- a/com/isoterminal/mnt/alert +++ b/com/isoterminal/mnt/alert @@ -1,2 +1,2 @@ #!/bin/sh -jsh alert "$1" +echo "$(printf "\033[0m")[i] $1 $(printf "\033[0m")" diff --git a/com/isoterminal/mnt/confirm b/com/isoterminal/mnt/confirm index c10d2c4..4045f01 100755 --- a/com/isoterminal/mnt/confirm +++ b/com/isoterminal/mnt/confirm @@ -1,10 +1,4 @@ #!/bin/sh -test -f /mnt/V86 && { - jsh confirm $1 $2 -} - -test -f /mnt/V86 || { - read -p "$(printf "\033[0m")[?] $1 [y/n] $(printf "\033[0m")" y - test $y = y && echo true && exit - echo false -} +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/gui b/com/isoterminal/mnt/gui deleted file mode 100644 index 47135f0..0000000 --- a/com/isoterminal/mnt/gui +++ /dev/null @@ -1,7 +0,0 @@ -PS3="Enter a number: " - -select character in Sheldon Leonard Penny Howard Raj -do - echo "Selected character: $character" - echo "Selected number: $REPLY" -done diff --git a/com/isoterminal/mnt/hook b/com/isoterminal/mnt/hook new file mode 100755 index 0000000..f984f11 --- /dev/null +++ b/com/isoterminal/mnt/hook @@ -0,0 +1,15 @@ +#!/bin/sh + +test -z $1 && { echo "usage: hook [args]"; exit 0; } + +cmd=$1 +shift + +test -d ~/hook.d/$cmd && { + chmod +x ~/hook.d/$cmd/* + find ~/hook.d/$cmd/ -type f -executable | while read hook; do + $hook "$@" || true + done +} + +#test $BROWSER = 1 && { diff --git a/com/isoterminal/mnt/js b/com/isoterminal/mnt/js index 7cfbf53..338e6fc 100755 --- a/com/isoterminal/mnt/js +++ b/com/isoterminal/mnt/js @@ -1,5 +1,16 @@ #!/bin/sh -flock /dev/browser/js -c "echo '$*' > /dev/browser/js; cat /dev/browser/js" +test -z $1 && { echo "Usage: js 'somefunction(1)'"; exit 0; } + +javascript="$*" + +# if we are run as shebang, use the file as input +case "$1" in + */*) javascript="$(cat $1 | tail +2)" + ;; +esac + + +flock /dev/browser/js -c "echo '$javascript' > /dev/browser/js; sleep 0.5; cat /dev/browser/js" # we use flock, an awesome way to make processes read/write the same file -# whilr preventing 1001 concurrency issues +# while preventing 1001 concurrency issues diff --git a/com/isoterminal/mnt/jsh b/com/isoterminal/mnt/jsh index 5764221..69237e1 100755 --- a/com/isoterminal/mnt/jsh +++ b/com/isoterminal/mnt/jsh @@ -7,11 +7,31 @@ to_js(){ printf "%s(" "$1" shift for arg in "$@"; do - printf '"%s",' "$arg" + case "$arg" in + (*[\.0-9]*) + printf '%s,' "$arg" + ;; + (*) + printf '"%s",' "$arg" + ;; + esac done printf ")\n" } -func=$(to_js "$@") -func=${func/,)/)} -/mnt/js "$func" +# run argument as js +test -z $1 || { + func=$(to_js "$@") + func=${func/,)/)} + js "$func" +} + +# otherwise start repl +while true; do + echo -n "$(printf "\033[0m")jsh> $(printf "\033[0m")" + read input + test $input = exit && exit + js "$input" +done + + diff --git a/com/isoterminal/mnt/profile b/com/isoterminal/mnt/profile index 7d7405b..acc6bf5 100644 --- a/com/isoterminal/mnt/profile +++ b/com/isoterminal/mnt/profile @@ -1,16 +1,34 @@ install_xrsh(){ - chmod +x /mnt/confirm /mnt/prompt /mnt/alert /mnt/js* + + setup_binaries(){ + for bin in /mnt/prompt /mnt/alert /mnt/confirm /mnt/hook /mnt/js*; do + chmod +x $bin + ln -s $bin /bin/. + done + } setup_browser_dev(){ mkdir -p /mnt/dev/browser touch /mnt/dev/browser/js touch /mnt/dev/browser/html + touch /mnt/dev/browser/console ln -s /mnt/dev/browser /dev/browser test -f /etc/profile && rm /etc/profile ln -s /mnt/profile /etc/profile } + setup_hook_dirs(){ # see /mnt/hook for usage + mkdir -p ~/hook.d/alert + mkdir -p ~/hook.d/confirm + mkdir -p ~/hook.d/prompt + echo -e "#!/bin/sh\necho yo" > ~/hook.d/alert/yo + echo -e "#!/bin/js\nalert('yo')" > ~/hook.d/alert/yo.js + echo -e "#!/usr/bin/lua\nalert('yo')" > ~/hook.d/alert/yo.lua + } + + setup_binaries setup_browser_dev + setup_hook_dirs } test -d /dev/browser || install_xrsh @@ -21,9 +39,20 @@ test -f /mnt/V86 && { echo 0 > /proc/sys/kernel/printk } +## forward not-found commands to javascript (via jsh) +command_not_found_handle(){ + echo "$1 not found" + alert "did you mean $1(...) (javascript?)" + alert "TIP: run 'jsh $1 hello' to run $1('hello')" + alert " or simply 'jsh' for a js console" +} + +# source javascript functions +#js "$(cat /mnt/profile.js)" + resize #clear cat /mnt/motd export PATH=$PATH:/mnt export PS1="\nxrsh # \033[0m" - +export BROWSER=0 # running inside v86 (wasm) will set this to 1 diff --git a/com/isoterminal/mnt/profile.js b/com/isoterminal/mnt/profile.js new file mode 100644 index 0000000..aea7364 --- /dev/null +++ b/com/isoterminal/mnt/profile.js @@ -0,0 +1,3 @@ +window.helloworld = function(){ + alert("hello world") +} diff --git a/com/isoterminal/mnt/prompt b/com/isoterminal/mnt/prompt index dff4c78..c57061b 100755 --- a/com/isoterminal/mnt/prompt +++ b/com/isoterminal/mnt/prompt @@ -1,9 +1,3 @@ #!/bin/sh -test -f /mnt/V86 && { - jsh prompt "$1" "$2" -} - -test -f /mnt/V86 || { - read -p "$(printf "\033[0m")[?] $1: $(printf "\033[0m")" answer - echo "$answer" -} +read -p "$(printf "\033[0m")[?] $1: $(printf "\033[0m")" answer +echo "$answer"