« get me outta code hell

Add setValue and moveToEnd methods to TextInput - tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2017-12-09 17:24:19 -0400
committerFlorrie <towerofnix@gmail.com>2017-12-09 17:24:19 -0400
commit21d1a6724eadcf91a616beb06ad763e7e5fafcdc (patch)
tree256b0f83b6dd7b28a17dd2effdf128ee13eb689d
parent0c58ec6048c282c7c0ae22ebfe2860f2cef40df5 (diff)
Add setValue and moveToEnd methods to TextInput
-rw-r--r--ui/form/TextInput.js16
1 files changed, 13 insertions, 3 deletions
diff --git a/ui/form/TextInput.js b/ui/form/TextInput.js
index a10a26f..11e4060 100644
--- a/ui/form/TextInput.js
+++ b/ui/form/TextInput.js
@@ -82,6 +82,16 @@ module.exports = class TextInput extends FocusElement {
     this.keepCursorInRange()
   }
 
+  setValue(value) {
+    this.value = value
+    this.moveToEnd()
+  }
+
+  moveToEnd() {
+    this.cursorIndex = this.value.length
+    this.keepCursorInRange()
+  }
+
   keepCursorInRange() {
     // Keep the cursor inside or at the end of the input value.
 
@@ -95,18 +105,18 @@ module.exports = class TextInput extends FocusElement {
 
     // Scroll right, if the cursor is past the right edge of where text is
     // displayed.
-    if (this.cursorIndex - this.scrollChars > this.w - 3) {
+    while (this.cursorIndex - this.scrollChars > this.w - 3) {
       this.scrollChars++
     }
 
     // Scroll left, if the cursor is behind the left edge of where text is
     // displayed.
-    if (this.cursorIndex - this.scrollChars < 0) {
+    while (this.cursorIndex - this.scrollChars < 0) {
       this.scrollChars--
     }
 
     // Scroll left, if we can see past the end of the text.
-    if (this.scrollChars > 0 && (
+    while (this.scrollChars > 0 && (
       this.scrollChars + this.w - 3 > this.value.length)
     ) {
       this.scrollChars--