-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathterm.ts
More file actions
158 lines (132 loc) · 3.81 KB
/
term.ts
File metadata and controls
158 lines (132 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { type Op, pack } from "./ops.ts";
import { type BoundingBox, createTermNative } from "./term-native.ts";
export interface TermOptions {
height: number;
width: number;
}
export interface RenderOptions {
mode?: "line";
/**
* Row where to begin rendering. This should only be used when
* rendering into a region as part of the CLI main screen. For
* interfaces that use the entire screen, leave unset which will
* default to 0. This is 1-based which which is the DSR native
* format.
*
* https://www.ecma-international.org/publications-and-standards/standards/ecma-48/
*/
row?: number;
pointer?: {
x: number;
y: number;
down: boolean;
};
}
export type PointerEvent =
| { type: "pointerenter"; id: string }
| { type: "pointerleave"; id: string }
| { type: "pointerclick"; id: string };
export type { BoundingBox };
export interface ElementInfo {
bounds: BoundingBox;
}
const ERROR_TYPES = [
"TEXT_MEASUREMENT_FUNCTION_NOT_PROVIDED",
"ARENA_CAPACITY_EXCEEDED",
"ELEMENTS_CAPACITY_EXCEEDED",
"TEXT_MEASUREMENT_CAPACITY_EXCEEDED",
"DUPLICATE_ID",
"FLOATING_CONTAINER_PARENT_NOT_FOUND",
"PERCENTAGE_OVER_1",
"INTERNAL_ERROR",
"UNBALANCED_OPEN_CLOSE",
] as const;
export interface ClayError {
type: string;
message: string;
}
export interface RenderInfo {
get(id: string): ElementInfo | undefined;
}
export interface RenderResult {
output: Uint8Array;
events: PointerEvent[];
info: RenderInfo;
errors: ClayError[];
}
export interface Term {
render(ops: Op[], options?: RenderOptions): RenderResult;
}
export async function createTerm(options: TermOptions): Promise<Term> {
let { width, height } = options;
let native = await createTermNative(width, height);
let { memory, statePtr, opsBuf } = native;
let prev = new Set<string>();
let pressed = new Set<string>();
let wasDown = false;
return {
render(ops: Op[], options?: RenderOptions): RenderResult {
let len = pack(ops, memory.buffer, opsBuf, memory.buffer.byteLength);
let mode = options?.mode === "line" ? 1 : 0;
let row = options?.row ?? 1;
native.reduce(statePtr, opsBuf, len, mode, row);
if (options?.pointer) {
let { x, y, down } = options.pointer;
native.setPointer(x, y, down);
}
let output = new Uint8Array(
memory.buffer,
native.output(statePtr),
native.length(statePtr),
);
let current = new Set(
options?.pointer ? native.getPointerOverIds() : [],
);
let down = options?.pointer?.down ?? false;
let events: PointerEvent[] = [];
for (let id of current) {
if (!prev.has(id)) {
events.push({ type: "pointerenter", id });
}
}
for (let id of prev) {
if (!current.has(id)) {
events.push({ type: "pointerleave", id });
}
}
if (wasDown && !down) {
for (let id of pressed) {
if (current.has(id)) {
events.push({ type: "pointerclick", id });
}
}
}
if (down && !wasDown) {
pressed = new Set(current);
} else if (!down) {
pressed.clear();
}
prev = current;
wasDown = down;
let info: RenderInfo = {
get(id: string): ElementInfo | undefined {
let bounds = native.getElementBounds(id);
if (bounds) {
return { bounds };
}
return undefined;
},
};
let errors: ClayError[] = [];
let count = native.errorCount(statePtr);
for (let i = 0; i < count; i++) {
let code = native.errorType(statePtr, i);
errors.push({
type: ERROR_TYPES[code] ?? `UNKNOWN_${code}`,
message: native.errorMessage(statePtr, i),
});
}
return { output, events, info, errors };
},
};
}