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

Parked opened this
Closed without merging

mason/forge:mason/6783-a-source-file-over-1-500-lines-shows-its → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
3 files changed +54 −30
src/web/pages/repo.ts +9 −19
@@ -98,7 +98,7 @@ const MAX_RENDER_BYTES = 1_000_000;
9898 // A blob under the byte cap can still be pathological (a megabyte of newlines
9999 // is ~490k lines). Bounding rendered lines bounds the response independently.
100100 /** 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;
102102 const MAX_RENDER_LINES = 10_000;
103103
104104 import { sshCloneUrl } from "../../ssh/command.ts";
@@ -677,25 +677,15 @@ export function blobBody(
677677 )}
678678 </table>
679679 </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
688685 ? 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>`,
699689 )}`,
700690 };
701691 }
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. */
22 import { describe, expect, test } from "bun:test";
33 import { blobBody } from "../src/web/pages/repo.ts";
44 import { highlightPrefix } from "../src/web/code.ts";
@@ -16,13 +16,13 @@ const draw = (all = false) =>
1616 const rows = (h: string) => h.split('<tr id="L').length - 1;
1717
1818 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", () => {
2020 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");
2323 expect(page).toContain("/alice/app/blob/main/src/big.ts?all=1");
2424 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);
2626 expect(page).toContain('id="code-cut"');
2727 });
2828
@@ -33,16 +33,16 @@ describe("long file page", () => {
3333 });
3434
3535 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),
3838 );
3939 });
4040
4141 test("the prefix highlights like the whole file's first lines", () => {
4242 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]));
4646 });
4747
4848 test("an anchor past the cut is sent to the page that has the line", () => {
@@ -52,6 +52,6 @@ describe("long file page", () => {
5252 });
5353
5454 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");
5656 });
5757 });
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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs