From 63ed43e8a0fbe5f8d17fe67b2b262356eeb5c47c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Smodi=C5=A1?= Date: Tue, 1 Sep 2015 06:40:33 +0200 Subject: Bugfix terminal: cursor movement on input. --- js/codeq/console.js | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) (limited to 'js') diff --git a/js/codeq/console.js b/js/codeq/console.js index 7dba8e1..bc7f3c6 100644 --- a/js/codeq/console.js +++ b/js/codeq/console.js @@ -414,17 +414,51 @@ this.splice(row, col, row, col, text, className || 'output'); }, - 'append': function (text, className) { + 'append': function (text, className, dontMoveCursor) { // we add to the end, so optimize: do not call splice(), it is too heavy for this purpose var newLines = formNewLines(text), - startRow = currentRow, + originalCurrentRow = currentRow, + startRow = lines.length - 1, i, lineDescriptor; if (newLines.length == 0) return; // nothing to do - lineDescriptor = lines[currentRow]; + lineDescriptor = lines[startRow]; lineDescriptor.content = lineDescriptor.content + newLines[0]; - renderLine(lineDescriptor); for (i = 1; i < newLines.length; i++) { - renderLine(this.insertRow(startRow + i, newLines[i], className, true)); + lineDescriptor = this.insertRow(startRow + i, newLines[i], className, true); + } + if (!dontMoveCursor) { + currentRow = lines.length - 1; + currentCol = lines[currentRow].content.length; + } + this.reflow(startRow < originalCurrentRow ? startRow : originalCurrentRow); + }, + + 'insertAtCursor': function (text, className, dontMoveCursor) { + var startRow = currentRow, + currentLine = lines[currentRow], + currentContent = currentLine.content, + part1 = currentContent.substring(0, currentCol), + part2 = currentContent.substring(currentCol), + newLines = formNewLines(text), + i, n; + if (newLines.length == 0) return; // nothing to do + else if (newLines.length == 1) { + currentLine.content = part1 + newLines[0] + part2; + if (!dontMoveCursor) currentCol += newLines[0].length; + renderLine(currentLine); + } + else { + currentLine.content = part1 + newLines[0]; + n = newLines.length - 1; + for (i = 1; i < n; i++) { + this.insertRow(startRow + i, newLines[i], className, true); + } + this.insertRow(startRow + n, newLines[n] + part2, className, true); + if (!dontMoveCursor) { + currentRow = startRow + n; + currentCol = newLines[n].length; + } + this.reflow(startRow); } }, @@ -537,7 +571,7 @@ } // now serve the cooking - handler.append(cookedChars, 'input'); // append what we have to the display + handler.insertAtCursor(cookedChars, 'input'); // append what we have to the display if (typeof handler.onInput === 'function') { // now tell the owner what we've done if (lineBuffered) { if (currentRow !== startingRow) { -- cgit v1.2.1