« 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
diff options
context:
space:
mode:
Diffstat (limited to 'util')
-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 &&