« get me outta code hell

mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--guess.js41
1 files changed, 23 insertions, 18 deletions
diff --git a/guess.js b/guess.js
index 0748ce8..db9f8e8 100644
--- a/guess.js
+++ b/guess.js
@@ -36,13 +36,15 @@ async function game() {
     process.exit(1)
   }
 
+  const QP = await backend.addQueuePlayer()
+
   // TODO: nah
-  backend.setVolume(60)
+  QP.setVolume(60)
 
   process.stdin.setRawMode(true)
   process.stdin.on('data', async data => {
     if (data[0] === 0x03) {
-      await backend.stopPlaying()
+      await QP.stopPlaying()
       process.exit(0)
     }
   })
@@ -50,9 +52,6 @@ async function game() {
   const sourcePath = process.argv[2] || os.homedir() + '/Music'
   let grouplike = {source: ['crawl-local', sourcePath]}
   grouplike = await processSmartPlaylist(grouplike)
-
-  // TODO: Actually let the user choose this..!
-  const group = grouplike.items.find(item => item.name === 'library')
   const allTracks = flattenGrouplike(grouplike).items
 
   const displayTrack = (track, shouldLimit) => {
@@ -68,8 +67,10 @@ async function game() {
 
   while (allTracks.length) {
     const track = allTracks[Math.floor(Math.random() * allTracks.length)]
-    backend.play(track)
-    await untilEvent(backend, 'playing')
+    QP.setPause(false)
+    const promise = untilEvent(QP, 'playing')
+    QP.play(track)
+    await promise
     console.log('-- Listen! Then press space to pause and make a guess. --')
 
     let startTime = Date.now()
@@ -94,42 +95,46 @@ async function game() {
       write(`\r\x1b[${4 + input.length}C`)
     }
 
-    const echoFn = () => {
+    const fmtTime = () => {
       let t = (playTime + Date.now() - startTime) / 1000
       t = Math.floor(t * 10) / 10
       if (t % 1 === 0) {
         t = t + '.0'
       }
-      write(resetLine + t + 's')
+      return t + 's'
+    }
+
+    const echoFn = () => {
+      write(resetLine + fmtTime())
     }
 
     while (true) {
       let echo
-      if (!backend.player.isPaused) {
+      if (!QP.player.isPaused) {
         echo = setInterval(echoFn, 50)
       }
       const key = await untilEvent(process.stdin, 'data')
       clearInterval(echo)
-      if (key[0] === 0x10 || (key[0] === 0x20 && !backend.player.isPaused)) {
-        if (backend.player.isPaused) {
+      if (key[0] === 0x10 || (key[0] === 0x20 && !QP.player.isPaused)) {
+        if (QP.player.isPaused) {
           startTime = Date.now()
           console.log(resetLine + dim + '<Unpaused.>')
           write(ansi.resetAttributes())
         } else {
+          console.log(resetLine + dim + `<Paused @ ${fmtTime()}. Type the track's name below! ^P to resume.>`)
           playTime += Date.now() - startTime
-          console.log(resetLine + dim + '<Paused. Type the track\'s name below! ^P to resume.>')
           write(ansi.resetAttributes())
           echoFn()
           displayInput()
         }
-        backend.togglePause()
+        QP.togglePause()
       /*
-      } else if (key[0] === 0x3f && (!key.length || !backend.player.isPaused)) {
-        backend.setPause(false)
+      } else if (key[0] === 0x3f && (!key.length || !QP.player.isPaused)) {
+        QP.setPause(false)
         gaveUp = true
         break
       */
-      } else if (backend.player.isPaused) {
+      } else if (QP.player.isPaused) {
         if (telc.isBackspace(key)) {
           input = input.slice(0, -1)
           giveUpNext = false
@@ -153,7 +158,7 @@ async function game() {
             }
           } else {
             if (giveUpNext) {
-              backend.setPause(false)
+              QP.setPause(false)
               gaveUp = true
               break
             } else {