返回 CodeWhale
terminal-capture.tsx
根目录 / web / components / terminal-capture.tsx
1 import type { CSSProperties } from "react";
2 import type { TerminalCaptureFrame, TerminalCaptureStyle } from "@/lib/terminal-capture-types";
3
4 export type TerminalCaptureData = Pick<TerminalCaptureFrame, "file" | "cols"> & {
5 styles: readonly TerminalCaptureStyle[];
6 lines: readonly (readonly (readonly [string, number])[])[];
7 };
8
9 const BRAILLE = /[⠀-⣿]/;
10
11 function runStyle(style: TerminalCaptureStyle): CSSProperties {
12 return {
13 color: style.fg,
14 backgroundColor: style.bg,
15 fontWeight: style.bold ? 700 : undefined,
16 fontStyle: style.italic ? "italic" : undefined,
17 opacity: style.dim ? 0.6 : undefined,
18 textDecoration: style.underline ? "underline" : style.strike ? "line-through" : undefined,
19 };
20 }
21
22 /**
23 * A real terminal frame as live HTML text: the PTY cells the terminal
24 * emulator parsed (lib/terminal-captures/released, compacted by
25 * scripts/render-released-capture.mjs), drawn as styled runs in the system
26 * monospace font. Nothing is retouched or staged.
27 *
28 * The frame or a contiguous detail scales with the frame's width and the
29 * grid's column count, staying live, selectable text.
30 * Assistive technology gets one summary (`label`); the glyph-by-glyph grid
31 * is presentation. Braille cells (the whale mark) are pinned to one column
32 * each, because system monospace fonts draw them from a fallback font.
33 */
34 export function TerminalCapture({
35 label,
36 regionLabel,
37 rows,
38 data,
39 }: {
40 data: TerminalCaptureData;
41 label: string;
42 regionLabel: string;
43 rows?: readonly [number, number];
44 }) {
45 const start = rows ? Math.max(0, Math.min(data.lines.length - 1, rows[0])) : 0;
46 const end = rows ? Math.max(start + 1, Math.min(data.lines.length, rows[1])) : data.lines.length;
47 const lines = data.lines.slice(start, end);
48 const styles = data.styles;
49 // The padding around the grid continues the frame's own top and bottom rows.
50 const top = styles[lines[0][0][1]].bg;
51 const bottom = styles[lines[lines.length - 1][0][1]].bg;
52 return (
53 <div className="term-capture-scroll" dir="ltr" lang="en" role="region" aria-label={regionLabel} tabIndex={0}>
54 <pre
55 className="term-capture"
56 role="img"
57 aria-label={label}
58 data-capture-file={data.file}
59 data-capture-row-start={start + 1}
60 data-capture-row-end={end}
61 style={{ background: `linear-gradient(${top}, ${bottom})`, ["--cols" as string]: data.cols } as CSSProperties}
62 >
63 {lines.map((runs, row) => (
64 <span key={row} className="term-capture-row">
65 {runs.map(([text, index], run) => (
66 <span key={run} style={runStyle(styles[index])}>
67 {BRAILLE.test(text)
68 ? [...text].map((ch, i) =>
69 BRAILLE.test(ch) ? (
70 <span key={i} className="term-capture-cell">
71 {ch}
72 </span>
73 ) : (
74 ch
75 ),
76 )
77 : text}
78 </span>
79 ))}
80 </span>
81 ))}
82 </pre>
83 </div>
84 );
85 }
86
86 lines Plain Text