« get me outta code hell

Merge remote-tracking branch 'notabug/master' - tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
path: root/ui/presentation
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-05-13 13:20:59 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-05-13 13:20:59 -0300
commit34b7ff22766bae0e4b1b3121bd63d037c27285c7 (patch)
treef2f66a17c67bdf9be0b1576b28ffd0cf107b483c /ui/presentation
parent684e369c7b01b4c69995fc604ef33919077ffdf5 (diff)
parenta37d6be77261b2aae25c4235dcb38dd7b1bb60b1 (diff)
Merge remote-tracking branch 'notabug/master'
Diffstat (limited to 'ui/presentation')
-rw-r--r--ui/presentation/Label.js17
-rw-r--r--ui/presentation/WrapLabel.js8
2 files changed, 18 insertions, 7 deletions
diff --git a/ui/presentation/Label.js b/ui/presentation/Label.js
index 81223df..ed45601 100644
--- a/ui/presentation/Label.js
+++ b/ui/presentation/Label.js
@@ -4,6 +4,16 @@ import * as ansi from 'tui-lib/util/ansi'
 
 export default class Label extends DisplayElement {
   // A simple text display. Automatically adjusts size to fit text.
+  //
+  // Supports formatted text in two ways:
+  // 1) Modify the textAttributes to be an array containing the ANSI numerical
+  //    codes for any wanted attributes, and/or
+  // 2) Supply full ANSI escape codes within the text itself. (The reset
+  //    attributes code, ESC[0m, will be processed to reset to the provided
+  //    values in textAttributes.
+  //
+  // Subclasses overriding the writeTextTo function should be sure to call
+  // processFormatting before actually writing text.
 
   constructor(text = '') {
     super()
@@ -32,7 +42,12 @@ export default class Label extends DisplayElement {
 
   writeTextTo(writable) {
     writable.write(ansi.moveCursor(this.absTop, this.absLeft))
-    writable.write(this.text)
+    writable.write(this.processFormatting(this.text))
+  }
+
+  processFormatting(text) {
+    return text.replace(new RegExp(ansi.ESC + '\\[0m', 'g'),
+      ansi.setAttributes([ansi.A_RESET, ...this.textAttributes]))
   }
 
   set text(newText) {
diff --git a/ui/presentation/WrapLabel.js b/ui/presentation/WrapLabel.js
index 0ecc777..eae8960 100644
--- a/ui/presentation/WrapLabel.js
+++ b/ui/presentation/WrapLabel.js
@@ -1,5 +1,3 @@
-import wrap from 'word-wrap'
-
 import * as ansi from 'tui-lib/util/ansi'
 
 import Label from './Label.js'
@@ -21,7 +19,7 @@ export default class WrapLabel extends Label {
     const lines = this.getWrappedLines()
     for (let i = 0; i < lines.length; i++) {
       writable.write(ansi.moveCursor(this.absTop + i, this.absLeft))
-      writable.write(lines[i])
+      writable.write(this.processFormatting(lines[i]))
     }
   }
 
@@ -30,9 +28,7 @@ export default class WrapLabel extends Label {
       return []
     }
 
-    const options = {width: this.w, indent: ''}
-    return wrap(this.text, options).split('\n')
-      .map(l => l.trim())
+    return ansi.wrapToColumns(this.text, this.w - 1).map(l => l.trim())
   }
 
   get h() {