« 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/form
diff options
context:
space:
mode:
Diffstat (limited to 'ui/form')
-rw-r--r--ui/form/TextInput.js76
1 files changed, 39 insertions, 37 deletions
diff --git a/ui/form/TextInput.js b/ui/form/TextInput.js
index 766f959..5b5fa06 100644
--- a/ui/form/TextInput.js
+++ b/ui/form/TextInput.js
@@ -48,50 +48,52 @@ module.exports = class TextInput extends FocusElement {
   }
 
   keyPressed(keyBuf) {
-    if (keyBuf[0] === 127) {
-      this.value = (
-        this.value.slice(0, this.cursorIndex - 1) +
-        this.value.slice(this.cursorIndex)
-      )
-      this.cursorIndex--
-      this.root.cursorMoved()
-    } else if (keyBuf[0] === 13) {
-      this.emit('value', this.value)
-    } else if (keyBuf[0] === 0x1b && keyBuf[1] === 0x5b) {
-      // Keyboard navigation
-      if (keyBuf[2] === 0x44) {
-        this.cursorIndex--
-        this.root.cursorMoved()
-      } else if (keyBuf[2] === 0x43) {
-        this.cursorIndex++
-        this.root.cursorMoved()
-      }
-    } else if (telc.isEscape(keyBuf)) {
-      // ESC is bad and we don't want that in the text input!
-      // Also emit a "cancel" event, which doesn't necessarily do anything,
-      // but can be listened to.
-      this.emit('cancel')
-      return
-    } else {
-      const isTextInput = keyBuf.toString().split('').every(chr => {
-        const n = chr.charCodeAt(0)
-        return n > 31 && n < 127
-      })
-
-      if (isTextInput) {
+    try {
+      if (keyBuf[0] === 127) {
         this.value = (
-          this.value.slice(0, this.cursorIndex) + keyBuf.toString() +
+          this.value.slice(0, this.cursorIndex - 1) +
           this.value.slice(this.cursorIndex)
         )
-        this.cursorIndex += keyBuf.toString().length
+        this.cursorIndex--
         this.root.cursorMoved()
-        this.keepCursorInRange()
-
         return false
+      } else if (keyBuf[0] === 13) {
+        this.emit('value', this.value)
+      } else if (keyBuf[0] === 0x1b && keyBuf[1] === 0x5b) {
+        // Keyboard navigation
+        if (keyBuf[2] === 0x44) {
+          this.cursorIndex--
+          this.root.cursorMoved()
+        } else if (keyBuf[2] === 0x43) {
+          this.cursorIndex++
+          this.root.cursorMoved()
+        }
+        return false
+      } else if (telc.isEscape(keyBuf)) {
+        // ESC is bad and we don't want that in the text input!
+        // Also emit a "cancel" event, which doesn't necessarily do anything,
+        // but can be listened to.
+        this.emit('cancel')
+      } else {
+        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()
+
+          return false
+        }
       }
+    } finally {
+      this.keepCursorInRange()
     }
-
-    this.keepCursorInRange()
   }
 
   setValue(value) {