« get me outta code hell

mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/general-util.js
diff options
context:
space:
mode:
Diffstat (limited to 'general-util.js')
-rw-r--r--general-util.js97
1 files changed, 56 insertions, 41 deletions
diff --git a/general-util.js b/general-util.js
index 536b3fd..d369848 100644
--- a/general-util.js
+++ b/general-util.js
@@ -147,62 +147,77 @@ export function getSecFromTimestamp(timestamp) {
   }
 }
 
-export function getTimeStringsFromSec(curSecTotal, lenSecTotal, fraction = false) {
-  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)
-  let leftFrac = lenSecTotal % 1
-
-  // Yeah, yeah, duplicate math.
+export function getTimeStringsFromSec(curSecTotal, lenSecTotal = null, fraction = false) {
+  const pad = val => val.toString().padStart(2, '0')
+  const padFrac = val => Math.floor(val * 1000).toString().padEnd(3, '0')
+
+  // We don't want to display hour counters if the total length is less
+  // than an hour.
+  const displayAsHours = Math.max(curSecTotal, lenSecTotal ?? 0) >= 3600
+
+  const strings = {curSecTotal, lenSecTotal}
+
   let curHour = Math.floor(curSecTotal / 3600)
   let curMin = Math.floor((curSecTotal - curHour * 3600) / 60)
   let curSec = Math.floor(curSecTotal - curHour * 3600 - curMin * 60)
   let curFrac = curSecTotal % 1
 
-  // Wee!
-  let lenHour = Math.floor(lenSecTotal / 3600)
-  let lenMin = Math.floor((lenSecTotal - lenHour * 3600) / 60)
-  let lenSec = Math.floor(lenSecTotal - lenHour * 3600 - lenMin * 60)
-  let lenFrac = lenSecTotal % 1
-
-  const pad = val => val.toString().padStart(2, '0')
-  const padFrac = val => Math.floor(val * 1000).toString().padEnd(3, '0')
   curMin = pad(curMin)
   curSec = pad(curSec)
-  lenMin = pad(lenMin)
-  lenSec = pad(lenSec)
-  leftMin = pad(leftMin)
-  leftSec = pad(leftSec)
   curFrac = padFrac(curFrac)
-  lenFrac = padFrac(lenFrac)
-  leftFrac = padFrac(leftFrac)
 
-  // 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 || parseInt(curHour) > 0) {
-    timeDone = `${curHour}:${curMin}:${curSec}`
-    timeLeft = `${leftHour}:${leftMin}:${leftSec}`
-    duration = `${lenHour}:${lenMin}:${lenSec}`
+  if (displayAsHours) {
+    strings.timeDone = `${curHour}:${curMin}:${curSec}`
   } else {
-    timeDone = `${curMin}:${curSec}`
-    timeLeft = `${leftMin}:${leftSec}`
-    duration = `${lenMin}:${lenSec}`
+    strings.timeDone = `${curMin}:${curSec}`
   }
 
   if (fraction) {
-    timeDone += '.' + curFrac
-    timeLeft += '.' + leftFrac
-    duration += '.' + lenFrac
+    strings.timeDone += '.' + curFrac
+  }
+
+  if (typeof lenSecTotal === 'number') {
+    const percentVal = (100 / lenSecTotal) * curSecTotal
+    strings.percentDone = (Math.trunc(percentVal * 100) / 100).toFixed(2) + '%'
+
+    // Yeah, yeah, duplicate math.
+    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)
+    let leftFrac = leftSecTotal % 1
+
+    // Wee!
+    let lenHour = Math.floor(lenSecTotal / 3600)
+    let lenMin = Math.floor((lenSecTotal - lenHour * 3600) / 60)
+    let lenSec = Math.floor(lenSecTotal - lenHour * 3600 - lenMin * 60)
+    let lenFrac = lenSecTotal % 1
+
+    lenMin = pad(lenMin)
+    lenSec = pad(lenSec)
+    lenFrac = padFrac(lenFrac)
+
+    leftMin = pad(leftMin)
+    leftSec = pad(leftSec)
+    leftFrac = padFrac(leftFrac)
+
+    if (typeof lenSecTotal === 'number') {
+      if (displayAsHours) {
+        strings.timeLeft = `${leftHour}:${leftMin}:${leftSec}`
+        strings.duration = `${lenHour}:${lenMin}:${lenSec}`
+      } else {
+        strings.timeLeft = `${leftMin}:${leftSec}`
+        strings.duration = `${lenMin}:${lenSec}`
+      }
+
+      if (fraction) {
+        strings.timeLeft += '.' + leftFrac
+        strings.duration += '.' + lenFrac
+      }
+    }
   }
 
-  return {percentDone, timeDone, timeLeft, duration, curSecTotal, lenSecTotal}
+  return strings
 }
 
 export function getTimeStrings({curHour, curMin, curSec, lenHour, lenMin, lenSec}) {