« get me outta code hell

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:
Diffstat (limited to 'src')
-rw-r--r--src/loop-play.js30
1 files changed, 19 insertions, 11 deletions
diff --git a/src/loop-play.js b/src/loop-play.js
index f01ee5f..d5343a9 100644
--- a/src/loop-play.js
+++ b/src/loop-play.js
@@ -29,34 +29,42 @@ const {
 const { processTemplateString } = require('./general-util')
 
 function getTimeStrings({curHour, curMin, curSec, lenHour, lenMin, lenSec}) {
-  let timeDone, duration
+  // Multiplication casts to numbers; addition prioritizes strings.
+  // Thanks, JavaScript!
+  const curSecTotal = (3600 * curHour) + (60 * curMin) + (1 * curSec)
+  const lenSecTotal = (3600 * lenHour) + (60 * lenMin) + (1 * lenSec)
+  const percentVal = (100 / lenSecTotal) * curSecTotal
+  const percentDone = (
+    (Math.trunc(percentVal * 100) / 100).toFixed(2) + '%'
+  )
+
+  const leftSecTotal = lenSecTotal - curSecTotal
+  let leftHour = Math.floor(leftSecTotal / 3600)
+  let leftMin = Math.floor((leftSecTotal - leftHour * 3600) / 60)
+  let leftSec = Math.floor(leftSecTotal - leftHour * 3600 - leftMin * 60)
 
   const pad = val => val.toString().padStart(2, '0')
   curMin = pad(curMin)
   curSec = pad(curSec)
   lenMin = pad(lenMin)
   lenSec = pad(lenSec)
+  leftMin = pad(leftMin)
+  leftSec = pad(leftSec)
 
   // We don't want to display hour counters if the total length is less
   // than an hour.
+  let timeDone, timeLeft, duration
   if (parseInt(lenHour) > 0) {
     timeDone = `${curHour}:${curMin}:${curSec}`
+    timeLeft = `${leftHour}:${leftMin}:${leftSec}`
     duration = `${lenHour}:${lenMin}:${lenSec}`
   } else {
     timeDone = `${curMin}:${curSec}`
+    timeLeft = `${leftMin}:${leftSec}`
     duration = `${lenMin}:${lenSec}`
   }
 
-  // Multiplication casts to numbers; addition prioritizes strings.
-  // Thanks, JavaScript!
-  const curSecTotal = (3600 * curHour) + (60 * curMin) + (1 * curSec)
-  const lenSecTotal = (3600 * lenHour) + (60 * lenMin) + (1 * lenSec)
-  const percentVal = (100 / lenSecTotal) * curSecTotal
-  const percentDone = (
-    (Math.trunc(percentVal * 100) / 100).toFixed(2) + '%'
-  )
-
-  return {percentDone, timeDone, duration}
+  return {percentDone, timeDone, timeLeft, duration}
 }
 
 class Player extends EventEmitter {