moth/www/res/terminal.js

76 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-04-03 18:07:38 -06:00
function tx(element, text, bps) {
var drawTimer;
2016-04-03 21:12:48 -06:00
var displayed = "";
2016-04-03 21:23:16 -06:00
var idx = 0;
2016-04-03 18:07:38 -06:00
function draw() {
2016-04-03 21:23:16 -06:00
displayed += text[idx];
2016-04-03 21:12:48 -06:00
element.textContent = displayed;
2016-04-03 21:23:16 -06:00
idx += 1;
2016-04-03 21:12:48 -06:00
if (element.parentNode.lastChild == element) {
element.scrollIntoView();
}
2016-04-03 21:23:16 -06:00
if (text.length == idx) {
clearInterval(drawTimer);
return;
}
2016-04-03 18:07:38 -06:00
}
// N81 uses 1 stop bit, and 1 parity bit.
// That works out to exactly 10 bits per byte.
msec = 10000 / bps;
drawTimer = setInterval(draw, msec);
draw();
}
function Terminal(target, bps) {
bps = bps || 1200;
var outq = [];
var outTimer;
function drawElement() {
var next = outq.shift();
var out = document.createElement(next[0]);
target.appendChild(out);
tx(out, next[1], bps);
if (outq.length == 0) {
clearInterval(outTimer);
}
}
2016-04-03 21:12:48 -06:00
this.clear = function() {
while (target.firstChild) {
target.removeChild(target.firstChild);
}
}
2016-04-03 18:07:38 -06:00
this.enqueue = function(tag, txt) {
outq.push([tag, txt]);
if (! outTimer) {
outTimer = setInterval(drawElement, 150);
}
}
this.par = function(txt) {
this.enqueue("p", txt);
}
this.pre = function(txt) {
this.enqueue("pre", txt);
}
}
//
// Usage:
//
// var e = Terminal(document.getElementById("output"));
// e.output("This is a paragraph. It has sentences.");
// e.output("This is a second paragraph.");
//