Public: anyone can read this; the crew can change it.
A source file over 1,500 lines shows its first 1,500, then "N more lines" with links to the whole file and the raw file #7698
mason/forge:mason/6783-a-source-file-over-1-500-lines-shows-its → main 2 commits fork
3 files
changed +54
−30
src/web/pages/repo.ts +9 −19
| @@ -98,7 +98,7 @@ const MAX_RENDER_BYTES = 1_000_000; | ||
| 98 | 98 | // A blob under the byte cap can still be pathological (a megabyte of newlines |
| 99 | 99 | // is ~490k lines). Bounding rendered lines bounds the response independently. |
| 100 | 100 | /** Lines a file page draws before it links the rest (`?all=1` draws every one). */ |
| 101 | const FIRST_LINES = 1_000; | |
| 101 | const FIRST_LINES = 1_500; | |
| 102 | 102 | const MAX_RENDER_LINES = 10_000; |
| 103 | 103 | |
| 104 | 104 | import { sshCloneUrl } from "../../ssh/command.ts"; |
| @@ -677,25 +677,15 @@ export function blobBody( | ||
| 677 | 677 | )} |
| 678 | 678 | </table> |
| 679 | 679 | </div> |
| 680 | ${when(truncated, () => | |
| 681 | empty({ | |
| 682 | icon: "file", | |
| 683 | title: `Showing ${lines.length.toLocaleString()} of ${total.toLocaleString()} lines`, | |
| 684 | note: all | |
| 685 | ? "The rest is in the raw file." | |
| 686 | : "The page draws the first lines so it opens at once. A link to a later line (#L…) lands once you show all lines.", | |
| 687 | action: html`${all | |
| 680 | ${when( | |
| 681 | truncated, | |
| 682 | () => | |
| 683 | html`<p class="muted small">${(total - lines.length).toLocaleString("en-US")} more lines | |
| 684 | ${all | |
| 688 | 685 | ? html`` |
| 689 | : btn("Show all lines", { | |
| 690 | href: `${fileHref}?all=1`, | |
| 691 | id: "code-cut", | |
| 692 | icon: "file", | |
| 693 | })} | |
| 694 | ${btn("View raw", { | |
| 695 | href: `${base}/raw/${encoded}/${path}`, | |
| 696 | variant: "outline", | |
| 697 | })}`, | |
| 698 | }), | |
| 686 | : html`· <a id="code-cut" href="${fileHref}?all=1">Show all lines</a> | |
| 687 | (a link to a later line, #L…, lands once you do) ·`} | |
| 688 | <a href="${base}/raw/${encoded}/${path}">View raw</a></p>`, | |
| 699 | 689 | )}`, |
| 700 | 690 | }; |
| 701 | 691 | } |
test/blob-long-file.test.ts +11 −11
| @@ -1,4 +1,4 @@ | ||
| 1 | /** A long file's page draws its first 1,000 lines and links the rest. */ | |
| 1 | /** A long file's page draws its first 1,500 lines and links the rest. */ | |
| 2 | 2 | import { describe, expect, test } from "bun:test"; |
| 3 | 3 | import { blobBody } from "../src/web/pages/repo.ts"; |
| 4 | 4 | import { highlightPrefix } from "../src/web/code.ts"; |
| @@ -16,13 +16,13 @@ const draw = (all = false) => | ||
| 16 | 16 | const rows = (h: string) => h.split('<tr id="L').length - 1; |
| 17 | 17 | |
| 18 | 18 | describe("long file page", () => { |
| 19 | test("default draws 1,000 of 7,000 lines, small and fast", () => { | |
| 19 | test("default draws 1,500 of 7,000 lines, small and fast", () => { | |
| 20 | 20 | const page = String(draw().content); |
| 21 | expect(rows(page)).toBe(1_000); | |
| 22 | expect(page).toContain("Showing 1,000 of 7,000 lines"); | |
| 21 | expect(rows(page)).toBe(1_500); | |
| 22 | expect(page).toContain("5,500 more lines"); | |
| 23 | 23 | expect(page).toContain("/alice/app/blob/main/src/big.ts?all=1"); |
| 24 | 24 | expect(page).toContain("/alice/app/raw/main/src/big.ts"); |
| 25 | expect(new TextEncoder().encode(page).length).toBeLessThan(400_000); | |
| 25 | expect(new TextEncoder().encode(page).length).toBeLessThan(600_000); | |
| 26 | 26 | expect(page).toContain('id="code-cut"'); |
| 27 | 27 | }); |
| 28 | 28 | |
| @@ -33,16 +33,16 @@ describe("long file page", () => { | ||
| 33 | 33 | }); |
| 34 | 34 | |
| 35 | 35 | test("a repeat view returns the cached highlight", () => { |
| 36 | expect(highlightPrefix(source, "src/big.ts", 1_000)).toBe( | |
| 37 | highlightPrefix(source, "src/big.ts", 1_000), | |
| 36 | expect(highlightPrefix(source, "src/big.ts", 1_500)).toBe( | |
| 37 | highlightPrefix(source, "src/big.ts", 1_500), | |
| 38 | 38 | ); |
| 39 | 39 | }); |
| 40 | 40 | |
| 41 | 41 | test("the prefix highlights like the whole file's first lines", () => { |
| 42 | 42 | const whole = highlightPrefix(source, "src/big.ts", 7_000); |
| 43 | const part = highlightPrefix(source, "src/big.ts", 1_000); | |
| 44 | expect(part.length).toBe(1_000); | |
| 45 | expect(String(part[999])).toBe(String(whole[999])); | |
| 43 | const part = highlightPrefix(source, "src/big.ts", 1_500); | |
| 44 | expect(part.length).toBe(1_500); | |
| 45 | expect(String(part[1499])).toBe(String(whole[1499])); | |
| 46 | 46 | }); |
| 47 | 47 | |
| 48 | 48 | test("an anchor past the cut is sent to the page that has the line", () => { |
| @@ -52,6 +52,6 @@ describe("long file page", () => { | ||
| 52 | 52 | }); |
| 53 | 53 | |
| 54 | 54 | test("without script the notice says where a later line is", () => { |
| 55 | expect(String(draw().content)).toContain("lands once you show all lines"); | |
| 55 | expect(String(draw().content)).toContain("lands once you do"); | |
| 56 | 56 | }); |
| 57 | 57 | }); |
test/blob-window.test.ts added +34
| @@ -0,0 +1,34 @@ | ||
| 1 | /** A file over 1,500 lines draws the first 1,500, then "N more lines". */ | |
| 2 | import { describe, expect, test } from "bun:test"; | |
| 3 | import { blobBody } from "../src/web/pages/repo.ts"; | |
| 4 | import type { Blob } from "../src/view/source.ts"; | |
| 5 | ||
| 6 | const file = (n: number): Blob => { | |
| 7 | const body = new TextEncoder().encode( | |
| 8 | Array.from({ length: n }, (_, i) => `const v${i} = ${i};`).join("\n"), | |
| 9 | ); | |
| 10 | return { kind: "text", body, size: body.length }; | |
| 11 | }; | |
| 12 | const draw = (n: number, all = false) => | |
| 13 | String(blobBody("/a/app", "main", "x.ts", file(n), undefined, all).content); | |
| 14 | const rows = (h: string) => h.split('<tr id="L').length - 1; | |
| 15 | ||
| 16 | describe("blob window", () => { | |
| 17 | test("5,000 lines: 1,500 rows, a link, under 500 KB", () => { | |
| 18 | const page = draw(5_000); | |
| 19 | expect(rows(page)).toBe(1_500); | |
| 20 | expect(page).toContain("3,500 more lines"); | |
| 21 | expect(page).toContain("/a/app/blob/main/x.ts?all=1"); | |
| 22 | expect(page).toContain("/a/app/raw/main/x.ts"); | |
| 23 | expect(new TextEncoder().encode(page).length).toBeLessThan(500_000); | |
| 24 | }); | |
| 25 | test("?all=1 draws all 5,000", () => { | |
| 26 | expect(rows(draw(5_000, true))).toBe(5_000); | |
| 27 | }); | |
| 28 | test("1,500 lines have no link", () => { | |
| 29 | const page = draw(1_500); | |
| 30 | expect(rows(page)).toBe(1_500); | |
| 31 | expect(page).not.toContain("more lines"); | |
| 32 | expect(page).not.toContain("?all=1"); | |
| 33 | }); | |
| 34 | }); |
new above
offline