So I'm mucking about in node.js and trying to see if i can build a web server that gives me access cmd.exe
on a windows machine. for the most part this works as expected.
The problem i'm having you can see at the bottom of the image
Proccessed 0 out of 26 and 0 have failedProccessed 1 out of 26 and 0 have failedProccessed 2 out of 30 and 0 have failedProccessed 3 out of 30 and 0 have failedProccessed 4 out of 30 and 0 have failedProccessed 5 out of 30 and 0 have failedProccessed 6 out of 30 and 0 have failed
This is because the script that is being run is using \r
as a CR so it's supposed to be returning to the start of the line that is failing with <pre>
and being ignored so I'm trying to work out how I could do this.
So I have tried to implement a render method that handles this by checking char by char and building an array of positions of new lines, and when it comes across the CR \r
it truncates the string for display from 0 to the position of the last LF \n
The problem I have this is that I none of my CR output is being displayed now.
function renderTerminal(text){
let lf = [];
let newText = "";
let checkNext = false;
text.split('').forEach((char, idx) => {
if(checkNext){
if(char === "\n"){
lf.push(idx);
newText += char;
checkNext = false;
}else{
let pos = lf[lf.length];
newText = newText.substr(0, pos);
}
}else{
if(char === "\n"){ lf.push(idx); }
if(char === "\r"){ checkNext = true; }
newText += char;
}
});
$(".terminal pre").text(newText);
}
So after lots of fiddling and debugging, I discovered a couple of problems
1) After a CR \r
it was getting stuck with a check next turned on there for the next, so it was always returning to the last newline after every char wrote out
2) if check next was enabled it was not writing out the next char unless it was a LF\n
The implemented code is
function renderTerminal(text){
let lf = [];
let newText = "";
let checkNext = false;
text.split('').forEach((char, idx) => {
if(checkNext){
if(char === "\n"){
lf.push(idx);
newText += char;
checkNext = false;
}else{
let pos = lf[lf.length-1];
newText = newText.substr(0, pos+1);
newText += char;
checkNext = false;
}
}else{
if(char === "\n"){ lf.push(idx); }
if(char === "\r"){ checkNext = true; }
newText += char;
}
});
$(".terminal pre").text(newText);
// handle auto scrolling down
$('.terminal pre').scrollTop($('.terminal pre')[0].scrollHeight);
}