@gjermundgaraba/clankermux 0.5.0 → 0.6.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.
@@ -1,21 +1,6 @@
1
1
  import { type Engine, Terminal } from "./index.ts";
2
- /** Position just past the `READY` record, once every byte through it is in `buf`. */
3
2
  export declare function readyEnd(buf: Uint8Array, len?: number): number | undefined;
4
- /** Position just past the next `PAGE` or `FINISH` record at or after `pos`. */
5
3
  export declare function stepEnd(buf: Uint8Array, pos: number, len?: number): number | undefined;
6
- /**
7
- * An incremental snapshot decoder over a buffer of known capacity, filled
8
- * as bytes arrive. The library reads the buffer lazily, so `ready` and
9
- * `nextPage` are called only once the bytes they consume are in place.
10
- *
11
- * This leans on the engine's behaviour, not its contract: the header asks
12
- * for an immutable buffer. A `GhosttyReader` callback from wasm would
13
- * need a JS function placed in the module's function table, which takes
14
- * a generated wasm wrapper per signature; filling the buffer behind the
15
- * decoder is less machinery. The engine is pinned; its decoder test
16
- * pushes the bytes piecewise, so an engine that validated ahead would
17
- * fail there before shipping.
18
- */
19
4
  export declare class Decoder {
20
5
  readonly engine: Engine;
21
6
  readonly capacity: number;
@@ -29,17 +14,12 @@ export declare class Decoder {
29
14
  setMaxContinuationBytes(max: number): void;
30
15
  push(bytes: Uint8Array): void;
31
16
  private get source();
32
- /** Whether every byte through `READY` is buffered. */
33
17
  readyAvailable(): boolean;
34
18
  stepAvailable(): boolean;
35
- /** Decodes through `READY`; the terminal is then renderable. */
36
19
  ready(): Terminal;
37
- /** Decodes one history page. False once `FINISH` is validated. */
38
20
  nextPage(): boolean;
39
21
  terminal(): Terminal | undefined;
40
- /** Whether `FINISH` was validated: the snapshot is complete. */
41
22
  finished(): boolean;
42
- /** Frees the decoder and its buffer; the terminal, finished or not, is the caller's. */
43
23
  release(): Terminal | undefined;
44
24
  free(): void;
45
25
  }
@@ -1,18 +1,20 @@
1
1
  import { 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;
2
+ const MAGIC_LEN = 8;
3
+ const VERSION_LEN = 2;
4
+ const ENVELOPE_LEN = MAGIC_LEN + VERSION_LEN;
5
+ const TAG_LEN = 2;
6
+ const PAYLOAD_LENGTH_LEN = 4;
7
+ const CRC32C_LEN = 4;
8
+ const RECORD_HEADER_LEN = TAG_LEN + PAYLOAD_LENGTH_LEN + CRC32C_LEN;
6
9
  const TAG_PAGE = 3;
7
10
  const TAG_READY = 5;
8
11
  const TAG_FINISH = 6;
9
- /** The record starting at `pos`: its tag and the position just past it. */
10
12
  function recordAt(buf, len, pos) {
11
13
  if (pos + RECORD_HEADER_LEN > len)
12
14
  return undefined;
13
15
  const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
14
16
  const tag = view.getUint16(pos, true);
15
- const payload = view.getUint32(pos + 2, true);
17
+ const payload = view.getUint32(pos + TAG_LEN, true);
16
18
  const end = pos + RECORD_HEADER_LEN + payload;
17
19
  return end <= len ? [tag, end] : undefined;
18
20
  }
@@ -26,27 +28,12 @@ function endOfStep(buf, len, pos, last) {
26
28
  pos = record[1];
27
29
  }
28
30
  }
29
- /** Position just past the `READY` record, once every byte through it is in `buf`. */
30
31
  export function readyEnd(buf, len = buf.length) {
31
32
  return endOfStep(buf, len, ENVELOPE_LEN, (tag) => tag === TAG_READY);
32
33
  }
33
- /** Position just past the next `PAGE` or `FINISH` record at or after `pos`. */
34
34
  export function stepEnd(buf, pos, len = buf.length) {
35
35
  return endOfStep(buf, len, pos, (tag) => tag === TAG_PAGE || tag === TAG_FINISH);
36
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
37
  export class Decoder {
51
38
  engine;
52
39
  capacity;
@@ -60,7 +47,6 @@ export class Decoder {
60
47
  this.engine = engine;
61
48
  this.capacity = capacity;
62
49
  this.buffer = engine.alloc(capacity);
63
- // Never let the decoder see another allocation's leftovers.
64
50
  engine.memory.fill(0, this.buffer, this.buffer + capacity);
65
51
  this.decoder = engine.construct("ghostty_snapshot_decoder_new_buf", this.buffer, capacity);
66
52
  }
@@ -80,14 +66,12 @@ export class Decoder {
80
66
  get source() {
81
67
  return this.engine.memory.subarray(this.buffer, this.buffer + this.capacity);
82
68
  }
83
- /** Whether every byte through `READY` is buffered. */
84
69
  readyAvailable() {
85
70
  return readyEnd(this.source, this.filled) !== undefined;
86
71
  }
87
72
  stepAvailable() {
88
73
  return !this.complete && stepEnd(this.source, this.consumed, this.filled) !== undefined;
89
74
  }
90
- /** Decodes through `READY`; the terminal is then renderable. */
91
75
  ready() {
92
76
  const end = readyEnd(this.source, this.filled);
93
77
  if (end === undefined)
@@ -96,7 +80,6 @@ export class Decoder {
96
80
  this.consumed = end;
97
81
  return this.term;
98
82
  }
99
- /** Decodes one history page. False once `FINISH` is validated. */
100
83
  nextPage() {
101
84
  const end = stepEnd(this.source, this.consumed, this.filled);
102
85
  if (end === undefined)
@@ -110,11 +93,9 @@ export class Decoder {
110
93
  terminal() {
111
94
  return this.term;
112
95
  }
113
- /** Whether `FINISH` was validated: the snapshot is complete. */
114
96
  finished() {
115
97
  return this.complete;
116
98
  }
117
- /** Frees the decoder and its buffer; the terminal, finished or not, is the caller's. */
118
99
  release() {
119
100
  const term = this.term;
120
101
  this.term = undefined;
@@ -6,7 +6,6 @@ export type { Cell, Frame, FrameRow, PackedCells, PackedFrame } from "./render.t
6
6
  export { ENGINE } from "./pin.ts";
7
7
  export { SelectionGesture } from "./selection.ts";
8
8
  export type { CellPoint } from "./selection.ts";
9
- /** Struct fields to set: numbers, or the fields of a nested struct. */
10
9
  type Packed = {
11
10
  [field: string]: number | Packed;
12
11
  };
@@ -19,7 +18,6 @@ export interface Scrollbar {
19
18
  /** The viewport's rows. */
20
19
  len: number;
21
20
  }
22
- /** Position and width of a bit field, from the manifest. */
23
21
  export interface BitField {
24
22
  lsb: number;
25
23
  width: number;
@@ -29,13 +27,11 @@ export declare class EngineError extends Error {
29
27
  readonly code: number;
30
28
  constructor(call: string, code: number);
31
29
  }
32
- /** A loaded ghostty-vt.wasm instance. */
33
30
  export declare class Engine {
34
31
  private readonly exports;
35
32
  private readonly types;
36
33
  private memoryView;
37
34
  private dataView;
38
- /** The `GhosttyResult` members the wrapper branches on. */
39
35
  readonly result: {
40
36
  SUCCESS: number;
41
37
  OUT_OF_SPACE: number;
@@ -48,60 +44,35 @@ export declare class Engine {
48
44
  get memory(): Uint8Array;
49
45
  get view(): DataView;
50
46
  call(name: string, ...args: number[]): number;
51
- /** Calls a function returning GhosttyResult and throws on failure. */
52
47
  check(name: string, ...args: number[]): void;
53
- /** Calls a function returning GhosttyResult: true for a value, false
54
- * for `NO_VALUE`; throws on failure. */
55
48
  present(name: string, ...args: number[]): boolean;
56
49
  alloc(len: number): number;
57
50
  free(ptr: number, len: number): void;
58
- /** Runs `f` with `bytes` copied into wasm memory, or with a null
59
- * pointer for no bytes (which the allocator cannot serve). */
60
51
  withBytes<T>(bytes: Uint8Array, f: (ptr: number) => T): T;
61
- /** Runs `f` with a scratch allocation of `len` bytes. */
62
52
  scratch<T>(len: number, f: (ptr: number) => T): T;
63
- /** Runs `f` with an opaque-handle slot; the handle is taken out before the slot is freed. */
64
53
  private withSlot;
65
- /** Creates an opaque handle through a `(allocator, out_slot, ...args)` constructor. */
66
54
  construct(name: string, ...args: number[]): number;
67
- /** Takes an opaque handle from a `(...args, out_slot)` call. */
68
55
  take(name: string, ...args: number[]): number;
69
56
  private cString;
70
57
  private struct;
71
- /** Builds a zeroed, sized struct with the given fields set. */
72
58
  pack(name: string, values: Packed): Uint8Array;
73
- /** Reads one numeric or boolean field of a struct at `ptr`. */
74
59
  field(name: string, ptr: number, key: string): number;
75
- /** A bit field of a packed type: `path` names the field, or a union
76
- * field, its arm and the arm's field. */
77
60
  bits(name: string, ...path: [field: string] | [field: string, arm: string, inner: string]): BitField;
78
- /** Reads a bit field of a 64-bit packed value at `ptr`. */
79
61
  readBits(ptr: number, field: BitField): number;
80
- /** Runs an encoder that reports `OUT_OF_SPACE` with the needed size,
81
- * growing the output buffer until it fits. */
82
62
  growing(name: string, initial: number, encode: (buf: number, cap: number, len: number) => number): Uint8Array;
83
- /** Runs an `*_alloc` call that returns bytes through `(ptr, len)` out
84
- * parameters and frees them after copying. */
85
63
  allocated(call: (ptr: number, len: number) => void): Uint8Array;
86
- /** The offset of a struct field, for nested reads. */
87
64
  offsetOf(name: string, key: string): number;
88
65
  enumValue(name: string, member: string): number;
89
66
  hasEnumValue(name: string, member: string): boolean;
90
67
  sizeOf(name: string): number;
91
- /** Reads a GhosttyString at `ptr` using the manifest's layout. */
68
+ alignOf(name: string): number;
69
+ private type;
92
70
  readString(ptr: number): string;
93
- /** A `GhosttyColorRgb` at `ptr` as 0xRRGGBB. */
94
71
  readRgb(ptr: number): number;
95
72
  terminalNew(cols: number, rows: number): Terminal;
96
- /** Decodes a complete snapshot held in memory. */
97
73
  decodeSnapshot(bytes: Uint8Array): Terminal;
98
- /**
99
- * Paste text encoded for the PTY: unsafe bytes blanked, wrapped in
100
- * bracketed paste when `bracketed`, newlines to carriage returns otherwise.
101
- */
102
74
  encodePaste(text: string, bracketed: boolean): Uint8Array;
103
75
  }
104
- /** `ghostty_terminal_new` with the given `GhosttyTerminal` handle wrapped. */
105
76
  export declare class Terminal {
106
77
  readonly engine: Engine;
107
78
  readonly handle: number;
@@ -111,39 +82,21 @@ export declare class Terminal {
111
82
  get cols(): number;
112
83
  get rows(): number;
113
84
  get scrollbackRows(): number;
114
- /** Whether the alternate screen (full-screen programs) is active; it
115
- * has no history to scroll. */
116
85
  get alternate(): boolean;
117
86
  get scrollbar(): Scrollbar;
118
- /** Moves the viewport `rows` rows (up when negative) through the
119
- * history, or to its top or the live screen at its bottom. Output does
120
- * not move a viewport scrolled into the history. */
121
87
  scroll(to: number | "top" | "bottom"): void;
122
88
  get title(): string;
123
- /** An untracked `GhosttyGridRef` to the viewport cell at `at`, valid
124
- * until the terminal changes. */
125
89
  gridRef(at: {
126
90
  x: number;
127
91
  y: number;
128
92
  }): Uint8Array;
129
- /** Installs `selection` (a `GhosttySelection`) as the terminal's, which
130
- * the terminal then tracks through output, scrolling and resizes, or
131
- * clears it. */
132
93
  select(selection: Uint8Array | undefined): void;
133
- /** The selection's text: soft-wrapped lines joined, trailing blanks
134
- * trimmed, as Ghostty copies it; undefined with no selection. */
135
94
  selectionText(): string | undefined;
136
- /** Whether the active screen has a selection. */
137
95
  get selected(): boolean;
138
- /** Whether DEC private mode `number` is set. */
139
96
  mode(number: number): boolean;
140
- /** No cell size in pixels: `RESIZED` carries none. */
141
97
  resize(cols: number, rows: number): void;
142
- /** Sets a default colour (0xRRGGBB), as OSC 10, 11 and 12 report it. */
143
98
  setColor(kind: "foreground" | "background" | "cursor", color: number): void;
144
- /** The whole terminal, history included, as a snapshot. */
145
99
  snapshot(): Uint8Array;
146
- /** The active screen as plain text, one line per row. */
147
100
  formatPlain(): string;
148
101
  free(): void;
149
102
  }
@@ -12,13 +12,11 @@ export class EngineError extends Error {
12
12
  this.code = code;
13
13
  }
14
14
  }
15
- /** A loaded ghostty-vt.wasm instance. */
16
15
  export class Engine {
17
16
  exports;
18
17
  types;
19
18
  memoryView;
20
19
  dataView;
21
- /** The `GhosttyResult` members the wrapper branches on. */
22
20
  result;
23
21
  constructor(exports) {
24
22
  this.exports = exports;
@@ -55,14 +53,11 @@ export class Engine {
55
53
  throw new Error(`wasm export ${name} missing`);
56
54
  return fn(...args);
57
55
  }
58
- /** Calls a function returning GhosttyResult and throws on failure. */
59
56
  check(name, ...args) {
60
57
  const code = this.call(name, ...args);
61
58
  if (code !== this.result.SUCCESS)
62
59
  throw new EngineError(name, code);
63
60
  }
64
- /** Calls a function returning GhosttyResult: true for a value, false
65
- * for `NO_VALUE`; throws on failure. */
66
61
  present(name, ...args) {
67
62
  const code = this.call(name, ...args);
68
63
  if (code === this.result.NO_VALUE)
@@ -80,8 +75,6 @@ export class Engine {
80
75
  free(ptr, len) {
81
76
  this.call("ghostty_wasm_free", ptr, len);
82
77
  }
83
- /** Runs `f` with `bytes` copied into wasm memory, or with a null
84
- * pointer for no bytes (which the allocator cannot serve). */
85
78
  withBytes(bytes, f) {
86
79
  if (bytes.length === 0)
87
80
  return f(0);
@@ -94,7 +87,6 @@ export class Engine {
94
87
  this.free(ptr, bytes.length);
95
88
  }
96
89
  }
97
- /** Runs `f` with a scratch allocation of `len` bytes. */
98
90
  scratch(len, f) {
99
91
  const ptr = this.alloc(len);
100
92
  try {
@@ -104,7 +96,6 @@ export class Engine {
104
96
  this.free(ptr, len);
105
97
  }
106
98
  }
107
- /** Runs `f` with an opaque-handle slot; the handle is taken out before the slot is freed. */
108
99
  withSlot(f) {
109
100
  const slot = this.call("ghostty_wasm_alloc_opaque");
110
101
  try {
@@ -114,14 +105,12 @@ export class Engine {
114
105
  this.call("ghostty_wasm_free_opaque", slot);
115
106
  }
116
107
  }
117
- /** Creates an opaque handle through a `(allocator, out_slot, ...args)` constructor. */
118
108
  construct(name, ...args) {
119
109
  return this.withSlot((slot) => {
120
110
  this.check(name, 0, slot, ...args);
121
111
  return this.call("ghostty_wasm_take_opaque", slot);
122
112
  });
123
113
  }
124
- /** Takes an opaque handle from a `(...args, out_slot)` call. */
125
114
  take(name, ...args) {
126
115
  return this.withSlot((slot) => {
127
116
  this.check(name, ...args, slot);
@@ -141,7 +130,6 @@ export class Engine {
141
130
  throw new Error(`unknown struct ${name}`);
142
131
  return info;
143
132
  }
144
- /** Builds a zeroed, sized struct with the given fields set. */
145
133
  pack(name, values) {
146
134
  const info = this.struct(name);
147
135
  const out = new Uint8Array(info.size);
@@ -171,7 +159,6 @@ export class Engine {
171
159
  }
172
160
  return out;
173
161
  }
174
- /** Reads one numeric or boolean field of a struct at `ptr`. */
175
162
  field(name, ptr, key) {
176
163
  const field = this.struct(name).fields[key];
177
164
  if (!field)
@@ -189,8 +176,6 @@ export class Engine {
189
176
  throw new Error(`${name}.${key}: unsupported size ${field.size}`);
190
177
  }
191
178
  }
192
- /** A bit field of a packed type: `path` names the field, or a union
193
- * field, its arm and the arm's field. */
194
179
  bits(name, ...path) {
195
180
  const info = this.types[name];
196
181
  if (!info || info.kind !== "packed")
@@ -206,7 +191,6 @@ export class Engine {
206
191
  throw new Error(`${name}.${field} has no ${arm}.${inner}`);
207
192
  return { lsb: top.lsb + nested.lsb, width: nested.width };
208
193
  }
209
- /** Reads a bit field of a 64-bit packed value at `ptr`. */
210
194
  readBits(ptr, field) {
211
195
  const bit = field.lsb % 32;
212
196
  if (bit + field.width <= 32) {
@@ -217,8 +201,6 @@ export class Engine {
217
201
  const value = this.view.getBigUint64(ptr, true);
218
202
  return Number((value >> BigInt(field.lsb)) & ((1n << BigInt(field.width)) - 1n));
219
203
  }
220
- /** Runs an encoder that reports `OUT_OF_SPACE` with the needed size,
221
- * growing the output buffer until it fits. */
222
204
  growing(name, initial, encode) {
223
205
  let cap = initial;
224
206
  for (;;) {
@@ -236,8 +218,6 @@ export class Engine {
236
218
  cap = out;
237
219
  }
238
220
  }
239
- /** Runs an `*_alloc` call that returns bytes through `(ptr, len)` out
240
- * parameters and frees them after copying. */
241
221
  allocated(call) {
242
222
  return this.scratch(8, (out) => {
243
223
  call(out, out + 4);
@@ -251,7 +231,6 @@ export class Engine {
251
231
  }
252
232
  });
253
233
  }
254
- /** The offset of a struct field, for nested reads. */
255
234
  offsetOf(name, key) {
256
235
  const field = this.struct(name).fields[key];
257
236
  if (!field)
@@ -272,18 +251,22 @@ export class Engine {
272
251
  return info?.kind === "enum" && member in info.values;
273
252
  }
274
253
  sizeOf(name) {
254
+ return this.type(name).size;
255
+ }
256
+ alignOf(name) {
257
+ return this.type(name).align;
258
+ }
259
+ type(name) {
275
260
  const info = this.types[name];
276
261
  if (!info)
277
262
  throw new Error(`unknown type ${name}`);
278
- return info.size;
263
+ return info;
279
264
  }
280
- /** Reads a GhosttyString at `ptr` using the manifest's layout. */
281
265
  readString(ptr) {
282
266
  const data = this.field("GhosttyString", ptr, "ptr");
283
267
  const len = this.field("GhosttyString", ptr, "len");
284
268
  return new TextDecoder().decode(this.memory.subarray(data, data + len));
285
269
  }
286
- /** A `GhosttyColorRgb` at `ptr` as 0xRRGGBB. */
287
270
  readRgb(ptr) {
288
271
  const channel = (key) => this.field("GhosttyColorRgb", ptr, key);
289
272
  return ((channel("r") << 16) | (channel("g") << 8) | channel("b")) >>> 0;
@@ -291,7 +274,6 @@ export class Engine {
291
274
  terminalNew(cols, rows) {
292
275
  return new Terminal(this, this.construct("ghostty_terminal_new", cols, rows));
293
276
  }
294
- /** Decodes a complete snapshot held in memory. */
295
277
  decodeSnapshot(bytes) {
296
278
  return this.withBytes(bytes, (src) => {
297
279
  const decoder = this.construct("ghostty_snapshot_decoder_new_buf", src, bytes.length);
@@ -303,10 +285,6 @@ export class Engine {
303
285
  }
304
286
  });
305
287
  }
306
- /**
307
- * Paste text encoded for the PTY: unsafe bytes blanked, wrapped in
308
- * bracketed paste when `bracketed`, newlines to carriage returns otherwise.
309
- */
310
288
  encodePaste(text, bracketed) {
311
289
  const data = new TextEncoder().encode(text);
312
290
  if (data.length === 0)
@@ -314,7 +292,6 @@ export class Engine {
314
292
  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)));
315
293
  }
316
294
  }
317
- /** `ghostty_terminal_new` with the given `GhosttyTerminal` handle wrapped. */
318
295
  export class Terminal {
319
296
  engine;
320
297
  handle;
@@ -342,8 +319,6 @@ export class Terminal {
342
319
  get scrollbackRows() {
343
320
  return this.get("SCROLLBACK_ROWS", 4);
344
321
  }
345
- /** Whether the alternate screen (full-screen programs) is active; it
346
- * has no history to scroll. */
347
322
  get alternate() {
348
323
  return this.get("ACTIVE_SCREEN", 4) === this.engine.enumValue("GhosttyTerminalScreen", "ALTERNATE");
349
324
  }
@@ -355,9 +330,6 @@ export class Terminal {
355
330
  return { total: u64("total"), offset: u64("offset"), len: u64("len") };
356
331
  });
357
332
  }
358
- /** Moves the viewport `rows` rows (up when negative) through the
359
- * history, or to its top or the live screen at its bottom. Output does
360
- * not move a viewport scrolled into the history. */
361
333
  scroll(to) {
362
334
  const e = this.engine;
363
335
  const size = e.sizeOf("GhosttyTerminalScrollViewport");
@@ -377,8 +349,6 @@ export class Terminal {
377
349
  ? this.engine.readString(out)
378
350
  : "");
379
351
  }
380
- /** An untracked `GhosttyGridRef` to the viewport cell at `at`, valid
381
- * until the terminal changes. */
382
352
  gridRef(at) {
383
353
  const e = this.engine;
384
354
  const point = e.sizeOf("GhosttyPoint");
@@ -397,9 +367,6 @@ export class Terminal {
397
367
  return e.memory.slice(ptr + point, ptr + point + ref);
398
368
  });
399
369
  }
400
- /** Installs `selection` (a `GhosttySelection`) as the terminal's, which
401
- * the terminal then tracks through output, scrolling and resizes, or
402
- * clears it. */
403
370
  select(selection) {
404
371
  const option = this.engine.enumValue("GhosttyTerminalOption", "SELECTION");
405
372
  if (!selection)
@@ -407,8 +374,6 @@ export class Terminal {
407
374
  else
408
375
  this.engine.withBytes(selection, (ptr) => this.engine.check("ghostty_terminal_set", this.handle, option, ptr));
409
376
  }
410
- /** The selection's text: soft-wrapped lines joined, trailing blanks
411
- * trimmed, as Ghostty copies it; undefined with no selection. */
412
377
  selectionText() {
413
378
  const e = this.engine;
414
379
  const options = e.pack("GhosttyTerminalSelectionFormatOptions", {
@@ -432,7 +397,6 @@ export class Terminal {
432
397
  }
433
398
  }));
434
399
  }
435
- /** Whether the active screen has a selection. */
436
400
  get selected() {
437
401
  const e = this.engine;
438
402
  const size = e.sizeOf("GhosttySelection");
@@ -441,7 +405,6 @@ export class Terminal {
441
405
  return e.present("ghostty_terminal_get", this.handle, e.enumValue("GhosttyTerminalData", "SELECTION"), out);
442
406
  });
443
407
  }
444
- /** Whether DEC private mode `number` is set. */
445
408
  mode(number) {
446
409
  const config = this.engine.pack("GhosttyTerminalModeConfig", { mode: number });
447
410
  return this.engine.withBytes(config, (ptr) => {
@@ -449,21 +412,17 @@ export class Terminal {
449
412
  return this.engine.field("GhosttyTerminalModeConfig", ptr, "value") !== 0;
450
413
  });
451
414
  }
452
- /** No cell size in pixels: `RESIZED` carries none. */
453
415
  resize(cols, rows) {
454
416
  this.engine.check("ghostty_terminal_resize", this.handle, cols, rows, 0, 0);
455
417
  }
456
- /** Sets a default colour (0xRRGGBB), as OSC 10, 11 and 12 report it. */
457
418
  setColor(kind, color) {
458
419
  const option = this.engine.enumValue("GhosttyTerminalOption", `COLOR_${kind.toUpperCase()}`);
459
420
  const rgb = this.engine.pack("GhosttyColorRgb", { r: (color >> 16) & 0xff, g: (color >> 8) & 0xff, b: color & 0xff });
460
421
  this.engine.withBytes(rgb, (ptr) => this.engine.check("ghostty_terminal_set", this.handle, option, ptr));
461
422
  }
462
- /** The whole terminal, history included, as a snapshot. */
463
423
  snapshot() {
464
424
  return this.engine.allocated((ptr, len) => this.engine.check("ghostty_snapshot_encode_alloc", this.handle, 0, ptr, len));
465
425
  }
466
- /** The active screen as plain text, one line per row. */
467
426
  formatPlain() {
468
427
  const options = this.engine.pack("GhosttyFormatterTerminalOptions", {
469
428
  emit: this.engine.enumValue("GhosttyFormatterFormat", "PLAIN"),
@@ -19,21 +19,14 @@ export interface KeyInput {
19
19
  * key: Option on macOS, AltGr elsewhere. The text is sent as typed. */
20
20
  altComposed?: boolean;
21
21
  }
22
- /** `KeyboardEvent.code` as a `GhosttyKey` member name: `KeyA` → `A`, `Digit0` → `DIGIT_0`. */
23
22
  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
23
  export declare function isText(key: string): boolean;
29
- /** Encodes browser key events with the engine, following the terminal's modes. */
30
24
  export declare class KeyEncoder {
31
25
  readonly engine: Engine;
32
26
  private readonly encoder;
33
27
  private readonly event;
34
28
  private readonly unidentified;
35
29
  constructor(engine: Engine);
36
- /** The bytes to send for `input`, empty when the key encodes to nothing. */
37
30
  encode(term: Terminal, input: KeyInput): Uint8Array;
38
31
  free(): void;
39
32
  }
@@ -1,5 +1,4 @@
1
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
2
  export function keyName(code) {
4
3
  if (/^F\d+$/.test(code))
5
4
  return code;
@@ -9,10 +8,6 @@ export function keyName(code) {
9
8
  .replace(/([0-9])([A-Z])/g, "$1_$2")
10
9
  .toUpperCase();
11
10
  }
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
11
  export function isText(key) {
17
12
  if (key.length === 0)
18
13
  return false;
@@ -21,7 +16,14 @@ export function isText(key) {
21
16
  return false;
22
17
  return key.length === 1 || !/^[A-Z][A-Za-z0-9]+$/.test(key);
23
18
  }
24
- /** Encodes browser key events with the engine, following the terminal's modes. */
19
+ function shiftProducedText(input, text) {
20
+ if (!input.shift || text === "")
21
+ return false;
22
+ const layoutUnknown = input.unshifted === undefined;
23
+ if (layoutUnknown)
24
+ return text !== " ";
25
+ return text !== input.unshifted;
26
+ }
25
27
  export class KeyEncoder {
26
28
  engine;
27
29
  encoder;
@@ -33,13 +35,11 @@ export class KeyEncoder {
33
35
  this.event = engine.construct("ghostty_key_event_new");
34
36
  this.unidentified = engine.enumValue("GhosttyKey", "UNIDENTIFIED");
35
37
  }
36
- /** The bytes to send for `input`, empty when the key encodes to nothing. */
37
38
  encode(term, input) {
38
39
  const e = this.engine;
39
40
  e.call("ghostty_key_encoder_setopt_from_terminal", this.encoder, term.handle);
40
41
  const name = keyName(input.code);
41
42
  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
43
  const composed = input.altComposed === true;
44
44
  const mods = (input.shift ? MODS.SHIFT : 0) |
45
45
  (input.ctrl && !composed ? MODS.CTRL : 0) |
@@ -49,13 +49,7 @@ export class KeyEncoder {
49
49
  e.call("ghostty_key_event_set_key", this.event, key);
50
50
  e.call("ghostty_key_event_set_mods", this.event, mods);
51
51
  const text = isText(input.key) ? input.key : "";
52
- // Shift that made the text is consumed, as a keyboard reports it, so
53
- // the kitty protocol's disambiguation sends the text (`:`, `A`) rather
54
- // than shift and the key. It made it when the text is not the key's
55
- // own character; with no layout to say (no map, or a key it does not
56
- // cover, such as the numpad), when it is any text but a space.
57
- const shiftMade = input.shift && text !== "" && (input.unshifted !== undefined ? text !== input.unshifted : text !== " ");
58
- e.call("ghostty_key_event_set_consumed_mods", this.event, shiftMade ? MODS.SHIFT : 0);
52
+ e.call("ghostty_key_event_set_consumed_mods", this.event, shiftProducedText(input, text) ? MODS.SHIFT : 0);
59
53
  e.call("ghostty_key_event_set_composing", this.event, input.composing ? 1 : 0);
60
54
  const utf8 = new TextEncoder().encode(text);
61
55
  return e.withBytes(utf8, (textPtr) => {