@gjermundgaraba/clankermux 0.0.0-stage → 0.1.1
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/README.md +96 -2
- package/dist/client/attachment.d.ts +71 -0
- package/dist/client/attachment.js +240 -0
- package/dist/client/connection.d.ts +51 -0
- package/dist/client/connection.js +171 -0
- package/dist/client/index.d.ts +10 -0
- package/dist/client/index.js +4 -0
- package/dist/client/keyboard.d.ts +24 -0
- package/dist/client/keyboard.js +32 -0
- package/dist/client/mount.d.ts +65 -0
- package/dist/client/mount.js +177 -0
- package/dist/client/osc52.d.ts +14 -0
- package/dist/client/osc52.js +88 -0
- package/dist/client/profile.d.ts +10 -0
- package/dist/client/profile.js +30 -0
- package/dist/client/worker.d.ts +120 -0
- package/dist/client/worker.js +566 -0
- package/dist/engine/decoder.d.ts +45 -0
- package/dist/engine/decoder.js +137 -0
- package/dist/engine/index.d.ts +110 -0
- package/dist/engine/index.js +372 -0
- package/dist/engine/keys.d.ts +39 -0
- package/dist/engine/keys.js +66 -0
- package/dist/engine/pin.d.ts +2 -0
- package/dist/engine/pin.js +3 -0
- package/dist/engine/render.d.ts +79 -0
- package/dist/engine/render.js +224 -0
- package/dist/proto/frame.d.ts +47 -0
- package/dist/proto/frame.js +78 -0
- package/dist/proto/index.d.ts +3 -0
- package/dist/proto/index.js +3 -0
- package/dist/proto/messages.d.ts +218 -0
- package/dist/proto/messages.js +1 -0
- package/dist/proto/stream.d.ts +11 -0
- package/dist/proto/stream.js +30 -0
- package/dist/renderer/index.d.ts +44 -0
- package/dist/renderer/index.js +138 -0
- package/ghostty-vt.wasm +0 -0
- package/package.json +48 -3
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const MODS = { SHIFT: 1 << 0, CTRL: 1 << 1, ALT: 1 << 2, SUPER: 1 << 3 };
|
|
2
|
+
/** `KeyboardEvent.code` as a `GhosttyKey` member name: `KeyA` → `A`, `Digit0` → `DIGIT_0`. */
|
|
3
|
+
export function keyName(code) {
|
|
4
|
+
if (/^F\d+$/.test(code))
|
|
5
|
+
return code;
|
|
6
|
+
const stripped = code.startsWith("Key") ? code.slice(3) : code;
|
|
7
|
+
return stripped
|
|
8
|
+
.replace(/([a-z])([A-Z0-9])/g, "$1_$2")
|
|
9
|
+
.replace(/([0-9])([A-Z])/g, "$1_$2")
|
|
10
|
+
.toUpperCase();
|
|
11
|
+
}
|
|
12
|
+
/** Whether `key` is text the key produces rather than a key name such as
|
|
13
|
+
* "Enter" or "ArrowUp" (which, per UI Events, is ASCII letters and digits
|
|
14
|
+
* starting with a capital, two or more of them; text of that shape is one
|
|
15
|
+
* character). */
|
|
16
|
+
export function isText(key) {
|
|
17
|
+
if (key.length === 0)
|
|
18
|
+
return false;
|
|
19
|
+
const cp = key.codePointAt(0);
|
|
20
|
+
if (cp < 0x20 || cp === 0x7f)
|
|
21
|
+
return false;
|
|
22
|
+
return key.length === 1 || !/^[A-Z][A-Za-z0-9]+$/.test(key);
|
|
23
|
+
}
|
|
24
|
+
/** Encodes browser key events with the engine, following the terminal's modes. */
|
|
25
|
+
export class KeyEncoder {
|
|
26
|
+
engine;
|
|
27
|
+
encoder;
|
|
28
|
+
event;
|
|
29
|
+
unidentified;
|
|
30
|
+
constructor(engine) {
|
|
31
|
+
this.engine = engine;
|
|
32
|
+
this.encoder = engine.construct("ghostty_key_encoder_new");
|
|
33
|
+
this.event = engine.construct("ghostty_key_event_new");
|
|
34
|
+
this.unidentified = engine.enumValue("GhosttyKey", "UNIDENTIFIED");
|
|
35
|
+
}
|
|
36
|
+
/** The bytes to send for `input`, empty when the key encodes to nothing. */
|
|
37
|
+
encode(term, input) {
|
|
38
|
+
const e = this.engine;
|
|
39
|
+
e.call("ghostty_key_encoder_setopt_from_terminal", this.encoder, term.handle);
|
|
40
|
+
const name = keyName(input.code);
|
|
41
|
+
const key = e.hasEnumValue("GhosttyKey", name) ? e.enumValue("GhosttyKey", name) : this.unidentified;
|
|
42
|
+
// Modifiers that composed the text are not modifiers of the key.
|
|
43
|
+
const composed = input.altComposed === true;
|
|
44
|
+
const mods = (input.shift ? MODS.SHIFT : 0) |
|
|
45
|
+
(input.ctrl && !composed ? MODS.CTRL : 0) |
|
|
46
|
+
(input.alt && !composed ? MODS.ALT : 0) |
|
|
47
|
+
(input.meta ? MODS.SUPER : 0);
|
|
48
|
+
e.call("ghostty_key_event_set_action", this.event, e.enumValue("GhosttyKeyAction", input.action.toUpperCase()));
|
|
49
|
+
e.call("ghostty_key_event_set_key", this.event, key);
|
|
50
|
+
e.call("ghostty_key_event_set_mods", this.event, mods);
|
|
51
|
+
e.call("ghostty_key_event_set_consumed_mods", this.event, 0);
|
|
52
|
+
e.call("ghostty_key_event_set_composing", this.event, input.composing ? 1 : 0);
|
|
53
|
+
const text = isText(input.key) ? input.key : "";
|
|
54
|
+
const utf8 = new TextEncoder().encode(text);
|
|
55
|
+
return e.withBytes(utf8, (textPtr) => {
|
|
56
|
+
e.call("ghostty_key_event_set_utf8", this.event, textPtr, utf8.length);
|
|
57
|
+
const base = input.unshifted ?? (text ? text.toLowerCase() : "");
|
|
58
|
+
e.call("ghostty_key_event_set_unshifted_codepoint", this.event, base ? base.codePointAt(0) : 0);
|
|
59
|
+
return e.growing("ghostty_key_encoder_encode", 64, (buf, cap, len) => e.call("ghostty_key_encoder_encode", this.encoder, this.event, buf, cap, len));
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
free() {
|
|
63
|
+
this.engine.call("ghostty_key_event_free", this.event);
|
|
64
|
+
this.engine.call("ghostty_key_encoder_free", this.encoder);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { type Engine, type Terminal } from "./index.ts";
|
|
2
|
+
/** Style flag bits of a [`Cell`]. */
|
|
3
|
+
export declare const FLAG: {
|
|
4
|
+
readonly BOLD: 1;
|
|
5
|
+
readonly ITALIC: 2;
|
|
6
|
+
readonly FAINT: 4;
|
|
7
|
+
readonly BLINK: 8;
|
|
8
|
+
readonly INVERSE: 16;
|
|
9
|
+
readonly INVISIBLE: 32;
|
|
10
|
+
readonly STRIKETHROUGH: 64;
|
|
11
|
+
readonly OVERLINE: 128;
|
|
12
|
+
/** The cell is the left half of a wide character. */
|
|
13
|
+
readonly WIDE: 256;
|
|
14
|
+
/** The cell is the right half of a wide character; draw nothing. */
|
|
15
|
+
readonly SPACER: 512;
|
|
16
|
+
};
|
|
17
|
+
/** No explicit colour; the renderer uses the default. */
|
|
18
|
+
export declare const NO_COLOR = -1;
|
|
19
|
+
export interface Cell {
|
|
20
|
+
/** The grapheme, empty for a blank cell. */
|
|
21
|
+
text: string;
|
|
22
|
+
/** 0xRRGGBB or NO_COLOR. */
|
|
23
|
+
fg: number;
|
|
24
|
+
bg: number;
|
|
25
|
+
flags: number;
|
|
26
|
+
/** `GhosttySgrUnderline`: 0 none, 1 single, 2 double, 3 curly, 4 dotted, 5 dashed. */
|
|
27
|
+
underline: number;
|
|
28
|
+
}
|
|
29
|
+
/** `GhosttyRenderStateCursorVisualStyle` by name. */
|
|
30
|
+
declare const CURSOR_STYLES: readonly ["bar", "block", "underline", "block_hollow"];
|
|
31
|
+
type CursorStyle = (typeof CURSOR_STYLES)[number];
|
|
32
|
+
export interface FrameRow {
|
|
33
|
+
y: number;
|
|
34
|
+
cells: Cell[];
|
|
35
|
+
}
|
|
36
|
+
export interface Frame {
|
|
37
|
+
cols: number;
|
|
38
|
+
rows: number;
|
|
39
|
+
/** Every row is included and the renderer should clear first. */
|
|
40
|
+
full: boolean;
|
|
41
|
+
background: number;
|
|
42
|
+
foreground: number;
|
|
43
|
+
cursor: {
|
|
44
|
+
x: number;
|
|
45
|
+
y: number;
|
|
46
|
+
visible: boolean;
|
|
47
|
+
blinking: boolean;
|
|
48
|
+
style: CursorStyle;
|
|
49
|
+
color: number;
|
|
50
|
+
} | undefined;
|
|
51
|
+
/** The rows that changed, or every row when `full`. */
|
|
52
|
+
lines: FrameRow[];
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The engine's render state: updated from a terminal, read into a
|
|
56
|
+
* [`Frame`] of the rows that changed, then cleaned.
|
|
57
|
+
*/
|
|
58
|
+
export declare class RenderState {
|
|
59
|
+
readonly engine: Engine;
|
|
60
|
+
private readonly state;
|
|
61
|
+
private readonly iterator;
|
|
62
|
+
private readonly cells;
|
|
63
|
+
private readonly keys;
|
|
64
|
+
private readonly tags;
|
|
65
|
+
private readonly wide;
|
|
66
|
+
/** `GhosttyCell` bit fields, from the manifest. */
|
|
67
|
+
private readonly cell;
|
|
68
|
+
private readonly cursorStyles;
|
|
69
|
+
constructor(engine: Engine);
|
|
70
|
+
/** Captures the terminal and returns the rows that changed since the last `clean`. */
|
|
71
|
+
update(term: Terminal): Frame;
|
|
72
|
+
private readRow;
|
|
73
|
+
private color;
|
|
74
|
+
private graphemes;
|
|
75
|
+
/** Marks the frame as drawn. */
|
|
76
|
+
clean(): void;
|
|
77
|
+
free(): void;
|
|
78
|
+
}
|
|
79
|
+
export {};
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/** Style flag bits of a [`Cell`]. */
|
|
2
|
+
export const FLAG = {
|
|
3
|
+
BOLD: 1,
|
|
4
|
+
ITALIC: 2,
|
|
5
|
+
FAINT: 4,
|
|
6
|
+
BLINK: 8,
|
|
7
|
+
INVERSE: 16,
|
|
8
|
+
INVISIBLE: 32,
|
|
9
|
+
STRIKETHROUGH: 64,
|
|
10
|
+
OVERLINE: 128,
|
|
11
|
+
/** The cell is the left half of a wide character. */
|
|
12
|
+
WIDE: 256,
|
|
13
|
+
/** The cell is the right half of a wide character; draw nothing. */
|
|
14
|
+
SPACER: 512,
|
|
15
|
+
};
|
|
16
|
+
/** No explicit colour; the renderer uses the default. */
|
|
17
|
+
export const NO_COLOR = -1;
|
|
18
|
+
/** `GhosttyRenderStateCursorVisualStyle` by name. */
|
|
19
|
+
const CURSOR_STYLES = ["bar", "block", "underline", "block_hollow"];
|
|
20
|
+
/**
|
|
21
|
+
* The engine's render state: updated from a terminal, read into a
|
|
22
|
+
* [`Frame`] of the rows that changed, then cleaned.
|
|
23
|
+
*/
|
|
24
|
+
export class RenderState {
|
|
25
|
+
engine;
|
|
26
|
+
state;
|
|
27
|
+
iterator;
|
|
28
|
+
cells;
|
|
29
|
+
keys;
|
|
30
|
+
tags;
|
|
31
|
+
wide;
|
|
32
|
+
/** `GhosttyCell` bit fields, from the manifest. */
|
|
33
|
+
cell;
|
|
34
|
+
cursorStyles;
|
|
35
|
+
constructor(engine) {
|
|
36
|
+
this.engine = engine;
|
|
37
|
+
this.state = engine.construct("ghostty_render_state_new");
|
|
38
|
+
this.iterator = engine.construct("ghostty_render_state_row_iterator_new");
|
|
39
|
+
this.cells = engine.construct("ghostty_render_state_row_cells_new");
|
|
40
|
+
const values = (name, members) => Object.fromEntries(members.map((m) => [m, engine.enumValue(name, m)]));
|
|
41
|
+
this.keys = {
|
|
42
|
+
data: values("GhosttyRenderStateData", ["COLS", "ROWS", "DIRTY", "ROW_ITERATOR", "COLORS", "CURSOR"]),
|
|
43
|
+
row: values("GhosttyRenderStateRowData", ["CELLS"]),
|
|
44
|
+
cell: values("GhosttyRenderStateRowCellsData", [
|
|
45
|
+
"RAW",
|
|
46
|
+
"STYLE",
|
|
47
|
+
"BG_COLOR",
|
|
48
|
+
"FG_COLOR",
|
|
49
|
+
"HAS_STYLING",
|
|
50
|
+
"GRAPHEMES_UTF8",
|
|
51
|
+
]),
|
|
52
|
+
};
|
|
53
|
+
this.tags = {
|
|
54
|
+
codepoint: engine.enumValue("GhosttyCellContentTag", "CODEPOINT"),
|
|
55
|
+
grapheme: engine.enumValue("GhosttyCellContentTag", "CODEPOINT_GRAPHEME"),
|
|
56
|
+
};
|
|
57
|
+
this.wide = {
|
|
58
|
+
wide: engine.enumValue("GhosttyCellWide", "WIDE"),
|
|
59
|
+
tail: engine.enumValue("GhosttyCellWide", "SPACER_TAIL"),
|
|
60
|
+
};
|
|
61
|
+
this.cell = {
|
|
62
|
+
tag: engine.bits("GhosttyCell", "content_tag"),
|
|
63
|
+
codepoint: engine.bits("GhosttyCell", "content", "CODEPOINT", "codepoint"),
|
|
64
|
+
wide: engine.bits("GhosttyCell", "wide"),
|
|
65
|
+
};
|
|
66
|
+
this.cursorStyles = new Map(CURSOR_STYLES.map((style) => [engine.enumValue("GhosttyRenderStateCursorVisualStyle", style.toUpperCase()), style]));
|
|
67
|
+
}
|
|
68
|
+
/** Captures the terminal and returns the rows that changed since the last `clean`. */
|
|
69
|
+
update(term) {
|
|
70
|
+
const e = this.engine;
|
|
71
|
+
e.check("ghostty_render_state_update", this.state, term.handle);
|
|
72
|
+
const u16 = (key) => e.scratch(4, (out) => {
|
|
73
|
+
e.check("ghostty_render_state_get", this.state, key, out);
|
|
74
|
+
return e.view.getUint16(out, true);
|
|
75
|
+
});
|
|
76
|
+
const cols = u16(this.keys.data["COLS"]);
|
|
77
|
+
const rows = u16(this.keys.data["ROWS"]);
|
|
78
|
+
const dirty = e.scratch(4, (out) => {
|
|
79
|
+
e.check("ghostty_render_state_get", this.state, this.keys.data["DIRTY"], out);
|
|
80
|
+
return e.view.getInt32(out, true);
|
|
81
|
+
});
|
|
82
|
+
const full = dirty === e.enumValue("GhosttyRenderStateDirty", "FULL");
|
|
83
|
+
const { background, foreground, cursorColor } = e.withBytes(e.pack("GhosttyRenderStateColors", {}), (colors) => {
|
|
84
|
+
e.check("ghostty_render_state_get", this.state, this.keys.data["COLORS"], colors);
|
|
85
|
+
const rgb = (key) => e.readRgb(colors + e.offsetOf("GhosttyRenderStateColors", key));
|
|
86
|
+
return {
|
|
87
|
+
background: rgb("background"),
|
|
88
|
+
foreground: rgb("foreground"),
|
|
89
|
+
cursorColor: e.field("GhosttyRenderStateColors", colors, "cursor_has_value") ? rgb("cursor") : NO_COLOR,
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
const cursor = e.withBytes(e.pack("GhosttyRenderStateCursor", {}), (ptr) => {
|
|
93
|
+
e.check("ghostty_render_state_get", this.state, this.keys.data["CURSOR"], ptr);
|
|
94
|
+
const f = (key) => e.field("GhosttyRenderStateCursor", ptr, key);
|
|
95
|
+
return f("viewport_has_value")
|
|
96
|
+
? {
|
|
97
|
+
x: f("viewport_x"),
|
|
98
|
+
y: f("viewport_y"),
|
|
99
|
+
visible: f("visible") !== 0,
|
|
100
|
+
blinking: f("blinking") !== 0,
|
|
101
|
+
style: this.cursorStyles.get(f("visual_style")) ?? "block",
|
|
102
|
+
color: cursorColor,
|
|
103
|
+
}
|
|
104
|
+
: undefined;
|
|
105
|
+
});
|
|
106
|
+
const lines = [];
|
|
107
|
+
// Iterator and cells containers are passed by address of their handle.
|
|
108
|
+
e.scratch(8, (out) => {
|
|
109
|
+
e.view.setUint32(out, this.iterator, true);
|
|
110
|
+
e.check("ghostty_render_state_get", this.state, this.keys.data["ROW_ITERATOR"], out);
|
|
111
|
+
while (e.call("ghostty_render_state_row_iterator_next_dirty", this.iterator, out + 4)) {
|
|
112
|
+
const y = e.view.getUint16(out + 4, true);
|
|
113
|
+
lines.push({ y, cells: this.readRow(cols) });
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
return { cols, rows, full, background, foreground, cursor, lines };
|
|
117
|
+
}
|
|
118
|
+
readRow(cols) {
|
|
119
|
+
const e = this.engine;
|
|
120
|
+
e.scratch(4, (out) => {
|
|
121
|
+
e.view.setUint32(out, this.cells, true);
|
|
122
|
+
e.check("ghostty_render_state_row_get", this.iterator, this.keys.row["CELLS"], out);
|
|
123
|
+
});
|
|
124
|
+
const styleSize = e.sizeOf("GhosttyStyle");
|
|
125
|
+
const cells = [];
|
|
126
|
+
// Scratch: raw cell (8), colour (3, padded to 8), the 8-aligned style,
|
|
127
|
+
// a UTF-8 buffer struct and its bytes.
|
|
128
|
+
const utf8Cap = 64;
|
|
129
|
+
e.scratch(16 + styleSize + e.sizeOf("GhosttyBuffer") + utf8Cap, (scratch) => {
|
|
130
|
+
const raw = scratch;
|
|
131
|
+
const color = scratch + 8;
|
|
132
|
+
const style = scratch + 16;
|
|
133
|
+
const buffer = style + styleSize;
|
|
134
|
+
const utf8 = buffer + e.sizeOf("GhosttyBuffer");
|
|
135
|
+
while (e.call("ghostty_render_state_row_cells_next", this.cells) && cells.length < cols) {
|
|
136
|
+
e.check("ghostty_render_state_row_cells_get", this.cells, this.keys.cell["RAW"], raw);
|
|
137
|
+
const tag = e.readBits(raw, this.cell.tag);
|
|
138
|
+
const wide = e.readBits(raw, this.cell.wide);
|
|
139
|
+
let text = "";
|
|
140
|
+
if (tag === this.tags.codepoint) {
|
|
141
|
+
const cp = e.readBits(raw, this.cell.codepoint);
|
|
142
|
+
if (cp !== 0)
|
|
143
|
+
text = String.fromCodePoint(cp);
|
|
144
|
+
}
|
|
145
|
+
else if (tag === this.tags.grapheme) {
|
|
146
|
+
text = this.graphemes(buffer, utf8, utf8Cap);
|
|
147
|
+
}
|
|
148
|
+
let flags = 0;
|
|
149
|
+
let underline = 0;
|
|
150
|
+
if (wide === this.wide.wide)
|
|
151
|
+
flags |= FLAG.WIDE;
|
|
152
|
+
if (wide === this.wide.tail)
|
|
153
|
+
flags |= FLAG.SPACER;
|
|
154
|
+
const fg = this.color(this.keys.cell["FG_COLOR"], color);
|
|
155
|
+
const bg = this.color(this.keys.cell["BG_COLOR"], color);
|
|
156
|
+
e.check("ghostty_render_state_row_cells_get", this.cells, this.keys.cell["HAS_STYLING"], raw);
|
|
157
|
+
if (e.memory[raw]) {
|
|
158
|
+
e.view.setUint32(style, styleSize, true);
|
|
159
|
+
e.check("ghostty_render_state_row_cells_get", this.cells, this.keys.cell["STYLE"], style);
|
|
160
|
+
const f = (key) => e.field("GhosttyStyle", style, key) !== 0;
|
|
161
|
+
if (f("bold"))
|
|
162
|
+
flags |= FLAG.BOLD;
|
|
163
|
+
if (f("italic"))
|
|
164
|
+
flags |= FLAG.ITALIC;
|
|
165
|
+
if (f("faint"))
|
|
166
|
+
flags |= FLAG.FAINT;
|
|
167
|
+
if (f("blink"))
|
|
168
|
+
flags |= FLAG.BLINK;
|
|
169
|
+
if (f("inverse"))
|
|
170
|
+
flags |= FLAG.INVERSE;
|
|
171
|
+
if (f("invisible"))
|
|
172
|
+
flags |= FLAG.INVISIBLE;
|
|
173
|
+
if (f("strikethrough"))
|
|
174
|
+
flags |= FLAG.STRIKETHROUGH;
|
|
175
|
+
if (f("overline"))
|
|
176
|
+
flags |= FLAG.OVERLINE;
|
|
177
|
+
underline = e.field("GhosttyStyle", style, "underline");
|
|
178
|
+
}
|
|
179
|
+
cells.push({ text, fg, bg, flags, underline });
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
return cells;
|
|
183
|
+
}
|
|
184
|
+
color(key, out) {
|
|
185
|
+
const e = this.engine;
|
|
186
|
+
const code = e.call("ghostty_render_state_row_cells_get", this.cells, key, out);
|
|
187
|
+
if (code === e.result.INVALID_VALUE)
|
|
188
|
+
return NO_COLOR;
|
|
189
|
+
if (code !== e.result.SUCCESS)
|
|
190
|
+
throw new Error(`cell colour: ${code}`);
|
|
191
|
+
return e.readRgb(out);
|
|
192
|
+
}
|
|
193
|
+
graphemes(buffer, utf8, cap) {
|
|
194
|
+
const e = this.engine;
|
|
195
|
+
e.view.setUint32(buffer + e.offsetOf("GhosttyBuffer", "ptr"), utf8, true);
|
|
196
|
+
e.view.setUint32(buffer + e.offsetOf("GhosttyBuffer", "cap"), cap, true);
|
|
197
|
+
e.view.setUint32(buffer + e.offsetOf("GhosttyBuffer", "len"), 0, true);
|
|
198
|
+
const code = e.call("ghostty_render_state_row_cells_get", this.cells, this.keys.cell["GRAPHEMES_UTF8"], buffer);
|
|
199
|
+
if (code === e.result.SUCCESS) {
|
|
200
|
+
const len = e.field("GhosttyBuffer", buffer, "len");
|
|
201
|
+
return new TextDecoder().decode(e.memory.slice(utf8, utf8 + len));
|
|
202
|
+
}
|
|
203
|
+
if (code !== e.result.OUT_OF_SPACE)
|
|
204
|
+
throw new Error(`cell graphemes: ${code}`);
|
|
205
|
+
// The buffer struct reports the needed length; retry through it.
|
|
206
|
+
const needed = e.field("GhosttyBuffer", buffer, "len");
|
|
207
|
+
return new TextDecoder().decode(e.growing("ghostty_render_state_row_cells_get", needed, (big, bigCap, len) => {
|
|
208
|
+
e.view.setUint32(buffer + e.offsetOf("GhosttyBuffer", "ptr"), big, true);
|
|
209
|
+
e.view.setUint32(buffer + e.offsetOf("GhosttyBuffer", "cap"), bigCap, true);
|
|
210
|
+
const again = e.call("ghostty_render_state_row_cells_get", this.cells, this.keys.cell["GRAPHEMES_UTF8"], buffer);
|
|
211
|
+
e.view.setUint32(len, e.field("GhosttyBuffer", buffer, "len"), true);
|
|
212
|
+
return again;
|
|
213
|
+
}));
|
|
214
|
+
}
|
|
215
|
+
/** Marks the frame as drawn. */
|
|
216
|
+
clean() {
|
|
217
|
+
this.engine.check("ghostty_render_state_clean", this.state);
|
|
218
|
+
}
|
|
219
|
+
free() {
|
|
220
|
+
this.engine.call("ghostty_render_state_row_cells_free", this.cells);
|
|
221
|
+
this.engine.call("ghostty_render_state_row_iterator_free", this.iterator);
|
|
222
|
+
this.engine.call("ghostty_render_state_free", this.state);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export declare const HEADER_LEN = 10;
|
|
2
|
+
export declare const FrameType: {
|
|
3
|
+
readonly Hello: 1;
|
|
4
|
+
readonly Request: 2;
|
|
5
|
+
readonly Open: 16;
|
|
6
|
+
readonly Input: 17;
|
|
7
|
+
readonly Viewport: 18;
|
|
8
|
+
readonly Close: 19;
|
|
9
|
+
readonly Welcome: 129;
|
|
10
|
+
readonly Reply: 130;
|
|
11
|
+
readonly Event: 131;
|
|
12
|
+
readonly Error: 143;
|
|
13
|
+
readonly Attached: 144;
|
|
14
|
+
readonly Snapshot: 145;
|
|
15
|
+
readonly SnapshotReady: 146;
|
|
16
|
+
readonly SnapshotEnd: 147;
|
|
17
|
+
readonly Output: 148;
|
|
18
|
+
readonly Resized: 149;
|
|
19
|
+
readonly Exited: 150;
|
|
20
|
+
readonly Gap: 151;
|
|
21
|
+
readonly Closed: 153;
|
|
22
|
+
};
|
|
23
|
+
export type FrameType = (typeof FrameType)[keyof typeof FrameType];
|
|
24
|
+
export interface Header {
|
|
25
|
+
type: FrameType;
|
|
26
|
+
channel: number;
|
|
27
|
+
length: number;
|
|
28
|
+
}
|
|
29
|
+
export type FrameErrorKind = "unknown_type" | "too_long" | "short" | "length_mismatch";
|
|
30
|
+
export declare class FrameError extends Error {
|
|
31
|
+
readonly kind: FrameErrorKind;
|
|
32
|
+
constructor(kind: FrameErrorKind, message: string);
|
|
33
|
+
}
|
|
34
|
+
/** Parses a header, ignoring `flags`. */
|
|
35
|
+
export declare function parseHeader(bytes: Uint8Array, maxFrame: number): Header;
|
|
36
|
+
export declare function encodeFrame(type: FrameType, channel: number, payload: Uint8Array): Uint8Array;
|
|
37
|
+
export interface Frame {
|
|
38
|
+
header: Header;
|
|
39
|
+
payload: Uint8Array;
|
|
40
|
+
}
|
|
41
|
+
/** Parses one message that carries exactly one frame. */
|
|
42
|
+
export declare function parseFrame(message: Uint8Array, maxFrame: number): Frame;
|
|
43
|
+
/** `OUTPUT` payload: the stream offset of the first byte (u64 LE), then the bytes. */
|
|
44
|
+
export declare function parseOutput(payload: Uint8Array): {
|
|
45
|
+
offset: number;
|
|
46
|
+
bytes: Uint8Array;
|
|
47
|
+
} | undefined;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export const HEADER_LEN = 10;
|
|
2
|
+
export const FrameType = {
|
|
3
|
+
Hello: 0x01,
|
|
4
|
+
Request: 0x02,
|
|
5
|
+
Open: 0x10,
|
|
6
|
+
Input: 0x11,
|
|
7
|
+
Viewport: 0x12,
|
|
8
|
+
Close: 0x13,
|
|
9
|
+
Welcome: 0x81,
|
|
10
|
+
Reply: 0x82,
|
|
11
|
+
Event: 0x83,
|
|
12
|
+
Error: 0x8f,
|
|
13
|
+
Attached: 0x90,
|
|
14
|
+
Snapshot: 0x91,
|
|
15
|
+
SnapshotReady: 0x92,
|
|
16
|
+
SnapshotEnd: 0x93,
|
|
17
|
+
Output: 0x94,
|
|
18
|
+
Resized: 0x95,
|
|
19
|
+
Exited: 0x96,
|
|
20
|
+
Gap: 0x97,
|
|
21
|
+
Closed: 0x99,
|
|
22
|
+
};
|
|
23
|
+
const KNOWN_TYPES = new Set(Object.values(FrameType));
|
|
24
|
+
function isFrameType(byte) {
|
|
25
|
+
return KNOWN_TYPES.has(byte);
|
|
26
|
+
}
|
|
27
|
+
export class FrameError extends Error {
|
|
28
|
+
kind;
|
|
29
|
+
// Erasable syntax only: node runs this package's sources with type
|
|
30
|
+
// stripping (the headless driver).
|
|
31
|
+
constructor(kind, message) {
|
|
32
|
+
super(message);
|
|
33
|
+
this.kind = kind;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** Parses a header, ignoring `flags`. */
|
|
37
|
+
export function parseHeader(bytes, maxFrame) {
|
|
38
|
+
if (bytes.length < HEADER_LEN)
|
|
39
|
+
throw new FrameError("short", "message shorter than a frame header");
|
|
40
|
+
const type = bytes[0];
|
|
41
|
+
if (!isFrameType(type)) {
|
|
42
|
+
throw new FrameError("unknown_type", `unknown frame type 0x${type.toString(16).padStart(2, "0")}`);
|
|
43
|
+
}
|
|
44
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, HEADER_LEN);
|
|
45
|
+
const channel = view.getUint32(2, true);
|
|
46
|
+
const length = view.getUint32(6, true);
|
|
47
|
+
if (length > maxFrame)
|
|
48
|
+
throw new FrameError("too_long", `frame length ${length} exceeds max_frame ${maxFrame}`);
|
|
49
|
+
return { type, channel, length };
|
|
50
|
+
}
|
|
51
|
+
export function encodeFrame(type, channel, payload) {
|
|
52
|
+
const out = new Uint8Array(HEADER_LEN + payload.length);
|
|
53
|
+
const view = new DataView(out.buffer);
|
|
54
|
+
out[0] = type;
|
|
55
|
+
view.setUint32(2, channel, true);
|
|
56
|
+
view.setUint32(6, payload.length, true);
|
|
57
|
+
out.set(payload, HEADER_LEN);
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
/** Parses one message that carries exactly one frame. */
|
|
61
|
+
export function parseFrame(message, maxFrame) {
|
|
62
|
+
const header = parseHeader(message, maxFrame);
|
|
63
|
+
const payload = message.subarray(HEADER_LEN);
|
|
64
|
+
if (header.length !== payload.length) {
|
|
65
|
+
throw new FrameError("length_mismatch", `frame length ${header.length} with ${payload.length} payload bytes`);
|
|
66
|
+
}
|
|
67
|
+
return { header, payload };
|
|
68
|
+
}
|
|
69
|
+
/** `OUTPUT` payload: the stream offset of the first byte (u64 LE), then the bytes. */
|
|
70
|
+
export function parseOutput(payload) {
|
|
71
|
+
if (payload.length < 8)
|
|
72
|
+
return undefined;
|
|
73
|
+
const view = new DataView(payload.buffer, payload.byteOffset, 8);
|
|
74
|
+
const offset = view.getUint32(4, true) * 2 ** 32 + view.getUint32(0, true);
|
|
75
|
+
if (!Number.isSafeInteger(offset))
|
|
76
|
+
return undefined;
|
|
77
|
+
return { offset, bytes: payload.subarray(8) };
|
|
78
|
+
}
|