fix: Fixes backspacing to work fully properly.

This commit is contained in:
greysoh 2024-05-06 21:28:00 -04:00
parent e85838d438
commit ea9279b16d
No known key found for this signature in database
GPG key ID: FE0F173B8FC01571

View file

@ -22,28 +22,27 @@ export async function readFromKeyboard(stream: ServerChannel, disableEcho: boole
return; return;
} else if (readStreamData.includes("\x7F")) { } else if (readStreamData.includes("\x7F")) {
// TODO (greysoh): investigate maybe potential deltaCursor overflow (haven't tested)
// TODO (greysoh): investigate deltaCursor underflow
// TODO (greysoh): make it not run like shit (I don't know how to describe it)
if (line.length == 0) return setTimeout(eventLoop, 5); // Here because if we do it in the parent if statement, shit breaks if (line.length == 0) return setTimeout(eventLoop, 5); // Here because if we do it in the parent if statement, shit breaks
line = line.substring(0, lineIndex - 1) + line.substring(lineIndex); line = line.substring(0, lineIndex - 1) + line.substring(lineIndex);
if (!disableEcho) { if (!disableEcho) {
let deltaCursor = line.length - lineIndex; const deltaCursor = line.length - lineIndex;
if (deltaCursor == line.length) return setTimeout(eventLoop, 5);
// wtf?
if (deltaCursor < 0) { if (deltaCursor < 0) {
console.log("FIXME: somehow, our deltaCursor value is negative! please investigate me"); // Use old technique if the delta is < 0, as the new one is tailored to the start + 1 to end - 1
return setTimeout(eventLoop, 5); stream.write("\u0008 \u0008");
} } else {
// Jump forward to the front, and remove the last character // Jump forward to the front, and remove the last character
stream.write(rightEscape.repeat(deltaCursor) + " " + ourBackspace); stream.write(rightEscape.repeat(deltaCursor) + " " + ourBackspace);
// Go backwards & rerender text & go backwards again (wtf?) // Go backwards & rerender text & go backwards again (wtf?)
stream.write(leftEscape.repeat(deltaCursor + 1) + line.substring(lineIndex - 1) + leftEscape.repeat(deltaCursor + 1)); stream.write(leftEscape.repeat(deltaCursor + 1) + line.substring(lineIndex - 1) + leftEscape.repeat(deltaCursor + 1));
} }
lineIndex -= 1;
}
} else if (readStreamData.includes("\x1B")) { } else if (readStreamData.includes("\x1B")) {
if (readStreamData.includes(rightEscape)) { if (readStreamData.includes(rightEscape)) {
if (lineIndex + 1 > line.length) return setTimeout(eventLoop, 5); if (lineIndex + 1 > line.length) return setTimeout(eventLoop, 5);