@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,137 @@
|
|
|
1
|
+
import { EngineError, Terminal } from "./index.js";
|
|
2
|
+
/** Bytes before the first record: the 8-byte magic and the u16 version. */
|
|
3
|
+
const ENVELOPE_LEN = 10;
|
|
4
|
+
/** Bytes in a record header: tag u16, payload length u32, CRC32C u32. */
|
|
5
|
+
const RECORD_HEADER_LEN = 10;
|
|
6
|
+
const TAG_PAGE = 3;
|
|
7
|
+
const TAG_READY = 5;
|
|
8
|
+
const TAG_FINISH = 6;
|
|
9
|
+
/** The record starting at `pos`: its tag and the position just past it. */
|
|
10
|
+
function recordAt(buf, len, pos) {
|
|
11
|
+
if (pos + RECORD_HEADER_LEN > len)
|
|
12
|
+
return undefined;
|
|
13
|
+
const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
14
|
+
const tag = view.getUint16(pos, true);
|
|
15
|
+
const payload = view.getUint32(pos + 2, true);
|
|
16
|
+
const end = pos + RECORD_HEADER_LEN + payload;
|
|
17
|
+
return end <= len ? [tag, end] : undefined;
|
|
18
|
+
}
|
|
19
|
+
function endOfStep(buf, len, pos, last) {
|
|
20
|
+
for (;;) {
|
|
21
|
+
const record = recordAt(buf, len, pos);
|
|
22
|
+
if (!record)
|
|
23
|
+
return undefined;
|
|
24
|
+
if (last(record[0]))
|
|
25
|
+
return record[1];
|
|
26
|
+
pos = record[1];
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** Position just past the `READY` record, once every byte through it is in `buf`. */
|
|
30
|
+
export function readyEnd(buf, len = buf.length) {
|
|
31
|
+
return endOfStep(buf, len, ENVELOPE_LEN, (tag) => tag === TAG_READY);
|
|
32
|
+
}
|
|
33
|
+
/** Position just past the next `PAGE` or `FINISH` record at or after `pos`. */
|
|
34
|
+
export function stepEnd(buf, pos, len = buf.length) {
|
|
35
|
+
return endOfStep(buf, len, pos, (tag) => tag === TAG_PAGE || tag === TAG_FINISH);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* An incremental snapshot decoder over a buffer of known capacity, filled
|
|
39
|
+
* as bytes arrive. The library reads the buffer lazily, so `ready` and
|
|
40
|
+
* `nextPage` are called only once the bytes they consume are in place.
|
|
41
|
+
*
|
|
42
|
+
* This leans on the engine's behaviour, not its contract: the header asks
|
|
43
|
+
* for an immutable buffer. A `GhosttyReader` callback from wasm would
|
|
44
|
+
* need a JS function placed in the module's function table, which takes
|
|
45
|
+
* a generated wasm wrapper per signature; filling the buffer behind the
|
|
46
|
+
* decoder is less machinery. The engine is pinned; its decoder test
|
|
47
|
+
* pushes the bytes piecewise, so an engine that validated ahead would
|
|
48
|
+
* fail there before shipping.
|
|
49
|
+
*/
|
|
50
|
+
export class Decoder {
|
|
51
|
+
engine;
|
|
52
|
+
capacity;
|
|
53
|
+
buffer;
|
|
54
|
+
decoder;
|
|
55
|
+
filled = 0;
|
|
56
|
+
consumed = 0;
|
|
57
|
+
term;
|
|
58
|
+
complete = false;
|
|
59
|
+
constructor(engine, capacity) {
|
|
60
|
+
this.engine = engine;
|
|
61
|
+
this.capacity = capacity;
|
|
62
|
+
this.buffer = engine.alloc(capacity);
|
|
63
|
+
// Never let the decoder see another allocation's leftovers.
|
|
64
|
+
engine.memory.fill(0, this.buffer, this.buffer + capacity);
|
|
65
|
+
this.decoder = engine.construct("ghostty_snapshot_decoder_new_buf", this.buffer, capacity);
|
|
66
|
+
}
|
|
67
|
+
setMaxContinuationBytes(max) {
|
|
68
|
+
this.engine.scratch(4, (value) => {
|
|
69
|
+
this.engine.view.setUint32(value, max, true);
|
|
70
|
+
this.engine.check("ghostty_snapshot_decoder_set", this.decoder, this.engine.enumValue("GhosttySnapshotDecoderOption", "MAX_CONTINUATION_BYTES"), value);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
push(bytes) {
|
|
74
|
+
if (this.filled + bytes.length > this.capacity) {
|
|
75
|
+
throw new Error(`snapshot exceeds its declared ${this.capacity} bytes`);
|
|
76
|
+
}
|
|
77
|
+
this.engine.memory.set(bytes, this.buffer + this.filled);
|
|
78
|
+
this.filled += bytes.length;
|
|
79
|
+
}
|
|
80
|
+
get source() {
|
|
81
|
+
return this.engine.memory.subarray(this.buffer, this.buffer + this.capacity);
|
|
82
|
+
}
|
|
83
|
+
/** Whether every byte through `READY` is buffered. */
|
|
84
|
+
readyAvailable() {
|
|
85
|
+
return readyEnd(this.source, this.filled) !== undefined;
|
|
86
|
+
}
|
|
87
|
+
stepAvailable() {
|
|
88
|
+
return !this.complete && stepEnd(this.source, this.consumed, this.filled) !== undefined;
|
|
89
|
+
}
|
|
90
|
+
/** Decodes through `READY`; the terminal is then renderable. */
|
|
91
|
+
ready() {
|
|
92
|
+
const end = readyEnd(this.source, this.filled);
|
|
93
|
+
if (end === undefined)
|
|
94
|
+
throw new Error("READY not buffered");
|
|
95
|
+
this.term = new Terminal(this.engine, this.engine.take("ghostty_snapshot_decoder_ready", this.decoder));
|
|
96
|
+
this.consumed = end;
|
|
97
|
+
return this.term;
|
|
98
|
+
}
|
|
99
|
+
/** Decodes one history page. False once `FINISH` is validated. */
|
|
100
|
+
nextPage() {
|
|
101
|
+
const end = stepEnd(this.source, this.consumed, this.filled);
|
|
102
|
+
if (end === undefined)
|
|
103
|
+
throw new Error("next page not buffered");
|
|
104
|
+
const code = this.engine.call("ghostty_snapshot_decoder_next", this.decoder);
|
|
105
|
+
this.consumed = end;
|
|
106
|
+
if (code === this.engine.result.NO_VALUE) {
|
|
107
|
+
this.complete = true;
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
if (code !== this.engine.result.SUCCESS)
|
|
111
|
+
throw new EngineError("ghostty_snapshot_decoder_next", code);
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
terminal() {
|
|
115
|
+
return this.term;
|
|
116
|
+
}
|
|
117
|
+
/** Whether `FINISH` was validated: the snapshot is complete. */
|
|
118
|
+
finished() {
|
|
119
|
+
return this.complete;
|
|
120
|
+
}
|
|
121
|
+
/** Frees the decoder and its buffer; the terminal, finished or not, is the caller's. */
|
|
122
|
+
release() {
|
|
123
|
+
const term = this.term;
|
|
124
|
+
this.term = undefined;
|
|
125
|
+
this.free();
|
|
126
|
+
return term;
|
|
127
|
+
}
|
|
128
|
+
free() {
|
|
129
|
+
if (this.decoder === 0)
|
|
130
|
+
return;
|
|
131
|
+
this.engine.call("ghostty_snapshot_decoder_free", this.decoder);
|
|
132
|
+
this.engine.free(this.buffer, this.capacity);
|
|
133
|
+
this.decoder = 0;
|
|
134
|
+
this.term?.free();
|
|
135
|
+
this.term = undefined;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
export { Decoder, readyEnd } from "./decoder.ts";
|
|
2
|
+
export { KeyEncoder, isText, keyName } from "./keys.ts";
|
|
3
|
+
export type { KeyInput } from "./keys.ts";
|
|
4
|
+
export { FLAG, NO_COLOR, RenderState } from "./render.ts";
|
|
5
|
+
export type { Cell, Frame, FrameRow } from "./render.ts";
|
|
6
|
+
export { ENGINE } from "./pin.ts";
|
|
7
|
+
/** Struct fields to set: numbers, or the fields of a nested struct. */
|
|
8
|
+
type Packed = {
|
|
9
|
+
[field: string]: number | Packed;
|
|
10
|
+
};
|
|
11
|
+
/** Position and width of a bit field, from the manifest. */
|
|
12
|
+
export interface BitField {
|
|
13
|
+
lsb: number;
|
|
14
|
+
width: number;
|
|
15
|
+
}
|
|
16
|
+
export declare class EngineError extends Error {
|
|
17
|
+
readonly call: string;
|
|
18
|
+
readonly code: number;
|
|
19
|
+
constructor(call: string, code: number);
|
|
20
|
+
}
|
|
21
|
+
/** A loaded ghostty-vt.wasm instance. */
|
|
22
|
+
export declare class Engine {
|
|
23
|
+
private readonly exports;
|
|
24
|
+
private readonly types;
|
|
25
|
+
/** The `GhosttyResult` members the wrapper branches on. */
|
|
26
|
+
readonly result: {
|
|
27
|
+
SUCCESS: number;
|
|
28
|
+
OUT_OF_SPACE: number;
|
|
29
|
+
NO_VALUE: number;
|
|
30
|
+
INVALID_VALUE: number;
|
|
31
|
+
};
|
|
32
|
+
private constructor();
|
|
33
|
+
static load(wasm: BufferSource): Promise<Engine>;
|
|
34
|
+
/** The current linear memory; reacquired on every access because it may grow. */
|
|
35
|
+
get memory(): Uint8Array;
|
|
36
|
+
get view(): DataView;
|
|
37
|
+
call(name: string, ...args: number[]): number;
|
|
38
|
+
/** Calls a function returning GhosttyResult and throws on failure. */
|
|
39
|
+
check(name: string, ...args: number[]): void;
|
|
40
|
+
alloc(len: number): number;
|
|
41
|
+
free(ptr: number, len: number): void;
|
|
42
|
+
/** Runs `f` with `bytes` copied into wasm memory, or with a null
|
|
43
|
+
* pointer for no bytes (which the allocator cannot serve). */
|
|
44
|
+
withBytes<T>(bytes: Uint8Array, f: (ptr: number) => T): T;
|
|
45
|
+
/** Runs `f` with a scratch allocation of `len` bytes. */
|
|
46
|
+
scratch<T>(len: number, f: (ptr: number) => T): T;
|
|
47
|
+
/** Runs `f` with an opaque-handle slot; the handle is taken out before the slot is freed. */
|
|
48
|
+
private withSlot;
|
|
49
|
+
/** Creates an opaque handle through a `(allocator, out_slot, ...args)` constructor. */
|
|
50
|
+
construct(name: string, ...args: number[]): number;
|
|
51
|
+
/** Takes an opaque handle from a `(...args, out_slot)` call. */
|
|
52
|
+
take(name: string, ...args: number[]): number;
|
|
53
|
+
private cString;
|
|
54
|
+
private struct;
|
|
55
|
+
/** Builds a zeroed, sized struct with the given fields set. */
|
|
56
|
+
pack(name: string, values: Packed): Uint8Array;
|
|
57
|
+
/** Reads one numeric or boolean field of a struct at `ptr`. */
|
|
58
|
+
field(name: string, ptr: number, key: string): number;
|
|
59
|
+
/** A bit field of a packed type: `path` names the field, or a union
|
|
60
|
+
* field, its arm and the arm's field. */
|
|
61
|
+
bits(name: string, ...path: [field: string] | [field: string, arm: string, inner: string]): BitField;
|
|
62
|
+
/** Reads a bit field of a 64-bit packed value at `ptr`. */
|
|
63
|
+
readBits(ptr: number, field: BitField): number;
|
|
64
|
+
/** Runs an encoder that reports `OUT_OF_SPACE` with the needed size,
|
|
65
|
+
* growing the output buffer until it fits. */
|
|
66
|
+
growing(name: string, initial: number, encode: (buf: number, cap: number, len: number) => number): Uint8Array;
|
|
67
|
+
/** Runs an `*_alloc` call that returns bytes through `(ptr, len)` out
|
|
68
|
+
* parameters and frees them after copying. */
|
|
69
|
+
allocated(call: (ptr: number, len: number) => void): Uint8Array;
|
|
70
|
+
/** The offset of a struct field, for nested reads. */
|
|
71
|
+
offsetOf(name: string, key: string): number;
|
|
72
|
+
enumValue(name: string, member: string): number;
|
|
73
|
+
hasEnumValue(name: string, member: string): boolean;
|
|
74
|
+
sizeOf(name: string): number;
|
|
75
|
+
/** Reads a GhosttyString at `ptr` using the manifest's layout. */
|
|
76
|
+
readString(ptr: number): string;
|
|
77
|
+
/** A `GhosttyColorRgb` at `ptr` as 0xRRGGBB. */
|
|
78
|
+
readRgb(ptr: number): number;
|
|
79
|
+
terminalNew(cols: number, rows: number): Terminal;
|
|
80
|
+
/** Decodes a complete snapshot held in memory. */
|
|
81
|
+
decodeSnapshot(bytes: Uint8Array): Terminal;
|
|
82
|
+
/**
|
|
83
|
+
* Paste text encoded for the PTY: unsafe bytes blanked, wrapped in
|
|
84
|
+
* bracketed paste when `bracketed`, newlines to carriage returns otherwise.
|
|
85
|
+
*/
|
|
86
|
+
encodePaste(text: string, bracketed: boolean): Uint8Array;
|
|
87
|
+
}
|
|
88
|
+
/** `ghostty_terminal_new` with the given `GhosttyTerminal` handle wrapped. */
|
|
89
|
+
export declare class Terminal {
|
|
90
|
+
readonly engine: Engine;
|
|
91
|
+
readonly handle: number;
|
|
92
|
+
constructor(engine: Engine, handle: number);
|
|
93
|
+
write(bytes: Uint8Array): void;
|
|
94
|
+
private get;
|
|
95
|
+
get cols(): number;
|
|
96
|
+
get rows(): number;
|
|
97
|
+
get scrollbackRows(): number;
|
|
98
|
+
get title(): string;
|
|
99
|
+
/** Whether DEC private mode `number` is set. */
|
|
100
|
+
mode(number: number): boolean;
|
|
101
|
+
/** No cell size in pixels: `RESIZED` carries none. */
|
|
102
|
+
resize(cols: number, rows: number): void;
|
|
103
|
+
/** Sets a default colour (0xRRGGBB), as OSC 10, 11 and 12 report it. */
|
|
104
|
+
setColor(kind: "foreground" | "background" | "cursor", color: number): void;
|
|
105
|
+
/** The whole terminal, history included, as a snapshot. */
|
|
106
|
+
snapshot(): Uint8Array;
|
|
107
|
+
/** The active screen as plain text, one line per row. */
|
|
108
|
+
formatPlain(): string;
|
|
109
|
+
free(): void;
|
|
110
|
+
}
|
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
export { Decoder, readyEnd } from "./decoder.js";
|
|
2
|
+
export { KeyEncoder, isText, keyName } from "./keys.js";
|
|
3
|
+
export { FLAG, NO_COLOR, RenderState } from "./render.js";
|
|
4
|
+
export { ENGINE } from "./pin.js";
|
|
5
|
+
export class EngineError extends Error {
|
|
6
|
+
call;
|
|
7
|
+
code;
|
|
8
|
+
constructor(call, code) {
|
|
9
|
+
super(`${call} returned ${code}`);
|
|
10
|
+
this.call = call;
|
|
11
|
+
this.code = code;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/** A loaded ghostty-vt.wasm instance. */
|
|
15
|
+
export class Engine {
|
|
16
|
+
exports;
|
|
17
|
+
types;
|
|
18
|
+
/** The `GhosttyResult` members the wrapper branches on. */
|
|
19
|
+
result;
|
|
20
|
+
constructor(exports) {
|
|
21
|
+
this.exports = exports;
|
|
22
|
+
const ptr = this.call("ghostty_type_json");
|
|
23
|
+
this.types = JSON.parse(this.cString(ptr)).types;
|
|
24
|
+
const result = (member) => this.enumValue("GhosttyResult", member);
|
|
25
|
+
this.result = {
|
|
26
|
+
SUCCESS: result("SUCCESS"),
|
|
27
|
+
OUT_OF_SPACE: result("OUT_OF_SPACE"),
|
|
28
|
+
NO_VALUE: result("NO_VALUE"),
|
|
29
|
+
INVALID_VALUE: result("INVALID_VALUE"),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
static async load(wasm) {
|
|
33
|
+
const { instance } = await WebAssembly.instantiate(wasm, {});
|
|
34
|
+
return new Engine(instance.exports);
|
|
35
|
+
}
|
|
36
|
+
/** The current linear memory; reacquired on every access because it may grow. */
|
|
37
|
+
get memory() {
|
|
38
|
+
return new Uint8Array(this.exports.memory.buffer);
|
|
39
|
+
}
|
|
40
|
+
get view() {
|
|
41
|
+
return new DataView(this.exports.memory.buffer);
|
|
42
|
+
}
|
|
43
|
+
call(name, ...args) {
|
|
44
|
+
const fn = this.exports[name];
|
|
45
|
+
if (!fn)
|
|
46
|
+
throw new Error(`wasm export ${name} missing`);
|
|
47
|
+
return fn(...args);
|
|
48
|
+
}
|
|
49
|
+
/** Calls a function returning GhosttyResult and throws on failure. */
|
|
50
|
+
check(name, ...args) {
|
|
51
|
+
const code = this.call(name, ...args);
|
|
52
|
+
if (code !== this.result.SUCCESS)
|
|
53
|
+
throw new EngineError(name, code);
|
|
54
|
+
}
|
|
55
|
+
alloc(len) {
|
|
56
|
+
const ptr = this.call("ghostty_wasm_alloc", len);
|
|
57
|
+
if (ptr === 0)
|
|
58
|
+
throw new Error("wasm allocation failed");
|
|
59
|
+
return ptr;
|
|
60
|
+
}
|
|
61
|
+
free(ptr, len) {
|
|
62
|
+
this.call("ghostty_wasm_free", ptr, len);
|
|
63
|
+
}
|
|
64
|
+
/** Runs `f` with `bytes` copied into wasm memory, or with a null
|
|
65
|
+
* pointer for no bytes (which the allocator cannot serve). */
|
|
66
|
+
withBytes(bytes, f) {
|
|
67
|
+
if (bytes.length === 0)
|
|
68
|
+
return f(0);
|
|
69
|
+
const ptr = this.alloc(bytes.length);
|
|
70
|
+
this.memory.set(bytes, ptr);
|
|
71
|
+
try {
|
|
72
|
+
return f(ptr);
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
this.free(ptr, bytes.length);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** Runs `f` with a scratch allocation of `len` bytes. */
|
|
79
|
+
scratch(len, f) {
|
|
80
|
+
const ptr = this.alloc(len);
|
|
81
|
+
try {
|
|
82
|
+
return f(ptr);
|
|
83
|
+
}
|
|
84
|
+
finally {
|
|
85
|
+
this.free(ptr, len);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/** Runs `f` with an opaque-handle slot; the handle is taken out before the slot is freed. */
|
|
89
|
+
withSlot(f) {
|
|
90
|
+
const slot = this.call("ghostty_wasm_alloc_opaque");
|
|
91
|
+
try {
|
|
92
|
+
return f(slot);
|
|
93
|
+
}
|
|
94
|
+
finally {
|
|
95
|
+
this.call("ghostty_wasm_free_opaque", slot);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** Creates an opaque handle through a `(allocator, out_slot, ...args)` constructor. */
|
|
99
|
+
construct(name, ...args) {
|
|
100
|
+
return this.withSlot((slot) => {
|
|
101
|
+
this.check(name, 0, slot, ...args);
|
|
102
|
+
return this.call("ghostty_wasm_take_opaque", slot);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/** Takes an opaque handle from a `(...args, out_slot)` call. */
|
|
106
|
+
take(name, ...args) {
|
|
107
|
+
return this.withSlot((slot) => {
|
|
108
|
+
this.check(name, ...args, slot);
|
|
109
|
+
return this.call("ghostty_wasm_take_opaque", slot);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
cString(ptr) {
|
|
113
|
+
const mem = this.memory;
|
|
114
|
+
let end = ptr;
|
|
115
|
+
while (mem[end] !== 0)
|
|
116
|
+
end++;
|
|
117
|
+
return new TextDecoder().decode(mem.subarray(ptr, end));
|
|
118
|
+
}
|
|
119
|
+
struct(name) {
|
|
120
|
+
const info = this.types[name];
|
|
121
|
+
if (!info || info.kind !== "struct")
|
|
122
|
+
throw new Error(`unknown struct ${name}`);
|
|
123
|
+
return info;
|
|
124
|
+
}
|
|
125
|
+
/** Builds a zeroed, sized struct with the given fields set. */
|
|
126
|
+
pack(name, values) {
|
|
127
|
+
const info = this.struct(name);
|
|
128
|
+
const out = new Uint8Array(info.size);
|
|
129
|
+
const view = new DataView(out.buffer);
|
|
130
|
+
const sizeField = info.fields["size"];
|
|
131
|
+
if (sizeField)
|
|
132
|
+
view.setUint32(sizeField.offset, info.size, true);
|
|
133
|
+
for (const [key, value] of Object.entries(values)) {
|
|
134
|
+
const field = info.fields[key];
|
|
135
|
+
if (!field)
|
|
136
|
+
throw new Error(`${name} has no field ${key}`);
|
|
137
|
+
if (typeof value === "object") {
|
|
138
|
+
out.set(this.pack(field.type, value), field.offset);
|
|
139
|
+
}
|
|
140
|
+
else if (field.size === 4) {
|
|
141
|
+
view.setInt32(field.offset, value, true);
|
|
142
|
+
}
|
|
143
|
+
else if (field.size === 2) {
|
|
144
|
+
view.setUint16(field.offset, value, true);
|
|
145
|
+
}
|
|
146
|
+
else if (field.size === 1) {
|
|
147
|
+
out[field.offset] = value;
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
throw new Error(`${name}.${key}: unsupported size ${field.size}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return out;
|
|
154
|
+
}
|
|
155
|
+
/** Reads one numeric or boolean field of a struct at `ptr`. */
|
|
156
|
+
field(name, ptr, key) {
|
|
157
|
+
const field = this.struct(name).fields[key];
|
|
158
|
+
if (!field)
|
|
159
|
+
throw new Error(`${name} has no field ${key}`);
|
|
160
|
+
const view = this.view;
|
|
161
|
+
const at = ptr + field.offset;
|
|
162
|
+
switch (field.size) {
|
|
163
|
+
case 1:
|
|
164
|
+
return view.getUint8(at);
|
|
165
|
+
case 2:
|
|
166
|
+
return view.getUint16(at, true);
|
|
167
|
+
case 4:
|
|
168
|
+
return field.type.startsWith("u") ? view.getUint32(at, true) : view.getInt32(at, true);
|
|
169
|
+
default:
|
|
170
|
+
throw new Error(`${name}.${key}: unsupported size ${field.size}`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/** A bit field of a packed type: `path` names the field, or a union
|
|
174
|
+
* field, its arm and the arm's field. */
|
|
175
|
+
bits(name, ...path) {
|
|
176
|
+
const info = this.types[name];
|
|
177
|
+
if (!info || info.kind !== "packed")
|
|
178
|
+
throw new Error(`unknown packed type ${name}`);
|
|
179
|
+
const top = info.bits[path[0]];
|
|
180
|
+
if (!top)
|
|
181
|
+
throw new Error(`${name} has no bit field ${path[0]}`);
|
|
182
|
+
if (path.length === 1)
|
|
183
|
+
return { lsb: top.lsb, width: top.width };
|
|
184
|
+
const [field, arm, inner] = path;
|
|
185
|
+
const nested = top.arms?.[arm]?.bits[inner];
|
|
186
|
+
if (!nested)
|
|
187
|
+
throw new Error(`${name}.${field} has no ${arm}.${inner}`);
|
|
188
|
+
return { lsb: top.lsb + nested.lsb, width: nested.width };
|
|
189
|
+
}
|
|
190
|
+
/** Reads a bit field of a 64-bit packed value at `ptr`. */
|
|
191
|
+
readBits(ptr, field) {
|
|
192
|
+
const value = this.view.getBigUint64(ptr, true);
|
|
193
|
+
return Number((value >> BigInt(field.lsb)) & ((1n << BigInt(field.width)) - 1n));
|
|
194
|
+
}
|
|
195
|
+
/** Runs an encoder that reports `OUT_OF_SPACE` with the needed size,
|
|
196
|
+
* growing the output buffer until it fits. */
|
|
197
|
+
growing(name, initial, encode) {
|
|
198
|
+
let cap = initial;
|
|
199
|
+
for (;;) {
|
|
200
|
+
const out = this.scratch(cap, (buf) => this.scratch(4, (len) => {
|
|
201
|
+
const code = encode(buf, cap, len);
|
|
202
|
+
const n = this.view.getUint32(len, true);
|
|
203
|
+
if (code === this.result.OUT_OF_SPACE)
|
|
204
|
+
return n;
|
|
205
|
+
if (code !== this.result.SUCCESS)
|
|
206
|
+
throw new EngineError(name, code);
|
|
207
|
+
return this.memory.slice(buf, buf + n);
|
|
208
|
+
}));
|
|
209
|
+
if (typeof out !== "number")
|
|
210
|
+
return out;
|
|
211
|
+
cap = out;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/** Runs an `*_alloc` call that returns bytes through `(ptr, len)` out
|
|
215
|
+
* parameters and frees them after copying. */
|
|
216
|
+
allocated(call) {
|
|
217
|
+
return this.scratch(8, (out) => {
|
|
218
|
+
call(out, out + 4);
|
|
219
|
+
const ptr = this.view.getUint32(out, true);
|
|
220
|
+
const len = this.view.getUint32(out + 4, true);
|
|
221
|
+
try {
|
|
222
|
+
return this.memory.slice(ptr, ptr + len);
|
|
223
|
+
}
|
|
224
|
+
finally {
|
|
225
|
+
this.call("ghostty_free", 0, ptr, len);
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
/** The offset of a struct field, for nested reads. */
|
|
230
|
+
offsetOf(name, key) {
|
|
231
|
+
const field = this.struct(name).fields[key];
|
|
232
|
+
if (!field)
|
|
233
|
+
throw new Error(`${name} has no field ${key}`);
|
|
234
|
+
return field.offset;
|
|
235
|
+
}
|
|
236
|
+
enumValue(name, member) {
|
|
237
|
+
const info = this.types[name];
|
|
238
|
+
if (!info || info.kind !== "enum")
|
|
239
|
+
throw new Error(`unknown enum ${name}`);
|
|
240
|
+
const value = info.values[member];
|
|
241
|
+
if (value === undefined)
|
|
242
|
+
throw new Error(`${name} has no member ${member}`);
|
|
243
|
+
return value;
|
|
244
|
+
}
|
|
245
|
+
hasEnumValue(name, member) {
|
|
246
|
+
const info = this.types[name];
|
|
247
|
+
return info?.kind === "enum" && member in info.values;
|
|
248
|
+
}
|
|
249
|
+
sizeOf(name) {
|
|
250
|
+
const info = this.types[name];
|
|
251
|
+
if (!info)
|
|
252
|
+
throw new Error(`unknown type ${name}`);
|
|
253
|
+
return info.size;
|
|
254
|
+
}
|
|
255
|
+
/** Reads a GhosttyString at `ptr` using the manifest's layout. */
|
|
256
|
+
readString(ptr) {
|
|
257
|
+
const data = this.field("GhosttyString", ptr, "ptr");
|
|
258
|
+
const len = this.field("GhosttyString", ptr, "len");
|
|
259
|
+
return new TextDecoder().decode(this.memory.subarray(data, data + len));
|
|
260
|
+
}
|
|
261
|
+
/** A `GhosttyColorRgb` at `ptr` as 0xRRGGBB. */
|
|
262
|
+
readRgb(ptr) {
|
|
263
|
+
const channel = (key) => this.field("GhosttyColorRgb", ptr, key);
|
|
264
|
+
return ((channel("r") << 16) | (channel("g") << 8) | channel("b")) >>> 0;
|
|
265
|
+
}
|
|
266
|
+
terminalNew(cols, rows) {
|
|
267
|
+
return new Terminal(this, this.construct("ghostty_terminal_new", cols, rows));
|
|
268
|
+
}
|
|
269
|
+
/** Decodes a complete snapshot held in memory. */
|
|
270
|
+
decodeSnapshot(bytes) {
|
|
271
|
+
return this.withBytes(bytes, (src) => {
|
|
272
|
+
const decoder = this.construct("ghostty_snapshot_decoder_new_buf", src, bytes.length);
|
|
273
|
+
try {
|
|
274
|
+
return new Terminal(this, this.take("ghostty_snapshot_decoder_decode", decoder));
|
|
275
|
+
}
|
|
276
|
+
finally {
|
|
277
|
+
this.call("ghostty_snapshot_decoder_free", decoder);
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Paste text encoded for the PTY: unsafe bytes blanked, wrapped in
|
|
283
|
+
* bracketed paste when `bracketed`, newlines to carriage returns otherwise.
|
|
284
|
+
*/
|
|
285
|
+
encodePaste(text, bracketed) {
|
|
286
|
+
const data = new TextEncoder().encode(text);
|
|
287
|
+
if (data.length === 0)
|
|
288
|
+
return data;
|
|
289
|
+
return this.withBytes(data, (src) => this.growing("ghostty_paste_encode", data.length + 16, (buf, cap, len) => this.call("ghostty_paste_encode", src, data.length, bracketed ? 1 : 0, buf, cap, len)));
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
/** `ghostty_terminal_new` with the given `GhosttyTerminal` handle wrapped. */
|
|
293
|
+
export class Terminal {
|
|
294
|
+
engine;
|
|
295
|
+
handle;
|
|
296
|
+
constructor(engine, handle) {
|
|
297
|
+
this.engine = engine;
|
|
298
|
+
this.handle = handle;
|
|
299
|
+
}
|
|
300
|
+
write(bytes) {
|
|
301
|
+
if (bytes.length === 0)
|
|
302
|
+
return;
|
|
303
|
+
this.engine.withBytes(bytes, (ptr) => this.engine.call("ghostty_terminal_vt_write", this.handle, ptr, bytes.length));
|
|
304
|
+
}
|
|
305
|
+
get(key, size) {
|
|
306
|
+
return this.engine.scratch(size, (out) => {
|
|
307
|
+
this.engine.check("ghostty_terminal_get", this.handle, this.engine.enumValue("GhosttyTerminalData", key), out);
|
|
308
|
+
return size === 2 ? this.engine.view.getUint16(out, true) : this.engine.view.getUint32(out, true);
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
get cols() {
|
|
312
|
+
return this.get("COLS", 2);
|
|
313
|
+
}
|
|
314
|
+
get rows() {
|
|
315
|
+
return this.get("ROWS", 2);
|
|
316
|
+
}
|
|
317
|
+
get scrollbackRows() {
|
|
318
|
+
return this.get("SCROLLBACK_ROWS", 4);
|
|
319
|
+
}
|
|
320
|
+
get title() {
|
|
321
|
+
const size = this.engine.sizeOf("GhosttyString");
|
|
322
|
+
return this.engine.scratch(size, (out) => {
|
|
323
|
+
const code = this.engine.call("ghostty_terminal_get", this.handle, this.engine.enumValue("GhosttyTerminalData", "TITLE"), out);
|
|
324
|
+
if (code === this.engine.result.NO_VALUE)
|
|
325
|
+
return "";
|
|
326
|
+
if (code !== this.engine.result.SUCCESS)
|
|
327
|
+
throw new EngineError("ghostty_terminal_get", code);
|
|
328
|
+
return this.engine.readString(out);
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
/** Whether DEC private mode `number` is set. */
|
|
332
|
+
mode(number) {
|
|
333
|
+
const config = this.engine.pack("GhosttyTerminalModeConfig", { mode: number });
|
|
334
|
+
return this.engine.withBytes(config, (ptr) => {
|
|
335
|
+
this.engine.check("ghostty_terminal_get", this.handle, this.engine.enumValue("GhosttyTerminalData", "MODE"), ptr);
|
|
336
|
+
return this.engine.field("GhosttyTerminalModeConfig", ptr, "value") !== 0;
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
/** No cell size in pixels: `RESIZED` carries none. */
|
|
340
|
+
resize(cols, rows) {
|
|
341
|
+
this.engine.check("ghostty_terminal_resize", this.handle, cols, rows, 0, 0);
|
|
342
|
+
}
|
|
343
|
+
/** Sets a default colour (0xRRGGBB), as OSC 10, 11 and 12 report it. */
|
|
344
|
+
setColor(kind, color) {
|
|
345
|
+
const option = this.engine.enumValue("GhosttyTerminalOption", `COLOR_${kind.toUpperCase()}`);
|
|
346
|
+
const rgb = this.engine.pack("GhosttyColorRgb", { r: (color >> 16) & 0xff, g: (color >> 8) & 0xff, b: color & 0xff });
|
|
347
|
+
this.engine.withBytes(rgb, (ptr) => this.engine.check("ghostty_terminal_set", this.handle, option, ptr));
|
|
348
|
+
}
|
|
349
|
+
/** The whole terminal, history included, as a snapshot. */
|
|
350
|
+
snapshot() {
|
|
351
|
+
return this.engine.allocated((ptr, len) => this.engine.check("ghostty_snapshot_encode_alloc", this.handle, 0, ptr, len));
|
|
352
|
+
}
|
|
353
|
+
/** The active screen as plain text, one line per row. */
|
|
354
|
+
formatPlain() {
|
|
355
|
+
const options = this.engine.pack("GhosttyFormatterTerminalOptions", {
|
|
356
|
+
emit: this.engine.enumValue("GhosttyFormatterFormat", "PLAIN"),
|
|
357
|
+
extra: { screen: {} },
|
|
358
|
+
});
|
|
359
|
+
return this.engine.withBytes(options, (optionsPtr) => {
|
|
360
|
+
const formatter = this.engine.construct("ghostty_formatter_terminal_new", this.handle, optionsPtr);
|
|
361
|
+
try {
|
|
362
|
+
return new TextDecoder().decode(this.engine.allocated((ptr, len) => this.engine.check("ghostty_formatter_format_alloc", formatter, 0, ptr, len)));
|
|
363
|
+
}
|
|
364
|
+
finally {
|
|
365
|
+
this.engine.call("ghostty_formatter_free", formatter);
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
free() {
|
|
370
|
+
this.engine.call("ghostty_terminal_free", this.handle);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Engine, Terminal } from "./index.ts";
|
|
2
|
+
/** A key event as the browser reports it. */
|
|
3
|
+
export interface KeyInput {
|
|
4
|
+
/** `KeyboardEvent.code`, the physical key. */
|
|
5
|
+
code: string;
|
|
6
|
+
/** `KeyboardEvent.key`, the text or the key name. */
|
|
7
|
+
key: string;
|
|
8
|
+
action: "press" | "release" | "repeat";
|
|
9
|
+
shift: boolean;
|
|
10
|
+
ctrl: boolean;
|
|
11
|
+
alt: boolean;
|
|
12
|
+
meta: boolean;
|
|
13
|
+
/** An input method is composing; the engine encodes nothing. */
|
|
14
|
+
composing?: boolean;
|
|
15
|
+
/** The character the key produces with no modifiers, from the layout
|
|
16
|
+
* when the browser exposes it. */
|
|
17
|
+
unshifted?: string;
|
|
18
|
+
/** Alt (with Ctrl, for AltGr) composed `key` rather than modifying the
|
|
19
|
+
* key: Option on macOS, AltGr elsewhere. The text is sent as typed. */
|
|
20
|
+
altComposed?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/** `KeyboardEvent.code` as a `GhosttyKey` member name: `KeyA` → `A`, `Digit0` → `DIGIT_0`. */
|
|
23
|
+
export declare function keyName(code: string): string;
|
|
24
|
+
/** Whether `key` is text the key produces rather than a key name such as
|
|
25
|
+
* "Enter" or "ArrowUp" (which, per UI Events, is ASCII letters and digits
|
|
26
|
+
* starting with a capital, two or more of them; text of that shape is one
|
|
27
|
+
* character). */
|
|
28
|
+
export declare function isText(key: string): boolean;
|
|
29
|
+
/** Encodes browser key events with the engine, following the terminal's modes. */
|
|
30
|
+
export declare class KeyEncoder {
|
|
31
|
+
readonly engine: Engine;
|
|
32
|
+
private readonly encoder;
|
|
33
|
+
private readonly event;
|
|
34
|
+
private readonly unidentified;
|
|
35
|
+
constructor(engine: Engine);
|
|
36
|
+
/** The bytes to send for `input`, empty when the key encodes to nothing. */
|
|
37
|
+
encode(term: Terminal, input: KeyInput): Uint8Array;
|
|
38
|
+
free(): void;
|
|
39
|
+
}
|