|
|
|
function TtyBlock (divName, numLines, rollOverLength, rollOverRemove) { |
|
var i; |
|
|
|
this.elem = document.getElementById(divName); |
|
|
|
|
|
this.curPos = 0; |
|
this.cursorShown = false; |
|
|
|
|
|
this.timePassedType = 0; |
|
this.timePassedFlash = 0; |
|
|
|
|
|
this.typePeriod = 30; |
|
this.flashPeriod = 300; |
|
|
|
this.lines = []; |
|
for (i = 0; i < numLines; i += 1) { |
|
this.lines.push(""); |
|
} |
|
|
|
this.rollOverLength = rollOverLength || 9; |
|
this.rollOverRemove = rollOverRemove || 3; |
|
|
|
this.backlog = []; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
TtyBlock.prototype.draw = function (dTime) { |
|
var i, |
|
outputString = "", |
|
lastLine; |
|
|
|
this.timePassedType += dTime; |
|
|
|
while (this.timePassedType > this.typePeriod) { |
|
this.curPos += 1; |
|
this.timePassedType -= this.typePeriod; |
|
} |
|
|
|
lastLine = this.lines[this.lines.length-1]; |
|
|
|
if (this.curPos > lastLine.length) { |
|
this.timePassedFlash += dTime; |
|
while (this.timePassedFlash > this.flashPeriod) { |
|
this.cursorShown = !this.cursorShown; |
|
this.timePassedFlash -= this.flashPeriod; |
|
} |
|
} |
|
|
|
|
|
if (this.curPos > lastLine.length && this.backlog.length > 0) { |
|
this.lines.shift(); |
|
lastLine = this.backlog.shift(); |
|
this.lines.push(lastLine); |
|
this.curPos = 0; |
|
} |
|
|
|
|
|
for (i = 0; i < this.lines.length - 1; i += 1) { |
|
outputString += this.lines[i] + "<br/>"; |
|
} |
|
outputString += lastLine.slice(0, Math.min(this.curPos, lastLine.length)); |
|
if (this.cursorShown) { |
|
outputString += "_"; |
|
} |
|
|
|
outputString.replace('>', '>'); |
|
this.elem.innerHTML = outputString; |
|
}; |
|
|
|
TtyBlock.prototype.addLine = function(str) { |
|
|
|
if (this.backlog.length > this.rollOverLength) { |
|
this.backlog.splice(this.backlog.length - this.rollOverRemove, this.rollOverRemove); |
|
} |
|
|
|
this.backlog.push(" > " + str); |
|
}; |
|
|