« get me outta code hell

Prioritize searching past current index - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-12-28 19:59:40 -0400
committerFlorrie <towerofnix@gmail.com>2018-12-28 19:59:40 -0400
commita2cc42234b93ba08bf19173b07420f7a42d74d89 (patch)
treeb77876dd056f92f2115605209bb792659b1f8f46
parent36328925615b6a01231257f1a736b53ca26139be (diff)
Prioritize searching past current index
Well, not the current index; rather the index which was selected when
the jump element was opened.
-rw-r--r--ui.js35
1 files changed, 30 insertions, 5 deletions
diff --git a/ui.js b/ui.js
index cf39bf5..c20d319 100644
--- a/ui.js
+++ b/ui.js
@@ -1048,14 +1048,39 @@ class GrouplikeListingElement extends Form {
   handleJumpValue(value, isConfirm) {
     // Don't perform the search if the user didn't enter anything.
     if (value.length) {
+      // We prioritize searching past the index that the user opened the jump
+      // element from (oldFocusedIndex). This is so that it's more practical
+      // to do a "repeated" search, wherein the user searches for the same
+      // value over and over, each time jumping to the next match, until they
+      // have found the one they're looking for.
+
       const lower = value.toLowerCase()
       const getName = inp => (inp.item && inp.item.name) ? inp.item.name.toLowerCase().trim() : ''
-      // TODO: Search past the current index, for repeated searches?
-      const startsIndex = this.form.inputs.findIndex(inp => getName(inp).startsWith(lower))
-      const includesIndex = this.form.inputs.findIndex(inp => getName(inp).includes(lower))
-      const matchedIndex = startsIndex >= 0 ? startsIndex : includesIndex
 
-      if (matchedIndex >= 0) {
+      const testStartsWith = inp => getName(inp).startsWith(lower)
+      const testIncludes = inp => getName(inp).includes(lower)
+
+      const searchPastCurrentIndex = test => {
+        const start = this.oldFocusedIndex + 1
+        const match = this.form.inputs.slice(start).findIndex(test)
+        if (match === -1) {
+          return -1
+        } else {
+          return start + match
+        }
+      }
+
+      const start = this.oldFocusedIndex
+      const allIndexes = [
+        searchPastCurrentIndex(testStartsWith),
+        searchPastCurrentIndex(testIncludes),
+        this.form.inputs.findIndex(testStartsWith),
+        this.form.inputs.findIndex(testIncludes)
+      ]
+
+      const matchedIndex = allIndexes.find(value => value >= 0)
+
+      if (typeof matchedIndex !== 'undefined') {
         this.form.curIndex = matchedIndex
         this.form.scrollSelectedElementIntoView()
       } else {