urbit/main/app/terminal/hymn/script.js

100 lines
2.5 KiB
JavaScript
Raw Normal View History

2014-10-14 00:00:28 +04:00
function jpok(a,b){
var dat = {pax:urb.term.pax, act:{}}
dat.act[a] = b
2014-10-24 01:00:23 +04:00
urb.send({data:dat,mark:"term-in"}, function(e,dat){
2014-10-14 00:00:28 +04:00
if(a === 'line' && dat.data.err){
hist.unshift(prom.val())
prom.val(b)
hind = 0
}
})
}
var prom, prom_size, hist, hind, size, focus, pos, pax
$(function() {
prom = $("#prom")
prom_size = $("#prom-size")
hist = []
hind = 0
var keys = ['l', 'x', 'r']
var mod = /Mac|iPod|iPhone|iPad/.test(navigator.platform) ? 'ctrl' : 'alt'
2014-10-14 00:00:28 +04:00
for (i in keys) (function(k){
Mousetrap.bind(mod + '+' + k, function(){
2014-10-14 00:00:28 +04:00
jpok('cmd', k)
})
})(keys[i])
prom.keydown(function(e){
switch(e.which){
default: return true
break;
case 13: // %retn
if(e.shiftKey) return true
v = prom.val().replace(/\xa0/g, ' ')
$(cont).append($('<b>').html(prem.innerHTML),
2014-10-25 00:08:19 +04:00
$('<div class="prom">').text(v))
2014-10-14 00:00:28 +04:00
jpok('line', v)
hist.unshift(v)
prom.val('')
return false
break;
case 38: // %up
if(hind == hist.length) return true
if(pos().top === false) return true
prom.val([hist[hind], hist[hind] = prom.val()][0]) // swap
size()
hind++
return false
break;
case 40: // %down
if(hind == 0) return true
if(pos().bot === false) return true
size()
hind--
prom.val([hist[hind], hist[hind] = prom.val()][0]) // swap
return false
}
})
focus = function() { $(prom).focus(); }
$('body').on('click', focus)
focus()
size = function() {
prom_size.html(prom.val()+"<br />")
}
size()
prom.on('input propertychange', size)
pos = function() {
v = prom.val()
_top = v.slice(0,prom[0].selectionStart).indexOf("\n") === -1
_bot = v.slice(prom[0].selectionStart).indexOf("\n") === -1
return {top:_top,bot:_bot}
}
2014-10-23 07:02:18 +04:00
pax = '/lines'
2014-10-14 00:00:28 +04:00
if(urb.term.pax != "/") pax += urb.term.pax
urb.subscribe({path: pax}, function(e, dat){
if(dat.data.ok) return;
2014-10-22 06:59:05 +04:00
hist = dat.data.history.concat(hist)
2014-10-14 00:00:28 +04:00
hind = 0
2014-10-22 06:59:05 +04:00
// cont.innerHTML = ''
2014-10-14 00:00:28 +04:00
for(var i in dat.data.lines){
var lom = dat.data.lines[i]
if(typeof lom == 'string')
$(cont).append($('<div>').text(lom))
else {
2014-10-23 03:20:34 +04:00
$(cont).append($('<b>').text(lom.prompt),
2014-10-25 00:08:19 +04:00
$('<div class="prom">').text(lom.task))
2014-10-14 00:00:28 +04:00
}
}
window.scrollTo(0,document.body.scrollHeight)
2014-10-23 03:20:34 +04:00
prem.textContent = dat.data.prompt
2014-10-14 00:00:28 +04:00
})
});