« get me outta code hell

tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
path: root/util/ansi.js
diff options
context:
space:
mode:
Diffstat (limited to 'util/ansi.js')
-rw-r--r--util/ansi.js19
1 files changed, 15 insertions, 4 deletions
diff --git a/util/ansi.js b/util/ansi.js
index cfe7b1f..a303cd6 100644
--- a/util/ansi.js
+++ b/util/ansi.js
@@ -119,7 +119,10 @@ const ansi = {
   },
 
 
-  interpret(text, scrRows, scrCols, oldChars = null, lastChar = null) {
+  interpret(text, scrRows, scrCols, {
+    oldChars = null, oldLastChar = null,
+    oldCursorRow = 0, oldCursorCol = 0
+  } = {}) {
     // Interprets the given ansi code, more or less.
 
     const blank = {
@@ -360,7 +363,7 @@ const ansi = {
 
     // Character concatenation -----------
 
-    lastChar = lastChar || {
+    let lastChar = oldLastChar || {
       char: '',
       attributes: []
     }
@@ -397,9 +400,17 @@ const ansi = {
       }
     }
 
+    // If anything changed *or* the cursor moved, we need to put it back where
+    // it was before:
+    if (result.length || cursorCol !== oldCursorCol || cursorRow !== oldCursorRow) {
+      result.push(ansi.moveCursor(cursorRow, cursorCol))
+    }
+
     return {
-      newChars: newChars.slice(),
-      lastChar: Object.assign({}, lastChar),
+      oldChars: newChars.slice(),
+      oldCursorRow: cursorRow,
+      oldCursorCol: cursorCol,
+      oldLastChar: Object.assign({}, lastChar),
       screen: result.join('')
     }
   }