« get me outta code hell

Make --track-display-file show SOURCE path to track - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-02-16 11:02:51 -0400
committerFlorrie <towerofnix@gmail.com>2018-02-16 11:02:53 -0400
commit1b5d6f8f96baae53367a7a7d0f9485a42029eaa3 (patch)
treec5eeedd7e0bac198c4070c3cb688a5dfadc8fa77
parente249bda854212d9ba29015b0c895b72aa2ee3cad (diff)
Make --track-display-file show SOURCE path to track
This also means we're keeping track of the source item of items, which
is sure to be useful more later.
-rw-r--r--src/loop-play.js6
-rwxr-xr-xsrc/play.js4
-rw-r--r--src/playlist-utils.js15
3 files changed, 17 insertions, 8 deletions
diff --git a/src/loop-play.js b/src/loop-play.js
index e82e77e..83cbcfe 100644
--- a/src/loop-play.js
+++ b/src/loop-play.js
@@ -23,7 +23,7 @@ const {
 } = require('./downloaders')
 
 const {
-  getItemPathString, safeUnlink, parentSymbol
+  getItemPathString, safeUnlink, parentSymbol, sourceSymbol
 } = require('./playlist-utils')
 
 function createStatusLine({percentStr, curStr, lenStr}) {
@@ -450,7 +450,9 @@ class PlayController extends EventEmitter {
 
       if (next) {
         if (this.trackDisplayFile) {
-          await writeFile(this.trackDisplayFile, getItemPathString(this.currentTrack))
+          await writeFile(this.trackDisplayFile,
+            getItemPathString(this.currentTrack[sourceSymbol])
+          )
         }
 
         await this.playFile(next)
diff --git a/src/play.js b/src/play.js
index a3481ea..d5df591 100755
--- a/src/play.js
+++ b/src/play.js
@@ -158,7 +158,9 @@ async function main(args) {
 
     // ..And finally, we have to update the playlist format again, since
     // processSmartPlaylist might have added new (un-updated) items:
-    const finalPlaylist = updatePlaylistFormat(processedPlaylist)
+    const finalPlaylist = updatePlaylistFormat(processedPlaylist, true)
+    // We also pass true so that the playlist-format-updater knows that this
+    // is the source playlist.
 
     sourcePlaylist = finalPlaylist
 
diff --git a/src/playlist-utils.js b/src/playlist-utils.js
index 5343fb4..dad828c 100644
--- a/src/playlist-utils.js
+++ b/src/playlist-utils.js
@@ -8,8 +8,9 @@ const unlink = promisify(fs.unlink)
 
 const parentSymbol = Symbol('Parent group')
 const oldSymbol = Symbol('Old track or group reference')
+const sourceSymbol = Symbol('Source-playlist item reference')
 
-function updatePlaylistFormat(playlist) {
+function updatePlaylistFormat(playlist, firstTime = false) {
   const defaultPlaylist = {
     options: [],
     items: []
@@ -39,10 +40,10 @@ function updatePlaylistFormat(playlist) {
 
   const fullPlaylistObj = Object.assign(defaultPlaylist, playlistObj)
 
-  return updateGroupFormat(fullPlaylistObj)
+  return updateGroupFormat(fullPlaylistObj, firstTime)
 }
 
-function updateGroupFormat(group) {
+function updateGroupFormat(group, firstTime = false) {
   const defaultGroup = {
     name: '',
     items: [],
@@ -62,7 +63,7 @@ function updateGroupFormat(group) {
   groupObj.items = groupObj.items.map(item => {
     // Check if it's a group; if not, it's probably a track.
     if (typeof item[1] === 'array' || item.items) {
-      item = updateGroupFormat(item)
+      item = updateGroupFormat(item, firstTime)
     } else {
       item = updateTrackFormat(item)
 
@@ -79,6 +80,10 @@ function updateGroupFormat(group) {
 
     item[parentSymbol] = groupObj
 
+    if (firstTime) {
+      item[sourceSymbol] = item
+    }
+
     return item
   })
 
@@ -517,7 +522,7 @@ async function safeUnlink(file, playlist) {
 }
 
 module.exports = {
-  parentSymbol, oldSymbol,
+  parentSymbol, oldSymbol, sourceSymbol,
   updatePlaylistFormat, updateTrackFormat,
   flattenGrouplike,
   partiallyFlattenGrouplike, collapseGrouplike,