« 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/Label.js
diff options
context:
space:
mode:
Diffstat (limited to 'ui/Label.js')
-rw-r--r--ui/Label.js39
1 files changed, 39 insertions, 0 deletions
diff --git a/ui/Label.js b/ui/Label.js
new file mode 100644
index 0000000..60ece15
--- /dev/null
+++ b/ui/Label.js
@@ -0,0 +1,39 @@
+const ansi = require('../ansi')
+
+const DisplayElement = require('./DisplayElement')
+
+module.exports = class Label extends DisplayElement {
+  // A simple text display. Automatically adjusts size to fit text.
+
+  constructor(text='') {
+    super()
+
+    this.text = text
+    this.textAttributes = []
+  }
+
+  drawTo(writable) {
+    if (this.textAttributes.length) {
+      writable.write(ansi.setAttributes(this.textAttributes))
+    }
+
+    writable.write(ansi.moveCursor(this.absTop, this.absLeft))
+    writable.write(this.text)
+
+    if (this.textAttributes.length) {
+      writable.write(ansi.resetAttributes())
+    }
+
+    super.drawTo(writable)
+  }
+
+  set text(newText) {
+    this._text = newText
+
+    this.w = newText.length
+  }
+
+  get text() {
+    return this._text
+  }
+}