« get me outta code hell

Separate remaining duration calculation - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/backend.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2019-08-08 19:36:36 -0300
committerFlorrie <towerofnix@gmail.com>2019-08-08 20:07:01 -0300
commitf0e6dd7283b3825d8ded273179ba113924605979 (patch)
treec75578ca87cd34b891d7c391ff3c505b963f025b /backend.js
parent7552ecaecb4d9c6ca786422ec955be7eb3b1ca71 (diff)
Separate remaining duration calculation
...into its own function. To be used to get the total duration string of
a grouplike.

(This is stored on the backend instead of a more general playlist-utils
function because it requires access to the metadata code specific to
mtui.)
Diffstat (limited to 'backend.js')
-rw-r--r--backend.js26
1 files changed, 26 insertions, 0 deletions
diff --git a/backend.js b/backend.js
index 23a9309..b8071d1 100644
--- a/backend.js
+++ b/backend.js
@@ -9,6 +9,7 @@ const { getPlayer } = require('./players')
 const RecordStore = require('./record-store')
 
 const {
+  getTimeStringsFromSec,
   shuffleArray,
   throttlePromise
 } = require('./general-util')
@@ -518,6 +519,31 @@ class Backend extends EventEmitter {
     }
   }
 
+  getDuration(item) {
+    let noticedMissingMetadata = false
+
+    const durationFn = (acc, track) => {
+      const metadata = this.getMetadataFor(track)
+      if (!metadata) noticedMissingMetadata = true
+      return acc + (metadata && metadata.duration) || 0
+    }
+
+    let items
+    if (isGroup(item)) {
+      items = flattenGrouplike(item).items
+    } else {
+      items = [item]
+    }
+
+    const seconds = items.reduce(durationFn, 0)
+
+    let { duration: string } = getTimeStringsFromSec(0, seconds)
+    const approxSymbol = noticedMissingMetadata ? '+' : ''
+    string += approxSymbol
+
+    return {seconds, string, noticedMissingMetadata, approxSymbol}
+  }
+
   async download(item) {
     if (isGroup(item)) {
       // TODO: Download all children (recursively), show a confirmation prompt