Skip to content

🎥 Make videos programmatically with React

reshuffle formatting

Jonny Burgerb6aaed94d9b6
diff --git a/packages/studio-server/src/preview-server/routes/log-updates/format-side-props.ts b/packages/studio-server/src/preview-server/routes/log-updates/format-side-props.ts
new file mode 100644
index 0000000000..7b81f3d163
--- /dev/null
+++ b/packages/studio-server/src/preview-server/routes/log-updates/format-side-props.ts
@@ -0,0 +1,78 @@
+import {RenderInternals} from '@remotion/renderer';
+import {
+ attrName,
+ colorValue,
+ equals,
+ punctuation,
+ strikeThrough,
+} from './formatting';
+
+export type PropDelta = {
+ key: string;
+ valueString: string;
+};
+
+const colorEnabled = () => RenderInternals.chalk.enabled();
+
+// Format key={value} with Monokai syntax highlighting
+const formatSimpleProp = (key: string, value: string) => {
+ return `${attrName(key)}${equals('=')}${punctuation('{')}${colorValue(value)}${punctuation('}')}`;
+};
+
+// Format parentKey={{childKey: value}} with Monokai syntax highlighting
+const formatNestedProp = (
+ parentKey: string,
+ childKey: string,
+ value: string,
+) => {
+ return `${attrName(parentKey)}${equals('=')}${punctuation('{{')}${punctuation(childKey)}${punctuation(':')} ${colorValue(value)}${punctuation('}}')}`;
+};
+
+const formatPropDelta = ({key, valueString}: PropDelta) => {
+ if (!colorEnabled()) {
+ const dotIndex = key.indexOf('.');
+ if (dotIndex === -1) {
+ return `${key}={${valueString}}`;
+ }
+
+ const parent = key.slice(0, dotIndex);
+ const child = key.slice(dotIndex + 1);
+ return `${parent}={{${child}: ${valueString}}}`;
+ }
+
+ const dotIdx = key.indexOf('.');
+ if (dotIdx === -1) {
+ return formatSimpleProp(key, valueString);
+ }
+
+ return formatNestedProp(
+ key.slice(0, dotIdx),
+ key.slice(dotIdx + 1),
+ valueString,
+ );
+};
+
+export const formatSideProps = ({
+ removedProps,
+ addedProps,
+}: {
+ removedProps: PropDelta[];
+ addedProps: PropDelta[];
+}) => {
+ const parts: string[] = [];
+
+ for (const prop of removedProps) {
+ const formatted = formatPropDelta(prop);
+ parts.push(colorEnabled() ? strikeThrough(formatted) : formatted);
+ }
+
+ for (const prop of addedProps) {
+ parts.push(formatPropDelta(prop));
+ }
+
+ if (parts.length === 0) {
+ return '';
+ }
+
+ return `, ${parts.join(', ')}`;
+};
diff --git a/packages/studio-server/src/preview-server/routes/log-updates/formatting.ts b/packages/studio-server/src/preview-server/routes/log-updates/formatting.ts
new file mode 100644
index 0000000000..1398c2bd21
--- /dev/null
+++ b/packages/studio-server/src/preview-server/routes/log-updates/formatting.ts
@@ -0,0 +1,32 @@
+export const colorValue = (str: string) => {
+ if (
+ (str.startsWith("'") && str.endsWith("'")) ||
+ (str.startsWith('"') && str.endsWith('"'))
+ ) {
+ return stringValue(str);
+ }
+
+ if (/^-?\d+(\.\d+)?$/.test(str)) {
+ return numberValue(str);
+ }
+
+ return punctuation(str);
+};
+
+// eslint-disable-next-line no-control-regex
+const stripAnsi = (str: string) => str.replace(/\u001b\[[0-9;]*m/g, '');
+
+// 24-bit ANSI helpers
+export const fg = (r: number, g: number, b: number, str: string) =>
+ `\u001b[38;2;${r};${g};${b}m${str}\u001b[39m`;
+export const bg = (r: number, g: number, b: number, str: string) =>
+ `\u001b[48;2;${r};${g};${b}m${str}\u001b[49m`;
+
+// Monokai-inspired syntax colors
+export const attrName = (str: string) => fg(166, 226, 46, str);
+export const equals = (str: string) => fg(249, 38, 114, str);
+export const punctuation = (str: string) => fg(248, 248, 242, str);
+export const stringValue = (str: string) => fg(230, 219, 116, str);
+export const numberValue = (str: string) => fg(174, 129, 255, str);
+export const strikeThrough = (str: string) =>
+ `\u001b[9m\u001b[38;2;255;85;85m${stripAnsi(str)}\u001b[39m\u001b[29m`;
diff --git a/packages/studio-server/src/preview-server/routes/log-updates/log-update.ts b/packages/studio-server/src/preview-server/routes/log-updates/log-update.ts
index f0cbf21684..255de3181d 100644
--- a/packages/studio-server/src/preview-server/routes/log-updates/log-update.ts
+++ b/packages/studio-server/src/preview-server/routes/log-updates/log-update.ts
@@ -1,5 +1,13 @@
import {RenderInternals} from '@remotion/renderer';
import type {LogLevel} from '@remotion/renderer';
+import {formatSideProps, type PropDelta} from './format-side-props';
+import {
+ attrName,
+ colorValue,
+ equals,
+ punctuation,
+ strikeThrough,
+} from './formatting';
let warnedAboutPrettier = false;
@@ -29,38 +37,6 @@ export const normalizeQuotes = (str: string): string => {
return str;
};
-// 24-bit ANSI helpers
-export const fg = (r: number, g: number, b: number, str: string) =>
- `\u001b[38;2;${r};${g};${b}m${str}\u001b[39m`;
-export const bg = (r: number, g: number, b: number, str: string) =>
- `\u001b[48;2;${r};${g};${b}m${str}\u001b[49m`;
-// eslint-disable-next-line no-control-regex
-const stripAnsi = (str: string) => str.replace(/\u001b\[[0-9;]*m/g, '');
-export const strikeThrough = (str: string) =>
- `\u001b[9m\u001b[38;2;255;85;85m${stripAnsi(str)}\u001b[39m\u001b[29m`;
-
-// Monokai-inspired syntax colors
-const attrName = (str: string) => fg(166, 226, 46, str);
-const equals = (str: string) => fg(249, 38, 114, str);
-const punctuation = (str: string) => fg(248, 248, 242, str);
-const stringValue = (str: string) => fg(230, 219, 116, str);
-const numberValue = (str: string) => fg(174, 129, 255, str);
-
-const colorValue = (str: string) => {
- if (
- (str.startsWith("'") && str.endsWith("'")) ||
- (str.startsWith('"') && str.endsWith('"'))
- ) {
- return stringValue(str);
- }
-
- if (/^-?\d+(\.\d+)?$/.test(str)) {
- return numberValue(str);
- }
-
- return punctuation(str);
-};
-
const colorEnabled = () => RenderInternals.chalk.enabled();
// Format key={value} with Monokai syntax highlighting
@@ -77,60 +53,6 @@ const formatNestedProp = (
return `${attrName(parentKey)}${equals('=')}${punctuation('{{')}${punctuation(childKey)}${punctuation(':')} ${colorValue(value)}${punctuation('}}')}`;
};
-export type PropDelta = {
- key: string;
- valueString: string;
-};
-
-const formatPropDelta = ({key, valueString}: PropDelta) => {
- if (!colorEnabled()) {
- const dotIndex = key.indexOf('.');
- if (dotIndex === -1) {
- return `${key}={${valueString}}`;
- }
-
- const parent = key.slice(0, dotIndex);
- const child = key.slice(dotIndex + 1);
- return `${parent}={{${child}: ${valueString}}}`;
- }
-
- const dotIdx = key.indexOf('.');
- if (dotIdx === -1) {
- return formatSimpleProp(key, valueString);
- }
-
- return formatNestedProp(
- key.slice(0, dotIdx),
- key.slice(dotIdx + 1),
- valueString,
- );
-};
-
-const formatSideProps = ({
- removedProps,
- addedProps,
-}: {
- removedProps: PropDelta[];
- addedProps: PropDelta[];
-}) => {
- const parts: string[] = [];
-
- for (const prop of removedProps) {
- const formatted = formatPropDelta(prop);
- parts.push(colorEnabled() ? strikeThrough(formatted) : formatted);
- }
-
- for (const prop of addedProps) {
- parts.push(formatPropDelta(prop));
- }
-
- if (parts.length === 0) {
- return '';
- }
-
- return `, ${parts.join(', ')}`;
-};
-
export const formatPropChange = ({
key,
oldValueString,
diff --git a/packages/studio-server/src/test/log-update.test.ts b/packages/studio-server/src/test/log-update.test.ts
index c38a345e2d..7ea7009102 100644
--- a/packages/studio-server/src/test/log-update.test.ts
+++ b/packages/studio-server/src/test/log-update.test.ts
@@ -9,11 +9,17 @@ import {RenderInternals} from '@remotion/renderer';
import {Internals} from 'remotion';
import {updateSequenceProps} from '../codemods/update-sequence-props/update-sequence-props';
import {
- fg,
+ attrName,
+ equals,
+ numberValue,
+ punctuation,
+ strikeThrough,
+ stringValue,
+} from '../preview-server/routes/log-updates/formatting';
+import {
formatPropChange,
logUpdate,
normalizeQuotes,
- strikeThrough,
} from '../preview-server/routes/log-updates/log-update';
import {lineColumnToNodePath} from './test-utils';
@@ -77,12 +83,6 @@ test('logUpdate emits Monokai-colored output after an AST update', async () => {
expect(consoleSpy).toHaveBeenCalledTimes(1);
const logged = consoleSpy.mock.calls[0].join(' ');
- // Mirror the Monokai palette from log-update.ts.
- const attrName = (s: string) => fg(166, 226, 46, s);
- const equals = (s: string) => fg(249, 38, 114, s);
- const punctuation = (s: string) => fg(248, 248, 242, s);
- const numberValue = (s: string) => fg(174, 129, 255, s);
-
const simpleProp = (key: string, value: string) =>
`${attrName(key)}${equals('=')}${punctuation('{')}${numberValue(value)}${punctuation('}')}`;
@@ -146,11 +146,6 @@ test('logUpdate emits change-from-default output for discriminated union enum ch
expect(consoleSpy).toHaveBeenCalledTimes(1);
const logged = consoleSpy.mock.calls[0].join(' ');
- const attrName = (s: string) => fg(166, 226, 46, s);
- const equals = (s: string) => fg(249, 38, 114, s);
- const punctuation = (s: string) => fg(248, 248, 242, s);
- const stringValue = (s: string) => fg(230, 219, 116, s);
-
const simpleProp = (key: string, value: string) =>
`${attrName(key)}${equals('=')}${punctuation('{')}${stringValue(value)}${punctuation('}')}`;