« get me outta code hell

Add %timeLeft% template replacement - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-04-08 21:20:06 -0300
committerFlorrie <towerofnix@gmail.com>2018-04-08 21:20:06 -0300
commite9b3668e0b3f8e1e493f1fc93ab220bc3ea4f939 (patch)
treef39b0a58453a7687bb080e54e5859b79dfbfea18
parent84f9955826be2f536d770790658aab8b5cba51f0 (diff)
Add %timeLeft% template replacement
-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 {