@gjermundgaraba/clankermux 0.7.0 → 0.8.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 CHANGED
@@ -40,7 +40,7 @@ A page holds one client: one worker, one WebSocket, the session list and the eve
40
40
 
41
41
  ## `connect`
42
42
 
43
- `connect({url, worker, wasm, client?})` starts the worker and resolves with the `Client` once the engine is loaded, before the connection is up: the worker connects, and after a loss reconnects, by itself, and the client's `onLink`, and each terminal's, says how that goes (below). Requests need a `connected` link. It rejects when the worker script or the engine cannot be loaded. An exception in the worker after that is the browser's to report, as before: it costs the message being handled, and the client goes on. `client` is the `{name, version}` the connection gives in `HELLO`, which the server lists in each session's `attachments`; `clankermux-web` and this package's version by default.
43
+ `connect({url, worker, wasm, client?})` starts the worker and resolves with the `Client` once the engine is loaded, before the connection is up: the worker connects, and after a loss reconnects, by itself, and the client's `onLink`, and each terminal's, says how that goes (below). Requests need a `connected` link. It rejects when the worker script or the engine cannot be loaded. An exception in the worker after that is the browser's to report: it costs the message being handled, and the client goes on. `client` is the `{name, version}` the connection gives in `HELLO`, which the server lists in each session's `attachments`; `clankermux-web` and this package's version by default.
44
44
 
45
45
  ## `Client`
46
46
 
@@ -113,7 +113,7 @@ The terminal's input element is a textarea the mount places after the canvas: an
113
113
 
114
114
  A page that holds input back (a pause) holds these events, which the terminal acts on: on the input element `keydown`, `keyup`, `beforeinput`, `compositionstart`, `compositionend`, `paste`, `beforecopy` and `copy`; on the canvas `mousedown` (focus and selection; the drag continues on `window` `mousemove`/`mouseup`) and `wheel` (which sends arrow keys on the alternate screen). macOS press-and-hold accents send the base letter on its keydown and then the accented one, as in xterm.js.
115
115
 
116
- Verified in headless Chrome on macOS through CDP (`Input.imeSetComposition`, `Input.insertText`, `Input.dispatchKeyEvent`): a Japanese composition sent once at its end, nothing during it; a dead key (Option+E, then é) with no Alt+E sent; an emoji inserted with no key; plain keys sent once. Not verified by hand with a real input method, in Safari or in Firefox.
116
+ No test covers input methods; they are untested with a real input method, in Safari and in Firefox.
117
117
 
118
118
  ## `onSessions`
119
119
 
@@ -195,7 +195,7 @@ The selection is the engine's, drawn inverted, and the engine keeps it on its ce
195
195
 
196
196
  It is cleared by a click (a press that selects nothing), by input sent to the session (a key that sends something, a paste, composed text; not a modifier alone), by a switch to the alternate screen (it is not back when the program leaves it), by a full reset (RIS), and by a new screen (attaching, a gap).
197
197
 
198
- The copy on release starts within `mouseup`, as Safari requires: `navigator.clipboard.write` with a `ClipboardItem` whose text arrives when the worker has it, a few milliseconds later; when nothing was selected it fails and the clipboard is left alone. Where `ClipboardItem` is missing, or takes no text to come, it is `writeText` once the text arrives, which a browser that wants the gesture still running may refuse; Cmd+C then copies. Verified in headless Chrome only. A `beforecopy` listener lets WebKit offer Copy on the empty input element while there is a selection. Where the browser keeps Ctrl+Shift+C for itself (a developer tools shortcut), Cmd+C or the Copy menu copies. Programs that ask for mouse reports get none from this client, so the mouse always selects.
198
+ The copy on release starts within `mouseup`, as Safari requires: `navigator.clipboard.write` with a `ClipboardItem` whose text arrives when the worker has it, a few milliseconds later; when nothing was selected it fails and the clipboard is left alone. Where `ClipboardItem` is missing, or takes no text to come, it is `writeText` once the text arrives, which a browser that wants the gesture still running may refuse; Cmd+C then copies. A `beforecopy` listener lets WebKit offer Copy on the empty input element while there is a selection. Where the browser keeps Ctrl+Shift+C for itself (a developer tools shortcut), Cmd+C or the Copy menu copies. Programs that ask for mouse reports get none from this client, so the mouse always selects.
199
199
 
200
200
  ### Copy outcome
201
201
 
@@ -214,7 +214,7 @@ type CopySource = "release" | "keys" | "browser" | "program";
214
214
  - `refused`: the browser rejected the write, `message` its error (`NotAllowedError: …`): a permission denied, a document without the focus, an Electron shell that does not allow it. `timeout`: the copy did not finish within two seconds (the worker's text or the write); without the worker's text it has no `chars`. `unavailable`: the page has no `navigator.clipboard` (it is not a secure context).
215
215
  - A release that selected nothing (a click) is no copy and tells nothing; nor does a release whose copy a newer one replaced before its text arrived. Each copy is told once.
216
216
 
217
- A page shows a brief notification on `ok: true` ("Copied 42 characters"), and on a failure says the clipboard did not take it (with Cmd+C as the way to try again, since the browser's own copy is the one a browser that refuses `navigator.clipboard` still allows). A mouseup with `selection()` is no substitute: it says nothing about whether the write succeeded. `program` copies arrive without the user's gesture, which a browser may refuse; a page that announces only the user's copies leaves them out. Verified in headless Chrome: a double click's copy is told `ok` with the clipboard holding the word, and `refused` with the clipboard permission denied; the rest (timeouts, no `ClipboardItem`, no clipboard) by the package's tests against a stand-in clipboard. Not verified in Safari, Firefox or Electron.
217
+ A page shows a brief notification on `ok: true` ("Copied 42 characters"), and on a failure says the clipboard did not take it (with Cmd+C as the way to try again, since the browser's own copy is the one a browser that refuses `navigator.clipboard` still allows). A mouseup with `selection()` is no substitute: it says nothing about whether the write succeeded. `program` copies arrive without the user's gesture, which a browser may refuse; a page that announces only the user's copies leaves them out. Copying is tested in headless Chrome only, not in Safari, Firefox or Electron.
218
218
 
219
219
  ## Scrollback
220
220
 
@@ -32,8 +32,6 @@ export declare class Attachment {
32
32
  private continuation;
33
33
  session: Session | undefined;
34
34
  constructor(engine: Engine);
35
- /** Readies an `OPEN` on the connection `welcome` began: the cursor to
36
- * resume from, when there is one to keep. */
37
35
  open(welcome: Welcome): Resume | undefined;
38
36
  resume(): Resume | undefined;
39
37
  ended(): boolean;
@@ -17,8 +17,6 @@ export class Attachment {
17
17
  constructor(engine) {
18
18
  this.engine = engine;
19
19
  }
20
- /** Readies an `OPEN` on the connection `welcome` began: the cursor to
21
- * resume from, when there is one to keep. */
22
20
  open(welcome) {
23
21
  this.incarnation = welcome.incarnation;
24
22
  this.continuation = welcome.limits.continuation;
@@ -1,14 +1,3 @@
1
- /**
2
- * Finds OSC 52 clipboard writes in output bytes, across chunk boundaries,
3
- * ending them as the engine's parser does: BEL or any byte after ESC
4
- * dispatches the sequence (ESC `\` is the string terminator; ESC `]`
5
- * begins the next one), CAN and SUB abandon it. The engine raises the
6
- * clipboard effect through a callback, which from wasm would need a JS
7
- * function in the module's function table (a generated wrapper per
8
- * signature); the scan is less machinery. OSC 1337 and 5522 clipboard
9
- * forms are not scanned for, and an OSC 52 inside another string (a DCS
10
- * passthrough) is taken as one, where the engine would not.
11
- */
12
1
  const ESC = 0x1b;
13
2
  const BEL = 0x07;
14
3
  const CAN = 0x18;
@@ -1,5 +1,9 @@
1
1
  import { type Engine, Terminal } from "./index.ts";
2
2
  export declare function readyEnd(buf: Uint8Array, len?: number): number | undefined;
3
+ /** Where the step one decoder `next` call takes ends, from the first record
4
+ * it has not returned: just past the next PAGE or FINISH. After a PAGE the
5
+ * decoder also reads the next known record header, which must be buffered
6
+ * too and starts the next step. */
3
7
  export declare function stepEnd(buf: Uint8Array, pos: number, len?: number): number | undefined;
4
8
  export declare class Decoder {
5
9
  readonly engine: Engine;
@@ -9,6 +9,9 @@ const RECORD_HEADER_LEN = TAG_LEN + PAYLOAD_LENGTH_LEN + CRC32C_LEN;
9
9
  const TAG_PAGE = 3;
10
10
  const TAG_READY = 5;
11
11
  const TAG_FINISH = 6;
12
+ /** The record tags the wasm build decodes. It has no Kitty graphics, so it
13
+ * skips Kitty records (tags 8 to 10) whole, as it does any unknown record. */
14
+ const LAST_KNOWN_TAG = 7;
12
15
  function recordAt(buf, len, pos) {
13
16
  if (pos + RECORD_HEADER_LEN > len)
14
17
  return undefined;
@@ -31,8 +34,36 @@ function endOfStep(buf, len, pos, last) {
31
34
  export function readyEnd(buf, len = buf.length) {
32
35
  return endOfStep(buf, len, ENVELOPE_LEN, (tag) => tag === TAG_READY);
33
36
  }
37
+ /** Where the step one decoder `next` call takes ends, from the first record
38
+ * it has not returned: just past the next PAGE or FINISH. After a PAGE the
39
+ * decoder also reads the next known record header, which must be buffered
40
+ * too and starts the next step. */
34
41
  export function stepEnd(buf, pos, len = buf.length) {
35
- return endOfStep(buf, len, pos, (tag) => tag === TAG_PAGE || tag === TAG_FINISH);
42
+ for (;;) {
43
+ const record = recordAt(buf, len, pos);
44
+ if (!record)
45
+ return undefined;
46
+ const [tag, end] = record;
47
+ if (tag === TAG_FINISH)
48
+ return end;
49
+ if (tag === TAG_PAGE)
50
+ return afterPage(buf, len, end);
51
+ pos = end;
52
+ }
53
+ }
54
+ function afterPage(buf, len, pos) {
55
+ const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
56
+ for (;;) {
57
+ if (pos + RECORD_HEADER_LEN > len)
58
+ return undefined;
59
+ const tag = view.getUint16(pos, true);
60
+ if (tag >= 1 && tag <= LAST_KNOWN_TAG)
61
+ return pos;
62
+ const record = recordAt(buf, len, pos);
63
+ if (!record)
64
+ return undefined;
65
+ pos = record[1];
66
+ }
36
67
  }
37
68
  export class Decoder {
38
69
  engine;
@@ -98,5 +98,7 @@ export declare class Terminal {
98
98
  setColor(kind: "foreground" | "background" | "cursor", color: number): void;
99
99
  snapshot(): Uint8Array;
100
100
  formatPlain(): string;
101
+ /** Plain text from a formatter, after `configure` sets its options. */
102
+ private format;
101
103
  free(): void;
102
104
  }
@@ -361,7 +361,6 @@ export class Terminal {
361
361
  const coordinate = ptr + e.offsetOf("GhosttyPoint", "value");
362
362
  e.view.setUint16(coordinate + e.offsetOf("GhosttyPointCoordinate", "x"), x, true);
363
363
  e.view.setUint32(coordinate + e.offsetOf("GhosttyPointCoordinate", "y"), y, true);
364
- e.view.setUint32(ptr + point, ref, true);
365
364
  // The point is passed by value, which the wasm ABI passes by address.
366
365
  e.check("ghostty_terminal_grid_ref", this.handle, ptr, ptr + point);
367
366
  return e.memory.slice(ptr + point, ptr + point + ref);
@@ -376,34 +375,20 @@ export class Terminal {
376
375
  }
377
376
  selectionText() {
378
377
  const e = this.engine;
379
- const options = e.pack("GhosttyTerminalSelectionFormatOptions", {
380
- emit: e.enumValue("GhosttyFormatterFormat", "PLAIN"),
381
- unwrap: 1,
382
- trim: 1,
383
- });
384
- return e.withBytes(options, (optionsPtr) => e.scratch(8, (out) => {
385
- e.view.setUint32(out, 0, true);
386
- e.view.setUint32(out + 4, 0, true);
387
- // The options are passed by value, which the wasm ABI passes by address.
388
- if (!e.present("ghostty_terminal_selection_format_alloc", this.handle, 0, optionsPtr, out, out + 4))
378
+ return e.scratch(e.sizeOf("GhosttySelection"), (selection) => {
379
+ if (!e.present("ghostty_terminal_get", this.handle, e.enumValue("GhosttyTerminalData", "SELECTION"), selection)) {
389
380
  return undefined;
390
- const ptr = e.view.getUint32(out, true);
391
- const len = e.view.getUint32(out + 4, true);
392
- try {
393
- return new TextDecoder().decode(e.memory.slice(ptr, ptr + len));
394
381
  }
395
- finally {
396
- e.call("ghostty_free", 0, ptr, len);
397
- }
398
- }));
382
+ return this.format((set) => {
383
+ set("SELECTION", selection);
384
+ set("UNWRAP", new Uint8Array([1]));
385
+ });
386
+ });
399
387
  }
400
388
  get selected() {
401
389
  const e = this.engine;
402
390
  const size = e.sizeOf("GhosttySelection");
403
- return e.scratch(size, (out) => {
404
- e.view.setUint32(out, size, true);
405
- return e.present("ghostty_terminal_get", this.handle, e.enumValue("GhosttyTerminalData", "SELECTION"), out);
406
- });
391
+ return e.scratch(size, (out) => e.present("ghostty_terminal_get", this.handle, e.enumValue("GhosttyTerminalData", "SELECTION"), out));
407
392
  }
408
393
  mode(number) {
409
394
  const config = this.engine.pack("GhosttyTerminalModeConfig", { mode: number });
@@ -424,19 +409,25 @@ export class Terminal {
424
409
  return this.engine.allocated((ptr, len) => this.engine.check("ghostty_snapshot_encode_alloc", this.handle, 0, ptr, len));
425
410
  }
426
411
  formatPlain() {
427
- const options = this.engine.pack("GhosttyFormatterTerminalOptions", {
428
- emit: this.engine.enumValue("GhosttyFormatterFormat", "PLAIN"),
429
- extra: { screen: {} },
430
- });
431
- return this.engine.withBytes(options, (optionsPtr) => {
432
- const formatter = this.engine.construct("ghostty_formatter_terminal_new", this.handle, optionsPtr);
433
- try {
434
- return new TextDecoder().decode(this.engine.allocated((ptr, len) => this.engine.check("ghostty_formatter_format_alloc", formatter, 0, ptr, len)));
435
- }
436
- finally {
437
- this.engine.call("ghostty_formatter_free", formatter);
438
- }
439
- });
412
+ return this.format((set) => set("TRIM", new Uint8Array([0])));
413
+ }
414
+ /** Plain text from a formatter, after `configure` sets its options. */
415
+ format(configure) {
416
+ const e = this.engine;
417
+ const formatter = e.construct("ghostty_formatter_terminal_new", this.handle);
418
+ try {
419
+ configure((option, value) => {
420
+ const key = e.enumValue("GhosttyFormatterOption", option);
421
+ if (typeof value === "number")
422
+ e.check("ghostty_formatter_set", formatter, key, value);
423
+ else
424
+ e.withBytes(value, (ptr) => e.check("ghostty_formatter_set", formatter, key, ptr));
425
+ });
426
+ return new TextDecoder().decode(e.allocated((ptr, len) => e.check("ghostty_formatter_format_alloc", formatter, 0, ptr, len)));
427
+ }
428
+ finally {
429
+ e.call("ghostty_formatter_free", formatter);
430
+ }
440
431
  }
441
432
  free() {
442
433
  this.engine.call("ghostty_terminal_free", this.handle);
@@ -37,7 +37,7 @@ export class KeyEncoder {
37
37
  }
38
38
  encode(term, input) {
39
39
  const e = this.engine;
40
- e.call("ghostty_key_encoder_setopt_from_terminal", this.encoder, term.handle);
40
+ e.check("ghostty_key_encoder_set_from_terminal", this.encoder, term.handle);
41
41
  const name = keyName(input.code);
42
42
  const key = e.hasEnumValue("GhosttyKey", name) ? e.enumValue("GhosttyKey", name) : this.unidentified;
43
43
  const composed = input.altComposed === true;
@@ -53,7 +53,9 @@ export class KeyEncoder {
53
53
  e.call("ghostty_key_event_set_composing", this.event, input.composing ? 1 : 0);
54
54
  const utf8 = new TextEncoder().encode(text);
55
55
  return e.withBytes(utf8, (textPtr) => {
56
- e.call("ghostty_key_event_set_utf8", this.event, textPtr, utf8.length);
56
+ const string = e.pack("GhosttyString", { ptr: textPtr, len: utf8.length });
57
+ // The string is passed by value, which the wasm ABI passes by address.
58
+ e.withBytes(string, (stringPtr) => e.check("ghostty_key_event_set_utf8", this.event, stringPtr));
57
59
  const base = input.unshifted ?? (text ? text.toLowerCase() : "");
58
60
  e.call("ghostty_key_event_set_unshifted_codepoint", this.event, base ? base.codePointAt(0) : 0);
59
61
  return e.growing("ghostty_key_encoder_encode", 64, (buf, cap, len) => e.call("ghostty_key_encoder_encode", this.encoder, this.event, buf, cap, len));
@@ -1,2 +1 @@
1
- /** Engine id of the vendored libghostty-vt build, matched by the server. */
2
- export declare const ENGINE = "ghostty-vt@1.3.2-main+d2865b8";
1
+ export declare const ENGINE = "ghostty-vt@1.0.1";
@@ -1,3 +1 @@
1
- // Written by scripts/pin.sh from vendor/pin.json.
2
- /** Engine id of the vendored libghostty-vt build, matched by the server. */
3
- export const ENGINE = "ghostty-vt@1.3.2-main+d2865b8";
1
+ export const ENGINE = "ghostty-vt@1.0.1";
@@ -74,6 +74,7 @@ export declare class RenderState {
74
74
  private readonly tags;
75
75
  private readonly wide;
76
76
  private readonly cell;
77
+ private readonly style;
77
78
  private readonly cursorStyles;
78
79
  constructor(engine: Engine);
79
80
  update(term: Terminal): Frame;
@@ -13,6 +13,7 @@ export const FLAG = {
13
13
  };
14
14
  export const NO_COLOR = -1;
15
15
  const CURSOR_STYLES = ["bar", "block", "underline", "block_hollow"];
16
+ const STYLE_FLAGS = ["bold", "italic", "faint", "blink", "inverse", "invisible", "strikethrough", "overline"];
16
17
  export const PACKED_STRIDE = 4;
17
18
  export const PACKED_TEXT = 0;
18
19
  export const PACKED_FG = 1;
@@ -44,6 +45,7 @@ export class RenderState {
44
45
  tags;
45
46
  wide;
46
47
  cell;
48
+ style;
47
49
  cursorStyles;
48
50
  constructor(engine) {
49
51
  this.engine = engine;
@@ -76,7 +78,8 @@ export class RenderState {
76
78
  codepoint: engine.bits("GhosttyCell", "content", "CODEPOINT", "codepoint"),
77
79
  wide: engine.bits("GhosttyCell", "wide"),
78
80
  };
79
- this.cursorStyles = new Map(CURSOR_STYLES.map((style) => [engine.enumValue("GhosttyRenderStateCursorVisualStyle", style.toUpperCase()), style]));
81
+ this.style = Object.fromEntries([...STYLE_FLAGS, "underline"].map((key) => [key, engine.bits("GhosttyStyleFlags", key)]));
82
+ this.cursorStyles = new Map(CURSOR_STYLES.map((style) => [engine.enumValue("GhosttyCursorStyle", style.toUpperCase()), style]));
80
83
  }
81
84
  update(term) {
82
85
  return this.frame(term, (cols) => {
@@ -179,7 +182,6 @@ export class RenderState {
179
182
  e.view.setUint32(out, this.cells, true);
180
183
  e.check("ghostty_render_state_row_get", this.iterator, this.keys.row["CELLS"], out);
181
184
  });
182
- const styleSize = e.sizeOf("GhosttyStyle");
183
185
  let count = 0;
184
186
  const utf8Cap = 64;
185
187
  const layout = scratchLayout(e, ["GhosttyCell", "GhosttyColorRgb", "GhosttyStyle", "GhosttyBuffer"]);
@@ -208,26 +210,13 @@ export class RenderState {
208
210
  const bg = this.color(this.keys.cell["BG_COLOR"], color);
209
211
  e.check("ghostty_render_state_row_cells_get", this.cells, this.keys.cell["HAS_STYLING"], raw);
210
212
  if (e.memory[raw]) {
211
- e.view.setUint32(style, styleSize, true);
212
213
  e.check("ghostty_render_state_row_cells_get", this.cells, this.keys.cell["STYLE"], style);
213
- const f = (key) => e.field("GhosttyStyle", style, key) !== 0;
214
- if (f("bold"))
215
- flags |= FLAG.BOLD;
216
- if (f("italic"))
217
- flags |= FLAG.ITALIC;
218
- if (f("faint"))
219
- flags |= FLAG.FAINT;
220
- if (f("blink"))
221
- flags |= FLAG.BLINK;
222
- if (f("inverse"))
223
- flags |= FLAG.INVERSE;
224
- if (f("invisible"))
225
- flags |= FLAG.INVISIBLE;
226
- if (f("strikethrough"))
227
- flags |= FLAG.STRIKETHROUGH;
228
- if (f("overline"))
229
- flags |= FLAG.OVERLINE;
230
- underline = e.field("GhosttyStyle", style, "underline");
214
+ const styleFlags = style + e.offsetOf("GhosttyStyle", "flags");
215
+ for (const key of STYLE_FLAGS) {
216
+ if (e.readBits(styleFlags, this.style[key]))
217
+ flags |= FLAG[key.toUpperCase()];
218
+ }
219
+ underline = e.readBits(styleFlags, this.style.underline);
231
220
  }
232
221
  write(codepoint, text, fg, bg, flags, underline);
233
222
  count++;
@@ -237,7 +226,7 @@ export class RenderState {
237
226
  color(key, out) {
238
227
  const e = this.engine;
239
228
  const code = e.call("ghostty_render_state_row_cells_get", this.cells, key, out);
240
- if (code === e.result.INVALID_VALUE)
229
+ if (code === e.result.NO_VALUE)
241
230
  return NO_COLOR;
242
231
  if (code !== e.result.SUCCESS)
243
232
  throw new EngineError("ghostty_render_state_row_cells_get", code);
@@ -315,7 +315,7 @@ export type Empty = Record<string, unknown>;
315
315
  export type ErrorCode =
316
316
  /** A connection error under Framing; the connection closes. */
317
317
  "protocol_error"
318
- /** `HELLO.version` is not 1; the connection closes. */
318
+ /** `HELLO.version` is not 2; the connection closes. */
319
319
  | "version_mismatch"
320
320
  /**
321
321
  * Malformed payload, a field the protocol does not define, unknown method, a
@@ -1,4 +1,3 @@
1
- // Generated from the catalog by `cargo xtask protocol`; do not edit.
2
1
  export const VERSION = 2;
3
2
  /** Each frame type's header byte. */
4
3
  export const FrameType = {
package/ghostty-vt.wasm CHANGED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjermundgaraba/clankermux",
3
- "version": "0.7.0",
3
+ "version": "0.8.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -36,12 +36,12 @@
36
36
  "access": "public"
37
37
  },
38
38
  "dependencies": {
39
- "xstate": "6.0.0-alpha.71"
39
+ "xstate": "6.0.0-beta.6"
40
40
  },
41
41
  "devDependencies": {
42
- "@types/ws": "^8.18.0",
42
+ "@types/ws": "^8.18.2",
43
43
  "ajv": "^8.20.0",
44
- "ws": "^8.22.2"
44
+ "ws": "^8.23.1"
45
45
  },
46
46
  "scripts": {
47
47
  "typecheck": "tsc -p tsconfig.json && tsc -p tsconfig.worker.json",