« get me outta code hell

General improvements - 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:
authorliam4 <towerofnix@gmail.com>2017-05-31 19:32:36 -0300
committerliam4 <towerofnix@gmail.com>2017-05-31 19:32:36 -0300
commit4b1a544510f436fac951c8d161c4fbce44c42580 (patch)
treedf7fc1716a892821202b123c52167a7168ecddbb /src
parent26663377fd7ea15a6c3d23a399d1266c8639d42e (diff)
General improvements
Diffstat (limited to 'src')
-rw-r--r--src/loop-play.js4
-rw-r--r--src/play.js35
-rw-r--r--src/playlist-utils.js56
-rw-r--r--src/process-argv.js2
-rw-r--r--src/promisify-process.js5
5 files changed, 61 insertions, 41 deletions
diff --git a/src/loop-play.js b/src/loop-play.js
index e59fbc2..48b5790 100644
--- a/src/loop-play.js
+++ b/src/loop-play.js
@@ -37,8 +37,8 @@ module.exports = async function loopPlay(fn) {
     try {
       await convert('./.temp-track', wavFile)
     } catch(err) {
-      console.warn('Failed to convert ' + title)
-      console.warn('Selecting a new track\n')
+      console.warn("Failed to convert " + title)
+      console.warn("Selecting a new track\n")
 
       return await downloadNext()
     }
diff --git a/src/play.js b/src/play.js
index 9b9a5cf..5e3e04b 100644
--- a/src/play.js
+++ b/src/play.js
@@ -8,7 +8,7 @@ const processArgv = require('./process-argv')
 const pickers = require('./pickers')
 
 const {
-  filterPlaylistByPathString, ignoreGroupByPathString, getPlaylistTreeString
+  filterPlaylistByPathString, removeGroupByPathString, getPlaylistTreeString
 } = require('./playlist-utils')
 
 const readFile = promisify(fs.readFile)
@@ -56,7 +56,7 @@ readFile('./playlist.json', 'utf-8')
         // 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 ignored an entire parent group, e.g. `-i foo -k foo/baz`.
+        // you've removed an entire parent group, e.g. `-r foo -k foo/baz`.
 
         const pathString = util.nextArg()
         const group = filterPlaylistByPathString(sourcePlaylist, pathString)
@@ -65,16 +65,17 @@ readFile('./playlist.json', 'utf-8')
 
       'k': util => util.alias('-keep'),
 
-      '-ignore': function(util) {
-        // --ignore <groupPath>  (alias: -i)
+      '-remove': function(util) {
+        // --remove <groupPath>  (alias: -r, -x)
         // Filters the playlist so that the given path is removed.
 
         const pathString = util.nextArg()
-        console.log('Ignoring path: ' + pathString)
-        ignoreGroupByPathString(curPlaylist, pathString)
+        console.log("Ignoring path: " + pathString)
+        removeGroupByPathString(curPlaylist, pathString)
       },
 
-      'i': util => util.alias('-ignore'),
+      'r': util => util.alias('-remove'),
+      'x': util => util.alias('-remove'),
 
       '-list-groups': function(util) {
         // --list-groups  (alias: -l, --list)
@@ -127,13 +128,6 @@ readFile('./playlist.json', 'utf-8')
 
       'np': util => util.alias('-no-play'),
 
-      '-debug-list': function(util) {
-        // --debug-list
-        // Prints out the JSON representation of the active playlist.
-
-        console.log(JSON.stringify(curPlaylist, null, 2))
-      },
-
       '-picker': function(util) {
         // --picker <shuffle|ordered>
         // Selects the mode that the song to play is picked.
@@ -141,19 +135,26 @@ readFile('./playlist.json', 'utf-8')
         // playlist.
 
         pickerType = util.nextArg()
+      },
+
+      '-debug-list': function(util) {
+        // --debug-list
+        // Prints out the JSON representation of the active playlist.
+
+        console.log(JSON.stringify(curPlaylist, null, 2))
       }
     })
 
     if (willPlay || (willPlay === null && shouldPlay)) {
       let picker
       if (pickerType === 'shuffle') {
-        console.log('Using shuffle picker')
+        console.log("Using shuffle picker.")
         picker = pickers.makeShufflePlaylistPicker(curPlaylist)
       } else if (pickerType === 'ordered') {
-        console.log('Using ordered picker')
+        console.log("Using ordered picker.")
         picker = pickers.makeOrderedPlaylistPicker(curPlaylist)
       } else {
-        console.error('Invalid picker type: ' + pickerType)
+        console.error("Invalid picker type: " + pickerType)
       }
 
       return loopPlay(picker)
diff --git a/src/playlist-utils.js b/src/playlist-utils.js
index 5266f1a..f65e247 100644
--- a/src/playlist-utils.js
+++ b/src/playlist-utils.js
@@ -5,9 +5,10 @@ function flattenPlaylist(playlist) {
   // levels in the playlist tree and returns them as a single-level array of
   // tracks.
 
-  const groups = playlist.filter(x => Array.isArray(x[1]))
-  const nonGroups = playlist.filter(x => x[1] && !(Array.isArray(x[1])))
-  return groups.map(g => flattenPlaylist(g[1]))
+  const groups = playlist.filter(x => isGroup(x))
+  const nonGroups = playlist.filter(x => !isGroup(x))
+
+  return groups.map(g => flattenPlaylist(getGroupContents(g)))
     .reduce((a, b) => a.concat(b), nonGroups)
 }
 
@@ -25,13 +26,15 @@ function filterPlaylistByPath(playlist, pathParts) {
 
   let cur = pathParts[0]
 
-  const match = playlist.find(g => g[0] === cur || g[0] === cur + '/')
+  const match = playlist.find(group => {
+    const title = getGroupTitle(group)
+    return title === cur || title === cur + '/'
+  })
 
   if (match) {
-    const groupContents = match[1]
     if (pathParts.length > 1) {
       const rest = pathParts.slice(1)
-      return filterPlaylistByPath(groupContents, rest)
+      return filterPlaylistByPath(getGroupContents(match), rest)
     } else {
       return match
     }
@@ -58,7 +61,7 @@ function removeGroupByPath(playlist, pathParts) {
   if (parentPath.length === 0) {
     parent = playlist
   } else {
-    parent = filterPlaylistByPath(playlist, pathParts.slice(0, -1))
+    parent = getGroupContents(filterPlaylistByPath(playlist, parentPath))
   }
 
   const index = parent.indexOf(groupToRemove)
@@ -67,31 +70,32 @@ function removeGroupByPath(playlist, pathParts) {
     parent.splice(index, 1)
   } else {
     console.error(
-      'Group ' + pathParts.join('/') + ' doesn\'t exist, so we can\'t ' +
-      'explicitly ignore it.'
+      `Group ${pathParts.join('/')} doesn't exist, so we can't explicitly ` +
+      "ignore it."
     )
   }
 }
 
 function getPlaylistTreeString(playlist, showTracks = false) {
   function recursive(group) {
-    const groups = group.filter(x => Array.isArray(x[1]))
-    const nonGroups = group.filter(x => x[1] && !(Array.isArray(x[1])))
+    const groups = group.filter(x => isGroup(x))
+    const nonGroups = group.filter(x => !isGroup(x))
 
-    const childrenString = groups.map(g => {
-      const groupString = recursive(g[1])
+    const childrenString = groups.map(group => {
+      const title = getGroupTitle(group)
+      const groupString = recursive(getGroupContents(group))
 
       if (groupString) {
         const indented = groupString.split('\n').map(l => '| ' + l).join('\n')
-        return '\n' + g[0] + '\n' + indented
+        return '\n' + title + '\n' + indented
       } else {
-        return g[0]
+        return title
       }
     }).join('\n')
 
-    let trackString = ''
+    let tracksString = ''
     if (showTracks) {
-      trackString = nonGroups.map(g => g[0]).join('\n')
+      tracksString = nonGroups.map(g => getGroupTitle(g)).join('\n')
     }
 
     if (tracksString && childrenString) {
@@ -113,9 +117,23 @@ function parsePathString(pathString) {
   return pathParts
 }
 
+function getGroupTitle(group) {
+  return group[0]
+}
+
+function getGroupContents(group) {
+  return group[1]
+}
+
+function isGroup(array) {
+  return Array.isArray(array[1])
+}
+
 module.exports = {
   flattenPlaylist,
   filterPlaylistByPathString, filterPlaylistByPath,
-  ignoreGroupByPathString, ignoreGroupByPath,
-  parsePathString
+  removeGroupByPathString, removeGroupByPath,
+  getPlaylistTreeString,
+  parsePathString,
+  getGroupTitle, getGroupContents
 }
diff --git a/src/process-argv.js b/src/process-argv.js
index d5f86f9..201f927 100644
--- a/src/process-argv.js
+++ b/src/process-argv.js
@@ -34,7 +34,7 @@ module.exports = async function processArgv(argv, handlers) {
         }
       })
     } else {
-      console.warn('Option not understood: ' + opt)
+      console.warn("Option not understood: " + opt)
     }
   }
 
diff --git a/src/promisify-process.js b/src/promisify-process.js
index ca49b31..ef8d0c6 100644
--- a/src/promisify-process.js
+++ b/src/promisify-process.js
@@ -2,7 +2,8 @@
 
 module.exports = function promisifyProcess(proc, showLogging = true) {
   // Takes a process (from child_process) and returns a promise that resolves
-  // when the process exits.
+  // when the process exits (or rejects with a warning, if the exit code is
+  // non-zero).
 
   return new Promise((resolve, reject) => {
     if (showLogging) {
@@ -14,7 +15,7 @@ module.exports = function promisifyProcess(proc, showLogging = true) {
       if (code === 0) {
         resolve()
       } else {
-        console.error('Process failed!', proc.spawnargs)
+        console.error("Process failed!", proc.spawnargs)
         reject(code)
       }
     })