HTML 预支持CR的正确用法(\r)

HTML Pre and supporting the correct usage of CR (\r)

所以我在 node.js 中闲逛,试图看看我是否可以构建一个 Web 服务器,让我可以在 windows 机器上访问 cmd.exe。在大多数情况下,这按预期工作。

我遇到的问题你可以在图片底部看到

 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

这是因为正在 运行 的脚本正在使用 \r 作为 CR,因此它应该返回到因 <pre> 而失败的行的开头,并且被忽略了,所以我正在想办法解决这个问题。

所以我尝试实现一个渲染方法来处理这个问题,方法是逐个检查一个字符并构建一个新行位置数组,当它遇到 CR \r 时 t运行将要显示的字符串从0到最后一个LF的位置\n

我遇到的问题是 none 的 CR 输出现在正在显示。

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);
}

所以经过大量的摆弄和调试,我发现了几个问题

1) 在 CR \r 之后,它卡住了检查下一个打开的下一个,所以它总是在每个字符写完后返回到最后一个换行符

2) 如果启用了下一个检查,它不会写出下一个字符,除非它是 LF\n

实现的代码是

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);
}