« 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
diff options
context:
space:
mode:
-rw-r--r--ui/form/ListScrollForm.js29
1 files changed, 22 insertions, 7 deletions
diff --git a/ui/form/ListScrollForm.js b/ui/form/ListScrollForm.js
index 77bebcd..d22cd35 100644
--- a/ui/form/ListScrollForm.js
+++ b/ui/form/ListScrollForm.js
@@ -158,14 +158,21 @@ module.exports = class ListScrollForm extends Form {
   getScrollPositionOfElementAtEndOfView(element) {
     // We can decide how many items to scroll past by moving forward until
     // the item's far edge is visible.
+    const pos = this.getItemPos(element);
     let edge = this.formEdge
-    let i = 0
-    for (; i < this.inputs.length; i++) {
-      if (this.getItemPos(element) <= edge) break
+    for (let i = 0; i < this.inputs.length; i++) {
+      if (pos <= edge) {
+        return i;
+      }
 
-      edge += this.inputs[i][this.sizeProp]
+      if (this.sizeProp === 'w') {
+        edge += this.inputs[i].w;
+      } else {
+        edge += this.inputs[i].h;
+      }
     }
-    return i
+    // No result? Well, it's at the end.
+    return this.inputs.length;
   }
 
   scrollElementIntoEndOfView(element) {
@@ -199,8 +206,16 @@ module.exports = class ListScrollForm extends Form {
   getItemPos(item) {
     // Gets the position of the item in an unscrolled view.
 
-    return this.inputs.slice(0, this.inputs.indexOf(item) + 1)
-      .reduce((a, b) => a + b[this.sizeProp], 0)
+    const index = this.inputs.indexOf(item);
+    let pos = 0;
+    for (let i = 0; i <= index; i++) {
+      if (this.sizeProp === 'w') {
+        pos += this.inputs[i].w;
+      } else {
+        pos += this.inputs[i].h;
+      }
+    }
+    return pos;
   }
 
   getSizeProp() {