Files
hyperframes/packages/cli/src/utils/compositionFps.ts
T
James RussoandClaude Fable 5.2 8948e63cb7 fix(cli): show control characters as text in more lines that repeat a stored value (#8603)
* fix(cli): show control characters as text in more lines that repeat a stored value

Lines printed by the timeline edit commands, check, diff, preview --list,
telemetry status, the update and skills notices, the FFmpeg install hint, the
cloud render data-fps warning, the capture font summary, the project key notice
and the plain progress lines of a run with no terminal now pass the repeated
value through the text helpers, as the other commands' lines do.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* fix(cli): leave Windows line endings out of a planned diff

A file saved with CRLF line endings had every changed line of the
planned diff end in a visible \x0d. The line ending is now dropped
before the line is shown; a carriage return inside a line still shows.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-23 16:58:39 +00:00

73 lines
2.7 KiB
TypeScript

import { terminalText } from "@hyperframes/core/terminal-text";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { DISTRIBUTED_FPS, type DistributedFps, isDistributedFps } from "@hyperframes/core";
import { parseHTML } from "linkedom";
import { c } from "../ui/colors.js";
function getHtmlAttribute(element: Element, name: string): string | null {
// linkedom preserves spelling; HTML names match without case and keep the first duplicate.
return (
Array.from(element.attributes).find((attribute) => attribute.name.toLowerCase() === name)
?.value ?? null
);
}
/**
* Read a composition's declared frame rate from its root element's `data-fps`
* attribute — the same attribute the runtime honors (core/runtime/init.ts) — so
* `hyperframes render` can default to it instead of a hard-coded 30 when `--fps`
* is not passed. Returns the raw attribute string (for the caller to validate
* via `parseFps`, which supports fractional rates like `30000/1001`), or `null`
* when no root `data-fps` is present.
*
* Root resolution mirrors the runtime: prefer an explicit
* `[data-composition-id][data-root="true"]`, else the outermost
* `[data-composition-id]` (one with no `[data-composition-id]` ancestor).
*/
export function readCompositionFps(html: string): string | null {
let doc: Document;
try {
doc = parseHTML(html).document as unknown as Document;
} catch {
return null;
}
const compositions = Array.from(doc.querySelectorAll("*")).filter(
(element) => getHtmlAttribute(element, "data-composition-id") !== null,
);
const root =
compositions.find((element) => getHtmlAttribute(element, "data-root") === "true") ??
compositions[0];
const raw = root ? getHtmlAttribute(root, "data-fps")?.trim() : undefined;
return raw ? raw : null;
}
const DEFAULT_CLOUD_FPS: DistributedFps = 30;
/**
* Cloud render fps when `--fps` is not passed: `<projectDir>/index.html`'s
* data-fps if it is in `DISTRIBUTED_FPS`, otherwise 30, with a warning when the
* project declared a rate cloud renders cannot use.
*/
export function defaultCloudFps(projectDir: string): DistributedFps {
let html: string;
try {
html = readFileSync(join(projectDir, "index.html"), "utf8");
} catch {
return DEFAULT_CLOUD_FPS;
}
const raw = readCompositionFps(html);
if (raw == null) return DEFAULT_CLOUD_FPS;
const n = Number(raw);
if (isDistributedFps(n)) return n;
console.warn(
c.warn(
`The composition declares data-fps="${terminalText(raw)}", which cloud renders do not support ` +
`(${DISTRIBUTED_FPS.join(", ")}). Rendering at ${DEFAULT_CLOUD_FPS} fps; pass --fps to choose.`,
),
);
return DEFAULT_CLOUD_FPS;
}