@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/dist/row.js
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { isSameStyle, PLAIN } from "./style.js";
|
|
2
|
+
import { clusters, clusterWidth, isNarrow } from "./width.js";
|
|
3
|
+
/**
|
|
4
|
+
* A line of the screen. Most lines are only ever added to, so text is kept in
|
|
5
|
+
* runs, which cost no more than the text itself. Cells are only made once
|
|
6
|
+
* something in the middle changes.
|
|
7
|
+
*/
|
|
8
|
+
export class Row {
|
|
9
|
+
id;
|
|
10
|
+
width = 0;
|
|
11
|
+
#runs = [];
|
|
12
|
+
#cells = null;
|
|
13
|
+
#line = null;
|
|
14
|
+
constructor(id) {
|
|
15
|
+
this.id = id;
|
|
16
|
+
}
|
|
17
|
+
/** Writes narrow text, or a single character of any width. */
|
|
18
|
+
write(x, text, width, style, link) {
|
|
19
|
+
this.#line = null;
|
|
20
|
+
if (!this.#cells && x >= this.width) {
|
|
21
|
+
if (x > this.width)
|
|
22
|
+
this.#append(" ".repeat(x - this.width), PLAIN, undefined);
|
|
23
|
+
this.#append(text, style, link);
|
|
24
|
+
this.width = x + width;
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const cells = this.#toCells();
|
|
28
|
+
this.#pad(cells, x);
|
|
29
|
+
this.#split(cells, x);
|
|
30
|
+
this.#split(cells, x + width);
|
|
31
|
+
if (isNarrow(text)) {
|
|
32
|
+
for (let i = 0; i < text.length; i++) {
|
|
33
|
+
this.#set(cells, x + i, text[i], style, link);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
this.#setWide(cells, x, text, width, style, link);
|
|
38
|
+
}
|
|
39
|
+
this.width = cells.chars.length;
|
|
40
|
+
}
|
|
41
|
+
/** Adds to the character that ends at `x`, which may make it wider. */
|
|
42
|
+
join(x, width, text, grow) {
|
|
43
|
+
this.#line = null;
|
|
44
|
+
if (!this.#cells && x === this.width) {
|
|
45
|
+
const run = this.#runs.at(-1);
|
|
46
|
+
if (run)
|
|
47
|
+
run.text += text;
|
|
48
|
+
this.width += grow;
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const cells = this.#toCells();
|
|
52
|
+
const start = x - width;
|
|
53
|
+
const style = cells.styles?.[start] ?? PLAIN;
|
|
54
|
+
const link = cells.links?.[start];
|
|
55
|
+
if (grow) {
|
|
56
|
+
this.#pad(cells, x);
|
|
57
|
+
this.#split(cells, x + grow);
|
|
58
|
+
for (let i = 0; i < grow; i++)
|
|
59
|
+
this.#set(cells, x + i, "", style, link);
|
|
60
|
+
}
|
|
61
|
+
cells.chars[start] += text;
|
|
62
|
+
this.width = cells.chars.length;
|
|
63
|
+
}
|
|
64
|
+
blank(from, to) {
|
|
65
|
+
const end = Math.min(to, this.width);
|
|
66
|
+
if (from >= end)
|
|
67
|
+
return;
|
|
68
|
+
this.#line = null;
|
|
69
|
+
const cells = this.#toCells();
|
|
70
|
+
this.#split(cells, from);
|
|
71
|
+
this.#split(cells, end);
|
|
72
|
+
for (let i = from; i < end; i++)
|
|
73
|
+
this.#set(cells, i, " ", PLAIN, undefined);
|
|
74
|
+
}
|
|
75
|
+
truncate(length) {
|
|
76
|
+
if (length >= this.width)
|
|
77
|
+
return;
|
|
78
|
+
this.#line = null;
|
|
79
|
+
if (length === 0) {
|
|
80
|
+
this.#runs = [];
|
|
81
|
+
this.#cells = null;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
const { chars, styles, links } = this.#toCells();
|
|
85
|
+
this.#split(this.#toCells(), length);
|
|
86
|
+
chars.length = length;
|
|
87
|
+
if (styles)
|
|
88
|
+
styles.length = length;
|
|
89
|
+
if (links)
|
|
90
|
+
links.length = length;
|
|
91
|
+
}
|
|
92
|
+
this.width = length;
|
|
93
|
+
}
|
|
94
|
+
remove(x, count) {
|
|
95
|
+
if (x >= this.width)
|
|
96
|
+
return;
|
|
97
|
+
this.#line = null;
|
|
98
|
+
const cells = this.#toCells();
|
|
99
|
+
this.#split(cells, x);
|
|
100
|
+
this.#split(cells, x + count);
|
|
101
|
+
cells.chars.splice(x, count);
|
|
102
|
+
cells.styles?.splice(x, count);
|
|
103
|
+
cells.links?.splice(x, count);
|
|
104
|
+
this.width = cells.chars.length;
|
|
105
|
+
}
|
|
106
|
+
insert(x, count, limit) {
|
|
107
|
+
// Everything after the cursor would be pushed off the line.
|
|
108
|
+
if (x + count >= limit)
|
|
109
|
+
return this.truncate(x);
|
|
110
|
+
if (x >= this.width || count <= 0)
|
|
111
|
+
return;
|
|
112
|
+
this.#line = null;
|
|
113
|
+
const cells = this.#toCells();
|
|
114
|
+
this.#split(cells, x);
|
|
115
|
+
cells.chars.splice(x, 0, ...fill(count, " "));
|
|
116
|
+
cells.styles?.splice(x, 0, ...fill(count, PLAIN));
|
|
117
|
+
cells.links?.splice(x, 0, ...fill(count, undefined));
|
|
118
|
+
this.width = cells.chars.length;
|
|
119
|
+
this.truncate(limit);
|
|
120
|
+
}
|
|
121
|
+
line() {
|
|
122
|
+
return (this.#line ??= { id: this.id, segments: this.#segments() });
|
|
123
|
+
}
|
|
124
|
+
/** Keeps the line as it is now and lets go of everything needed to change it. */
|
|
125
|
+
freeze() {
|
|
126
|
+
this.line();
|
|
127
|
+
this.#runs = [];
|
|
128
|
+
this.#cells = null;
|
|
129
|
+
}
|
|
130
|
+
#append(text, style, link) {
|
|
131
|
+
const last = this.#runs.at(-1);
|
|
132
|
+
if (last && last.link === link && isSameStyle(last.style, style)) {
|
|
133
|
+
last.text += text;
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
this.#runs.push({ text, style, link });
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
#toCells() {
|
|
140
|
+
if (this.#cells)
|
|
141
|
+
return this.#cells;
|
|
142
|
+
const cells = { chars: [], styles: null, links: null };
|
|
143
|
+
this.#cells = cells;
|
|
144
|
+
for (const { text, style, link } of this.#runs) {
|
|
145
|
+
for (const cluster of clusters(text)) {
|
|
146
|
+
const width = clusterWidth(cluster);
|
|
147
|
+
const end = cells.chars.length;
|
|
148
|
+
if (width === 0) {
|
|
149
|
+
let target = end - 1;
|
|
150
|
+
while (target > 0 && cells.chars[target] === "")
|
|
151
|
+
target--;
|
|
152
|
+
if (target >= 0)
|
|
153
|
+
cells.chars[target] += cluster;
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
this.#setWide(cells, end, cluster, width, style, link);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
this.#runs = [];
|
|
160
|
+
this.width = cells.chars.length;
|
|
161
|
+
return cells;
|
|
162
|
+
}
|
|
163
|
+
#pad(cells, length) {
|
|
164
|
+
for (let i = cells.chars.length; i < length; i++) {
|
|
165
|
+
this.#set(cells, i, " ", PLAIN, undefined);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
#setWide(cells, index, char, width, style, link) {
|
|
169
|
+
this.#set(cells, index, char, style, link);
|
|
170
|
+
for (let i = 1; i < width; i++)
|
|
171
|
+
this.#set(cells, index + i, "", style, link);
|
|
172
|
+
}
|
|
173
|
+
#set(cells, index, char, style, link) {
|
|
174
|
+
cells.chars[index] = char;
|
|
175
|
+
if (cells.styles)
|
|
176
|
+
cells.styles[index] = style;
|
|
177
|
+
else if (style !== PLAIN) {
|
|
178
|
+
cells.styles = fill(Math.max(cells.chars.length, index + 1), PLAIN);
|
|
179
|
+
cells.styles[index] = style;
|
|
180
|
+
}
|
|
181
|
+
if (cells.links)
|
|
182
|
+
cells.links[index] = link;
|
|
183
|
+
else if (link !== undefined) {
|
|
184
|
+
cells.links = fill(Math.max(cells.chars.length, index + 1), undefined);
|
|
185
|
+
cells.links[index] = link;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/** Part of a wide character is not a character, so it all becomes spaces. */
|
|
189
|
+
#split({ chars }, index) {
|
|
190
|
+
if (index <= 0 || index >= chars.length || chars[index] !== "")
|
|
191
|
+
return;
|
|
192
|
+
let start = index;
|
|
193
|
+
while (start > 0 && chars[start] === "")
|
|
194
|
+
start--;
|
|
195
|
+
let end = index;
|
|
196
|
+
while (end < chars.length && chars[end] === "")
|
|
197
|
+
end++;
|
|
198
|
+
for (let i = start; i < end; i++)
|
|
199
|
+
chars[i] = " ";
|
|
200
|
+
}
|
|
201
|
+
#segments() {
|
|
202
|
+
const segments = this.#cells
|
|
203
|
+
? cellSegments(this.#cells)
|
|
204
|
+
: this.#runs.map(toSegment);
|
|
205
|
+
// Plain spaces at the end were only skipped over or erased.
|
|
206
|
+
for (let last = segments.at(-1); last; last = segments.at(-1)) {
|
|
207
|
+
if (last.style !== PLAIN || last.link !== undefined)
|
|
208
|
+
break;
|
|
209
|
+
const text = trimSpaces(last.text);
|
|
210
|
+
if (text) {
|
|
211
|
+
segments[segments.length - 1] = { ...last, text };
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
segments.pop();
|
|
215
|
+
}
|
|
216
|
+
return segments;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
function toSegment({ text, style, link }) {
|
|
220
|
+
return link === undefined ? { text, style } : { text, style, link };
|
|
221
|
+
}
|
|
222
|
+
function cellSegments({ chars, styles, links }) {
|
|
223
|
+
const segments = [];
|
|
224
|
+
let start = 0;
|
|
225
|
+
for (let i = 1; i <= chars.length; i++) {
|
|
226
|
+
const style = styles?.[start] ?? PLAIN;
|
|
227
|
+
const link = links?.[start];
|
|
228
|
+
if (i === chars.length ||
|
|
229
|
+
links?.[i] !== link ||
|
|
230
|
+
!isSameStyle(styles?.[i] ?? PLAIN, style)) {
|
|
231
|
+
segments.push(toSegment({ text: chars.slice(start, i).join(""), style, link }));
|
|
232
|
+
start = i;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return segments;
|
|
236
|
+
}
|
|
237
|
+
/** Like `trimEnd`, but only for spaces, and fast for long lines. */
|
|
238
|
+
function trimSpaces(text) {
|
|
239
|
+
let end = text.length;
|
|
240
|
+
while (end > 0 && text.charCodeAt(end - 1) === 0x20)
|
|
241
|
+
end--;
|
|
242
|
+
return text.slice(0, end);
|
|
243
|
+
}
|
|
244
|
+
function fill(length, value) {
|
|
245
|
+
return Array.from({ length }, () => value);
|
|
246
|
+
}
|
package/dist/screen.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ParserOptions } from "./parser.ts";
|
|
2
|
+
import { type Line } from "./row.ts";
|
|
3
|
+
export type ScreenOptions = ParserOptions & {
|
|
4
|
+
/** Wraps lines at this width. Set it to the width of the PTY, if there is one. */
|
|
5
|
+
columns?: number;
|
|
6
|
+
/** Height of the part that can still change. Match the PTY. Defaults to 24. */
|
|
7
|
+
rows?: number;
|
|
8
|
+
/** Keeps only this many lines, dropping the oldest. Unlimited by default. */
|
|
9
|
+
maxLines?: number;
|
|
10
|
+
};
|
|
11
|
+
export type Screen = {
|
|
12
|
+
write(chunk: string): void;
|
|
13
|
+
/** Flushes a partial character. The screen stays readable and writable. */
|
|
14
|
+
end(): void;
|
|
15
|
+
/** What a person would see. The same array until something changes. */
|
|
16
|
+
lines(): readonly Line[];
|
|
17
|
+
/** How many lines `maxLines` has dropped. */
|
|
18
|
+
readonly droppedLines: number;
|
|
19
|
+
/** Back to an empty screen. */
|
|
20
|
+
reset(): void;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Follows the cursor the way a terminal does, so spinners, progress bars and
|
|
24
|
+
* redrawn lines end up as the lines a person would see. A line feed also
|
|
25
|
+
* returns to the first column, since captured output rarely has `\r\n`.
|
|
26
|
+
*/
|
|
27
|
+
export declare function createScreen({ columns, rows: height, maxLines, protocols, }?: ScreenOptions): Screen;
|