« get me outta code hell

Clean up old state code in ANSI interpreter - 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
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2017-12-10 11:13:22 -0400
committerFlorrie <towerofnix@gmail.com>2017-12-10 11:14:29 -0400
commitce20598ca90b922592e1ddf38eb8f218cc9daa73 (patch)
tree7590ed9b83b0e0fb1d36d44550dbe79a7536869d /util
parentd6495a9cfddb65fb9b3cb09771ee0bef88d5d9f7 (diff)
Clean up old state code in ANSI interpreter
Diffstat (limited to 'util')
-rw-r--r--util/Flushable.js12
-rw-r--r--util/ansi.js19
2 files changed, 21 insertions, 10 deletions
diff --git a/util/Flushable.js b/util/Flushable.js
index 78bf5c5..c852421 100644
--- a/util/Flushable.js
+++ b/util/Flushable.js
@@ -86,13 +86,13 @@ module.exports = class Flushable {
 
   compress(toWrite) {
     // TODO: customize screen size
-    let { newChars, lastChar, screen } = ansi.interpret(
-      toWrite, this.screenLines, this.screenCols,
-      this.lastFrameChars, this.lastFrameLastChar
+    const output = ansi.interpret(
+      toWrite, this.screenLines, this.screenCols, this.lastFrame
     )
 
-    this.lastFrameChars = newChars
-    this.lastFrameLastChar = lastChar
+    let { screen } = output
+
+    this.lastFrame = output
 
     if (this.shouldShowCompressionStatistics) {
       const pcSaved = Math.round(100 - (100 / toWrite.length * screen.length))
@@ -100,7 +100,7 @@ module.exports = class Flushable {
         '\x1b[H\x1b[0m(ANSI-interpret: ' +
         `${toWrite.length} -> ${screen.length} ${pcSaved}% saved)  `
       )
-      this.lastFrameLastChar.attributes = []
+      this.lastFrame.oldLastChar.attributes = []
     }
 
     return screen
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('')
     }
   }