From 7f2461a60fba35013551fdb27ba0bb8d0720021d Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sun, 14 May 2023 19:53:31 -0300 Subject: new GhostPlayer class & support 👻 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This also makes the UI not explode when duration strings aren't available for progress bar display, and shows getTimeStringsFromSec how to handle that. --- general-util.js | 97 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 56 insertions(+), 41 deletions(-) (limited to 'general-util.js') 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}) { -- cgit 1.3.0-6-gf8a5