« 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
diff options
context:
space:
mode:
Diffstat (limited to 'ui')
-rw-r--r--ui/Root.js2
-rw-r--r--ui/form/TextInput.js23
2 files changed, 17 insertions, 8 deletions
diff --git a/ui/Root.js b/ui/Root.js
index a6b3acf..7e031d3 100644
--- a/ui/Root.js
+++ b/ui/Root.js
@@ -27,7 +27,7 @@ module.exports = class Root extends DisplayElement {
   handleData(buffer) {
     if (this.selectedElement) {
       const els = [
-        ...this.selectedElement.directAncestors, this.selectedElement]
+        ...this.selectedElement.directAncestors, this.selectedElement].reverse()
       for (const el of els) {
         if (el instanceof FocusElement) {
           const shouldBreak = (el.keyPressed(buffer) === false)
diff --git a/ui/form/TextInput.js b/ui/form/TextInput.js
index 11e4060..f07b562 100644
--- a/ui/form/TextInput.js
+++ b/ui/form/TextInput.js
@@ -70,13 +70,22 @@ module.exports = class TextInput extends FocusElement {
       // ESC is bad and we don't want that in the text input!
       return
     } else {
-      // console.log(keyBuf, keyBuf[0], keyBuf[1], keyBuf[2])
-      this.value = (
-        this.value.slice(0, this.cursorIndex) + keyBuf.toString() +
-        this.value.slice(this.cursorIndex)
-      )
-      this.cursorIndex++
-      this.root.cursorMoved()
+      const isTextInput = keyBuf.toString().split('').every(chr => {
+        const n = chr.charCodeAt(0)
+        return n > 31 && n < 127
+      })
+
+      if (isTextInput) {
+        this.value = (
+          this.value.slice(0, this.cursorIndex) + keyBuf.toString() +
+          this.value.slice(this.cursorIndex)
+        )
+        this.cursorIndex += keyBuf.toString().length
+        this.root.cursorMoved()
+        this.keepCursorInRange()
+
+        return false
+      }
     }
 
     this.keepCursorInRange()