« get me outta code hell

Add trimToColumns ansi utility function - 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:
authorFlorrie <towerofnix@gmail.com>2019-08-22 11:39:32 -0300
committerFlorrie <towerofnix@gmail.com>2019-08-22 11:39:32 -0300
commitd809ba2081041504998ad3b77c66b24e051f9458 (patch)
treec262357c7e4041b865ca3d2024cc7584ee995126 /util/ansi.js
parent27c7e362d1f6719af0d2c47b815b23d648d699a6 (diff)
Add trimToColumns ansi utility function
Diffstat (limited to 'util/ansi.js')
-rw-r--r--util/ansi.js15
1 files changed, 15 insertions, 0 deletions
diff --git a/util/ansi.js b/util/ansi.js
index d58684f..6f3e23e 100644
--- a/util/ansi.js
+++ b/util/ansi.js
@@ -143,6 +143,21 @@ const ansi = {
     return wcwidth(text)
   },
 
+  trimToColumns(text, cols) {
+    // Trims off the end of the passed text so that its width doesn't exceed
+    // the size passed in columns.
+
+    let out = ''
+    for (const char of text) {
+      if (ansi.measureColumns(out + char) <= cols) {
+        out += char
+      } else {
+        break
+      }
+    }
+    return out
+  },
+
   isANSICommand(buffer, code = null) {
     return (
       buffer[0] === 0x1b && buffer[1] === 0x5b &&