From fd072ce0e9ab4408724cdc40f1f393b22bcf6daa Mon Sep 17 00:00:00 2001 From: Theis Pieter Hollebeek Date: Fri, 6 Dec 2024 12:54:43 +0100 Subject: [PATCH] blased web --- web/public/index.html | 1 - web/public/src/index.ts | 64 ++++++++++++++++++++++++++++++++--------- web/public/style.css | 15 ++++++++-- 3 files changed, 62 insertions(+), 18 deletions(-) diff --git a/web/public/index.html b/web/public/index.html index f1a1026..25d1473 100644 --- a/web/public/index.html +++ b/web/public/index.html @@ -30,7 +30,6 @@

                     
Process is currently running
- diff --git a/web/public/src/index.ts b/web/public/src/index.ts index 342d206..4ca4b6b 100644 --- a/web/public/src/index.ts +++ b/web/public/src/index.ts @@ -7,13 +7,11 @@ type CodeCovEntry = { function loadCodeCoverage( text: string, - codeCoverageData: CodeCovEntry[], - codeCoverageDiv: HTMLPreElement, + data: CodeCovEntry[], + container: HTMLPreElement, + tooltip: HTMLElement, ) { - const tooltip = document.createElement("span"); - tooltip.id = "covers-tooltip"; - codeCoverageDiv.append(tooltip); - const entries = codeCoverageData.toSorted(( + const entries = data.toSorted(( a: CodeCovEntry, b: CodeCovEntry, ) => b.index - a.index); @@ -61,11 +59,12 @@ function loadCodeCoverage( y >= boundingRect.bottom; return !outside; } - codeCoverageDiv.append(...elements); + container.append(...elements); document.addEventListener("mousemove", (event) => { const [x, y] = [event.clientX, event.clientY]; - const outerBox = codeCoverageDiv.getBoundingClientRect(); + const outerBox = container.getBoundingClientRect(); if (!positionInBox([x, y], outerBox)) { + tooltip.hidden = true; return; } const element = elements.find((element) => { @@ -82,7 +81,11 @@ function loadCodeCoverage( tooltip.hidden = true; return; } - const covers = parseInt(element.dataset.covers); + const maybeCovers = element.dataset.covers; + if (!maybeCovers) { + throw new Error("unreachable"); + } + const covers = parseInt(maybeCovers); tooltip.hidden = false; tooltip.style.left = `${event.clientX + 20}px`; tooltip.style.top = `${event.clientY + 20}px`; @@ -213,18 +216,51 @@ loop { "flame-graph": () => void; }; + function countLines(code: string) { + let lines = 0; + for (const char of code) { + if (char === "\n") lines += 1; + } + return lines; + } + + function createLineElement(code: string): HTMLPreElement { + const lines = countLines(code) + 1; + const maxLineWidth = lines.toString().length; + let text = ""; + for (let i = 1; i < lines; ++i) { + const node = i.toString().padStart(maxLineWidth); + text += node; + text += "\n"; + } + const lineElement = document.createElement("pre"); + lineElement.classList.add("code-lines"); + lineElement.innerText = text; + return lineElement; + } + const view = document.querySelector("#view")!; const renderFunctions: RenderFns = { "source-code": () => { + const container = document.createElement("div"); + container.classList.add("code-container"); + const lines = createLineElement(codeData); const code = document.createElement("pre"); - code.innerHTML = codeData; - view.replaceChildren(code); + code.innerText = codeData; + container.append(lines, code); + view.replaceChildren(container); }, "code-coverage": () => { - const codeCoverageElement = document.createElement("pre"); - loadCodeCoverage(codeData, codeCoverageData, codeCoverageElement); + const container = document.createElement("div"); + container.classList.add("code-container"); + const tooltip = document.createElement("div"); + tooltip.id = "covers-tooltip"; + const code = document.createElement("pre"); + loadCodeCoverage(codeData, codeCoverageData, code, tooltip); + const lines = createLineElement(codeData); + container.append(lines, code); const view = document.querySelector("#view")!; - view.replaceChildren(codeCoverageElement); + view.replaceChildren(container, tooltip); }, "flame-graph": () => {}, }; diff --git a/web/public/style.css b/web/public/style.css index b47a0f7..6e76882 100644 --- a/web/public/style.css +++ b/web/public/style.css @@ -88,10 +88,18 @@ main #cover { cursor: pointer; } -#view pre { +#view .code-container { + display: flex; font-size: 1rem; + gap: 1ch; } +#view .code-lines { + border-right: 1px solid var(--white); + padding-right: 0.5ch; +} + + #views-nav input:checked + label { background-color: var(--code-status); color: var(--black); @@ -113,8 +121,9 @@ main #cover { left: 0; padding: 3px; border-radius: 3px; - background-color: var(--bg-2); - box-shadow: 2px 2px 2px black; + background-color: var(--black); + border: 2px solid var(--code-status); + border-radius: 0.25rem; color: #eee; }