« get me outta code hell

past 3 second threshold, (P) to seek to start - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/ui.js
diff options
context:
space:
mode:
author(quasar) nebula <towerofnix@gmail.com>2021-07-17 20:09:09 -0300
committer(quasar) nebula <towerofnix@gmail.com>2021-07-17 20:09:09 -0300
commit7af31d6ccb2d1b0c47c0bbb60a7e51c64bb01bf1 (patch)
tree57f10337ee6a22155862e65086feaf9f51b69fe9 /ui.js
parent382d5afc7e2ac24f67b7c891328b8b9bb7e91058 (diff)
past 3 second threshold, (P) to seek to start
Diffstat (limited to 'ui.js')
-rw-r--r--ui.js33
1 files changed, 29 insertions, 4 deletions
diff --git a/ui.js b/ui.js
index 75dfe41..3601071 100644
--- a/ui.js
+++ b/ui.js
@@ -199,6 +199,7 @@ class AppElement extends FocusElement {
       canProcessMetadata: true,
       canSuspend: true,
       menubarColor: 4, // blue
+      seekToStartThreshold: 3,
       showTabberPane: true,
       stopPlayingUponQuit: true
     }, config)
@@ -1190,15 +1191,39 @@ class AppElement extends FocusElement {
     }
   }
 
+  skipBackOrSeekToStart() {
+    // Perform the same action - skipping to the previous track or seeking to
+    // the start of the current track - for all target queue players. If any is
+    // past an arbitrary time position (default 3 seconds), seek to start; if
+    // all are before this position, skip to previous.
+
+    let maxCurSec = 0
+    this.forEachQueuePlayerToActOn(({ timeData }) => {
+      if (timeData) {
+        maxCurSec = Math.max(maxCurSec, timeData.curSecTotal)
+      }
+    })
+
+    if (Math.floor(maxCurSec) < this.config.seekToStartThreshold) {
+      this.actOnQueuePlayers(qp => qp.playPrevious(qp.playingTrack, true))
+    } else {
+      this.actOnQueuePlayers(qp => qp.seekToStart())
+    }
+  }
+
   actOnQueuePlayers(fn) {
-    const actOn = this.queuePlayersToActOn.length ? this.queuePlayersToActOn : [this.SQP]
-    for (const queuePlayer of actOn) {
+    this.forEachQueuePlayerToActOn(queuePlayer => {
       fn(queuePlayer)
       const PIE = this.getPlaybackInfoElementForQueuePlayer(queuePlayer)
       if (PIE) {
         PIE.updateProgress()
       }
-    }
+    })
+  }
+
+  forEachQueuePlayerToActOn(fn) {
+    const actOn = this.queuePlayersToActOn.length ? this.queuePlayersToActOn : [this.SQP]
+    actOn.forEach(fn)
   }
 
   showMenuForItemElement(el, listing) {
@@ -1564,7 +1589,7 @@ class AppElement extends FocusElement {
       } else if (input.isStop(keyBuf)) {
         this.actOnQueuePlayers(qp => qp.stopPlaying())
       } else if (input.isSkipBack(keyBuf)) {
-        this.actOnQueuePlayers(qp => qp.playPrevious(qp.playingTrack, true))
+        this.skipBackOrSeekToStart()
       } else if (input.isSkipAhead(keyBuf)) {
         this.actOnQueuePlayers(qp => qp.playNext(qp.playingTrack, true))
       }