« get me outta code hell

don't include ANSI escape codes in measureColumns - tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2020-07-15 22:08:34 -0300
committerFlorrie <towerofnix@gmail.com>2020-07-15 22:08:34 -0300
commit3bd328e68894a7819aa26e2325d7984eaa3959e0 (patch)
tree43202cd7a0cbaa3d94a2116622f951a7a37b820a
parentbe0919ac31b1048248941ed0c290c03824732297 (diff)
don't include ANSI escape codes in measureColumns
This allows pretty much any code based around measureColumns
(e.g. buttons, labels, word-wrapping code) to handle ANSI-formatted
text without any miscalculated measurements.
-rw-r--r--package-lock.json2
-rw-r--r--package.json2
-rw-r--r--util/ansi.js7
3 files changed, 8 insertions, 3 deletions
diff --git a/package-lock.json b/package-lock.json
index cb7226e..8530981 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
 {
   "name": "tui-lib",
-  "version": "0.2.2",
+  "version": "0.2.3",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
diff --git a/package.json b/package.json
index a1d575d..2f3f82f 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "tui-lib",
-  "version": "0.2.2",
+  "version": "0.2.3",
   "description": "terminal ui library",
   "main": "index.js",
   "repository": "https://notabug.org/towerofnix/tui-lib.git",
diff --git a/util/ansi.js b/util/ansi.js
index ac511ed..f921349 100644
--- a/util/ansi.js
+++ b/util/ansi.js
@@ -153,7 +153,12 @@ const ansi = {
   },
 
   measureColumns(text) {
-    // Returns the number of columns the given text takes.
+    // Returns the number of columns the given text takes. Accounts for escape
+    // codes (by not including them in the returned width).
+
+    if (text.includes(ESC)) {
+      text = text.replace(new RegExp(ESC + '\\[\\??[0-9]*.', 'g'), '')
+    }
 
     return wcwidth(text)
   },