Skip to content

Commit

Permalink
Merge pull request #10 from boddiul/main
Browse files Browse the repository at this point in the history
char by char printing
  • Loading branch information
profelis authored Jan 14, 2023
2 parents cd3d2da + 0ece08d commit 861117e
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 41 deletions.
35 changes: 28 additions & 7 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class Editor {





let loadDefaultSample = true;


Expand Down Expand Up @@ -223,8 +225,8 @@ class Editor {
if ("message" in e)
msg = e.message;

this.outputView.print(msg,'error');
this.outputView.print("An error occurred. Environment will reload",'error');
this.outputView.printLine(msg,'error');
this.outputView.printLine("An error occurred. Environment will reload",'error');

this.setPageStatus("Reloading","waiting")

Expand All @@ -240,13 +242,32 @@ class Editor {
}.bind(this));


this.runtimeController.onPrint = function(text){
this.runtimeController.onPrintLine = function(text){


this.outputView.print(text,"");
this.outputView.printLine(text,"");

this.outputPool.push(text);

}.bind(this);

this.runtimeController.onPrintChar = function(ch) {

this.outputView.printChar(ch);

if (ch==="\n")
this.outputPool.push("")
else
{

if (this.outputPool.length === 0)
this.outputPool.push("");

this.outputPool[this.outputPool.length-1] += ch;
}



}.bind(this);


Expand Down Expand Up @@ -597,7 +618,7 @@ class Editor {

this.runtimeController.run(this.fileSystem.getFile("runtime").path+this.fileSystem.getFile("runtime").name,
fName,fName === "test" ? function () {
this.outputView.print( "TEST FINISHED" ,"test");
this.outputView.printLine( "TEST FINISHED" ,"test");
}.bind(this) : null);


Expand Down Expand Up @@ -627,7 +648,7 @@ class Editor {

this.loadSample('tests',i,false,function(fileSystem) {

this.outputView.print("Running Test "+(i+1)+"/"+this.samplesData["tests"].length+": "+this.samplesData["tests"][i].name,"test");
this.outputView.printLine("Running Test "+(i+1)+"/"+this.samplesData["tests"].length+": "+this.samplesData["tests"][i].name,"test");

this.outputPool = [];

Expand Down Expand Up @@ -677,7 +698,7 @@ class Editor {
ok = false;


this.outputView.print(this.samplesData["tests"][i].name+" Test "+(i+1)+"/"+this.samplesData["tests"].length+": "+(ok ? "SUCCESS" : "FAIL"),ok ? "success": 'error');
this.outputView.printLine(this.samplesData["tests"][i].name+" Test "+(i+1)+"/"+this.samplesData["tests"].length+": "+(ok ? "SUCCESS" : "FAIL"),ok ? "success": 'error');

if (i<this.samplesData["tests"].length-1)
this.runTest(i+1);
Expand Down
90 changes: 66 additions & 24 deletions src/outputview.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,101 @@ class OutputView

this.outputDiv = document.getElementById("output");



this.currentLine = null;
this.currentTime = null;
this.currentP = null;


this.currentText = "";

document.getElementById("clear_output").addEventListener("click",function(){

this.clear();
}.bind(this))


this.addLine();
}


clear()
{

while (this.outputDiv.firstChild) {
while (this.outputDiv.firstChild)
this.outputDiv.removeChild(this.outputDiv.lastChild);
}

this.currentTime = null;
this.currentP = null;
this.currentLine = null;

this.currentText = "";

this.addLine();
}


print(text,type) {

var currentdate = new Date();


addLine() {

let out = document.createElement("div");
out.classList.add("output_line");

out.classList.toggle("error",type=="error");
out.classList.toggle("test",type=="test");
out.classList.toggle("success",type=="success");


let outTime = document.createElement("div");
outTime.classList.add("output_line_time");
outTime.classList.add("unselectable");
outTime.innerText = currentdate.toISOString().substr(11, 12);

out.appendChild(outTime);


let outP = document.createElement("p");
outP.classList.add("output_line_text");
outP.innerText = text;


out.appendChild(outTime);
out.appendChild(outP);


this.outputDiv.appendChild(out);




this.currentLine = out;
this.currentTime = outTime;
this.currentP = outP;
this.currentText = "";

this.outputDiv.scrollTop = this.outputDiv.scrollHeight;
}



printChar(ch) {

if (ch==="\n")
this.addLine()
else
{
this.currentText += ch;
this.currentP.innerText = this.currentText;
}
this.currentTime.innerText = (new Date()).toISOString().substring(11, 23);

}


printLine(text,type) {


if (this.currentText!=="")
this.addLine();

this.currentLine.classList.toggle("error",type=="error");
this.currentLine.classList.toggle("test",type=="test");
this.currentLine.classList.toggle("success",type=="success");

/*
for (let i=0;i<text.length;i++)
this.printChar(text.charAt(i))*/


this.currentText = text;
this.currentP.innerText = this.currentText;
this.currentTime.innerText = (new Date()).toISOString().substring(11,23);

if (this.currentText!=="")
this.addLine();
}
}

Expand Down
50 changes: 40 additions & 10 deletions src/runtimecontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,44 @@ var Module = {




class RuntimeController
{
constructor()
{
this.loaded = false;

this.onInit = null;
this.onPrint = null;

Module.print = function(text) {
this.onPrintLine = null;
this.onPrintChar = null;

if (arguments.length > 1)
text = Array.prototype.slice.call(arguments).join(' ');

this.printByChar = true;

Module.print = [
function(char) {
if (this.onPrintChar && this.printByChar)
this.onPrintChar(char);
}.bind(this),

function(line) {
if (this.onPrintLine && !this.printByChar)
this.onPrintLine(line);
}.bind(this)
]


if (this.onPrint)
this.onPrint(text);
}.bind(this);


Module.onRuntimeInitialized = function() {


// print hack
this.fixTTY();

this.loaded = true;
if (this.onInit)
this.onInit()


}.bind(this)

Expand All @@ -44,6 +57,23 @@ class RuntimeController
return this.loaded;
}

fixTTY()
{

TTY.default_tty_ops.put_char = function(tty, val) {

out[0](String.fromCharCode(val))

if (val === null || val === 10) {
out[1](UTF8ArrayToString(tty.output, 0));
tty.output = []
} else {
if (val != 0)
tty.output.push(val)
}
}

}


setFS(fileSystem)
Expand Down Expand Up @@ -100,12 +130,12 @@ class RuntimeController
script.type = "text/javascript"
document.body.appendChild(script);



}





}

0 comments on commit 861117e

Please sign in to comment.