Skip to content

🎥 Make videos programmatically with React

inline ansi-diff

Jonny Burger306e4addd4a0
diff --git a/packages/cli/package.json b/packages/cli/package.json
index 863238e436..1cb33f3c69 100644
--- a/packages/cli/package.json
+++ b/packages/cli/package.json
@@ -23,7 +23,6 @@
"@remotion/media-utils": "2.5.7",
"@remotion/player": "2.5.7",
"@remotion/renderer": "2.5.7",
- "ansi-diff": "^1.1.1",
"better-opn": "^2.1.1",
"chalk": "^4.1.0",
"dotenv": "^9.0.0",
diff --git a/packages/cli/src/ansi/CREDITS.md b/packages/cli/src/ansi/CREDITS.md
new file mode 100644
index 0000000000..3e43a90754
--- /dev/null
+++ b/packages/cli/src/ansi/CREDITS.md
@@ -0,0 +1,7 @@
+Contents in this folder are inlined from and converted to Typescript:
+
+- https://github.com/chalk/ansi-regex
+- https://github.com/mafintosh/ansi-diff
+- https://github.com/mafintosh/ansi-split
+
+All MIT licensed.
diff --git a/packages/cli/src/ansi/ansi-diff.ts b/packages/cli/src/ansi/ansi-diff.ts
new file mode 100644
index 0000000000..cc2c418c2e
--- /dev/null
+++ b/packages/cli/src/ansi/ansi-diff.ts
@@ -0,0 +1,277 @@
+import {splitAnsi} from './ansi-split';
+
+const CLEAR_LINE = Buffer.from([0x1b, 0x5b, 0x30, 0x4b]);
+const NEWLINE = Buffer.from('\n');
+
+type Options = {width?: number; height?: number};
+
+export class AnsiDiff {
+ x: number;
+ y: number;
+ width: number;
+ height: number;
+ _buffer: string | null;
+ _out: Buffer[];
+ _lines: Line[];
+
+ constructor(opts?: Options) {
+ this.x = 0;
+ this.y = 0;
+ this.width = opts?.width || Infinity;
+ this.height = opts?.height || Infinity;
+
+ this._buffer = null;
+ this._out = [];
+ this._lines = [];
+ }
+
+ resize = (opts: Options) => {
+ if (!opts) opts = {};
+
+ if (opts.width) this.width = opts.width;
+ if (opts.height) this.height = opts.height;
+
+ if (this._buffer) this.update(this._buffer);
+
+ const last = top(this._lines);
+
+ // eslint-disable-next-line no-negated-condition
+ if (!last) {
+ this.x = 0;
+ this.y = 0;
+ } else {
+ this.x = last.remainder;
+ this.y = last.y + last.height;
+ }
+ };
+
+ toString = () => {
+ return this._buffer;
+ };
+
+ update = (buffer: string | Buffer, opts?: {moveTo?: [number, number]}) => {
+ this._buffer = Buffer.isBuffer(buffer) ? buffer.toString() : buffer;
+
+ const other = this._buffer;
+ const oldLines = this._lines;
+ const lines = split(other, this);
+
+ this._lines = lines;
+ this._out = [];
+
+ const min = Math.min(lines.length, oldLines.length);
+ let i = 0;
+ let a: Line;
+ let b: Line;
+ let scrub = false;
+
+ for (; i < min; i++) {
+ a = lines[i];
+ b = oldLines[i];
+
+ if (same(a, b)) continue;
+
+ // if x === width there is an edgecase with inline diffing
+ // easiest solution is just not to do it then! :)
+ if (!scrub && this.x !== this.width && inlineDiff(a, b)) {
+ const left = a.diffLeft(b);
+ const right = a.diffRight(b);
+ const slice = a.raw.slice(left, right ? -right : a.length);
+ if (left + right > 4 && left + slice.length < this.width - 1) {
+ this._moveTo(left, a.y);
+ this._push(Buffer.from(slice));
+ this.x += slice.length;
+ continue;
+ }
+ }
+
+ this._moveTo(0, a.y);
+ this._write(a);
+ if (a.y !== b.y || a.height !== b.height) scrub = true;
+ if (b.length > a.length || scrub) this._push(CLEAR_LINE);
+ if (a.newline) this._newline();
+ }
+
+ for (; i < lines.length; i++) {
+ a = lines[i];
+
+ this._moveTo(0, a.y);
+ this._write(a);
+ if (scrub) this._push(CLEAR_LINE);
+ if (a.newline) this._newline();
+ }
+
+ const oldLast = top(oldLines);
+ const last = top(lines);
+
+ if (
+ oldLast &&
+ (!last || last.y + last.height < oldLast.y + oldLast.height)
+ ) {
+ this._clearDown(oldLast.y + oldLast.height);
+ }
+
+ if (opts?.moveTo) {
+ this._moveTo(opts.moveTo[0], opts.moveTo[1]);
+ } else if (last) {
+ this._moveTo(last.remainder, last.y + last.height);
+ }
+
+ return Buffer.concat(this._out);
+ };
+
+ _clearDown = (y: number) => {
+ let {x} = this;
+ for (let i = this.y; i <= y; i++) {
+ this._moveTo(x, i);
+ this._push(CLEAR_LINE);
+ x = 0;
+ }
+ };
+
+ _newline = () => {
+ this._push(NEWLINE);
+ this.x = 0;
+ this.y++;
+ };
+
+ _write = (line: Line) => {
+ this._out.push(line.toBuffer());
+ this.x = line.remainder;
+ this.y += line.height;
+ };
+
+ _moveTo = (x: number, y: number) => {
+ const dx = x - this.x;
+ const dy = y - this.y;
+
+ if (dx > 0) this._push(moveRight(dx));
+ else if (dx < 0) this._push(moveLeft(-dx));
+ if (dy > 0) this._push(moveDown(dy));
+ else if (dy < 0) this._push(moveUp(-dy));
+
+ this.x = x;
+ this.y = y;
+ };
+
+ _push = (buf: Buffer) => {
+ this._out.push(buf);
+ };
+}
+
+function same(a: Line, b: Line) {
+ return (
+ a.y === b.y &&
+ a.width === b.width &&
+ a.raw === b.raw &&
+ a.newline === b.newline
+ );
+}
+
+function top(list: Line[]) {
+ return list.length ? list[list.length - 1] : null;
+}
+
+class Line {
+ y: number;
+ width: number;
+ parts: string[];
+ height: number;
+ remainder: number;
+ raw: string;
+ length: number;
+ newline: boolean;
+ constructor(str: string, y: number, nl: boolean, term: AnsiDiff) {
+ this.y = y;
+ this.width = term.width;
+ this.parts = splitAnsi(str);
+ this.length = length(this.parts);
+ this.raw = str;
+ this.newline = nl;
+ this.height = Math.floor(this.length / term.width);
+ this.remainder = this.length - (this.height && this.height * term.width);
+ if (this.height && !this.remainder) {
+ this.height--;
+ this.remainder = this.width;
+ }
+ }
+
+ diffLeft = (other: Line) => {
+ let left = 0;
+ for (; left < this.length; left++) {
+ if (this.raw[left] !== other.raw[left]) return left;
+ }
+
+ return left;
+ };
+
+ diffRight = (other: Line) => {
+ let right = 0;
+ for (; right < this.length; right++) {
+ const r = this.length - right - 1;
+ if (this.raw[r] !== other.raw[r]) return right;
+ }
+
+ return right;
+ };
+
+ toBuffer = () => {
+ return Buffer.from(this.raw);
+ };
+}
+
+function inlineDiff(a: Line, b: Line) {
+ return (
+ a.length === b.length &&
+ a.parts.length === 1 &&
+ b.parts.length === 1 &&
+ a.y === b.y &&
+ a.newline &&
+ b.newline &&
+ a.width === b.width
+ );
+}
+
+function split(str: string, term: AnsiDiff) {
+ let y = 0;
+ const lines = str.split('\n');
+ const wrapped = [];
+ let line;
+
+ for (let i = 0; i < lines.length; i++) {
+ line = new Line(lines[i], y, i < lines.length - 1, term);
+ y += line.height + (line.newline ? 1 : 0);
+ wrapped.push(line);
+ }
+
+ return wrapped;
+}
+
+function moveUp(n: number) {
+ return Buffer.from('1b5b' + toHex(n) + '41', 'hex');
+}
+
+function moveDown(n: number) {
+ return Buffer.from('1b5b' + toHex(n) + '42', 'hex');
+}
+
+function moveRight(n: number) {
+ return Buffer.from('1b5b' + toHex(n) + '43', 'hex');
+}
+
+function moveLeft(n: number) {
+ return Buffer.from('1b5b' + toHex(n) + '44', 'hex');
+}
+
+function length(parts: string[]) {
+ let len = 0;
+ for (let i = 0; i < parts.length; i += 2) {
+ len += parts[i].length;
+ }
+
+ return len;
+}
+
+function toHex(n: number) {
+ return Buffer.from(String(n)).toString('hex');
+}
diff --git a/packages/cli/src/ansi/ansi-regex.ts b/packages/cli/src/ansi/ansi-regex.ts
new file mode 100644
index 0000000000..9795fdc4a7
--- /dev/null
+++ b/packages/cli/src/ansi/ansi-regex.ts
@@ -0,0 +1,8 @@
+export default function ansiRegex({onlyFirst = false} = {}) {
+ const pattern = [
+ '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
+ '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))',
+ ].join('|');
+
+ return new RegExp(pattern, onlyFirst ? undefined : 'g');
+}
diff --git a/packages/cli/src/ansi/ansi-split.ts b/packages/cli/src/ansi/ansi-split.ts
new file mode 100644
index 0000000000..c4b9ed4cc6
--- /dev/null
+++ b/packages/cli/src/ansi/ansi-split.ts
@@ -0,0 +1,27 @@
+import ansiRegex from './ansi-regex';
+
+export function splitAnsi(str: string): string[] {
+ const parts = str.match(ansiRegex());
+ if (!parts) return [str];
+
+ const result = [];
+ let offset = 0;
+ let ptr = 0;
+
+ for (let i = 0; i < parts.length; i++) {
+ offset = str.indexOf(parts[i], offset);
+ if (offset === -1) throw new Error('Could not split string');
+ if (ptr !== offset) result.push(str.slice(ptr, offset));
+ if (ptr === offset && result.length) {
+ result[result.length - 1] += parts[i];
+ } else {
+ if (offset === 0) result.push('');
+ result.push(parts[i]);
+ }
+
+ ptr = offset + parts[i].length;
+ }
+
+ result.push(str.slice(ptr));
+ return result;
+}
diff --git a/packages/cli/src/progress-bar.ts b/packages/cli/src/progress-bar.ts
index 7c92ad07f3..813e4fb0ae 100644
--- a/packages/cli/src/progress-bar.ts
+++ b/packages/cli/src/progress-bar.ts
@@ -1,7 +1,6 @@
-// @ts-expect-error
-import ansiDiff from 'ansi-diff';
import chalk from 'chalk';
import {Internals} from 'remotion';
+import {AnsiDiff} from './ansi/ansi-diff';
export const createProgressBar = (): {
update: (str: string) => boolean;
@@ -10,7 +9,7 @@ export const createProgressBar = (): {
return {update: () => false};
}
- const diff = ansiDiff();
+ const diff = new AnsiDiff();
process.stdout.write('');
return {
update: (up: string): boolean => process.stdout.write(diff.update(up)),
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 4c0cd21984..8221cd780a 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -166,7 +166,6 @@ importers:
'@types/node': ^16.7.5
'@types/react': 17.0.27
'@types/react-dom': ^17.0.0
- ansi-diff: ^1.1.1
better-opn: ^2.1.1
chalk: ^4.1.0
dotenv: ^9.0.0
@@ -186,7 +185,6 @@ importers:
'@remotion/media-utils': link:../media-utils
'@remotion/player': link:../player
'@remotion/renderer': link:../renderer
- ansi-diff: 1.1.1
better-opn: 2.1.1
chalk: 4.1.2
dotenv: 9.0.2
@@ -7779,12 +7777,6 @@ packages:
resolution: {integrity: sha512-JoX0apGbHaUJBNl6yF+p6JAFYZ666/hhCGKN5t9QFjbJQKUU/g8MNbFDbvfrgKXvI1QpZplPOnwIo99lX/AAmA==}
engines: {node: '>=6'}
- /ansi-diff/1.1.1:
- resolution: {integrity: sha512-XnTdFDQzbEewrDx8epWXdw7oqHMvv315vEtfqDiEhhWghIf4++h26c3/FMz7iTLhNrnj56DNIXpbxHZq+3s6qw==}
- dependencies:
- ansi-split: 1.0.1
- dev: false
-
/ansi-escapes/4.3.2:
resolution: {integrity: sha512-gKXj5ALrKWQLsYG9jlTRmR/xKluxHV+Z9QEwNIgCfM1/uwPMCuzVVnh5mwTd+OuBZcwSIMbqssNWRm1lE51QaQ==}
engines: {node: '>=8'}
@@ -7807,11 +7799,6 @@ packages:
resolution: {integrity: sha1-w7M6te42DYbg5ijwRorn7yfWVN8=}
engines: {node: '>=0.10.0'}
- /ansi-regex/3.0.0:
- resolution: {integrity: sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=}
- engines: {node: '>=4'}
- dev: false
-
/ansi-regex/4.1.0:
resolution: {integrity: sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==}
engines: {node: '>=6'}
@@ -7821,12 +7808,6 @@ packages:
resolution: {integrity: sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==}
engines: {node: '>=8'}
- /ansi-split/1.0.1:
- resolution: {integrity: sha512-RRxQym4DFtDNmHIkW6aeFVvrXURb11lGAEPXNiryjCe8bK8RsANjzJ0M2aGOkvBYwP4Bl/xZ8ijtr6D3j1x/eg==}
- dependencies:
- ansi-regex: 3.0.0
- dev: false
-
/ansi-styles/3.2.1:
resolution: {integrity: sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==}
engines: {node: '>=4'}