« get me outta code hell

Clean-up and such - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorliam4 <towerofnix@gmail.com>2017-06-26 23:24:02 -0300
committerliam4 <towerofnix@gmail.com>2017-06-26 23:24:02 -0300
commit811997668969a5bf782f9edc6fba92db7aa35b6d (patch)
treec7383084c60ff5197a4e2e3899ea92f6e8998baf
parent0fcec41cd757adcd3c7f2f4c80b33141bb0a9bee (diff)
Clean-up and such
-rwxr-xr-xsrc/http-music.js23
-rw-r--r--src/loop-play.js46
-rw-r--r--todo.txt4
3 files changed, 30 insertions, 43 deletions
diff --git a/src/http-music.js b/src/http-music.js
index 863d170..e926e5d 100755
--- a/src/http-music.js
+++ b/src/http-music.js
@@ -2,14 +2,12 @@
 
 'use strict'
 
-const fs = require('fs')
-
 const { promisify } = require('util')
+const fs = require('fs')
+const pickers = require('./pickers')
 const loopPlay = require('./loop-play')
 const processArgv = require('./process-argv')
 
-const pickers = require('./pickers')
-
 const {
   filterPlaylistByPathString, removeGroupByPathString, getPlaylistTreeString
 } = require('./playlist-utils')
@@ -259,7 +257,10 @@ Promise.resolve()
         return
       }
 
-      const play = loopPlay(picker, playOpts)
+      const {
+        promise: playPromise,
+        controller: play
+      } = loopPlay(picker, playOpts)
 
       // We're looking to gather standard input one keystroke at a time.
       process.stdin.setRawMode(true)
@@ -303,11 +304,11 @@ Promise.resolve()
         }
 
         if (Buffer.from('s').equals(data)) {
-          // clearConsoleLine()
-          // console.log(
-          //   "Skipping the track that's currently playing. " +
-          //   "(Press I for track info!)"
-          // )
+          clearConsoleLine()
+          console.log(
+            "Skipping the track that's currently playing. " +
+            "(Press I for track info!)"
+          )
 
           play.skipCurrent()
         }
@@ -331,7 +332,7 @@ Promise.resolve()
         }
       })
 
-      return play.promise
+      return playPromise
     } else {
       return activePlaylist
     }
diff --git a/src/loop-play.js b/src/loop-play.js
index 3c00ef5..10b1d5f 100644
--- a/src/loop-play.js
+++ b/src/loop-play.js
@@ -1,13 +1,7 @@
 'use strict'
 
 const { spawn } = require('child_process')
-const promisifyProcess = require('./promisify-process')
-const sanitize = require('sanitize-filename')
-const tempy = require('tempy')
-const path = require('path')
-
 const FIFO = require('fifo-js')
-
 const EventEmitter = require('events')
 
 class PlayController {
@@ -84,7 +78,7 @@ class PlayController {
   }
 
   skipCurrent() {
-    this.killProcess()
+    this.kill()
   }
 
   seekAhead(secs) {
@@ -113,7 +107,7 @@ class PlayController {
     }
   }
 
-  killProcess() {
+  kill() {
     if (this.process) {
       this.process.kill()
     }
@@ -125,13 +119,20 @@ class PlayController {
 
     this.currentTrack = null
   }
+
+  logTrackInfo() {
+    if (this.currentTrack) {
+      const [ curTitle, curArg ] = this.currentTrack
+      console.log(`Playing: \x1b[1m${curTitle} \x1b[2m${curArg}\x1b[0m`)
+    } else {
+      console.log("No song currently playing.")
+    }
+  }
 }
 
 module.exports = function loopPlay(picker, playArgs = []) {
-  // Looping play function. Takes one argument, the "pick" function,
-  // which returns a track to play. Preemptively downloads the next
-  // track while the current one is playing for seamless continuation
-  // from one song to the next. Stops when the result of the pick
+  // Looping play function. Takes one argument, the "picker" function,
+  // which returns a track to play. Stops when the result of the picker
   // function is null (or similar). Optionally takes a second argument
   // used as arguments to the `play` process (before the file name).
 
@@ -142,25 +143,6 @@ module.exports = function loopPlay(picker, playArgs = []) {
 
   return {
     promise,
-
-    seekBack: secs => playController.seekBack(secs),
-    seekAhead: secs => playController.seekAhead(secs),
-    skipCurrent: () => playController.skipCurrent(),
-    volUp: amount => playController.volUp(amount),
-    volDown: amount => playController.volDown(amount),
-    togglePause: () => playController.togglePause(),
-
-    kill: function() {
-      playController.killProcess()
-    },
-
-    logTrackInfo: function() {
-      if (playController.currentTrack) {
-        const [ curTitle, curArg ] = playController.currentTrack
-        console.log(`Playing: \x1b[1m${curTitle} \x1b[2m${curArg}\x1b[0m`)
-      } else {
-        console.log("No song currently playing.")
-      }
-    }
+    controller: playController
   }
 }
diff --git a/todo.txt b/todo.txt
index a55893c..9317d3e 100644
--- a/todo.txt
+++ b/todo.txt
@@ -159,3 +159,7 @@ TODO: The results of pressing key commands aren't very clear currently. Useful
       things that come to mind would be presenting the volume when it's
       changed; making it clear that a song is being skipped when it is; and
       having "paused" be part of the status bar.
+
+TODO: Figure out a way to make the same mpv process be reused, so that options
+      such as volume can be remembered. (At the moment volume is reset to the
+      default whenever a new track is played!)