« get me outta code hell

http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rwxr-xr-xsrc/play.js58
-rw-r--r--todo.txt8
2 files changed, 44 insertions, 22 deletions
diff --git a/src/play.js b/src/play.js
index 462cd88..b049a0d 100755
--- a/src/play.js
+++ b/src/play.js
@@ -131,6 +131,8 @@ async function main(args) {
 
     const importedPlaylist = JSON.parse(playlistText)
 
+    hasOpenedPlaylist = true
+
     await loadPlaylist(importedPlaylist)
   }
 
@@ -185,14 +187,24 @@ async function main(args) {
     keybindings.unshift(...openedKeybindings)
   }
 
-  function requiresOpenPlaylist() {
+  let hasOpenedPlaylist = false
+
+  async function requiresOpenPlaylist() {
     if (activePlaylist === null) {
-      throw new Error(
-        "This action requires an open playlist - try --open (file)"
-      )
+      if (hasOpenedPlaylist === false) {
+        await openDefaultPlaylist()
+      } else {
+        throw new Error(
+          "This action requires an open playlist - try --open (file)"
+        )
+      }
     }
   }
 
+  function openDefaultPlaylist() {
+    return openPlaylist('./playlist.json', true)
+  }
+
   const optionFunctions = {
     '-help': function(util) {
       // --help  (alias: -h, -?)
@@ -229,13 +241,13 @@ async function main(args) {
 
     '-playlist-string': util => util.alias('-open-playlist-string'),
 
-    '-write-playlist': function(util) {
+    '-write-playlist': async function(util) {
       // --write-playlist <file>  (alias: --write, -w, --save)
       // Writes the active playlist to a file. This file can later be used
       // with --open <file>; you won't need to stick in all the filtering
       // options again.
 
-      requiresOpenPlaylist()
+      await requiresOpenPlaylist()
 
       const playlistString = JSON.stringify(activePlaylist, null, 2)
       const file = util.nextArg()
@@ -259,11 +271,11 @@ async function main(args) {
     'w': util => util.alias('-write-playlist'),
     '-save': util => util.alias('-write-playlist'),
 
-    '-print-playlist': function(util) {
+    '-print-playlist': async function(util) {
       // --print-playlist  (alias: --log-playlist, --json)
       // Prints out the JSON representation of the active playlist.
 
-      requiresOpenPlaylist()
+      await requiresOpenPlaylist()
 
       console.log(JSON.stringify(activePlaylist, null, 2))
 
@@ -290,26 +302,26 @@ async function main(args) {
       await openKeybindings(util.nextArg(), false)
     },
 
-    '-clear': function(util) {
+    '-clear': async function(util) {
       // --clear  (alias: -c)
       // Clears the active playlist. This does not affect the source
       // playlist.
 
-      requiresOpenPlaylist()
+      await requiresOpenPlaylist()
 
       activePlaylist.items = []
     },
 
     'c': util => util.alias('-clear'),
 
-    '-keep': function(util) {
+    '-keep': async function(util) {
       // --keep <groupPath>  (alias: -k)
       // Keeps a group by loading it from the source playlist into the
       // active playlist. This is usually useful after clearing the
       // active playlist; it can also be used to keep a subgroup when
       // you've removed an entire parent group, e.g. `-r foo -k foo/baz`.
 
-      requiresOpenPlaylist()
+      await requiresOpenPlaylist()
 
       const pathString = util.nextArg()
       const group = filterPlaylistByPathString(sourcePlaylist, pathString)
@@ -321,11 +333,11 @@ async function main(args) {
 
     'k': util => util.alias('-keep'),
 
-    '-remove': function(util) {
+    '-remove': async function(util) {
       // --remove <groupPath>  (alias: -r, -x)
       // Filters the playlist so that the given path is removed.
 
-      requiresOpenPlaylist()
+      await requiresOpenPlaylist()
 
       const pathString = util.nextArg()
       console.log("Ignoring path: " + pathString)
@@ -358,24 +370,24 @@ async function main(args) {
 
     'f': util => util.alias('-filter'),
 
-    '-collapse-groups': function() {
+    '-collapse-groups': async function() {
       // --collapse-groups  (alias: --collapse)
       // Collapses groups in the active playlist so that there is only one
       // level of sub-groups. Handy for shuffling the order groups play in;
       // try `--collapse-groups --sort shuffle-groups`.
 
-      requiresOpenPlaylist()
+      await requiresOpenPlaylist()
 
       activePlaylist = updatePlaylistFormat(collapseGrouplike(activePlaylist))
     },
 
     '-collapse': util => util.alias('-collapse-groups'),
 
-    '-list-groups': function(util) {
+    '-list-groups': async function(util) {
       // --list-groups  (alias: -l, --list)
       // Lists all groups in the playlist.
 
-      requiresOpenPlaylist()
+      await requiresOpenPlaylist()
 
       console.log(getPlaylistTreeString(activePlaylist))
 
@@ -390,11 +402,11 @@ async function main(args) {
     '-list': util => util.alias('-list-groups'),
     'l': util => util.alias('-list-groups'),
 
-    '-list-all': function(util) {
+    '-list-all': async function(util) {
       // --list-all  (alias: --list-tracks, -L)
       // Lists all groups and tracks in the playlist.
 
-      requiresOpenPlaylist()
+      await requiresOpenPlaylist()
 
       console.log(getPlaylistTreeString(activePlaylist, true))
 
@@ -615,10 +627,12 @@ async function main(args) {
     '-trust': util => util.alias('-trust-shell-commands')
   }
 
-  await openPlaylist('./playlist.json', true)
-
   await processArgv(args, optionFunctions)
 
+  if (!hasOpenedPlaylist) {
+    await openDefaultPlaylist()
+  }
+
   if (activePlaylist === null) {
     console.error(
       "Cannot play - no open playlist. Try --open <playlist file>?"
diff --git a/todo.txt b/todo.txt
index 06f1c51..83ed985 100644
--- a/todo.txt
+++ b/todo.txt
@@ -424,3 +424,11 @@ TODO: Make process-metadata work nicely with smart playlists, somehow...
 
 TODO: A way (key, option) to change the "/ duration" text in the status bar to
       "- remaining". This would work very nicely with the >/< status bar idea.
+
+TODO: Be a bit more loose (strict?) about what means crashing... Right now if
+      five tracks fail to play in a row, http-music stops. This is good for
+      dealing with, for example, a messed up playlist file that now references
+      moved MP3s, since "failing" means "the download failed". But if the PLAY
+      command fails (i.e. mpv or sox exits with code 1), THAT should also be
+      counted as a failure. (An example case of the "play" command failing --
+      trying to play a track when there is no audio device.)