« get me outta code hell

http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/loop-play.js31
-rw-r--r--src/pickers2.js23
-rwxr-xr-xsrc/play.js19
3 files changed, 60 insertions, 13 deletions
diff --git a/src/loop-play.js b/src/loop-play.js
index 4cccb35..aaaaafe 100644
--- a/src/loop-play.js
+++ b/src/loop-play.js
@@ -305,6 +305,7 @@ class PlayController extends EventEmitter {
     this.nextTrack = null
     this.nextFile = undefined // TODO: Why isn't this null?
     this.stopped = false
+    this.shouldMoveNext = true
   }
 
   async loopPlay() {
@@ -340,8 +341,12 @@ class PlayController extends EventEmitter {
         }
       }
 
-      this.historyController.timelineIndex++
-      this.historyController.fillTimeline()
+      if (!this.shouldMoveNext) {
+        this.shouldMoveNext = true
+      } else {
+        this.historyController.timelineIndex++
+        this.historyController.fillTimeline()
+      }
 
       await this.waitForDownload()
     }
@@ -420,6 +425,27 @@ class PlayController extends EventEmitter {
     // TODO: It would be nice if this returned the next track, but that
     // probably isn't possible with the current play/loop-setup.
 
+    if (this.nextTrack !== this.historyController.getNextTrack(false)) {
+      this.downloadController.cancel()
+      this.startNextDownload(this.historyController.getNextTrack())
+      this.shouldMoveNext = false
+    }
+
+    await this.player.kill()
+    this.currentTrack = null
+  }
+
+  async skipBack() {
+    // Usually the downloader moves forwards in time (so, the NEXT track will
+    // be pre-downloaded). Here, we want to move back, so we need to override
+    // the downloader ourselves.
+
+    if (this.nextTrack !== this.historyController.getBackTrack(false)) {
+      this.downloadController.cancel()
+      this.startNextDownload(this.historyController.getBackTrack())
+    }
+
+    this.shouldMoveNext = false
     await this.player.kill()
     this.currentTrack = null
   }
@@ -429,7 +455,6 @@ class PlayController extends EventEmitter {
       await safeUnlink(this.nextFile, this.playlist)
     }
 
-    // The timeline is always one index ahead.
     const tl = this.historyController.timeline
     tl.splice(this.historyController.timelineIndex + 1, 1)
     this.historyController.fillTimeline()
diff --git a/src/pickers2.js b/src/pickers2.js
index e36d257..af3fc2a 100644
--- a/src/pickers2.js
+++ b/src/pickers2.js
@@ -52,13 +52,28 @@ class HistoryController {
     }
   }
 
-  getNextTrack() {
+  getNextTrack(move = true) {
     // Moves the timeline index forwards and returns the track at the new index
     // (while refilling the timeline, so that the "up next" list is still full,
     // and so the picker is called if there is no track at the current index).
-    this.timelineIndex++
-    this.fillTimeline()
-    return this.currentTrack
+    if (move) {
+      this.timelineIndex++
+      this.fillTimeline()
+      return this.currentTrack
+    } else {
+      return this.timeline[this.timelineIndex + 1]
+    }
+  }
+
+  getBackTrack(move = true) {
+    if (move) {
+      if (this.timelineIndex > 0) {
+        this.timelineIndex--
+      }
+      return this.currentTrack
+    } else {
+      return this.timeline[Math.max(this.timelineIndex - 1, 0)]
+    }
   }
 
   get currentTrack() {
diff --git a/src/play.js b/src/play.js
index c955ce8..db83f13 100755
--- a/src/play.js
+++ b/src/play.js
@@ -397,15 +397,14 @@ async function main(args) {
         player.seekBack(30)
       }
 
-      if (esc(0x41).equals(data)) {
-        player.volUp(10)
-      }
+      if (esc(0x41).equals(data) || equalsChar('p')) { // Previous
+        clearConsoleLine()
+        console.log("Skipping backwards. (Press I for track info!")
 
-      if (esc(0x42).equals(data)) {
-        player.volDown(10)
+        playController.skipBack()
       }
 
-      if (equalsChar('s')) {
+      if (esc(0x42).equals(data) || equalsChar('s')) { // Skip
         clearConsoleLine()
         console.log(
           "Skipping the track that's currently playing. " +
@@ -415,6 +414,14 @@ async function main(args) {
         playController.skip()
       }
 
+      if (shiftEsc(0x41).equals(data)) {
+        player.volUp(10)
+      }
+
+      if (shiftEsc(0x42).equals(data)) {
+        player.volDown(10)
+      }
+
       if (Buffer.from([0x7f]).equals(data)) {
         clearConsoleLine()
         console.log("Skipping the track that's up next.")