« get me outta code hell

mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--ui.js49
1 files changed, 46 insertions, 3 deletions
diff --git a/ui.js b/ui.js
index 1bb74fc..e837256 100644
--- a/ui.js
+++ b/ui.js
@@ -390,13 +390,40 @@ class AppElement extends FocusElement {
     }
   }
 
+  playSooner(item) {
+    this.distributeQueueGrouplikeItem(item, {
+      how: 'randomly',
+      rangeEnd: this.queueGrouplike.items.indexOf(item)
+    })
+
+    // It may not have queued as soon as the user wants; in that acse, they'll
+    // want to queue it sooner again. Automatically reselect the track so that
+    // this they don't have to navigate back to it by hand.
+    this.queueListingElement.selectAndShow(item)
+  }
+
   playLater(item) {
     this.handleQueueOptions(item, {
       where: 'distribute-randomly',
       skip: true
     })
+
+    // Just for consistency with playSooner (you can press ^-L to quickly get
+    // back to the current track).
+    this.queueListingElement.selectAndShow(item)
   }
 
+  playLater(item) {
+    this.handleQueueOptions(item, {
+      where: 'distribute-randomly',
+      skip: true
+    })
+
+    // Just for consistency.
+    this.queueListingElement.selectAndShow(item)
+  }
+
+
   showMenuForItemElement(el, listing) {
     const emitControls = play => () => {
       this.handleQueueOptions(item, {
@@ -416,6 +443,7 @@ class AppElement extends FocusElement {
         {label: 'Reveal', action: () => this.reveal(item)},
         {divider: true},
         {label: 'Play later', action: () => this.playLater(item)},
+        {label: 'Play sooner', action: () => this.playSooner(item)},
         {label: 'Remove from queue', action: () => this.unqueueGrouplikeItem(item)}
       ]
     } else {
@@ -766,7 +794,9 @@ class AppElement extends FocusElement {
         movePlayingTrack: order === 'normal'
       })
     } else if (where.startsWith('distribute-')) {
-      this.distributeQueueGrouplikeItem(item, where.slice('distribute-'.length))
+      this.distributeQueueGrouplikeItem(item, {
+        how: where.slice('distribute-'.length)
+      })
     }
 
     if (play) {
@@ -834,7 +864,11 @@ class AppElement extends FocusElement {
     return newTrack
   }
 
-  distributeQueueGrouplikeItem(grouplike, how = 'evenly') {
+  distributeQueueGrouplikeItem(grouplike, {how = 'evenly', rangeEnd = 'end-of-queue'}) {
+    if (isTrack(grouplike)) {
+      grouplike = {items: [grouplike]}
+    }
+
     const queue = this.queueGrouplike
     const newItems = flattenGrouplike(grouplike).items
 
@@ -854,7 +888,16 @@ class AppElement extends FocusElement {
     }
 
     const distributeStart = queue.items.indexOf(this.playingTrack) + 1
-    const distributeEnd = queue.items.length
+
+    let distributeEnd
+    if (rangeEnd === 'end-of-queue') {
+      distributeEnd = queue.items.length
+    } else if (typeof rangeEnd === 'number') {
+      distributeEnd = Math.min(queue.items.length, rangeEnd)
+    } else {
+      throw new Error('Invalid rangeEnd: ' + rangeEnd)
+    }
+
     const distributeSize = distributeEnd - distributeStart
 
     const queueItem = (item, insertIndex) => {