1
0
mirror of https://github.com/coalaura/whiskr.git synced 2025-12-02 20:22:52 +00:00

improve time rendering

This commit is contained in:
Laura
2025-11-04 22:47:27 +01:00
parent 1030c86c1f
commit f7db78c08f
2 changed files with 14 additions and 5 deletions

View File

@@ -261,7 +261,7 @@
this.#_time = make("div", "time"); this.#_time = make("div", "time");
if (this.#time) { if (this.#time) {
this.#_time.textContent = `${this.#time.toFixed(1)}s`; this.#_time.textContent = formatMilliseconds(this.#time * 1000);
} }
_body.appendChild(this.#_time); _body.appendChild(this.#_time);
@@ -868,7 +868,7 @@
this.#time = time; this.#time = time;
if (this.#time) { if (this.#time) {
this.#_time.textContent = `${this.#time.toFixed(1)}s`; this.#_time.textContent = formatMilliseconds(this.#time * 1000);
} }
} }

View File

@@ -46,16 +46,25 @@ function escapeHtml(text) {
return text.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;"); return text.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
} }
const fracZerosRgx = /(?:(\.\d*?[1-9])0+|\.0+)$/;
function round(num, digits) {
return num.toFixed(digits).replace(fracZerosRgx, "$1") || "0";
}
function formatMilliseconds(ms) { function formatMilliseconds(ms) {
if (ms < 1000) { if (ms < 1000) {
return `${ms}ms`; return `${ms}ms`;
} }
if (ms < 10000) { if (ms < 60000) {
return `${(ms / 1000).toFixed(1)}s`; return `${round(ms / 1000, 1)}s`;
} }
return `${Math.round(ms / 1000)}s`; const minutes = Math.floor(ms / 60000),
seconds = ms - minutes * 60000;
return `${minutes}m ${round(seconds / 1000, 1)}s`;
} }
function formatTimestamp(ts) { function formatTimestamp(ts) {