[gjs/ewlsh/nova-repl] WIP - syncing cursor with history



commit 817d14004d64cc2483a3e6116733995f010ffca4
Author: Evan Welsh <contact evanwelsh com>
Date:   Tue Aug 31 23:09:02 2021 -0700

    WIP - syncing cursor with history

 modules/esm/repl.js | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)
---
diff --git a/modules/esm/repl.js b/modules/esm/repl.js
index 98977825..f8b72bfd 100644
--- a/modules/esm/repl.js
+++ b/modules/esm/repl.js
@@ -39,7 +39,7 @@ class ReplInput {
      * @returns {readonly string[]}
      */
     getValue() {
-        if (this.historyIndex > -1) {
+        if (this.historyIndex >= 0) {
             return this.history[this.historyIndex].join('');
         }
         return this.inputChars.join('');
@@ -65,13 +65,17 @@ class ReplInput {
     }
 
     historyUp() {
-        if (this.historyIndex < this.history.length - 1)
+        if (this.historyIndex < this.history.length - 1) {
             this.historyIndex++;
+            this.inputIndex = -1;
+        }
     }
 
     historyDown() {
-        if (this.historyIndex >= 0)
+        if (this.historyIndex >= 0) {
             this.historyIndex--;
+            this.inputIndex = -1;
+        }
     }
 
     moveCursorToBeginning() {
@@ -93,7 +97,8 @@ class ReplInput {
     }
 
     addChar(char) {
-        this.inputChars.splice(this.inputIndex, 0, char);
+        const editableValue = this.getEditableValue();
+        editableValue.splice(this.inputIndex, 0, char);
 
         this.inputIndex++;
     }
@@ -160,13 +165,23 @@ class ReplInput {
         this.addChar(key.sequence);
     }
 
+    updateInputIndex(value) {
+        if (this.inputIndex === -1) {
+            this.inputIndex = value.length;
+        }
+
+        // Ensure the input index isn't longer than the content...
+        this.inputIndex = Math.min(this.inputIndex, value.length);
+    }
+
     render() {
+        const value = this.getValue();
+
         this.writeSync(Ansi.cursorHide);
         this.clear();
-        const value = this.getValue();
+       
         this.writeSync(this.$prompt + value);
-        // Ensure the input index isn't longer than the content...
-        this.inputIndex = Math.min(this.inputIndex, value.length);
+        this.updateInputIndex(value);
         this.writeSync(Ansi.cursorTo(this.$promptLength + this.inputIndex + 1));
         this.writeSync(Ansi.cursorShow);
         this.flush();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]