« get me outta code hell

improve & use our own word wrapping code - 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-16 14:04:25 -0300
committerFlorrie <towerofnix@gmail.com>2020-07-16 14:05:39 -0300
commit7f0579fc6e5771bbcad36591ab54119c4fe66dbd (patch)
tree408c1ee3789645e5e89533a4bf7d9746719175a5
parentcff17d1f056a73714ff1ba5a735987d140a6b51c (diff)
improve & use our own word wrapping code
-rw-r--r--package-lock.json5
-rw-r--r--package.json3
-rw-r--r--ui/WrapLabel.js6
-rw-r--r--util/wrap.js8
4 files changed, 10 insertions, 12 deletions
diff --git a/package-lock.json b/package-lock.json
index 8530981..3a51d91 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -24,11 +24,6 @@
       "requires": {
         "defaults": "^1.0.3"
       }
-    },
-    "word-wrap": {
-      "version": "1.2.3",
-      "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.3.tgz",
-      "integrity": "sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ=="
     }
   }
 }
diff --git a/package.json b/package.json
index 2f3f82f..4308565 100644
--- a/package.json
+++ b/package.json
@@ -7,7 +7,6 @@
   "author": "Florrie <towerofnix@gmail.com>",
   "license": "GPL-3.0",
   "dependencies": {
-    "wcwidth": "^1.0.1",
-    "word-wrap": "^1.2.3"
+    "wcwidth": "^1.0.1"
   }
 }
diff --git a/ui/WrapLabel.js b/ui/WrapLabel.js
index babf462..7036908 100644
--- a/ui/WrapLabel.js
+++ b/ui/WrapLabel.js
@@ -1,5 +1,5 @@
 const ansi = require('../util/ansi')
-const wrap = require('word-wrap')
+const wrap = require('../util/wrap')
 
 const Label = require('./Label')
 
@@ -29,9 +29,7 @@ module.exports = class WrapLabel extends Label {
       return []
     }
 
-    const options = {width: this.w, indent: ''}
-    return wrap(this.text, options).split('\n')
-      .map(l => l.trim())
+    return wrap(this.text, this.w).map(l => l.trim())
   }
 
   get h() {
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
     }
   }