Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"type": "module",
"scripts": {
"dev": "vite",
"prebuild": "pnpm -F @ansi-tools/parser build && pnpm type-check && pnpm test",
"prebuild": "pnpm -r build && pnpm type-check && pnpm test",
"test": "node --test",
"build": "vite build",
"postbuild": "node --import isum/no-css scripts/build.ts",
Expand Down
28 changes: 28 additions & 0 deletions packages/renderer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# @ansi-tools/renderer

Renders strings containing ANSI codes (i.e. terminal output) into a "rendered"
representation, containing each visual frame which would have been displayed.

## Installation

```bash
npm install @ansi-tools/renderer
```

## Usage

```ts
import { renderString } from "@ansi-tools/renderer";

const input = "\x1b[31mHello\x1b[0m World";
const rendered = renderString(input);

for (const frame of rendered) {
// will render frame 1 which is "Hello World"
console.log(frame);
}
```

## License

ISC
56 changes: 56 additions & 0 deletions packages/renderer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "@ansi-tools/renderer",
"version": "1.0.7",
"description": "Renders strings containing ANSI escape codes to various outputs.",
"main": "./dist/index.js",
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"default": "./dist/index.js"
},
"./escaped": {
"types": "./dist/escaped.d.ts",
"import": "./dist/escaped.js",
"default": "./dist/escaped.js"
}
},
"files": [
"dist"
],
"scripts": {
"prebuild": "pnpm type-check && pnpm test",
"build": "tsdown --dts src/index.ts",
"dev": "tsdown --dts src/index.ts --watch",
"test": "node --test",
"type-check": "tsc",
"prepack": "pnpm build"
},
"keywords": [
"ansi",
"term",
"terminal",
"ink"
],
"author": "ANSI tools maintainers (https://github.com/webpro/ANSI.tools)",
"license": "ISC",
"publishConfig": {
"access": "public"
},
"homepage": "https://github.com/webpro/ANSI.tools/tree/main/packages/renderer",
"bugs": "https://github.com/webpro/ANSI.tools/issues",
"repository": {
"type": "git",
"url": "github:webpro/ANSI.tools",
"directory": "packages/renderer"
},
"dependencies": {
"@ansi-tools/parser": "workspace:*"
},
"devDependencies": {
"@types/node": "^24.0.13",
"tsdown": "^0.12.9",
"typescript": "^5.8.3"
}
}
34 changes: 34 additions & 0 deletions packages/renderer/src/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
type CODE,
type CONTROL_CODE,
} from '@ansi-tools/parser';

export const isCursorCommand = (code: CODE): code is CONTROL_CODE => {
return (
(
code.type === 'CSI' &&
(code.command === 'H' ||
code.command === 'A' ||
code.command === 'B' ||
code.command === 'C' ||
code.command === 'D' ||
code.command === 'E' ||
code.command === 'F' ||
code.command === 'G' ||
code.command === 'T' ||
code.command === 'S')) ||
(code.type === 'ESC' &&
(code.command === '8' || code.command === '7')) ||
(code.type === 'DEC' &&
(code.command === 'l' || code.command === 'h'))
);
};

export const isEraseCommand = (code: CODE): code is CONTROL_CODE => {
return (
(code.type === 'CSI' &&
(code.command === 'J' || code.command === 'K')) ||
(code.type === 'ESC' &&
code.command === 'c')
);
};
8 changes: 8 additions & 0 deletions packages/renderer/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {Renderer} from "./renderer.ts";
import {RenderStream} from "./render-stream.ts";

export async function renderString(input: string): Promise<Renderer> {
return Renderer.fromString(input);;
}

export { Renderer, RenderStream };
35 changes: 35 additions & 0 deletions packages/renderer/src/render-stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {Writable, WritableOptions} from "node:stream";
import {Renderer} from "./renderer.ts";
import {parse} from "@ansi-tools/parser";

const renderStreamBrand = Symbol.for('ansi-tools:render-stream');

export class RenderStream extends Writable {
[renderStreamBrand] = true;

get renderer(): Renderer {
const ast = parse(this.#buffer.join(''));
const renderer = new Renderer();

for (const code of ast) {
renderer.write(code);
}

return renderer;
}

#buffer: string[] = [];

constructor(opts?: WritableOptions) {
super(opts);
}

_write(
chunk: unknown,
_encoding: BufferEncoding,
callback: (error?: Error | null | undefined) => void
): void {
this.#buffer.push(String(chunk));
callback();
}
}
Loading