« 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--players.js43
-rw-r--r--todo.txt3
2 files changed, 36 insertions, 10 deletions
diff --git a/players.js b/players.js
index c707494..77f1246 100644
--- a/players.js
+++ b/players.js
@@ -1,7 +1,13 @@
 // stolen from http-music
 
+const {
+  commandExists,
+  killProcess,
+  getTimeStrings,
+  getTimeStringsFromSec
+} = require('./general-util')
+
 const { spawn } = require('child_process')
-const { commandExists, killProcess, getTimeStrings } = require('./general-util')
 const EventEmitter = require('events')
 const Socat = require('./socat')
 const fs = require('fs')
@@ -88,25 +94,42 @@ class Player extends EventEmitter {
 }
 
 module.exports.MPVPlayer = class extends Player {
+  // The more powerful MPV player. MPV is virtually impossible for a human
+  // being to install; if you're having trouble with it, try the SoX player.
+
   getMPVOptions(file, startTime) {
-    const opts = ['--no-video', file]
+    const opts = [
+      `--term-status-msg='${this.getMPVStatusMessage()}'`,
+      '--no-video',
+      file
+    ]
+
     if (this.isLooping) {
       opts.unshift('--loop')
     }
+
     if (this.isPaused) {
       opts.unshift('--pause')
     }
+
     if (startTime) {
       opts.unshift('--start=' + startTime)
     }
+
     opts.unshift('--volume=' + this.volume * this.volumeMultiplier)
+
     return opts
   }
 
-  playFile(file, startTime) {
-    // The more powerful MPV player. MPV is virtually impossible for a human
-    // being to install; if you're having trouble with it, try the SoX player.
+  getMPVStatusMessage() {
+    // Note: This function shouldn't include any single-quotes! It probably
+    // (NOTE: PROBABLY) wouldn't cause any security issues, but it will break
+    // --term-status-msg parsing and might keep mpv from starting at all.
 
+    return '${=time-pos} ${=duration} ${=percent-pos}'
+  }
+
+  playFile(file, startTime) {
     this.process = spawn('mpv', this.getMPVOptions(file, startTime).concat(this.processOptions))
 
     let lastPercent = 0
@@ -117,14 +140,14 @@ module.exports.MPVPlayer = class extends Player {
       }
 
       const match = data.toString().match(
-        /(..):(..):(..) \/ (..):(..):(..) \(([0-9]+)%\)/
+        /([0-9.]+) ([0-9.]+) ([0-9.]+)/
       )
 
       if (match) {
         const [
-          curHour, curMin, curSec, // ##:##:##
-          lenHour, lenMin, lenSec, // ##:##:##
-          percent // ###%
+          curSecTotal,
+          lenSecTotal,
+          percent
         ] = match.slice(1)
 
         if (parseInt(percent) < lastPercent) {
@@ -137,7 +160,7 @@ module.exports.MPVPlayer = class extends Player {
 
         lastPercent = parseInt(percent)
 
-        this.printStatusLine(getTimeStrings({curHour, curMin, curSec, lenHour, lenMin, lenSec}))
+        this.printStatusLine(getTimeStringsFromSec(curSecTotal, lenSecTotal))
       }
 
       this.updateVolume();
diff --git a/todo.txt b/todo.txt
index 3b29b7e..290b8c0 100644
--- a/todo.txt
+++ b/todo.txt
@@ -631,3 +631,6 @@ TODO: "Alphabetize order of groups" order option. Listen to the releases of
 TODO: "Reveal in queue" option in the context menu for tracks that are part of
       the queue! Also, rename existing "Reveal" option to "Reveal in library".
       (Done!)
+
+TODO: Timestamps which have a timestampEnd property (all of them I think?)
+      should display their duration in the right column.