« get me outta code hell

show timestamp hours column whenever appropriate - 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:
author(quasar) nebula <towerofnix@gmail.com>2021-08-14 00:44:47 -0300
committer(quasar) nebula <towerofnix@gmail.com>2021-08-14 00:44:47 -0300
commit39732bd31e13c9a785eac1fc724bc9fc768be2ab (patch)
tree2a40a9227f65832a202706cf2e9a1132e4024cd7 /general-util.js
parentc047b8c57d4e5012578c072420d2b73dd5b59c4c (diff)
show timestamp hours column whenever appropriate
Diffstat (limited to 'general-util.js')
-rw-r--r--general-util.js17
1 files changed, 15 insertions, 2 deletions
diff --git a/general-util.js b/general-util.js
index f63ae21..aba1541 100644
--- a/general-util.js
+++ b/general-util.js
@@ -149,7 +149,7 @@ module.exports.getSecFromTimestamp = function(timestamp) {
   }
 }
 
-module.exports.getTimeStringsFromSec = function(curSecTotal, lenSecTotal) {
+module.exports.getTimeStringsFromSec = function(curSecTotal, lenSecTotal, fraction = false) {
   const percentVal = (100 / lenSecTotal) * curSecTotal
   const percentDone = (
     (Math.trunc(percentVal * 100) / 100).toFixed(2) + '%'
@@ -159,29 +159,36 @@ module.exports.getTimeStringsFromSec = function(curSecTotal, lenSecTotal) {
   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.
   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) {
+  if (parseInt(lenHour) > 0 || parseInt(curHour) > 0) {
     timeDone = `${curHour}:${curMin}:${curSec}`
     timeLeft = `${leftHour}:${leftMin}:${leftSec}`
     duration = `${lenHour}:${lenMin}:${lenSec}`
@@ -191,6 +198,12 @@ module.exports.getTimeStringsFromSec = function(curSecTotal, lenSecTotal) {
     duration = `${lenMin}:${lenSec}`
   }
 
+  if (fraction) {
+    timeDone += '.' + curFrac
+    timeLeft += '.' + leftFrac
+    duration += '.' + lenFrac
+  }
+
   return {percentDone, timeDone, timeLeft, duration, curSecTotal, lenSecTotal}
 }