Skip to content

Commit 9698caf

Browse files
better js-to-terminal API
1 parent 1f0710b commit 9698caf

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

com/isoterminal.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ if( typeof AFRAME != 'undefined '){
100100
this.calculateDimension()
101101
this.initHud()
102102
this.setupPasteDrop()
103+
this.setupEvents()
103104

104105
fetch(this.data.iso,{method: 'HEAD'})
105106
.then( (res) => {
@@ -429,6 +430,21 @@ if( typeof AFRAME != 'undefined '){
429430
this.rows = Math.floor( (this.data.height*0.93)/this.data.lineHeight)-1
430431
},
431432

433+
setupEvents: function(){
434+
this.el.addEventListener('exec', (e) => this.term.exec( e.detail ) )
435+
this.el.addEventListener('hook', (e) => this.term.hook( e.detail[0], e.detail[1] ) )
436+
this.el.addEventListener('send', (e) => this.term.send( e.detail[0], e.detail[1] || 0 ) )
437+
this.el.addEventListener('create_file', async (e) => await this.term.worker.create_file( e.detail[0], this.term.convert.toUint8Array(e.detail[1]) ) )
438+
this.el.addEventListener('update_file', async (e) => await this.term.worker.update_file( e.detail[0], this.term.convert.toUint8Array(e.detail[1]) ) )
439+
this.el.addEventListener('append_file', async (e) => await this.term.worker.append_file( e.detail[0], this.term.convert.toUint8Array(e.detail[1]) ) )
440+
this.el.addEventListener('read_file', async (e) => {
441+
const buf = await this.term.worker.read_file( e.detail[0] )
442+
const str = new TextDecoder().decode(buf)
443+
if( typeof e.detail[1] == 'function' ) e.detail[1](str)
444+
else console.log(str)
445+
})
446+
},
447+
432448
events:{
433449

434450
// combined AFRAME+DOM reactive events

com/isoterminal/feat/autorestore.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@ if( typeof emulator != 'undefined' ){
4343
]).then( () => {
4444

4545
localforage.getItem("state", async (err,stateBase64) => {
46-
if( stateBase64 && !err && confirm('continue last session?') ){
46+
const askConfirm = () => {
47+
try{
48+
const scene = document.querySelector('a-scene');
49+
if( scene.is('ar-mode') ) scene.exitAR()
50+
if( scene.is('vr-mode') ) scene.exitVR()
51+
}catch(e){}
52+
return confirm('continue last session?')
53+
}
54+
if( stateBase64 && !err && askConfirm() ){
4755
this.noboot = true // see feat/boot.js
4856
try{
4957
await this.worker.restore_state()

com/isoterminal/feat/term.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,19 @@ ISOTerminal.prototype.TermInit = function(){
7171
// but instead extend/override ISOTerminal.prototype.boot.menu
7272
// as demonstrated in index.html
7373
this.term.setKeyHandler( (ch) => {
74-
let erase = false
74+
let erase = false
75+
const isEnter = ch == '\n' || ch == '\r'
7576
if( ch == '\x7F' ){
7677
ch = "\b \b" // why does write() not just support \x7F ?
7778
erase = true
7879
}
7980
if( this.boot.menu.selected ){
80-
this.boot.menu.selected.keyHandler.call(this,ch)
81-
}else if( (ch == "\n" || ch == "\r") ){
81+
if( isEnter ){
82+
this.boot.menu.selected.cmdHandler.call(this,this.lastCmd)
83+
this.lastCmd = ""
84+
}
85+
else this.boot.menu.selected.keyHandler.call(this,ch)
86+
}else if( isEnter ){
8287
let menuitem = this.boot.menu.find( (m) => m.key == this.lastChar )
8388
if( menuitem ){
8489
this.boot.menu.selected = menuitem
@@ -88,10 +93,14 @@ ISOTerminal.prototype.TermInit = function(){
8893
})
8994
}
9095
}else{
91-
this.term.write( ch )
96+
ch.split("").map( (ch) => this.term.write( ch ) )
9297
}
93-
if( !erase ) this.lastChar = ch
98+
if( !erase ){
99+
this.lastChar = ch
100+
this.lastCmd = this.lastCmd ? this.lastCmd + ch : ch
101+
}else this.lastCmd = this.lastCmd ? this.lastCmd.substr(0, this.lastCmd.length-1) : ""
94102
})
103+
95104
aEntity.el.addEventListener('focus', () => {
96105
let textarea = el.querySelector("textarea")
97106
textarea.focus()

0 commit comments

Comments
 (0)