@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 07:15 UTC

main
ts 240 lines 7.94 KB
Raw
1 import type { AppId, Beat, SceneCapture } from "../../shared/types.ts";
2 import { highlight } from "./highlight.ts";
3 import { patchView, staticView, type PatchView } from "./patch.ts";
4 import { splitEdits } from "./split.ts";
5
6 /** Frame budget for each kind of beat, at `fps`. */
7 export const TIMING = {
8 /** Cmd-Tab: switcher overlay, then the target window comes forward. */
9 switch: 20,
10 switchOverlay: 13,
11 /** Frames the picked window takes to come forward after the switcher closes. */
12 raise: 3,
13 openTab: 14,
14 /** A patch: scroll to it and show removed lines in red, swap in the new lines, then hold in green. */
15 patch: { show: 12, change: 14, hold: 16 },
16 /** Removed lines collapse over this long, then added lines stream in one per `perLine`. */
17 patchDelete: 8,
18 patchPerLine: 2,
19 patchStreamMax: 60,
20 /** Base time on the architecture window, plus time per new node or edge. */
21 diagram: 90,
22 diagramPerAdded: 10,
23 diagramMax: 240,
24 browserUpdate: 30,
25 /** A section heading holds as a caption over the current window this long. */
26 slide: 30,
27 /** Pointer glides to the element, clicks (or the text lands), the page updates, then a short hold. */
28 browserAction: { move: 16, act: 8, hold: 14 },
29 /** Click the address bar, paste, press Enter. */
30 urlPaste: 14,
31 pageLoad: 18,
32 browserHold: 12,
33 } as const;
34
35 export interface Segment {
36 beat: Beat;
37 from: number;
38 duration: number;
39 /** The window in front during this segment. */
40 app: AppId;
41 /** The window in front before it; differs from `app` when the segment starts with a switch. */
42 previous: AppId;
43 /** Frames at the start spent switching windows (0 when already focused). */
44 switchFrames: number;
45 /** editor.patch / editor.edit / editor.open: the file as diff rows. */
46 view?: PatchView;
47 }
48
49 /** One press of → in the presenter: frames `[from, to)` of the scene. */
50 export interface Step {
51 title: string;
52 notes: string;
53 from: number;
54 to: number;
55 }
56
57 export interface SceneSchedule {
58 segments: Segment[];
59 steps: Step[];
60 /** The tabs open when the scene starts, as static views. */
61 initialViews: Record<string, PatchView>;
62 durationInFrames: number;
63 }
64
65 const appOf = (beat: Beat, current: AppId): AppId => {
66 switch (beat.kind) {
67 case "focus":
68 return beat.app;
69 case "editor.open":
70 case "editor.edit":
71 case "editor.patch":
72 case "editor.delete":
73 return "editor";
74 case "step":
75 case "caption":
76 return current;
77 case "diagram":
78 return "diagram";
79 // Section headings stay on the current window; the step's title is the caption.
80 case "slide":
81 return current;
82 case "browser.update":
83 case "browser.action":
84 return "browser";
85 case "terminal":
86 return "terminal";
87 case "browser":
88 return "browser";
89 case "pause":
90 return current;
91 }
92 };
93
94 /**
95 * How long a patch's change phase lasts: removed lines collapse, then added
96 * lines stream in one at a time, so long additions take a little longer.
97 */
98 export const patchChange = (rows: readonly { kind: string }[]) => {
99 const added = rows.filter((r) => r.kind === "add").length;
100 const removed = rows.some((r) => r.kind === "del");
101 const stream = Math.min(TIMING.patchStreamMax, added * TIMING.patchPerLine);
102 return Math.max(TIMING.patch.change, (removed ? TIMING.patchDelete : 0) + stream);
103 };
104
105 export const schedule = async (capture: SceneCapture, fps: number): Promise<SceneSchedule> => {
106 const segments: Segment[] = [];
107 let frame = 0;
108 // The scene opens on the first beat's window, without a switch.
109 const firstBeat = capture.beats.find(
110 (b) => b.kind !== "pause" && b.kind !== "step" && b.kind !== "caption" && b.kind !== "slide",
111 );
112 let app: AppId = firstBeat ? appOf(firstBeat, "editor") : "editor";
113 for (const beat of splitEdits(capture.beats)) {
114 const next = appOf(beat, app);
115 // Windows cut straight to the next one: no switch animation.
116 const switchFrames = 0;
117 const segment: Segment = {
118 beat,
119 from: frame,
120 duration: 0,
121 app: next,
122 previous: app,
123 switchFrames,
124 };
125 let work = 0;
126 switch (beat.kind) {
127 case "focus":
128 case "step":
129 case "caption":
130 break;
131 case "pause":
132 work = Math.round(beat.seconds * fps);
133 break;
134 case "editor.open":
135 segment.view = staticView(beat.content, await highlight(beat.file, beat.content));
136 work = TIMING.openTab;
137 break;
138 case "editor.delete":
139 work = TIMING.openTab;
140 break;
141 case "diagram":
142 work = Math.min(
143 TIMING.diagramMax,
144 TIMING.diagram +
145 TIMING.diagramPerAdded * (beat.addedNodes.length + beat.addedEdges.length),
146 );
147 break;
148 case "slide":
149 work = TIMING.slide;
150 break;
151 case "browser.action":
152 work = TIMING.browserAction.move + TIMING.browserAction.act + TIMING.browserAction.hold;
153 break;
154 case "browser.update":
155 work = TIMING.browserUpdate;
156 break;
157 case "editor.patch":
158 case "editor.edit":
159 segment.view = patchView(
160 beat.before,
161 beat.after,
162 await highlight(beat.file, beat.before),
163 await highlight(beat.file, beat.after),
164 );
165 work = TIMING.patch.show + patchChange(segment.view.rows) + TIMING.patch.hold;
166 break;
167 case "terminal":
168 work = Math.max(1, Math.round((beat.end - beat.start) * fps));
169 break;
170 case "browser":
171 work = TIMING.urlPaste + TIMING.pageLoad + TIMING.browserHold;
172 break;
173 }
174 segment.duration = switchFrames + work;
175 segments.push(segment);
176 frame += segment.duration;
177 app = next;
178 }
179 const initialViews: Record<string, PatchView> = {};
180 for (const tab of capture.start.tabs) {
181 initialViews[tab.file] = staticView(tab.content, await highlight(tab.file, tab.content));
182 }
183 const durationInFrames = Math.max(1, frame);
184
185 // Steps: the presenter stops before every step beat, every code edit, and every
186 // switch to another window, so nothing advances without a keypress. Empty ones
187 // (two stops at the same frame) collapse into the later one.
188 const marks: { title: string; notes: string; from: number }[] = [
189 { title: capture.title, notes: capture.notes, from: 0 },
190 ];
191 let title = capture.title;
192 let notes = capture.notes;
193 for (const segment of segments) {
194 const beat = segment.beat;
195 if (beat.kind === "step") {
196 title = beat.title;
197 notes = beat.notes;
198 }
199 const stop =
200 beat.kind === "step" ||
201 beat.kind === "editor.patch" ||
202 beat.kind === "editor.edit" ||
203 beat.kind === "editor.delete" ||
204 beat.kind === "terminal" ||
205 beat.kind === "browser" ||
206 beat.kind === "browser.update" ||
207 beat.kind === "browser.action" ||
208 (segment.app !== segment.previous && segment.duration > 0);
209 if (stop) {
210 const mark = { title, notes, from: segment.from };
211 if (marks.at(-1)!.from === mark.from) marks[marks.length - 1] = mark;
212 else marks.push(mark);
213 }
214 // Inside a long terminal run, also stop where output resumes after a pause.
215 if (beat.kind === "terminal") {
216 for (const at of capture.terminal?.pauses ?? []) {
217 if (at <= beat.start + 0.5 || at >= beat.end - 0.5) continue;
218 const from = segment.from + segment.switchFrames + Math.round((at - beat.start) * fps);
219 if (from > marks.at(-1)!.from) marks.push({ title, notes, from });
220 }
221 }
222 }
223 const steps: Step[] = marks
224 .map((mark, i) => ({
225 ...mark,
226 to: marks[i + 1]?.from ?? durationInFrames,
227 }))
228 .filter((step) => step.to > step.from);
229 return { segments, steps, initialViews, durationInFrames };
230 };
231
232 /** The segment playing at `frame` (the last one once the scene has ended). */
233 export const segmentAt = (segments: Segment[], frame: number): Segment | undefined => {
234 let found: Segment | undefined;
235 for (const segment of segments) {
236 if (segment.from <= frame) found = segment;
237 else break;
238 }
239 return found;
240 };