Public: anyone can read this; the crew can change it.

A CSV file's table sorts by a column on tap, server-side, and keeps the header fixed #13350

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/13328-a-csv-file-s-table-sorts-by-a-column-on → main 8 commits fork 1 thread · 1 unresolved

  • Merged. scout/13328-a-csv-file-s-table-sorts-by-a-column-on was merged into main as 4742a08.

Review

Sign in to review this change.
10 files changed +390 −84
docs/features/csv-view.md +16 −1
@@ -4,9 +4,24 @@
44 RFC 4180 with quotes, embedded newlines, doubled quotes and a BOM; the delimiter (`,` `;` tab `|`) is detected from the first record.
55 The first 1,000 rows are drawn on the server with a sticky header and (on phones) a sticky first column; numeric columns are right-aligned.
66 `?all=1` ("Show all N rows", no JavaScript needed) draws up to 20,000. The header line shows rows × columns and the delimiter.
7 A small script adds sort (header click or Enter) and a filter box over the rows drawn; no library.
7 A small script adds a filter box over the rows drawn; no library.
88 A file that is not valid CSV shows the source with a one-line reason. `?view=source`, Raw and Download work as for any file.
99
10 ## Sorting
11
12 A column header is a link: `?sort=<column>&dir=asc|desc` on the blob page, so a sorted table has an address and works without JavaScript.
13 Clicking a header sorts ascending; clicking the sorted header flips it. The sorted header shows an arrow and `aria-sort`.
14 - `sort` must name a header cell exactly. A name that matches nothing, or a `dir` that is not `asc`/`desc`, is ignored (no sort, ascending): such a request reads the same rows and is kept under the same key as the page without it.
15 - Only a matching sort reads past the 1,000-row window: it sorts every row the page may draw (up to 20,000) and then cuts the window, so the first rows shown are the top of the whole file.
16 - Numeric columns sort by value, others by lowercase text; empty cells, and cells that are not numbers in a numeric column, go last either way; ties keep file order.
17 - "Show all N rows" and every header link carry the sort (and `all=1`).
18 - The sort is server-side: the in-browser sort script is gone, and so is its filter-after-sort interplay; the filter still works over the rows drawn.
19 - The same sort works on `.jsonl`/`.ndjson` tables.
20 - The header is the design system's sortable table (`.table[data-sortable]`, its arrows in `components.ts`); the link covers the whole header cell, 44px tall under a coarse pointer.
21 - Cost: a real sort parses up to 20,000 rows on the request's thread, once for each column and direction asked, then the page is kept; the distinct pages are bounded by the header's width (at most 200 columns, two directions).
22
23 In the terminal, on a `.csv`/`.tsv` open in the workspace, `o` orders the rows: file order, then each column ascending and descending in turn (the header shows ▲/▼), then file order again. It uses the page's `sortRows` (`src/text/csv-sort.ts`); `s` still flips to the source, which is never reordered.
24
1025 ## JSON Lines
1126
1227 `.jsonl`/`.ndjson` files use the same table (`src/web/render/jsonl-parse.ts` makes the rows; `csv.ts` draws them).
src/text/csv-sort.ts added +33
@@ -0,0 +1,33 @@
1 /**
2 * Sorting a table's rows by one column: the page's `?sort=` and the terminal's `o` share this.
3 * Numbers by value, text by lowercase; empties and (in a numeric column) cells that are not numbers
4 * go last whichever way it runs; ties keep file order.
5 */
6
7 const NUMBER = /^[-+]?(\d+\.?\d*|\.\d+)(e[-+]?\d+)?$/i;
8
9 /** Whether every non-empty cell of a column reads as a number. */
10 export function isNumericColumn(rows: readonly (readonly string[])[], column: number): boolean {
11 let seen = false;
12 for (const row of rows) {
13 const t = (row[column] ?? "").trim();
14 if (t === "") continue;
15 if (!NUMBER.test(t)) return false;
16 seen = true;
17 }
18 return seen;
19 }
20
21 export function sortRows(rows: string[][], column: number, numeric: boolean, descending: boolean): string[][] {
22 const keyed = rows.map((row, at) => {
23 const t = (row[column] ?? "").trim();
24 const key = numeric ? parseFloat(t) : t.toLowerCase();
25 return { row, at, last: t === "" || (numeric && Number.isNaN(key)), key };
26 });
27 keyed.sort((a, b) => {
28 if (a.last || b.last) return a.last === b.last ? a.at - b.at : a.last ? 1 : -1;
29 const d = a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
30 return d === 0 ? a.at - b.at : descending ? -d : d;
31 });
32 return keyed.map((k) => k.row);
33 }
src/tui/screens/workspace.ts +39 −1
@@ -56,6 +56,7 @@ import { isModelPath, modelFacts } from "../../web/render/model-facts.ts";
5656 import type { RepoFacts, RepoSource } from "../../view/source.ts";
5757 import { detectLanguage, highlightLines } from "../../web/highlight.ts";
5858 import { MAX_COLUMNS, parseCsv, type Table } from "../../text/csv.ts";
59 import { isNumericColumn, sortRows } from "../../text/csv-sort.ts";
5960 import { parseGeoJson, propertyRows } from "../../web/render/geojson.ts";
6061 import { gcodeFacts, parseGcode } from "../../web/render/gcode.ts";
6162 import { readStep, stepFacts } from "../../web/render/step.ts";
@@ -806,6 +807,8 @@ class Tab {
806807 /** The parsed file, when its name says table. Parse errors ride along. */
807808 table: Table | null = null;
808809 grid: Grid = { header: "", rows: [] };
810 /** `o` on a CSV/TSV: the column its rows are ordered by, or null for file order. */
811 order: { column: number; descending: boolean } | null = null;
809812 /** An .xlsx's first sheet: the bytes are not text, so the table is all there is. */
810813 sheet: Table | null = null;
811814 /** A SQLite file's bytes, kept so `>`/`<` and `}`/`{` can page it; null for any other file. */
@@ -837,6 +840,31 @@ class Tab {
837840 return this.kind === "markdown" || (this.kind === "patch" && this.patch !== null);
838841 }
839842
843 /**
844 * The table with its rows in `order`, and an arrow on that column's header; the table itself when
845 * unordered.
846 */
847 ordered(table: Table): Table {
848 const o = this.order;
849 if (o === null || table.error !== undefined) return table;
850 const numeric = isNumericColumn(table.rows, o.column);
851 const header = table.header.map((h, i) => (i === o.column ? `${h} ${o.descending ? "▼" : "▲"}` : h));
852 return { ...table, header, rows: sortRows(table.rows, o.column, numeric, o.descending) };
853 }
854
855 /** `o`: file order, then each column ascending and descending in turn, then file order again. */
856 cycleOrder(): string {
857 const t = this.table;
858 if (t === null || t.error !== undefined || !isTablePath(this.path) || t.header.length === 0) return "";
859 const o = this.order;
860 if (o === null) this.order = { column: 0, descending: false };
861 else if (!o.descending) this.order = { column: o.column, descending: true };
862 else this.order = o.column + 1 < t.header.length ? { column: o.column + 1, descending: false } : null;
863 this.grid = layoutTable(this.ordered(t));
864 const now = this.order;
865 return now === null ? "in file order" : `by ${t.header[now.column]} ${now.descending ? "descending" : "ascending"}`;
866 }
867
840868 /** True when the pane is showing a CSV/TSV as columns. */
841869 get tabular(): boolean {
842870 return (
@@ -1019,7 +1047,9 @@ class Tab {
10191047 // The decoded source, not `plain`: `expandTabs` has eaten every tab there.
10201048 const delimiter = /\.tsv$/i.test(this.path) ? "\t" : undefined;
10211049 this.table = parseCsv(new TextDecoder().decode(source), { delimiter });
1022 this.grid = layoutTable(this.table);
1050 // A re-read keeps the order only while its column is still in the header.
1051 if (this.order !== null && this.order.column >= this.table.header.length) this.order = null;
1052 this.grid = layoutTable(this.ordered(this.table));
10231053 } else if (kind?.table === undefined) this.table = null;
10241054 // The rendered document is derived from the same text, so an edit, a
10251055 // theme switch or a re-read has to throw it away with everything else.
@@ -2083,6 +2113,7 @@ export function workspaceScreen(
20832113 disabled: t.table != null && (t.table.error !== undefined || t.table.header.length === 0),
20842114 }),
20852115 ]),
2116 ...(t !== undefined && t.tabular && isTablePath(t.path) ? [binding(["o"], "order rows by a column")] : []),
20862117 ...(t?.db == null ? [] : [binding([">", "<", "}", "{"], "next / previous 100 rows, next / previous table")]),
20872118 binding(["tab"], "focus explorer"),
20882119 binding(["e"], "edit", { disabled: !editable() }),
@@ -2462,6 +2493,13 @@ export function workspaceScreen(
24622493 toggleTable(t);
24632494 return true;
24642495 }
2496 if (plain && e.key === "o" && focus === "file" && t !== undefined && t.tabular && isTablePath(t.path)) {
2497 const said = t.cycleOrder();
2498 t.scroll.top = 0;
2499 t.findHits();
2500 ctx.toast(`rows ${said}`);
2501 return true;
2502 }
24652503
24662504 // ---- the repository ----
24672505 if (plain && e.key === "c") {
src/web/pages/repo.ts +40 −21
@@ -90,7 +90,7 @@ import { gcodeOffThread, rowsOffThread } from "../code_rows_pool.ts";
9090 import { flashTo, fragment, page, redirect } from "../layout.ts";
9191 import { renderMarkdown, type MarkdownRepo } from "../markdown.ts";
9292 import { overSize } from "../render/archive.ts";
93 import { csvJs, isCsvPath } from "../render/csv.ts";
93 import { csvJs, isCsvPath, sortColumn } from "../render/csv.ts";
9494 import { fontJs } from "../render/font.ts";
9595 import { INLINE_MAX, keepGcode, keptGcode, layerCount } from "../render/gcode.ts";
9696 import { hasTable } from "../render/sqlite.ts";
@@ -1192,6 +1192,9 @@ export function renderBlob(
11921192 highlighted?: (ms: number) => void;
11931193 /** Draw every line instead of the first 1,500. */
11941194 all?: boolean;
1195 /** `?sort=` and `?dir=` on a table file. */
1196 sort?: string;
1197 dir?: string;
11951198 /** `?table=` on a SQLite file. */
11961199 table?: string;
11971200 /** `?page=` on a SQLite table, 1-based. */
@@ -1221,6 +1224,7 @@ export function renderBlob(
12211224 repo: { base, ref: encoded },
12221225 ...(o.exists !== undefined ? { exists: o.exists } : {}),
12231226 all: o.all === true,
1227 ...(o.sort !== undefined ? { sort: o.sort, dir: o.dir === "desc" ? "desc" : "asc" } : {}),
12241228 ...(o.table !== undefined ? { table: o.table } : {}),
12251229 ...(o.page !== undefined ? { page: o.page } : {}),
12261230 ...(o.layer !== undefined ? { layer: o.layer } : {}),
@@ -1754,6 +1758,8 @@ export async function blobPage(ctx: Ctx): Promise<Response> {
17541758 const view = blobView(ctx.url.searchParams.get("view"));
17551759 const all = ctx.url.searchParams.get("all") === "1";
17561760 const table = ctx.url.searchParams.get("table")?.slice(0, 256) ?? undefined;
1761 const sort = ctx.url.searchParams.get("sort")?.slice(0, 256) ?? undefined;
1762 const dir = ctx.url.searchParams.get("dir") === "desc" ? "desc" : "asc";
17571763 const pageText = ctx.url.searchParams.get("page");
17581764 const page = pageText !== null && /^\d{1,6}$/.test(pageText) && Number(pageText) > 1 ? Number(pageText) : undefined;
17591765 const layerText = ctx.url.searchParams.get("layer");
@@ -1768,26 +1774,31 @@ export async function blobPage(ctx: Ctx): Promise<Response> {
17681774 // A `?table=` that names nothing in the file is a stranger's string: its page is never kept.
17691775 let keepable = true;
17701776 const keepFinished = (response: Response) => (keptKey === undefined || !keepable ? response : keepPage(keptKey, response, true));
1771 if (kept) {
1772 const hit = cachedBlobPage(repo.id, ref, path, `${view}\0${table ?? ""}\0${layer ?? ""}\0${page ?? ""}`, all);
1773 if (hit !== undefined) {
1774 git.done();
1775 return immutableWhenAddressed(
1776 ctx.request,
1777 true,
1778 repo,
1777 // A sorted page is kept under the file, the column and the direction; a `?sort=` that names no
1778 // column is no sort and shares the unsorted page's key.
1779 const viewKey = (column: string | undefined) =>
1780 `${view}\0${table ?? ""}\0${layer ?? ""}\0${page ?? ""}${column === undefined ? "" : `\0${column}\0${dir}`}`;
1781 const keptHit = async (hit: NonNullable<ReturnType<typeof cachedBlobPage>>) => {
1782 git.done();
1783 return immutableWhenAddressed(
1784 ctx.request,
1785 true,
1786 repo,
1787 ref,
1788 await keepFinished(codePage(ctx, scope, {
1789 title: hit.title,
17791790 ref,
1780 await keepFinished(codePage(ctx, scope, {
1781 title: hit.title,
1782 ref,
1783 explorer: hit.explorer,
1784 content: hit.content,
1785 objectSrc: hit.objectSrc,
1786 frameSelf: hit.frame,
1787 ...(hit.font ? { fontSrc: true, extraJs: fontJs } : hit.csv ? { extraJs: csvJs } : {}),
1788 })),
1789 );
1790 }
1791 explorer: hit.explorer,
1792 content: hit.content,
1793 objectSrc: hit.objectSrc,
1794 frameSelf: hit.frame,
1795 ...(hit.font ? { fontSrc: true, extraJs: fontJs } : hit.csv ? { extraJs: csvJs } : {}),
1796 })),
1797 );
1798 };
1799 if (kept) {
1800 const hit = cachedBlobPage(repo.id, ref, path, viewKey(sort), all);
1801 if (hit !== undefined) return keptHit(hit);
17911802 }
17921803 const blob = await git.read(() => scope.source.blob(ref, path));
17931804 if (!blob.ok) return notFoundPage(ctx);
@@ -1798,6 +1809,13 @@ export async function blobPage(ctx: Ctx): Promise<Response> {
17981809 let highlightMs: number | undefined;
17991810 const renderStart = performance.now();
18001811 const shown = capped(blob.value, path);
1812 // The column `?sort=` names in this file's header, if it names one; otherwise the page is the
1813 // unsorted page and reads no more rows than it.
1814 const sortName = sort !== undefined && isCsvPath(path) && shown.kind !== "too_large" ? sortColumn(new TextDecoder().decode(shown.body), path, sort) : undefined;
1815 if (kept && sort !== undefined && sortName === undefined) {
1816 const plain = cachedBlobPage(repo.id, ref, path, viewKey(undefined), all);
1817 if (plain !== undefined) return keptHit(plain);
1818 }
18011819 if (table !== undefined) keepable = shown.kind !== "too_large" && hasTable(shown.body, table, page);
18021820 // A big sliced file is parsed on a worker, never on this thread; the renderer finds the parse
18031821 // kept.
@@ -1828,6 +1846,7 @@ export async function blobPage(ctx: Ctx): Promise<Response> {
18281846 view,
18291847 highlighted: (ms) => (highlightMs = (highlightMs ?? 0) + ms),
18301848 all,
1849 ...(sortName !== undefined ? { sort: sortName, dir } : {}),
18311850 ...(table !== undefined ? { table } : {}),
18321851 ...(page !== undefined ? { page } : {}),
18331852 ...(layer !== undefined ? { layer } : {}),
@@ -1861,7 +1880,7 @@ export async function blobPage(ctx: Ctx): Promise<Response> {
18611880 const frame = drawsHtml(scope, path, shown, view);
18621881 const font = drawsFont(scope, path, shown, view);
18631882 if (kept && keepable)
1864 keepBlobPage(repo.id, ref, path, `${view}\0${table ?? ""}\0${layer ?? ""}\0${page ?? ""}`, all, {
1883 keepBlobPage(repo.id, ref, path, viewKey(sortName), all, {
18651884 title: `${filename} · ${repo.name}`,
18661885 explorer: explorerHtml,
18671886 content: pane,
src/web/render/csv.ts +42 −46
@@ -1,12 +1,14 @@
11 /**
22 * A CSV or TSV file as a table: the first thousand rows drawn on the server,
33 * a sticky header, numbers right-aligned. Without JavaScript `?all=1` draws
4 * more, up to a cap; the script below adds sort and filter and nothing else.
4 * more, up to a cap; a header is a link that sorts on the server (`?sort=&dir=`); the script below
5 * adds the filter and nothing else.
56 */
67
78 import { escapeHtml, html, raw } from "../html.ts";
89 import { fileSize } from "../ui.ts";
910 import { numericColumns, parseCsv, type Delimiter } from "./csv-parse.ts";
11 import { sortRows } from "../../text/csv-sort.ts";
1012 import { parseJsonl } from "./jsonl-parse.ts";
1113 import type { Rendered, RenderInput } from "./renderers.ts";
1214
@@ -34,23 +36,55 @@ const cells = (tag: "th" | "td", row: string[], width: number, numeric: boolean[
3436 return out;
3537 };
3638
39 export { sortRows };
40
41 /**
42 * The header cell `sort` names, or undefined: only a name the file's header holds exactly may
43 * widen the parse or the cache key. The header costs one record (a JSON Lines file's, the first
44 * window), never the rows a sort reads.
45 */
46 export function sortColumn(text: string, path: string, sort: string | undefined): string | undefined {
47 if (sort === undefined) return undefined;
48 const head = isJsonlPath(path) ? parseJsonl(text, ROW_WINDOW, COLUMN_CAP) : parseCsv(text, { rows: 1, columns: COLUMN_CAP });
49 if ("error" in head) return undefined;
50 return head.rows[0]!.slice(0, COLUMN_CAP).includes(sort) ? sort : undefined;
51 }
52
3753 export function csv(input: RenderInput): Rendered {
3854 const want = input.all ? ROW_CAP : ROW_WINDOW;
55 // A sort needs every row the page may draw, so the window is cut after it; a sort that names no
56 // column is no sort and reads no more than a page without one.
3957 const text = new TextDecoder().decode(input.body);
58 const sort = sortColumn(text, input.path, input.sort);
59 const keep = sort === undefined ? want : ROW_CAP;
4060 const lines = isJsonlPath(input.path);
4161 const parsed = lines
42 ? parseJsonl(text, want, COLUMN_CAP)
43 : parseCsv(text, { rows: want + 1, columns: COLUMN_CAP });
62 ? parseJsonl(text, keep, COLUMN_CAP)
63 : parseCsv(text, { rows: keep + 1, columns: COLUMN_CAP });
4464 if ("error" in parsed) return { content: html``, lines: 0, fallback: parsed.error };
4565 const total = parsed.records - 1;
4666 const width = Math.min(parsed.columns, COLUMN_CAP);
4767 const numeric = numericColumns(parsed, width);
4868 // A wide table shrinks its row window so the page stays bounded.
4969 const shown = Math.min(total, want, Math.floor(CELL_CAP / width));
50 const [head, ...rest] = parsed.rows;
70 const [head, ...unsorted] = parsed.rows;
71 // `?sort=` must name a header cell exactly; anything else is ignored.
72 const column = sort === undefined ? -1 : head!.slice(0, width).indexOf(sort);
73 const descending = column !== -1 && input.dir === "desc";
74 const rest = column === -1 ? unsorted : sortRows(unsorted, column, numeric[column] === true, descending);
5175 let body = "";
5276 for (let r = 0; r < shown; r++) body += `<tr>${cells("td", rest[r]!, width, numeric)}</tr>`;
5377 const href = input.pathHref ?? "";
78 const heads = head!
79 .slice(0, width)
80 .map((v, c) => {
81 const on = c === column;
82 const next = on && !descending ? "desc" : "asc";
83 const to = `${href}?sort=${encodeURIComponent(v)}&dir=${next}${input.all ? "&all=1" : ""}`;
84 const aria = on ? ` aria-sort="${descending ? "descending" : "ascending"}"` : "";
85 return `<th${numeric[c] ? ' class="num"' : ""}${aria}><a href="${escapeHtml(to)}">${escapeHtml(v)}</a></th>`;
86 })
87 .join("") + (head!.length < width ? "<th></th>".repeat(width - head!.length) : "");
5488 const wide =
5589 parsed.columns > width
5690 ? `Showing ${width.toLocaleString()} of ${parsed.columns.toLocaleString()} columns. `
@@ -64,7 +98,7 @@ export function csv(input: RenderInput): Rendered {
6498 shown >= total
6599 ? ""
66100 : !input.all && shown === Math.min(total, want)
67 ? `<a id="csv-all" href="${escapeHtml(href)}?all=1">Show all ${total.toLocaleString()} rows</a>${
101 ? `<a id="csv-all" href="${escapeHtml(`${href}?${column === -1 ? "" : `sort=${encodeURIComponent(sort!)}&dir=${descending ? "desc" : "asc"}&`}all=1`)}">Show all ${total.toLocaleString()} rows</a>${
68102 total > ROW_CAP ? ` (the page draws at most ${ROW_CAP.toLocaleString()})` : ""
69103 }`
70104 : "The rest is in the raw file."
@@ -75,8 +109,8 @@ export function csv(input: RenderInput): Rendered {
75109 <span class="muted small num" data-csv-count aria-live="polite"></span>
76110 </div>
77111 <div class="csv-scroll">
78 <table class="table" data-dense data-sticky-lead aria-label="${input.path.slice(input.path.lastIndexOf("/") + 1)} data">
79 <thead><tr>${raw(cells("th", head!, width, numeric))}</tr></thead>
112 <table class="table" data-dense data-sortable data-sticky-lead aria-label="${input.path.slice(input.path.lastIndexOf("/") + 1)} data">
113 <thead><tr>${raw(heads)}</tr></thead>
80114 <tbody>${raw(body)}</tbody>
81115 </table>
82116 </div>
@@ -93,7 +127,7 @@ export function csv(input: RenderInput): Rendered {
93127 export const isCsvPath = (path: string): boolean => /\.(csv|tsv|jsonl|ndjson)$/i.test(path);
94128 const isJsonlPath = (path: string): boolean => /\.(jsonl|ndjson)$/i.test(path);
95129
96 /** Sort on a header click, filter on typing. Trusted static code. */
130 /** Filter on typing (sorting is the server's: a header is a link). Trusted static code. */
97131 export const csvJs = /* js */ `
98132 (function () {
99133 var doc = document;
@@ -102,40 +136,6 @@ export const csvJs = /* js */ `
102136 box.__csv = 1;
103137 var tools = box.querySelector('.csv-tools');
104138 if (tools) tools.hidden = false;
105 var heads = box.querySelectorAll('thead th');
106 for (var i = 0; i < heads.length; i++) {
107 var th = heads[i];
108 var btn = doc.createElement('button');
109 btn.type = 'button';
110 btn.className = 'csv-sort';
111 while (th.firstChild) btn.appendChild(th.firstChild);
112 th.appendChild(btn);
113 th.setAttribute('aria-sort', 'none');
114 }
115 }
116 function sort(th) {
117 var box = th.closest('[data-csv]');
118 var tbody = box.querySelector('tbody');
119 var idx = Array.prototype.indexOf.call(th.parentNode.children, th);
120 var num = th.classList.contains('num');
121 var dir = th.getAttribute('aria-sort') === 'ascending' ? -1 : 1;
122 var all = th.parentNode.children;
123 for (var k = 0; k < all.length; k++) all[k].setAttribute('aria-sort', 'none');
124 th.setAttribute('aria-sort', dir === 1 ? 'ascending' : 'descending');
125 var rows = Array.prototype.slice.call(tbody.rows);
126 var keys = rows.map(function (r) {
127 var t = r.cells[idx] ? r.cells[idx].textContent : '';
128 return num ? (t.trim() === '' ? null : parseFloat(t)) : t.toLowerCase();
129 });
130 var order = rows.map(function (_, j) { return j; });
131 order.sort(function (a, b) {
132 var x = keys[a], y = keys[b];
133 if (x === null || y === null) return x === y ? a - b : x === null ? 1 : -1;
134 return (x < y ? -1 : x > y ? 1 : a - b) * dir;
135 });
136 var frag = doc.createDocumentFragment();
137 for (var m = 0; m < order.length; m++) frag.appendChild(rows[order[m]]);
138 tbody.appendChild(frag);
139139 }
140140 function filter(input) {
141141 var box = input.closest('[data-csv]');
@@ -154,10 +154,6 @@ export const csvJs = /* js */ `
154154 var boxes = doc.querySelectorAll('[data-csv]');
155155 for (var i = 0; i < boxes.length; i++) setup(boxes[i]);
156156 }
157 doc.addEventListener('click', function (e) {
158 var b = e.target.closest && e.target.closest('[data-csv] .csv-sort');
159 if (b) sort(b.parentNode);
160 });
161157 doc.addEventListener('input', function (e) {
162158 if (e.target.matches && e.target.matches('[data-csv-filter]')) filter(e.target);
163159 });
src/web/render/renderers.ts +6
@@ -79,6 +79,12 @@ export interface RenderInput {
7979 readonly exists?: (path: string) => boolean;
8080 /** `?all=1`: draw past the default window, up to the renderer's cap. */
8181 readonly all?: boolean;
82 /**
83 * `?sort=<column>&dir=asc|desc`: unchecked text; the table renderer matches it against its
84 * header.
85 */
86 readonly sort?: string;
87 readonly dir?: string;
8288 /** `?table=<name>`: unchecked text; a renderer matches it against its own list. */
8389 readonly table?: string;
8490 /** `?page=N` (1-based) with `?table=`: the renderer clamps it to the pages the table has. */
src/web/style/content.ts +5 −7
@@ -467,15 +467,13 @@ form.inline { display: inline; margin-left: auto; }
467467 .csv-tools .input { max-width: 260px; }
468468 .csv-scroll { overflow: auto; max-height: 75vh; scrollbar-width: thin; }
469469 .csv-view th { position: sticky; top: 0; z-index: 2; background: var(--card); user-select: none; }
470 .csv-sort {
471 font: inherit; color: inherit; background: none; border: 0; padding: 0;
472 text-align: inherit; cursor: pointer;
473 }
474 .csv-sort:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 2px; }
470 /* A header is the sortable table's link (components.ts draws its arrow), and the link is the whole
471 cell: a thumb on the cell sorts, not only one on the word. */
outdated

Hand-rolled .csv-sort where the design system already has .table[data-sortable] th[aria-sort] a (components.ts:646). Mark the table data-sortable and reuse it; drop this class and the duplicate arrows at 477-478. Also padding is 0: give the link the full cell so a thumb can hit it on a 360px phone.

472 .csv-view .table[data-sortable] th > a { text-decoration: none; }
473 .csv-view .table[data-sortable] th > a::before { content: ""; position: absolute; inset: 0; }
474 @media (pointer: coarse) { .csv-view .table[data-sortable] th { height: 44px; } }
475475 .csv-view th:first-child { z-index: 3; }
476476 .csv-view td { max-width: 40ch; overflow-wrap: anywhere; }
477 .csv-view th[aria-sort="ascending"]::after { content: " \\25B2"; font-size: 12.5px; }
478 .csv-view th[aria-sort="descending"]::after { content: " \\25BC"; font-size: 12.5px; }
479477 .csv-view .num { text-align: right; }
480478 .csv-more { padding: 10px 12px; margin: 0; }
481479
test/csv-sort-page.test.ts added +72
@@ -0,0 +1,72 @@
1 /**
2 * A CSV's sort on the blob page: the query reaches the renderer, and a bad one costs nothing extra
3 * and is kept like none.
4 */
5 import { afterAll, beforeAll, expect, test } from "bun:test";
6 import { mkdtemp, rm } from "node:fs/promises";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { createApp, type App } from "../src/app.ts";
10 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
11 import { getRepo } from "../src/core/repos.ts";
12 import { createUser } from "../src/core/users.ts";
13 import { serve, type Servers } from "../src/http/server.ts";
14 import { clearTreeCache, treeCacheStats } from "../src/web/pages/tree_cache.ts";
15
16 let dataDir: string;
17 let app: App;
18 let servers: Servers;
19 let sha = "";
20
21 const BROWSER = "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 Chrome/130.0 Safari/537.36";
22 const get = (path: string, ip: string) =>
23 fetch(`${servers.url}${path}`, { headers: { "user-agent": BROWSER, "x-forwarded-for": ip, "accept-encoding": "gzip" }, redirect: "manual" });
24 const names = (html: string) => [...html.matchAll(/<td>([a-z])<\/td>/g)].map((m) => m[1]).join("");
25
26 beforeAll(async () => {
27 dataDir = await mkdtemp(join(tmpdir(), "forge-csv-sort-"));
28 app = createApp({ dataDir, port: 0 });
29 servers = await serve(app);
30 const u = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", isAdmin: true });
31 if (!u.ok) throw new Error(u.error);
32 const r = await createRepo(app, u.value, { name: "open", visibility: "public" });
33 if (!r.ok) throw new Error(String(r.error));
34 const c = await commitToBranch(app, u.value, getRepo(app.db, "alice", "open")!, {
35 message: "Start",
36 files: [{ path: "t.csv", content: "k,n\nb,2\na,3\nc,1\n" }],
37 });
38 if (!c.ok) throw new Error(String(c.error));
39 sha = Bun.spawnSync(["git", "--git-dir", join(dataDir, "repos", "alice", "open.git"), "rev-parse", "HEAD"]).stdout.toString().trim();
40 });
41
42 afterAll(async () => {
43 servers.stop();
44 app.close();
45 await rm(dataDir, { recursive: true, force: true });
46 });
47
48 test("?sort= and ?dir= reach the table on the blob page; a bad direction is ascending", async () => {
49 const base = `/alice/open/blob/${sha}/t.csv`;
50 expect(names(await (await get(`${base}?sort=n&dir=asc`, "10.9.0.1")).text())).toBe("cba");
51 expect(names(await (await get(`${base}?sort=n&dir=desc`, "10.9.0.1")).text())).toBe("abc");
52 expect(names(await (await get(`${base}?sort=n&dir=sideways`, "10.9.0.1")).text())).toBe("cba");
53 expect(names(await (await get(base, "10.9.0.1")).text())).toBe("bac");
54 });
55
56 test("a ?sort= naming no column is the unsorted page: it adds no page to the cache and is served from the unsorted one", async () => {
57 clearTreeCache();
58 const base = `/alice/open/blob/${sha}/t.csv`;
59 expect(names(await (await get(base, "10.9.1.1")).text())).toBe("bac");
60 const before = treeCacheStats();
61 for (let i = 0; i < 20; i++) {
62 const res = await get(`${base}?sort=nope${i}&dir=desc`, "10.9.1.2");
63 expect(res.status).toBe(200);
64 const html = await res.text();
65 expect(names(html)).toBe("bac");
66 expect(html).not.toContain("aria-sort");
67 }
68 expect(treeCacheStats().entries).toBe(before.entries);
69 // a real column is kept once, under its own key
70 await get(`${base}?sort=k&dir=asc`, "10.9.1.3");
71 expect(treeCacheStats().entries).toBe(before.entries + 1);
72 });
test/render-csv.test.ts +100 −8
@@ -1,12 +1,14 @@
11 /** CSV/TSV as a table: the parser, the shape of the page, and the budget. */
22 import { describe, expect, test } from "bun:test";
33 import { parseCsv, numericColumns, type Table } from "../src/web/render/csv-parse.ts";
4 import { COLUMN_CAP, csv, csvJs, isCsvPath, ROW_CAP } from "../src/web/render/csv.ts";
4 import { COLUMN_CAP, csv, csvJs, isCsvPath, ROW_CAP, sortColumn, sortRows } from "../src/web/render/csv.ts";
55 import { parseJsonl } from "../src/web/render/jsonl-parse.ts";
66 import { fileKind } from "../src/web/render/kinds.ts";
77 import { renderBlob } from "../src/web/pages/repo.ts";
88 import type { RepoScope } from "../src/web/scope.ts";
99 import { RENDERERS } from "../src/web/render/renderers.ts";
10 import { components as componentsCss } from "../src/web/style/components.ts";
11 import { content as contentCss } from "../src/web/style/content.ts";
1012
1113 const enc = (s: string): Uint8Array => new TextEncoder().encode(s);
1214 const ok = (s: string) => {
@@ -72,7 +74,7 @@ describe("csv page", () => {
7274 const r = draw("name,n\n<b>,1\nc,22\n");
7375 const h = String(r.content);
7476 expect(r.summary).toBe("2 rows × 2 columns · comma-separated · 18 B");
75 expect(h).toContain("<thead><tr><th>name</th><th class=\"num\">n</th>");
77 expect(h).toMatch(/<thead><tr><th><a [^>]*>name<\/a><\/th><th class="num"><a [^>]*>n<\/a><\/th>/);
7678 expect(h).toContain("<td>&lt;b&gt;</td>");
7779 expect(h).toContain('<td class="num">22</td>');
7880 expect(h).toContain("data-csv-filter");
@@ -91,17 +93,59 @@ describe("csv page", () => {
9193 test("a malformed file falls back with a reason", () => {
9294 expect(draw('a,b\n"x').fallback).toBe("A quoted field is never closed.");
9395 });
94 test("registered, and the script sorts and filters", () => {
96 test("registered, and the script filters", () => {
9597 expect(fileKind("x.tsv", enc("a\tb\n"))).toBe("csv");
9698 expect(RENDERERS.csv).toBe(csv);
97 expect(csvJs).toContain("aria-sort");
98 expect(csvJs).toContain("csv-sort");
99 expect(csvJs).not.toContain("csv-sort");
99100 expect(csvJs).not.toContain("MutationObserver");
100101 expect(csvJs).not.toContain("style.cursor");
101102 expect(csvJs).toContain("data-csv-filter");
102103 });
103104 });
104105
106 describe("csv sorts on the server", () => {
107 const text = "name,n\nb,10\nA,9\nc,\nd,100\n";
108 const col = (html: string, c: number) =>
109 [...html.matchAll(/<tr>(.*?)<\/tr>/g)].slice(1).map((m) => [...m[1]!.matchAll(/<td[^>]*>(.*?)<\/td>/g)][c]![1]);
110 const sorted = (sort?: string, dir?: string) =>
111 csv({ path: "d.csv", size: 1, body: enc(text), media: "/m", download: "/d", pathHref: "/o/r/blob/main/d.csv", ...(sort ? { sort } : {}), ...(dir ? { dir } : {}) });
112 const rows = (sort?: string, dir?: string, c = 0) => col(String(sorted(sort, dir).content), c);
113 test("numbers sort by value, empties last either way", () => {
114 expect(rows("n", "asc", 1)).toEqual(["9", "10", "100", ""]);
115 expect(rows("n", "desc", 1)).toEqual(["100", "10", "9", ""]);
116 });
117 test("text sorts case-insensitively", () => {
118 expect(rows("name", "asc")).toEqual(["A", "b", "c", "d"]);
119 expect(rows("name", "desc")).toEqual(["d", "c", "b", "A"]);
120 });
121 test("an unknown column or no sort leaves file order and marks nothing", () => {
122 for (const bad of ["nope", "__proto__", "", "N"]) {
123 const out = String(sorted(bad, "asc").content);
124 expect(col(out, 0)).toEqual(["b", "A", "c", "d"]);
125 expect(out).not.toContain("aria-sort");
126 }
127 });
128 test("the sorted header carries its arrow state and every header is a link", () => {
129 const out = String(sorted("n", "asc").content);
130 expect(out).toContain('aria-sort="ascending"');
131 expect(out).toContain('href="/o/r/blob/main/d.csv?sort=n&amp;dir=desc"');
132 expect(out).toContain('href="/o/r/blob/main/d.csv?sort=name&amp;dir=asc"');
133 expect(String(sorted("n", "desc").content)).toContain('aria-sort="descending"');
134 });
135 test("the header is the design system's sortable table, and its link fills the cell", () => {
136 const out = String(sorted("n", "asc").content);
137 expect(out).toMatch(/<table class="table"[^>]* data-sortable/);
138 expect(out).not.toContain("csv-sort");
139 expect(componentsCss).toContain('.table[data-sortable] th[aria-sort="ascending"] a::after');
140 expect(contentCss).not.toContain(".csv-sort");
141 expect(contentCss).not.toMatch(/\.csv-view th\[aria-sort[^\]]*\]::after/);
142 // The sticky cell is the link's box: its overlay covers the cell, 44px tall under a thumb.
143 expect(contentCss).toContain(".csv-view th { position: sticky;");
144 expect(contentCss).toContain(".csv-view .table[data-sortable] th > a::before { content: \"\"; position: absolute; inset: 0; }");
145 expect(contentCss).toContain("@media (pointer: coarse) { .csv-view .table[data-sortable] th { height: 44px; } }");
146 });
147 });
148
105149 describe("csv on the blob page", () => {
106150 const scope = { base: "/o/r", facts: { name: "r" }, caps: { write: false }, kind: "local" } as unknown as RepoScope;
107151 const page = (name: string, text: string, extra: { view?: "source"; all?: boolean } = {}) => {
@@ -163,7 +207,7 @@ describe("json lines as a table", () => {
163207 test("mixed keys in first-seen order, nested as compact JSON, a bad line as a row, script escaped", () => {
164208 const r = lines(text);
165209 const out = String(r.content);
166 expect(out).toMatch(/<th>a<\/th><th>b<\/th><th>c<\/th>/);
210 expect(out).toMatch(/<th><a [^>]*>a<\/a><\/th><th><a [^>]*>b<\/a><\/th><th><a [^>]*>c<\/a><\/th>/);
167211 expect(out).toContain("{&quot;k&quot;:[1,2]}");
168212 expect(out).toContain("Line 3: not valid JSON");
169213 expect(out).toContain("&lt;script&gt;alert(1)&lt;/script&gt;");
@@ -177,11 +221,11 @@ describe("json lines as a table", () => {
177221 test("a key first seen after the 1,000th object is not a column; huge key sets are capped", () => {
178222 const rows = Array.from({ length: 1_000 }, (_, i) => `{"a":${i}}`);
179223 const out = String(lines([...rows, '{"late":1}'].join("\n")).content);
180 expect(out).toContain(">a</th>");
224 expect(out).toContain(">a</a></th>");
181225 expect(out).not.toContain("late");
182226 const wide = lines(JSON.stringify(Object.fromEntries(Array.from({ length: COLUMN_CAP + 5 }, (_, i) => [`k${i}`, i]))));
183227 expect(String(wide.content)).toContain(`Showing ${COLUMN_CAP} of ${COLUMN_CAP + 5} columns`);
184 expect(String(wide.content)).not.toContain(`<th>k${COLUMN_CAP}</th>`);
228 expect(String(wide.content)).not.toContain(`>k${COLUMN_CAP}</a></th>`);
185229 });
186230 test("a missing key is an empty cell, never an inherited property", () => {
187231 const p = parseJsonl('{"constructor":1,"toString":2,"__proto__":3}\n{"a":1}\n', 10);
@@ -191,3 +235,51 @@ describe("json lines as a table", () => {
191235 expect(p.rows[2]).toEqual(["", "", "", "1"]);
192236 });
193237 });
238
239 describe("csv sort: links, windows and bad input", () => {
240 const big = "id,n\n" + Array.from({ length: 1_500 }, (_, i) => `${i},${1_500 - i}`).join("\n");
241 const make = (extra: Record<string, unknown>) =>
242 String(csv({ path: "d.csv", size: 1, body: enc(big), media: "/m", download: "/d", pathHref: "/o/r/blob/main/d.csv", ...extra }).content);
243 test("Show all keeps the sort", () => {
244 const out = make({ sort: "n", dir: "desc" });
245 expect(out).toContain('href="/o/r/blob/main/d.csv?sort=n&amp;dir=desc&amp;all=1">Show all 1,500 rows');
246 expect(make({})).toContain('href="/o/r/blob/main/d.csv?all=1">Show all');
247 expect(make({ sort: "nope" })).toContain('href="/o/r/blob/main/d.csv?all=1">Show all');
248 });
249 test("all=1 rides on every header link", () => {
250 const out = make({ all: true, sort: "n", dir: "asc" });
251 expect(out).toContain('href="/o/r/blob/main/d.csv?sort=id&amp;dir=asc&amp;all=1"');
252 expect(out).toContain('href="/o/r/blob/main/d.csv?sort=n&amp;dir=desc&amp;all=1"');
253 });
254 test("sort sees every row, not just the window", () => {
255 const out = make({ sort: "n", dir: "asc" });
256 expect(out).toContain('<td class="num">1499</td><td class="num">1</td>');
257 });
258 test("a sort that names no column resolves to nothing, so it reads the default window", () => {
259 const body = big;
260 expect(sortColumn(body, "d.csv", "n")).toBe("n");
261 for (const bad of ["nope", "N", "__proto__", "", "constructor"]) expect(sortColumn(body, "d.csv", bad)).toBeUndefined();
262 expect(sortColumn(body, "d.csv", undefined)).toBeUndefined();
263 expect(sortColumn('{"a":1}\n', "d.jsonl", "a")).toBe("a");
264 expect(sortColumn('a,b\n"x', "d.csv", "a")).toBeUndefined();
265 });
266 test("a direction that is neither asc nor desc is ascending", () => {
267 expect(make({ sort: "n", dir: "sideways" })).toContain('aria-sort="ascending"');
268 });
269 test("a cell that is no number sorts last, like an empty one, either way", () => {
270 const rows = [["a", "3"], ["b", "oops"], ["c", "1"], ["d", ""]];
271 expect(sortRows(rows, 1, true, false).map((r) => r[0])).toEqual(["c", "a", "b", "d"]);
272 expect(sortRows(rows, 1, true, true).map((r) => r[0])).toEqual(["a", "c", "b", "d"]);
273 });
274 test("the sort reaches the page through renderBlob, and a bad direction is ascending", () => {
275 const scope = { base: "/o/r", facts: { name: "r" }, caps: { write: false }, kind: "local" } as unknown as RepoScope;
276 const body = enc("k,n\nb,2\na,1\n");
277 const draw = (extra: { sort?: string; dir?: string }) =>
278 String(renderBlob(scope, { refs: { branches: ["main"], tags: [] }, ref: "main", path: "s.csv", blob: { kind: "text", body, size: body.length }, ...extra }));
279 const sorted = draw({ sort: "k", dir: "desc" });
280 expect(sorted).toContain('aria-sort="descending"');
281 expect(sorted.indexOf("<td>b</td>")).toBeLessThan(sorted.indexOf("<td>a</td>"));
282 expect(draw({ sort: "k", dir: "x" })).toContain('aria-sort="ascending"');
283 expect(draw({})).not.toContain("aria-sort");
284 });
285 });
test/tui-workspace-formats.test.ts +37
@@ -77,6 +77,43 @@ describe("the terminal workspace, by file type", () => {
7777 }
7878 });
7979
80 test("o orders a .csv by each column in turn, numbers by value, and back to file order", async () => {
81 const made = await createRepo(forge, alice, { name: "sorted", visibility: "public", description: "order" });
82 if (!made.ok) throw new Error("repo failed");
83 await commitToBranch(forge, alice, made.value, {
84 message: "seed",
85 files: [{ path: "n.csv", content: "name,n\nb,10\nA,9\nc,\nd,100\n" }],
86 });
87 const h = boot();
88 try {
89 await h.settle();
90 await open(h, "sorted");
91 await openFile(h, "n.csv");
92 const order = () => {
93 return h.text().split("\n").map((l) => /(?:^|\s)(A|b|c|d)\s+│/.exec(l)?.[1] ?? "").join("");
94 };
95 expect(order()).toBe("bAcd");
96 await h.press("o");
97 expect(h.text()).toContain("name ▲");
98 expect(order()).toBe("Abcd");
99 await h.press("o");
100 expect(h.text()).toContain("name ▼");
101 expect(order()).toBe("dcbA");
102 await h.press("o");
103 expect(h.text()).toContain("n ▲");
104 // 9, 10, 100 by value; the empty cell goes last
105 expect(order()).toBe("Abdc");
106 await h.press("o");
107 expect(order()).toBe("dbAc");
108 await h.press("o");
109 expect(order()).toBe("bAcd");
110 expect(h.text()).not.toContain("▲");
111 expect(h.text()).not.toContain("▼");
112 } finally {
113 h.stop();
114 }
115 });
116
80117 test("a .html file opens as its source: the page preview is a web-only door", async () => {
81118 const made = await createRepo(forge, alice, { name: "pages", visibility: "public", description: "pages" });
82119 if (!made.ok) throw new Error("repo failed");

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs