« 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/wrap.js8
1 files changed, 7 insertions, 1 deletions
diff --git a/util/wrap.js b/util/wrap.js
index 3c381d4..71a1f1c 100644
--- a/util/wrap.js
+++ b/util/wrap.js
@@ -1,3 +1,5 @@
+const ansi = require('./ansi')
+
 module.exports = function wrap(str, width) {
   // Wraps a string into separate lines. Returns an array of strings, for
   // each line of the text.
@@ -6,13 +8,17 @@ module.exports = function wrap(str, width) {
   const words = str.split(' ')
 
   let curLine = words[0]
+  let curColumns = ansi.measureColumns(curLine)
 
   for (const word of words.slice(1)) {
-    if (curLine.length + word.length > width) {
+    const wordColumns = ansi.measureColumns(word)
+    if (curColumns + wordColumns > width) {
       lines.push(curLine)
       curLine = word
+      curColumns = wordColumns
     } else {
       curLine += ' ' + word
+      curColumns += 1 + wordColumns
     }
   }