@userspace-dev/ansi 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +121 -2
- package/dist/charset.d.ts +2 -0
- package/dist/charset.js +6 -0
- package/dist/html.d.ts +18 -0
- package/dist/html.js +96 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +7 -0
- package/dist/link.d.ts +8 -0
- package/dist/link.js +22 -0
- package/dist/palette.d.ts +12 -0
- package/dist/palette.js +39 -0
- package/dist/parser.d.ts +24 -0
- package/dist/parser.js +81 -0
- package/dist/row.d.ts +32 -0
- package/dist/row.js +246 -0
- package/dist/screen.d.ts +27 -0
- package/dist/screen.js +508 -0
- package/dist/stream.d.ts +10 -0
- package/dist/stream.js +24 -0
- package/dist/strip.d.ts +6 -0
- package/dist/strip.js +33 -0
- package/dist/style.d.ts +29 -0
- package/dist/style.js +216 -0
- package/dist/text.d.ts +17 -0
- package/dist/text.js +34 -0
- package/dist/tokenizer.d.ts +32 -0
- package/dist/tokenizer.js +259 -0
- package/dist/width.d.ts +16 -0
- package/dist/width.js +111 -0
- package/package.json +44 -4
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rishabh Prashar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,122 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @userspace-dev/ansi
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Parse, clean and render terminal output for the web and for agents.
|
|
4
|
+
|
|
5
|
+
- **Streaming.** Output can be cut anywhere, even inside an escape code or a character.
|
|
6
|
+
- **Follows the cursor.** Spinners, progress bars and redrawn lines become the lines a person would see. Scroll regions, tab stops and line drawing work too.
|
|
7
|
+
- **Made for models too.** Plain text within a line budget, without text that was hidden from people.
|
|
8
|
+
- **Safe to render.** Text is escaped, links are checked, and a few bytes of hostile output cannot blow up memory.
|
|
9
|
+
- **No dependencies.** ESM, typed, for browsers, Node.js and edge runtimes.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @userspace-dev/ansi
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Show live output
|
|
18
|
+
|
|
19
|
+
`createScreen` keeps what is on screen. Unchanged lines keep their identity, so they can be memoized.
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { createScreen, toCss } from "@userspace-dev/ansi"
|
|
23
|
+
|
|
24
|
+
const screen = createScreen({ maxLines: 5000 })
|
|
25
|
+
|
|
26
|
+
for await (const chunk of process.stdout) {
|
|
27
|
+
screen.write(chunk)
|
|
28
|
+
render(screen.lines())
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// { id, segments: [{ text, style, link? }] }
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```tsx
|
|
35
|
+
{
|
|
36
|
+
screen.lines().map((line) => (
|
|
37
|
+
<div key={line.id}>
|
|
38
|
+
{line.segments.map((segment, i) => (
|
|
39
|
+
<span key={i} style={toCss(segment.style)}>
|
|
40
|
+
{segment.text}
|
|
41
|
+
</span>
|
|
42
|
+
))}
|
|
43
|
+
</div>
|
|
44
|
+
))
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Give output to a model
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { toText } from "@userspace-dev/ansi"
|
|
52
|
+
|
|
53
|
+
toText(output, { maxLines: 200 })
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
A hundred spinner frames become one line. Longer output keeps its start and, mostly, its end, where errors tend to be. Concealed text (`ESC [8m`) is dropped, because a person reviewing the same output never sees it and it can carry instructions meant only for the model.
|
|
57
|
+
|
|
58
|
+
## Render finished output
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { toHtml } from "@userspace-dev/ansi"
|
|
62
|
+
|
|
63
|
+
pre.innerHTML = toHtml(output)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Everything else
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import {
|
|
70
|
+
createAnsiStream,
|
|
71
|
+
createParser,
|
|
72
|
+
parse,
|
|
73
|
+
strip,
|
|
74
|
+
} from "@userspace-dev/ansi"
|
|
75
|
+
|
|
76
|
+
strip("\x1b[31merror\x1b[0m") // "error"
|
|
77
|
+
|
|
78
|
+
parse("\x1b[31merror\x1b[0m") // [{ text: "error", style: { fg: 1 } }]
|
|
79
|
+
|
|
80
|
+
const parser = createParser()
|
|
81
|
+
parser.write("\x1b[3") // []
|
|
82
|
+
parser.write("1merror") // [{ text: "error", style: { fg: 1 } }]
|
|
83
|
+
|
|
84
|
+
response.body
|
|
85
|
+
.pipeThrough(new TextDecoderStream())
|
|
86
|
+
.pipeThrough(createAnsiStream())
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
| Function | Use it for |
|
|
90
|
+
| ------------------------------------------- | ---------------------------------------- |
|
|
91
|
+
| `createScreen` | Live output that moves the cursor |
|
|
92
|
+
| `toText` | Models, search and plain logs |
|
|
93
|
+
| `toHtml`, `toCss` | Rendering on a page |
|
|
94
|
+
| `parse`, `createParser`, `createAnsiStream` | Styled segments, without cursor movement |
|
|
95
|
+
| `strip` | Removing codes and keeping all text |
|
|
96
|
+
|
|
97
|
+
## Colors
|
|
98
|
+
|
|
99
|
+
A color is a palette index from 0 to 255 or a hex string. The 16 named colors use the CSS variables `--ansi-0` to `--ansi-15` when you define them. Otherwise they come from a palette that follows `color-scheme` and has a contrast of at least 4.5:1 on white and near-black.
|
|
100
|
+
|
|
101
|
+
## Security
|
|
102
|
+
|
|
103
|
+
Treat terminal output as untrusted.
|
|
104
|
+
|
|
105
|
+
- `toHtml` escapes text and attributes, and only renders `http`, `https` and `mailto` links.
|
|
106
|
+
- `parse` and `createScreen` leave out links with any other protocol. Pass `protocols` to allow more.
|
|
107
|
+
- Titles, clipboard commands, images and other sequences are ignored.
|
|
108
|
+
- Nothing throws on malformed input, and cursor movement cannot grow a line without limit.
|
|
109
|
+
|
|
110
|
+
`toHtml` uses inline styles, which a strict `style-src` policy blocks. Rendering segments with `toCss` in React on the client does not need them.
|
|
111
|
+
|
|
112
|
+
## Limits
|
|
113
|
+
|
|
114
|
+
- Characters are measured the way `string-width` does it. Terminals disagree about some emoji, so a line that mixes them with cursor movement may be off by a column.
|
|
115
|
+
- A line costs about as much memory as its text. Once something in the middle of it changes, that becomes roughly 25 bytes per character.
|
|
116
|
+
- A single character is cut after 64 code units, which no real one reaches.
|
|
117
|
+
- Origin mode, insert mode and double-width lines are not supported.
|
|
118
|
+
- Full-screen programs run on the alternate screen, which is discarded when they exit.
|
|
119
|
+
|
|
120
|
+
## License
|
|
121
|
+
|
|
122
|
+
MIT
|
package/dist/charset.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** DEC special graphics, which older programs use to draw boxes. */
|
|
2
|
+
const GRAPHICS = "◆▒␉␌␍␊°±␋┘┐┌└┼⎺⎻─⎼⎽├┤┴┬│≤≥π≠£·";
|
|
3
|
+
/** Characters from ` to ~ as line drawing. */
|
|
4
|
+
export function toGraphics(text) {
|
|
5
|
+
return text.replace(/[`-~]/g, (char) => GRAPHICS[char.charCodeAt(0) - 0x60] ?? char);
|
|
6
|
+
}
|
package/dist/html.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type Segment } from "./parser.ts";
|
|
2
|
+
import type { Style } from "./style.ts";
|
|
3
|
+
export type HtmlOptions = {
|
|
4
|
+
/** Protocols that links may use. Defaults to `http:`, `https:`, `mailto:`. */
|
|
5
|
+
protocols?: readonly string[];
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* The style as CSS properties in camelCase, ready for React's `style`.
|
|
9
|
+
*
|
|
10
|
+
* The 16 named colors read `--ansi-0` to `--ansi-15`, so a theme can override
|
|
11
|
+
* them, and fall back to a palette that follows `color-scheme`.
|
|
12
|
+
*/
|
|
13
|
+
export declare function toCss(style: Style): Record<string, string>;
|
|
14
|
+
/**
|
|
15
|
+
* Renders output as HTML for `<pre>`. Text and attributes are escaped and links
|
|
16
|
+
* are checked, so untrusted output is safe to insert.
|
|
17
|
+
*/
|
|
18
|
+
export declare function toHtml(input: string | readonly Segment[], { protocols }?: HtmlOptions): string;
|
package/dist/html.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { isAllowed, SAFE_PROTOCOLS } from "./link.js";
|
|
2
|
+
import { DEFAULT_PALETTE, toHex } from "./palette.js";
|
|
3
|
+
import { parse } from "./parser.js";
|
|
4
|
+
/**
|
|
5
|
+
* The style as CSS properties in camelCase, ready for React's `style`.
|
|
6
|
+
*
|
|
7
|
+
* The 16 named colors read `--ansi-0` to `--ansi-15`, so a theme can override
|
|
8
|
+
* them, and fall back to a palette that follows `color-scheme`.
|
|
9
|
+
*/
|
|
10
|
+
export function toCss(style) {
|
|
11
|
+
const css = {};
|
|
12
|
+
let color = style.fg === undefined ? undefined : toCssColor(style.fg);
|
|
13
|
+
let background = style.bg === undefined ? undefined : toCssColor(style.bg);
|
|
14
|
+
if (style.inverse) {
|
|
15
|
+
const swapped = background ?? "var(--ansi-background, Canvas)";
|
|
16
|
+
background = color ?? "var(--ansi-foreground, CanvasText)";
|
|
17
|
+
color = swapped;
|
|
18
|
+
}
|
|
19
|
+
if (color)
|
|
20
|
+
css.color = color;
|
|
21
|
+
if (background)
|
|
22
|
+
css.backgroundColor = background;
|
|
23
|
+
if (style.bold)
|
|
24
|
+
css.fontWeight = "bold";
|
|
25
|
+
if (style.dim)
|
|
26
|
+
css.opacity = "0.6";
|
|
27
|
+
if (style.italic)
|
|
28
|
+
css.fontStyle = "italic";
|
|
29
|
+
if (style.hidden)
|
|
30
|
+
css.visibility = "hidden";
|
|
31
|
+
const lines = [
|
|
32
|
+
style.underline && "underline",
|
|
33
|
+
style.overline && "overline",
|
|
34
|
+
style.strike && "line-through",
|
|
35
|
+
].filter(Boolean);
|
|
36
|
+
if (lines.length > 0) {
|
|
37
|
+
css.textDecorationLine = lines.join(" ");
|
|
38
|
+
}
|
|
39
|
+
if (style.underline && style.underline !== "single") {
|
|
40
|
+
css.textDecorationStyle =
|
|
41
|
+
style.underline === "curly" ? "wavy" : style.underline;
|
|
42
|
+
}
|
|
43
|
+
if (style.underline && style.underlineColor !== undefined) {
|
|
44
|
+
css.textDecorationColor = toCssColor(style.underlineColor);
|
|
45
|
+
}
|
|
46
|
+
return css;
|
|
47
|
+
}
|
|
48
|
+
function toCssColor(color) {
|
|
49
|
+
const named = typeof color === "number" ? DEFAULT_PALETTE[color] : undefined;
|
|
50
|
+
return named
|
|
51
|
+
? `var(--ansi-${color}, light-dark(${named.light}, ${named.dark}))`
|
|
52
|
+
: toHex(color);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Renders output as HTML for `<pre>`. Text and attributes are escaped and links
|
|
56
|
+
* are checked, so untrusted output is safe to insert.
|
|
57
|
+
*/
|
|
58
|
+
export function toHtml(input, { protocols = SAFE_PROTOCOLS } = {}) {
|
|
59
|
+
const segments = typeof input === "string" ? parse(input, { protocols }) : input;
|
|
60
|
+
// Segments share their style objects, so each one is rendered once.
|
|
61
|
+
const attributes = new Map();
|
|
62
|
+
let html = "";
|
|
63
|
+
for (const segment of segments) {
|
|
64
|
+
let attribute = attributes.get(segment.style);
|
|
65
|
+
if (attribute === undefined) {
|
|
66
|
+
attribute = styleAttribute(segment.style);
|
|
67
|
+
attributes.set(segment.style, attribute);
|
|
68
|
+
}
|
|
69
|
+
html += renderSegment(segment, attribute, protocols);
|
|
70
|
+
}
|
|
71
|
+
return html;
|
|
72
|
+
}
|
|
73
|
+
function styleAttribute(style) {
|
|
74
|
+
const css = Object.entries(toCss(style))
|
|
75
|
+
.map(([property, value]) => `${toKebab(property)}:${value}`)
|
|
76
|
+
.join(";");
|
|
77
|
+
return css ? ` style="${escapeHtml(css)}"` : "";
|
|
78
|
+
}
|
|
79
|
+
function renderSegment({ text, link }, attribute, protocols) {
|
|
80
|
+
const content = escapeHtml(text);
|
|
81
|
+
if (link !== undefined && isAllowed(link, protocols)) {
|
|
82
|
+
return `<a href="${escapeHtml(link)}" target="_blank" rel="noopener noreferrer nofollow"${attribute}>${content}</a>`;
|
|
83
|
+
}
|
|
84
|
+
return attribute ? `<span${attribute}>${content}</span>` : content;
|
|
85
|
+
}
|
|
86
|
+
const toKebab = (property) => property.replace(/[A-Z]/g, (char) => `-${char.toLowerCase()}`);
|
|
87
|
+
const ENTITIES = {
|
|
88
|
+
"&": "&",
|
|
89
|
+
"<": "<",
|
|
90
|
+
">": ">",
|
|
91
|
+
'"': """,
|
|
92
|
+
"'": "'",
|
|
93
|
+
};
|
|
94
|
+
function escapeHtml(value) {
|
|
95
|
+
return value.replace(/[&<>"']/g, (char) => ENTITIES[char] ?? char);
|
|
96
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { toCss, toHtml, type HtmlOptions } from "./html.ts";
|
|
2
|
+
export { DEFAULT_PALETTE, toHex, type PaletteEntry } from "./palette.ts";
|
|
3
|
+
export { createParser, parse, type Parser, type ParserOptions, type Segment, } from "./parser.ts";
|
|
4
|
+
export type { Line } from "./row.ts";
|
|
5
|
+
export { createScreen, type Screen, type ScreenOptions } from "./screen.ts";
|
|
6
|
+
export { createAnsiStream } from "./stream.ts";
|
|
7
|
+
export { strip } from "./strip.ts";
|
|
8
|
+
export type { Color, Style, Underline } from "./style.ts";
|
|
9
|
+
export { toText, type TextOptions } from "./text.ts";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { toCss, toHtml } from "./html.js";
|
|
2
|
+
export { DEFAULT_PALETTE, toHex } from "./palette.js";
|
|
3
|
+
export { createParser, parse, } from "./parser.js";
|
|
4
|
+
export { createScreen } from "./screen.js";
|
|
5
|
+
export { createAnsiStream } from "./stream.js";
|
|
6
|
+
export { strip } from "./strip.js";
|
|
7
|
+
export { toText } from "./text.js";
|
package/dist/link.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const SAFE_PROTOCOLS: readonly string[];
|
|
2
|
+
/**
|
|
3
|
+
* Reads the target of a hyperlink command (`OSC 8 ; params ; uri`). Returns null
|
|
4
|
+
* for other commands, and undefined where a link ends or is not allowed.
|
|
5
|
+
*/
|
|
6
|
+
export declare function readLink(osc: string, protocols: readonly string[]): string | undefined | null;
|
|
7
|
+
/** Output is untrusted, so a link is only kept if its protocol is known. */
|
|
8
|
+
export declare function isAllowed(uri: string, protocols: readonly string[]): boolean;
|
package/dist/link.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export const SAFE_PROTOCOLS = ["http:", "https:", "mailto:"];
|
|
2
|
+
/**
|
|
3
|
+
* Reads the target of a hyperlink command (`OSC 8 ; params ; uri`). Returns null
|
|
4
|
+
* for other commands, and undefined where a link ends or is not allowed.
|
|
5
|
+
*/
|
|
6
|
+
export function readLink(osc, protocols) {
|
|
7
|
+
if (!osc.startsWith("8;")) {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
const separator = osc.indexOf(";", 2);
|
|
11
|
+
const uri = separator < 0 ? "" : osc.slice(separator + 1);
|
|
12
|
+
return isAllowed(uri, protocols) ? uri : undefined;
|
|
13
|
+
}
|
|
14
|
+
/** Output is untrusted, so a link is only kept if its protocol is known. */
|
|
15
|
+
export function isAllowed(uri, protocols) {
|
|
16
|
+
try {
|
|
17
|
+
return protocols.includes(new URL(uri).protocol);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Color } from "./style.ts";
|
|
2
|
+
export type PaletteEntry = {
|
|
3
|
+
light: string;
|
|
4
|
+
dark: string;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* The 16 named colors, for light and dark pages. Each one has a contrast of at
|
|
8
|
+
* least 4.5:1 on its background, so even "black" is readable on a dark page.
|
|
9
|
+
*/
|
|
10
|
+
export declare const DEFAULT_PALETTE: readonly PaletteEntry[];
|
|
11
|
+
/** The hex value of a color from the 256-color cube and gray ramp. */
|
|
12
|
+
export declare function toHex(color: Color): string;
|
package/dist/palette.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { hex } from "./style.js";
|
|
2
|
+
/**
|
|
3
|
+
* The 16 named colors, for light and dark pages. Each one has a contrast of at
|
|
4
|
+
* least 4.5:1 on its background, so even "black" is readable on a dark page.
|
|
5
|
+
*/
|
|
6
|
+
export const DEFAULT_PALETTE = [
|
|
7
|
+
{ light: "#24292f", dark: "#8b949e" },
|
|
8
|
+
{ light: "#cf222e", dark: "#ff7b72" },
|
|
9
|
+
{ light: "#116329", dark: "#3fb950" },
|
|
10
|
+
{ light: "#7d4e00", dark: "#d29922" },
|
|
11
|
+
{ light: "#0969da", dark: "#58a6ff" },
|
|
12
|
+
{ light: "#8250df", dark: "#bc8cff" },
|
|
13
|
+
{ light: "#1b7c83", dark: "#39c5cf" },
|
|
14
|
+
{ light: "#656d76", dark: "#b1bac4" },
|
|
15
|
+
{ light: "#57606a", dark: "#9ea7b3" },
|
|
16
|
+
{ light: "#a40e26", dark: "#ffa198" },
|
|
17
|
+
{ light: "#1a7f37", dark: "#56d364" },
|
|
18
|
+
{ light: "#633c01", dark: "#e3b341" },
|
|
19
|
+
{ light: "#0550ae", dark: "#79c0ff" },
|
|
20
|
+
{ light: "#6639ba", dark: "#d2a8ff" },
|
|
21
|
+
{ light: "#136061", dark: "#56d4dd" },
|
|
22
|
+
{ light: "#424a53", dark: "#ffffff" },
|
|
23
|
+
];
|
|
24
|
+
const CUBE = [0, 95, 135, 175, 215, 255];
|
|
25
|
+
/** The hex value of a color from the 256-color cube and gray ramp. */
|
|
26
|
+
export function toHex(color) {
|
|
27
|
+
if (typeof color === "string") {
|
|
28
|
+
return color;
|
|
29
|
+
}
|
|
30
|
+
if (color < 16) {
|
|
31
|
+
return DEFAULT_PALETTE[color]?.dark ?? "#000000";
|
|
32
|
+
}
|
|
33
|
+
if (color < 232) {
|
|
34
|
+
const index = color - 16;
|
|
35
|
+
return hex(CUBE[Math.floor(index / 36)] ?? 0, CUBE[Math.floor(index / 6) % 6] ?? 0, CUBE[index % 6] ?? 0);
|
|
36
|
+
}
|
|
37
|
+
const gray = 8 + (color - 232) * 10;
|
|
38
|
+
return hex(gray, gray, gray);
|
|
39
|
+
}
|
package/dist/parser.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type Style } from "./style.ts";
|
|
2
|
+
/** A run of text that looks the same throughout. */
|
|
3
|
+
export type Segment = {
|
|
4
|
+
readonly text: string;
|
|
5
|
+
readonly style: Style;
|
|
6
|
+
readonly link?: string;
|
|
7
|
+
};
|
|
8
|
+
export type ParserOptions = {
|
|
9
|
+
/** Protocols that links may use. Defaults to `http:`, `https:`, `mailto:`. */
|
|
10
|
+
protocols?: readonly string[];
|
|
11
|
+
};
|
|
12
|
+
export type Parser = {
|
|
13
|
+
/** Parses a chunk. Styles and partial sequences carry over to the next one. */
|
|
14
|
+
write(chunk: string): Segment[];
|
|
15
|
+
/** Flushes what is left and resets the parser. */
|
|
16
|
+
end(): Segment[];
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Parses output as it arrives. Text keeps its tabs, line feeds and carriage
|
|
20
|
+
* returns; cursor movement is dropped. Use a screen to follow it instead.
|
|
21
|
+
*/
|
|
22
|
+
export declare function createParser({ protocols, }?: ParserOptions): Parser;
|
|
23
|
+
/** Parses complete output into styled segments. */
|
|
24
|
+
export declare function parse(text: string, options?: ParserOptions): Segment[];
|
package/dist/parser.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { readLink, SAFE_PROTOCOLS } from "./link.js";
|
|
2
|
+
import { createSgr, isSameStyle, PLAIN } from "./style.js";
|
|
3
|
+
import { createTokenizer } from "./tokenizer.js";
|
|
4
|
+
/** Control characters that are part of the text. */
|
|
5
|
+
const TEXT_CONTROLS = {
|
|
6
|
+
0x09: "\t",
|
|
7
|
+
0x0a: "\n",
|
|
8
|
+
0x0d: "\r",
|
|
9
|
+
0x85: "\n",
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Parses output as it arrives. Text keeps its tabs, line feeds and carriage
|
|
13
|
+
* returns; cursor movement is dropped. Use a screen to follow it instead.
|
|
14
|
+
*/
|
|
15
|
+
export function createParser({ protocols = SAFE_PROTOCOLS, } = {}) {
|
|
16
|
+
const applySgr = createSgr();
|
|
17
|
+
let style = PLAIN;
|
|
18
|
+
let link;
|
|
19
|
+
let segments = [];
|
|
20
|
+
const push = (text) => {
|
|
21
|
+
const last = segments[segments.length - 1];
|
|
22
|
+
if (last && last.link === link && isSameStyle(last.style, style)) {
|
|
23
|
+
segments[segments.length - 1] = { ...last, text: last.text + text };
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
segments.push(link === undefined ? { text, style } : { text, style, link });
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const reset = () => {
|
|
30
|
+
style = PLAIN;
|
|
31
|
+
link = undefined;
|
|
32
|
+
};
|
|
33
|
+
const tokenizer = createTokenizer({
|
|
34
|
+
text: push,
|
|
35
|
+
control(code) {
|
|
36
|
+
const text = TEXT_CONTROLS[code];
|
|
37
|
+
if (text)
|
|
38
|
+
push(text);
|
|
39
|
+
},
|
|
40
|
+
csi({ prefix, params, intermediates, final }) {
|
|
41
|
+
if (final === "m" && !prefix && !intermediates) {
|
|
42
|
+
style = applySgr(style, params);
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
esc(intermediates, final) {
|
|
46
|
+
if (final === "c" && !intermediates)
|
|
47
|
+
reset();
|
|
48
|
+
},
|
|
49
|
+
osc(data) {
|
|
50
|
+
const next = readLink(data, protocols);
|
|
51
|
+
if (next !== null)
|
|
52
|
+
link = next;
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
const take = () => {
|
|
56
|
+
const taken = segments;
|
|
57
|
+
segments = [];
|
|
58
|
+
return taken;
|
|
59
|
+
};
|
|
60
|
+
return {
|
|
61
|
+
write(chunk) {
|
|
62
|
+
tokenizer.write(chunk);
|
|
63
|
+
return take();
|
|
64
|
+
},
|
|
65
|
+
end() {
|
|
66
|
+
tokenizer.end();
|
|
67
|
+
const rest = take();
|
|
68
|
+
reset();
|
|
69
|
+
return rest;
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/** Parses complete output into styled segments. */
|
|
74
|
+
export function parse(text, options) {
|
|
75
|
+
const parser = createParser(options);
|
|
76
|
+
const segments = parser.write(text);
|
|
77
|
+
for (const segment of parser.end()) {
|
|
78
|
+
segments.push(segment);
|
|
79
|
+
}
|
|
80
|
+
return segments;
|
|
81
|
+
}
|
package/dist/row.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Segment } from "./parser.ts";
|
|
2
|
+
import { type Style } from "./style.ts";
|
|
3
|
+
/** A line as shown. It is replaced, never changed, so it can be memoized. */
|
|
4
|
+
export type Line = {
|
|
5
|
+
/** Stays the same while the line is updated, and is never reused. */
|
|
6
|
+
readonly id: number;
|
|
7
|
+
readonly segments: readonly Segment[];
|
|
8
|
+
};
|
|
9
|
+
type Link = string | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* A line of the screen. Most lines are only ever added to, so text is kept in
|
|
12
|
+
* runs, which cost no more than the text itself. Cells are only made once
|
|
13
|
+
* something in the middle changes.
|
|
14
|
+
*/
|
|
15
|
+
export declare class Row {
|
|
16
|
+
#private;
|
|
17
|
+
readonly id: number;
|
|
18
|
+
width: number;
|
|
19
|
+
constructor(id: number);
|
|
20
|
+
/** Writes narrow text, or a single character of any width. */
|
|
21
|
+
write(x: number, text: string, width: number, style: Style, link: Link): void;
|
|
22
|
+
/** Adds to the character that ends at `x`, which may make it wider. */
|
|
23
|
+
join(x: number, width: number, text: string, grow: number): void;
|
|
24
|
+
blank(from: number, to: number): void;
|
|
25
|
+
truncate(length: number): void;
|
|
26
|
+
remove(x: number, count: number): void;
|
|
27
|
+
insert(x: number, count: number, limit: number): void;
|
|
28
|
+
line(): Line;
|
|
29
|
+
/** Keeps the line as it is now and lets go of everything needed to change it. */
|
|
30
|
+
freeze(): void;
|
|
31
|
+
}
|
|
32
|
+
export {};
|