« 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/ui/presentation/Label.js
diff options
context:
space:
mode:
Diffstat (limited to 'ui/presentation/Label.js')
-rw-r--r--ui/presentation/Label.js17
1 files changed, 16 insertions, 1 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) {