@gjermundgaraba/clankermux 0.4.0 → 0.5.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.
@@ -26,6 +26,18 @@ const BACKOFF_MAX_MS = 5000;
26
26
  const PERMANENT = new Set(["no_such_session", "engine_mismatch", "invalid_request"]);
27
27
  /** The records `ack` marks seen. */
28
28
  const ACKED = new Set(["done", "error"]);
29
+ /** Delays before trying again, doubling up to the maximum. */
30
+ class Backoff {
31
+ ms = BACKOFF_MIN_MS;
32
+ next() {
33
+ const delay = this.ms;
34
+ this.ms = Math.min(this.ms * 2, BACKOFF_MAX_MS);
35
+ return delay;
36
+ }
37
+ reset() {
38
+ this.ms = BACKOFF_MIN_MS;
39
+ }
40
+ }
29
41
  /** A WebSocket as the client's message transport. */
30
42
  class SocketTransport {
31
43
  socket;
@@ -79,16 +91,14 @@ class Hub {
79
91
  client = "clankermux-web";
80
92
  conn;
81
93
  /** The connection-level link, as a terminal mounted now first sees it:
82
- * `connecting`, `connected` (to no session), or `ended` for good. */
94
+ * `connecting`, `connected` (to no session), or `ended` for good, with
95
+ * the server's refusal or error, when an `attach` connects again. */
83
96
  state = { state: "connecting", attempt: 1 };
84
97
  /** The latest connection-level status text, for a terminal mounted later. */
85
98
  statusText = "connecting";
86
99
  /** Connection attempts since a server last answered. */
87
100
  attempt = 0;
88
- backoff = BACKOFF_MIN_MS;
89
- /** The connection ended for good with the server's refusal or error;
90
- * an `attach` connects again. */
91
- refused = false;
101
+ backoff = new Backoff();
92
102
  /** Frame channels, shared by the terminals of the one connection. */
93
103
  channel = 0;
94
104
  mounts = new Map();
@@ -115,11 +125,6 @@ class Hub {
115
125
  this.post({ type: "ready" });
116
126
  await this.connect();
117
127
  }
118
- delay() {
119
- const delay = this.backoff;
120
- this.backoff = Math.min(this.backoff * 2, BACKOFF_MAX_MS);
121
- return delay;
122
- }
123
128
  /** A connection-level change, to the page and every terminal. A
124
129
  * retrying end leaves the connection `connecting` meanwhile. */
125
130
  linkAll(link) {
@@ -139,7 +144,6 @@ class Hub {
139
144
  }
140
145
  /** The connection ended for good with `err`. */
141
146
  refuse(err) {
142
- this.refused = true;
143
147
  this.statusAll(err instanceof Error ? err.message : String(err));
144
148
  this.linkAll(endedWith(err, false));
145
149
  }
@@ -156,11 +160,11 @@ class Hub {
156
160
  this.refuse(err);
157
161
  return;
158
162
  }
159
- setTimeout(() => void this.connect(), this.delay());
163
+ setTimeout(() => void this.connect(), this.backoff.next());
160
164
  return;
161
165
  }
162
166
  this.attempt = 0;
163
- this.backoff = BACKOFF_MIN_MS;
167
+ this.backoff.reset();
164
168
  this.conn = conn;
165
169
  conn.onEnd = (err) => {
166
170
  if (this.conn !== conn)
@@ -171,7 +175,7 @@ class Hub {
171
175
  }
172
176
  else {
173
177
  this.linkAll({ state: "ended", reason: "closed", retry: true });
174
- setTimeout(() => void this.connect(), this.delay());
178
+ setTimeout(() => void this.connect(), this.backoff.next());
175
179
  }
176
180
  };
177
181
  conn.onEvent = (event) => this.event(event);
@@ -300,7 +304,7 @@ class Hub {
300
304
  const mount = new Mount(this, id, init);
301
305
  this.mounts.set(id, mount);
302
306
  mount.status(this.statusText);
303
- if (this.refused)
307
+ if (this.state.state === "ended")
304
308
  this.reattach(mount);
305
309
  else if (this.live())
306
310
  mount.connected();
@@ -315,8 +319,7 @@ class Hub {
315
319
  * otherwise opens `mount`'s session, or leaves it to the connection
316
320
  * coming up. */
317
321
  reattach(mount) {
318
- if (this.refused) {
319
- this.refused = false;
322
+ if (this.state.state === "ended") {
320
323
  this.attempt = 0;
321
324
  void this.connect();
322
325
  }
@@ -342,6 +345,7 @@ class Mount {
342
345
  profile;
343
346
  /** The page is in use; only then are its size and profile reported. */
344
347
  active;
348
+ visible;
345
349
  readOnly;
346
350
  ackOnFocus;
347
351
  /** The terminal has the keyboard and its window the focus. */
@@ -361,8 +365,11 @@ class Mount {
361
365
  writes = Promise.resolve();
362
366
  clipboard = new Osc52Scanner();
363
367
  paintTimer;
368
+ paintPending = false;
369
+ frameInFlight = false;
370
+ fullNext = false;
364
371
  /** Re-attaches after a failure; reset by a screen. */
365
- backoff = BACKOFF_MIN_MS;
372
+ backoff = new Backoff();
366
373
  reopenTimer;
367
374
  disposed = false;
368
375
  constructor(hub, id, init) {
@@ -372,14 +379,15 @@ class Mount {
372
379
  this.viewport = init.viewport;
373
380
  this.profile = init.profile;
374
381
  this.active = init.active;
382
+ this.visible = init.visible;
375
383
  this.readOnly = init.readOnly;
376
384
  this.ackOnFocus = init.ackOnFocus;
377
385
  this.render = new RenderState(hub.engine);
378
386
  this.gesture = new SelectionGesture(hub.engine);
379
387
  }
380
- post(message) {
388
+ post(message, options) {
381
389
  if (!this.disposed)
382
- this.hub.post(message);
390
+ this.hub.post(message, options);
383
391
  }
384
392
  link(link) {
385
393
  this.post({ type: "link", mount: this.id, link });
@@ -387,11 +395,6 @@ class Mount {
387
395
  status(text) {
388
396
  this.post({ type: "status", mount: this.id, text });
389
397
  }
390
- delay() {
391
- const delay = this.backoff;
392
- this.backoff = Math.min(this.backoff * 2, BACKOFF_MAX_MS);
393
- return delay;
394
- }
395
398
  /** The hub's connection is up: attach the session, if there is one. */
396
399
  connected() {
397
400
  if (this.session !== undefined) {
@@ -422,42 +425,37 @@ class Mount {
422
425
  ...(this.active && { viewport: this.viewport, profile: this.profile }),
423
426
  ...(resume && { resume }),
424
427
  };
425
- try {
426
- conn.open(channel, open, (frame) => {
427
- if (this.attachment !== attachment)
428
- return;
429
- let actions;
430
- try {
431
- actions = attachment.handle(frame);
432
- }
433
- catch (err) {
434
- // A lost place in the stream: CLOSE and OPEN again, afresh. Any
435
- // other fault would only repeat.
436
- this.status(String(err));
437
- const desync = err instanceof ClientError && err.kind === "desync";
438
- this.link(endedWith(err, desync));
439
- if (desync)
440
- this.reopen();
441
- else
442
- this.giveUp();
443
- return;
444
- }
445
- if (frame.header.type === FrameType.Attached && actions.length === 0) {
446
- // Resumed: the terminal shows the stream already, and an exit
447
- // still to come arrives as EXITED. A snapshot's screen comes
448
- // later, as an action.
449
- if (attachment.resume()) {
450
- this.status("resumed");
451
- this.link({ state: "connected", session: attachment.session });
452
- }
428
+ conn.open(channel, open, (frame) => {
429
+ if (this.attachment !== attachment)
430
+ return;
431
+ let actions;
432
+ try {
433
+ actions = attachment.handle(frame);
434
+ }
435
+ catch (err) {
436
+ // A lost place in the stream: CLOSE and OPEN again, afresh. Any
437
+ // other fault would only repeat.
438
+ this.status(String(err));
439
+ const desync = err instanceof ClientError && err.kind === "desync";
440
+ this.link(endedWith(err, desync));
441
+ if (desync)
442
+ this.reopen();
443
+ else
444
+ this.giveUp();
445
+ return;
446
+ }
447
+ if (frame.header.type === FrameType.Attached && actions.length === 0) {
448
+ // Resumed: the terminal shows the stream already, and an exit
449
+ // still to come arrives as EXITED. A snapshot's screen comes
450
+ // later, as an action.
451
+ if (attachment.resume()) {
452
+ this.status("resumed");
453
+ this.link({ state: "connected", session: attachment.session });
453
454
  }
454
- for (const action of actions)
455
- this.apply(action);
456
- });
457
- }
458
- catch (err) {
459
- this.status(String(err));
460
- }
455
+ }
456
+ for (const action of actions)
457
+ this.apply(action);
458
+ });
461
459
  }
462
460
  /** The screen of `session` is shown: `connected`, and at once `ended
463
461
  * exited` when its program has exited already. */
@@ -479,12 +477,12 @@ class Mount {
479
477
  this.reopenTimer = undefined;
480
478
  if (this.session !== undefined && !this.attachment)
481
479
  this.open();
482
- }, this.delay());
480
+ }, this.backoff.next());
483
481
  }
484
482
  apply(action) {
485
483
  switch (action.type) {
486
484
  case "screen": {
487
- this.backoff = BACKOFF_MIN_MS;
485
+ this.backoff.reset();
488
486
  this.forgetSelection();
489
487
  this.clipboard.reset();
490
488
  this.applyProfile(this.attachment?.term());
@@ -502,6 +500,9 @@ class Mount {
502
500
  break;
503
501
  }
504
502
  case "output":
503
+ // Selection state must reach the page even while painting is paused.
504
+ if (this.selected && !this.attachment?.term()?.selected)
505
+ this.unselected();
505
506
  for (const write of this.clipboard.feed(action.bytes)) {
506
507
  // Only the clipboard selection; primary and secondary have no
507
508
  // browser equivalent.
@@ -509,7 +510,9 @@ class Mount {
509
510
  this.post({ type: "clipboard", mount: this.id, text: write.text });
510
511
  }
511
512
  }
512
- this.paintTimer ??= setTimeout(() => this.paint(), PAINT_MS);
513
+ this.paintPending = true;
514
+ if (this.visible && !this.frameInFlight)
515
+ this.paintTimer ??= setTimeout(() => this.paint(), PAINT_MS);
513
516
  break;
514
517
  case "resized":
515
518
  // Another client's size, while this page kept its focus (the user
@@ -576,37 +579,65 @@ class Mount {
576
579
  }
577
580
  }
578
581
  repaint() {
579
- const term = this.attachment?.term();
580
- if (!term)
581
- return;
582
- // Switching the render state to a fresh terminal makes the next
583
- // update full; so does a resize. A full repaint of the same terminal
584
- // comes from a clean render state.
585
- this.render.free();
586
- this.render = new RenderState(this.hub.engine);
582
+ this.fullNext = true;
587
583
  this.paint();
588
584
  }
585
+ visibility(visible) {
586
+ if (this.visible === visible)
587
+ return;
588
+ this.visible = visible;
589
+ if (visible)
590
+ this.repaint();
591
+ else if (this.paintTimer !== undefined) {
592
+ clearTimeout(this.paintTimer);
593
+ this.paintTimer = undefined;
594
+ }
595
+ }
596
+ painted() {
597
+ if (!this.frameInFlight)
598
+ return;
599
+ this.frameInFlight = false;
600
+ if (this.paintPending)
601
+ this.paint();
602
+ }
589
603
  paint() {
590
604
  if (this.paintTimer !== undefined) {
591
605
  clearTimeout(this.paintTimer);
592
606
  this.paintTimer = undefined;
593
607
  }
608
+ this.paintPending = true;
594
609
  const term = this.attachment?.term();
595
- if (!term || this.disposed)
610
+ if (!term || this.disposed || !this.visible || this.frameInFlight)
596
611
  return;
597
- this.post({ type: "frame", mount: this.id, frame: this.render.update(term) });
612
+ if (this.fullNext) {
613
+ this.render.free();
614
+ this.render = new RenderState(this.hub.engine);
615
+ this.fullNext = false;
616
+ }
617
+ const frame = this.render.updatePacked(term);
618
+ // Clean only the captured rows. While this frame is in flight, no
619
+ // extraction runs, so subsequent changes accumulate in the engine.
598
620
  this.render.clean();
621
+ this.paintPending = false;
622
+ this.frameInFlight = true;
623
+ this.post({ type: "frame", mount: this.id, frame }, { transfer: frame.lines.map((row) => row.cells.data.buffer) });
599
624
  // A switch to the alternate screen or a full reset took the selection.
600
625
  if (this.selected && !term.selected)
601
626
  this.unselected();
602
627
  }
603
628
  /** The mouse: a press clears the selection, or selects a word or a line
604
629
  * at once; a drag selects; the release copies what is selected. */
605
- select(action, at, time) {
630
+ select(message) {
631
+ const { at, time } = message;
606
632
  const term = this.attachment?.term();
607
- if (!term)
633
+ if (!term) {
634
+ // Nothing to select: the page's copy waits for the answer even so.
635
+ if (message.action === "release") {
636
+ this.post({ type: "selection", mount: this.id, text: undefined, copy: message.copy });
637
+ }
608
638
  return;
609
- switch (action) {
639
+ }
640
+ switch (message.action) {
610
641
  case "press":
611
642
  this.claim();
612
643
  if (this.selected)
@@ -625,7 +656,7 @@ class Mount {
625
656
  // (trimmed to nothing) are no text to copy.
626
657
  const text = term.selectionText() || undefined;
627
658
  this.selected = text !== undefined;
628
- this.post({ type: "selection", mount: this.id, text, copy: true });
659
+ this.post({ type: "selection", mount: this.id, text, copy: message.copy });
629
660
  break;
630
661
  }
631
662
  }
@@ -643,7 +674,7 @@ class Mount {
643
674
  }
644
675
  unselected() {
645
676
  this.selected = false;
646
- this.post({ type: "selection", mount: this.id, text: undefined, copy: false });
677
+ this.post({ type: "selection", mount: this.id, text: undefined });
647
678
  }
648
679
  /** The terminal goes, and its selection and the gesture's anchor with it. */
649
680
  forgetSelection(term) {
@@ -662,14 +693,8 @@ class Mount {
662
693
  const conn = this.hub.conn;
663
694
  if (conn) {
664
695
  conn.release(this.channel);
665
- if (!this.attachment.closed()) {
666
- try {
667
- conn.send(FrameType.Close, this.channel, new Uint8Array());
668
- }
669
- catch {
670
- // The connection is gone; nothing to close.
671
- }
672
- }
696
+ if (!this.attachment.closed())
697
+ conn.send(FrameType.Close, this.channel, new Uint8Array());
673
698
  }
674
699
  this.attachment.free();
675
700
  this.attachment = undefined;
@@ -718,7 +743,7 @@ class Mount {
718
743
  return;
719
744
  }
720
745
  this.claim();
721
- this.live();
746
+ this.backToLive();
722
747
  this.unselect();
723
748
  // Each request's base64 fits the control body with room for the rest,
724
749
  // and its bytes fit the input queue (at least one byte, so the loop
@@ -790,14 +815,14 @@ class Mount {
790
815
  if (!term || term.alternate)
791
816
  return;
792
817
  if (rows === "live")
793
- this.live();
818
+ this.backToLive();
794
819
  else if (rows !== 0) {
795
820
  term.scroll(rows);
796
821
  this.paint();
797
822
  }
798
823
  }
799
824
  /** Back to the live screen before input, as any terminal does. */
800
- live() {
825
+ backToLive() {
801
826
  const term = this.attachment?.term();
802
827
  if (!term)
803
828
  return;
@@ -837,12 +862,7 @@ class Mount {
837
862
  }
838
863
  report() {
839
864
  this.reclaim = false;
840
- try {
841
- this.hub.conn?.sendJson(FrameType.Viewport, this.channel, { ...this.viewport, profile: this.profile });
842
- }
843
- catch {
844
- // Reported through onEnd.
845
- }
865
+ this.hub.conn?.sendJson(FrameType.Viewport, this.channel, { ...this.viewport, profile: this.profile });
846
866
  }
847
867
  /** Input from the page in use: its size again if another client took it. */
848
868
  claim() {
@@ -889,29 +909,27 @@ function describe(exit) {
889
909
  /** Serves a page's `connect` on `port`: the worker module the page starts
890
910
  * calls `runWorker(self)`. */
891
911
  export function runWorker(port) {
892
- const hub = new Hub((message) => port.postMessage(message));
912
+ const hub = new Hub((message, options) => port.postMessage(message, options));
893
913
  port.onmessage = (e) => {
894
914
  const message = e.data;
895
- if (message.type === "init") {
896
- void hub.init(message);
897
- return;
898
- }
899
- if (message.type === "request") {
900
- void hub.request(message.id, message.method, message.params);
901
- return;
902
- }
903
- if (message.type === "cancel") {
904
- hub.cancel(message.id);
905
- return;
906
- }
907
- if (message.type === "mount") {
908
- const { type: _, mount, ...init } = message;
909
- hub.mount(mount, init);
910
- return;
911
- }
912
- if (message.type === "unmount") {
913
- hub.unmount(message.mount);
914
- return;
915
+ switch (message.type) {
916
+ case "init":
917
+ void hub.init(message);
918
+ return;
919
+ case "request":
920
+ void hub.request(message.id, message.method, message.params);
921
+ return;
922
+ case "cancel":
923
+ hub.cancel(message.id);
924
+ return;
925
+ case "mount": {
926
+ const { type: _, mount, ...init } = message;
927
+ hub.mount(mount, init);
928
+ return;
929
+ }
930
+ case "unmount":
931
+ hub.unmount(message.mount);
932
+ return;
915
933
  }
916
934
  const mount = hub.mounts.get(message.mount);
917
935
  if (!mount)
@@ -933,7 +951,7 @@ export function runWorker(port) {
933
951
  mount.text(message.text);
934
952
  break;
935
953
  case "select":
936
- mount.select(message.action, message.at, message.time);
954
+ mount.select(message);
937
955
  break;
938
956
  case "wheel":
939
957
  mount.wheel(message.rows);
@@ -947,6 +965,12 @@ export function runWorker(port) {
947
965
  case "focus":
948
966
  mount.focus(message.focused);
949
967
  break;
968
+ case "visibility":
969
+ mount.visibility(message.visible);
970
+ break;
971
+ case "painted":
972
+ mount.painted();
973
+ break;
950
974
  case "repaint":
951
975
  mount.repaint();
952
976
  break;
@@ -1,4 +1,4 @@
1
- import { EngineError, Terminal } from "./index.js";
1
+ import { Terminal } from "./index.js";
2
2
  /** Bytes before the first record: the 8-byte magic and the u16 version. */
3
3
  const ENVELOPE_LEN = 10;
4
4
  /** Bytes in a record header: tag u16, payload length u32, CRC32C u32. */
@@ -101,15 +101,11 @@ export class Decoder {
101
101
  const end = stepEnd(this.source, this.consumed, this.filled);
102
102
  if (end === undefined)
103
103
  throw new Error("next page not buffered");
104
- const code = this.engine.call("ghostty_snapshot_decoder_next", this.decoder);
104
+ const page = this.engine.present("ghostty_snapshot_decoder_next", this.decoder);
105
105
  this.consumed = end;
106
- if (code === this.engine.result.NO_VALUE) {
106
+ if (!page)
107
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;
108
+ return page;
113
109
  }
114
110
  terminal() {
115
111
  return this.term;
@@ -2,7 +2,7 @@ export { Decoder, readyEnd } from "./decoder.ts";
2
2
  export { KeyEncoder, isText, keyName } from "./keys.ts";
3
3
  export type { KeyInput } from "./keys.ts";
4
4
  export { FLAG, NO_COLOR, RenderState } from "./render.ts";
5
- export type { Cell, Frame, FrameRow } from "./render.ts";
5
+ export type { Cell, Frame, FrameRow, PackedCells, PackedFrame } from "./render.ts";
6
6
  export { ENGINE } from "./pin.ts";
7
7
  export { SelectionGesture } from "./selection.ts";
8
8
  export type { CellPoint } from "./selection.ts";
@@ -33,6 +33,8 @@ export declare class EngineError extends Error {
33
33
  export declare class Engine {
34
34
  private readonly exports;
35
35
  private readonly types;
36
+ private memoryView;
37
+ private dataView;
36
38
  /** The `GhosttyResult` members the wrapper branches on. */
37
39
  readonly result: {
38
40
  SUCCESS: number;
@@ -42,12 +44,15 @@ export declare class Engine {
42
44
  };
43
45
  private constructor();
44
46
  static load(wasm: BufferSource): Promise<Engine>;
45
- /** The current linear memory; reacquired on every access because it may grow. */
47
+ /** Cached views are valid only until wasm grows its linear memory. */
46
48
  get memory(): Uint8Array;
47
49
  get view(): DataView;
48
50
  call(name: string, ...args: number[]): number;
49
51
  /** Calls a function returning GhosttyResult and throws on failure. */
50
52
  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
+ present(name: string, ...args: number[]): boolean;
51
56
  alloc(len: number): number;
52
57
  free(ptr: number, len: number): void;
53
58
  /** Runs `f` with `bytes` copied into wasm memory, or with a null
@@ -16,6 +16,8 @@ export class EngineError extends Error {
16
16
  export class Engine {
17
17
  exports;
18
18
  types;
19
+ memoryView;
20
+ dataView;
19
21
  /** The `GhosttyResult` members the wrapper branches on. */
20
22
  result;
21
23
  constructor(exports) {
@@ -34,12 +36,18 @@ export class Engine {
34
36
  const { instance } = await WebAssembly.instantiate(wasm, {});
35
37
  return new Engine(instance.exports);
36
38
  }
37
- /** The current linear memory; reacquired on every access because it may grow. */
39
+ /** Cached views are valid only until wasm grows its linear memory. */
38
40
  get memory() {
39
- return new Uint8Array(this.exports.memory.buffer);
41
+ const buffer = this.exports.memory.buffer;
42
+ if (this.memoryView?.buffer !== buffer)
43
+ this.memoryView = new Uint8Array(buffer);
44
+ return this.memoryView;
40
45
  }
41
46
  get view() {
42
- return new DataView(this.exports.memory.buffer);
47
+ const buffer = this.exports.memory.buffer;
48
+ if (this.dataView?.buffer !== buffer)
49
+ this.dataView = new DataView(buffer);
50
+ return this.dataView;
43
51
  }
44
52
  call(name, ...args) {
45
53
  const fn = this.exports[name];
@@ -53,6 +61,16 @@ export class Engine {
53
61
  if (code !== this.result.SUCCESS)
54
62
  throw new EngineError(name, code);
55
63
  }
64
+ /** Calls a function returning GhosttyResult: true for a value, false
65
+ * for `NO_VALUE`; throws on failure. */
66
+ present(name, ...args) {
67
+ const code = this.call(name, ...args);
68
+ if (code === this.result.NO_VALUE)
69
+ return false;
70
+ if (code !== this.result.SUCCESS)
71
+ throw new EngineError(name, code);
72
+ return true;
73
+ }
56
74
  alloc(len) {
57
75
  const ptr = this.call("ghostty_wasm_alloc", len);
58
76
  if (ptr === 0)
@@ -190,6 +208,12 @@ export class Engine {
190
208
  }
191
209
  /** Reads a bit field of a 64-bit packed value at `ptr`. */
192
210
  readBits(ptr, field) {
211
+ const bit = field.lsb % 32;
212
+ if (bit + field.width <= 32) {
213
+ const word = this.view.getUint32(ptr + 4 * Math.floor(field.lsb / 32), true) >>> bit;
214
+ // A 32-bit mask would be signed; preserve the unsigned full word.
215
+ return field.width === 32 ? word : word & (2 ** field.width - 1);
216
+ }
193
217
  const value = this.view.getBigUint64(ptr, true);
194
218
  return Number((value >> BigInt(field.lsb)) & ((1n << BigInt(field.width)) - 1n));
195
219
  }
@@ -349,14 +373,9 @@ export class Terminal {
349
373
  }
350
374
  get title() {
351
375
  const size = this.engine.sizeOf("GhosttyString");
352
- return this.engine.scratch(size, (out) => {
353
- const code = this.engine.call("ghostty_terminal_get", this.handle, this.engine.enumValue("GhosttyTerminalData", "TITLE"), out);
354
- if (code === this.engine.result.NO_VALUE)
355
- return "";
356
- if (code !== this.engine.result.SUCCESS)
357
- throw new EngineError("ghostty_terminal_get", code);
358
- return this.engine.readString(out);
359
- });
376
+ return this.engine.scratch(size, (out) => this.engine.present("ghostty_terminal_get", this.handle, this.engine.enumValue("GhosttyTerminalData", "TITLE"), out)
377
+ ? this.engine.readString(out)
378
+ : "");
360
379
  }
361
380
  /** An untracked `GhosttyGridRef` to the viewport cell at `at`, valid
362
381
  * until the terminal changes. */
@@ -401,11 +420,8 @@ export class Terminal {
401
420
  e.view.setUint32(out, 0, true);
402
421
  e.view.setUint32(out + 4, 0, true);
403
422
  // The options are passed by value, which the wasm ABI passes by address.
404
- const code = e.call("ghostty_terminal_selection_format_alloc", this.handle, 0, optionsPtr, out, out + 4);
405
- if (code === e.result.NO_VALUE)
423
+ if (!e.present("ghostty_terminal_selection_format_alloc", this.handle, 0, optionsPtr, out, out + 4))
406
424
  return undefined;
407
- if (code !== e.result.SUCCESS)
408
- throw new EngineError("ghostty_terminal_selection_format_alloc", code);
409
425
  const ptr = e.view.getUint32(out, true);
410
426
  const len = e.view.getUint32(out + 4, true);
411
427
  try {
@@ -422,12 +438,7 @@ export class Terminal {
422
438
  const size = e.sizeOf("GhosttySelection");
423
439
  return e.scratch(size, (out) => {
424
440
  e.view.setUint32(out, size, true);
425
- const code = e.call("ghostty_terminal_get", this.handle, e.enumValue("GhosttyTerminalData", "SELECTION"), out);
426
- if (code === e.result.NO_VALUE)
427
- return false;
428
- if (code !== e.result.SUCCESS)
429
- throw new EngineError("ghostty_terminal_get", code);
430
- return true;
441
+ return e.present("ghostty_terminal_get", this.handle, e.enumValue("GhosttyTerminalData", "SELECTION"), out);
431
442
  });
432
443
  }
433
444
  /** Whether DEC private mode `number` is set. */
@@ -48,9 +48,15 @@ export class KeyEncoder {
48
48
  e.call("ghostty_key_event_set_action", this.event, e.enumValue("GhosttyKeyAction", input.action.toUpperCase()));
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
- 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
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);
59
+ e.call("ghostty_key_event_set_composing", this.event, input.composing ? 1 : 0);
54
60
  const utf8 = new TextEncoder().encode(text);
55
61
  return e.withBytes(utf8, (textPtr) => {
56
62
  e.call("ghostty_key_event_set_utf8", this.event, textPtr, utf8.length);