« get me outta code hell

REALLY BAD DRAW CODE to support more animes - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2019-01-31 14:01:40 -0400
committerFlorrie <towerofnix@gmail.com>2019-01-31 14:01:40 -0400
commit82bbf5ca046323640c5de332bc33f2b5ae5f8efc (patch)
treed070d7e5ee48f6d999fc78e9315cf5b65eae319a
parentbbd1c3ce5f7a163b9c5fc21069daa120f773902e (diff)
REALLY BAD DRAW CODE to support more animes
-rw-r--r--ui.js55
1 files changed, 47 insertions, 8 deletions
diff --git a/ui.js b/ui.js
index 441db03..c86c8ad 100644
--- a/ui.js
+++ b/ui.js
@@ -1226,11 +1226,57 @@ class BasicGrouplikeItemElement extends Button {
     super()
 
     this.text = text
+    this.drawText = ''
   }
 
   fixLayout() {
     this.w = this.parent.contentW
     this.h = 1
+
+    this.computeText()
+  }
+
+  computeText() {
+    let text = ''
+    let done = false
+    let heckingWatchOut = false
+
+    const writable = {
+      write: characters => {
+        if (heckingWatchOut && done) {
+          return
+        }
+
+        for (const char of characters) {
+          if (heckingWatchOut) {
+            if (ansi.measureColumns(text + char) <= this.w - this.x) {
+              text += char
+            } else {
+              done = true
+              break
+            }
+          } else {
+            text += char
+          }
+        }
+      }
+    }
+
+    this.drawX = this.x
+    this.writeStatus(writable)
+
+    // This is the part where we want to be careful to not go over the width
+    // limit..
+    heckingWatchOut = true
+    writable.write(this.text)
+    this.drawX += ansi.measureColumns(this.text)
+    heckingWatchOut = false
+
+    writable.write(' '.repeat(Math.max(0, this.w - this.drawX)))
+
+    writable.write(ansi.resetAttributes())
+
+    this.drawText = text
   }
 
   drawTo(writable) {
@@ -1248,14 +1294,7 @@ class BasicGrouplikeItemElement extends Button {
     }
 
     writable.write(ansi.moveCursor(this.absTop, this.absLeft))
-
-    this.drawX = this.x
-    this.writeStatus(writable)
-    writable.write(this.text.slice(0, this.w - this.drawX))
-    this.drawX += ansi.measureColumns(this.text)
-    writable.write(' '.repeat(Math.max(0, this.w - this.drawX)))
-
-    writable.write(ansi.resetAttributes())
+    writable.write(this.drawText)
   }
 
   writeStatus(writable) {