« 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/examples/basic-app.js
diff options
context:
space:
mode:
Diffstat (limited to 'examples/basic-app.js')
-rw-r--r--examples/basic-app.js51
1 files changed, 51 insertions, 0 deletions
diff --git a/examples/basic-app.js b/examples/basic-app.js
new file mode 100644
index 0000000..bf8aa41
--- /dev/null
+++ b/examples/basic-app.js
@@ -0,0 +1,51 @@
+// Basic app demo:
+// - Structuring a basic element tree
+// - Creating a pane and text input
+// - Using content width/height to layout elements
+// - Subclassing a FocusElement and using its focused method
+// - Sending a quit-app request via Control-C
+//
+// This script cannot actually be used on its own; see the examples on
+// interfacers (interfacer-command-line.js and inerfacer-telnet.js) for a
+// working demo.
+
+const Pane = require('../ui/Pane')
+const FocusElement = require('../ui/form/FocusElement')
+const TextInput = require('../ui/form/TextInput')
+
+module.exports = class AppElement extends FocusElement {
+  constructor() {
+    super()
+
+    this.pane = new Pane()
+    this.addChild(this.pane)
+
+    this.textInput = new TextInput()
+    this.pane.addChild(this.textInput)
+  }
+
+  fixLayout() {
+    this.w = this.parent.contentW
+    this.h = this.parent.contentH
+
+    this.pane.w = this.contentW
+    this.pane.h = this.contentH
+
+    this.textInput.x = 4
+    this.textInput.y = 2
+    this.textInput.w = this.pane.contentW - 8
+  }
+
+  focused() {
+    this.root.select(this.textInput)
+  }
+
+  keyPressed(keyBuf) {
+    if (keyBuf[0] === 0x03) { // 0x03 is Control-C
+      this.emit('quitRequested')
+      return 
+    }
+
+    super.keyPressed(keyBuf)
+  }
+}