@gjermundgaraba/clankermux 0.4.0 → 0.4.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
@@ -97,14 +97,16 @@ While connected, a cancelled wait is cancelled by the server, so it rejects with
97
97
  | `onLink(listener)` | Each change of the link to the session (below). |
98
98
  | `onScroll(listener)` | The rows between the bottom of the view and the live screen's (below), from the first frame on. |
99
99
  | `onStatus(listener)` | The same moments as `onLink`, and more, as text for display. |
100
+ | `onCopy(listener)` | Each copy to the clipboard as it ends: copied, or failed and why (below). |
100
101
 
101
- The `on*` methods return a function that removes the listener; a listener added after a change gets the latest at once.
102
+ The `on*` methods return a function that removes the listener; a listener added after a change gets the latest at once, except `onCopy`'s, which is told only of copies after it.
102
103
 
103
104
  ## Keys and composed text
104
105
 
105
106
  The terminal's input element is a textarea the mount places after the canvas: an input method composes only into an editable element. It is invisible, out of the tab order (`tabIndex = -1`) and ignores the pointer. Focusing the canvas (a click, `canvas.focus()`, a tab stop the page gives it) or calling `focus()` focuses the textarea, so `document.activeElement` is the textarea while the terminal has the keyboard: test for focus with `container.contains(document.activeElement)`, not `=== canvas`. Key events bubble from it through the container as they did from the canvas.
106
107
 
107
108
  - Keys go to the engine's key encoder, which follows the program's modes (cursor keys, the kitty keyboard protocol). The browser keeps Cmd (Meta) chords and Ctrl+Shift+V, its paste.
109
+ - Shift that made a key's text (`:`, `"`, `A`) is consumed, as a keyboard reports it: under the kitty protocol's disambiguation, which neovim asks for, the text goes as typed, on any layout; reporting all keys, the key is the layout's own character with Shift, and the shifted one is among the alternates when the program asks for them. Shift made the text when it is not the key's own character, as the browser's layout map has it (Chromium); for a key the map does not cover (the numpad, Space) or without a map (Safari, Firefox), when it is any text but a space.
108
110
  - A key that belongs to an input method is left to it: one that is composing (`isComposing`), that it processes (`Process`, key code 229), or a dead key (`Dead`). The composition is shown at the cursor, in the terminal's colours, while it lasts, and its text is sent when it ends (`compositionend`). A key that ended a composition sends no release.
109
111
  - Text inserted with no key that sent it (a dead key's character, the emoji picker, dictation) is sent as it is inserted (`beforeinput` `insertText`); a line break or a backward delete from such a source is sent as Enter or Backspace.
110
112
  - Composed text goes to the program as UTF-8, as typed text does, not as a paste.
@@ -193,7 +195,26 @@ The selection is the engine's, drawn inverted, and the engine keeps it on its ce
193
195
 
194
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).
195
197
 
196
- 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 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. 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.
199
+
200
+ ### Copy outcome
201
+
202
+ `terminal.onCopy(listener)` tells the page what each copy to the clipboard came to, once it is known:
203
+
204
+ ```ts
205
+ type Copy =
206
+ | { ok: true; source: CopySource; chars: number }
207
+ | { ok: false; source: CopySource; reason: "refused" | "timeout" | "unavailable"; chars?: number; message?: string };
208
+ type CopySource = "release" | "keys" | "browser" | "program";
209
+ ```
210
+
211
+ - `source`: `release`, the selection copied as the button came up; `keys`, Ctrl+Shift+C; `browser`, the browser's own copy (Cmd+C, the Copy menu); `program`, the program's OSC 52 write.
212
+ - `chars` is the text's length in characters (code points). The text itself is in the clipboard, and `selection()` has it.
213
+ - `ok: true` once the browser's write resolved (for `browser`, once the `copy` event's data was set).
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
+ - 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
+
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.
197
218
 
198
219
  ## Scrollback
199
220
 
@@ -0,0 +1,69 @@
1
+ /** The page's copies to the clipboard, and what each came to. */
2
+ /** What started a copy: the release of a selection, Ctrl+Shift+C, the
3
+ * browser's copy (Cmd+C, the Copy menu), or the program (OSC 52). */
4
+ export type CopySource = "release" | "keys" | "browser" | "program";
5
+ /** Why a copy failed: the browser refused the write (`message` says
6
+ * what it gave), it did not finish within two seconds, or the page has no
7
+ * clipboard (outside a secure context). */
8
+ export type CopyFailure = "refused" | "timeout" | "unavailable";
9
+ /** A copy's outcome. `chars` is the text's length in characters (code
10
+ * points); a copy that timed out before the text was known has none. */
11
+ export type Copy = {
12
+ ok: true;
13
+ source: CopySource;
14
+ chars: number;
15
+ } | {
16
+ ok: false;
17
+ source: CopySource;
18
+ reason: CopyFailure;
19
+ chars?: number;
20
+ message?: string;
21
+ };
22
+ /** The parts of `navigator.clipboard` a copy uses. */
23
+ export interface ClipboardLike {
24
+ write?(items: ClipboardItem[]): Promise<void>;
25
+ writeText?(text: string): Promise<void>;
26
+ }
27
+ export interface CopyHost {
28
+ /** The clipboard, read at each copy; undefined where there is none. */
29
+ clipboard(): ClipboardLike | undefined;
30
+ /** A `ClipboardItem` of text to come; undefined where the browser has
31
+ * no `ClipboardItem`. */
32
+ item: ((text: Promise<Blob>) => ClipboardItem) | undefined;
33
+ report(copy: Copy): void;
34
+ }
35
+ /** How long a copy may take: the worker's answer and the write. */
36
+ export declare const COPY_TIMEOUT_MS = 2000;
37
+ /** A mounted terminal's copies. */
38
+ export declare class Copier {
39
+ private readonly host;
40
+ private readonly timeoutMs;
41
+ /** The releases' copies waiting for the worker's answer, by number. */
42
+ private readonly pending;
43
+ private next;
44
+ /** Every copy not yet reported or dropped, for `dispose`. */
45
+ private readonly live;
46
+ constructor(host: CopyHost, timeoutMs?: number);
47
+ /** Starts the copy of what a release selected, and returns its number
48
+ * for the worker's answer. Safari writes to the clipboard only within
49
+ * the user's gesture, which is over before the worker answers: the
50
+ * write starts here with the text to come, and fails, leaving the
51
+ * clipboard alone, when nothing was selected. Where the browser has no
52
+ * `ClipboardItem` the text is written once it arrives. An earlier
53
+ * release's copy still waiting is dropped, so the clipboard gets the
54
+ * latest. */
55
+ start(): number;
56
+ /** The worker's answer to release `id`: the selected text, or undefined
57
+ * when nothing was selected, which is no copy. An answer to a copy that
58
+ * was dropped or timed out is ignored. */
59
+ answer(id: number, text: string | undefined): void;
60
+ /** Copies `text` known now. */
61
+ write(text: string, source: CopySource): void;
62
+ /** A copy the browser made itself (the `copy` event's data). */
63
+ copied(text: string, source: CopySource): void;
64
+ /** Drops every copy under way, unreported. */
65
+ dispose(): void;
66
+ private attempt;
67
+ private dropOne;
68
+ private writeText;
69
+ }
@@ -0,0 +1,172 @@
1
+ /** The page's copies to the clipboard, and what each came to. */
2
+ /** How long a copy may take: the worker's answer and the write. */
3
+ export const COPY_TIMEOUT_MS = 2000;
4
+ /** Characters by code point, as a page counts them for display. */
5
+ const length = (text) => [...text].length;
6
+ /** One copy under way, reported once: when both its text's length and
7
+ * the write's result are known, or at the timeout. */
8
+ class Attempt {
9
+ source;
10
+ report;
11
+ ended;
12
+ done = false;
13
+ chars;
14
+ result;
15
+ timer;
16
+ constructor(source, report, timeoutMs,
17
+ /** Once, when it is reported or dropped. */
18
+ ended,
19
+ /** At the timeout, after the report. */
20
+ timedOut) {
21
+ this.source = source;
22
+ this.report = report;
23
+ this.ended = ended;
24
+ this.timer = setTimeout(() => {
25
+ this.finish({ ok: false, reason: "timeout" });
26
+ timedOut?.();
27
+ }, timeoutMs);
28
+ }
29
+ text(text) {
30
+ this.chars = length(text);
31
+ this.settle();
32
+ }
33
+ wrote(result) {
34
+ this.result ??= result;
35
+ this.settle();
36
+ }
37
+ /** Not a copy: nothing was selected, or a newer copy took its place. */
38
+ drop() {
39
+ if (this.done)
40
+ return;
41
+ this.done = true;
42
+ clearTimeout(this.timer);
43
+ this.ended();
44
+ }
45
+ settle() {
46
+ if (this.chars !== undefined && this.result)
47
+ this.finish(this.result);
48
+ }
49
+ finish(result) {
50
+ if (this.done)
51
+ return;
52
+ this.drop();
53
+ const chars = this.chars;
54
+ if (result.ok)
55
+ this.report({ ok: true, source: this.source, chars: chars });
56
+ else
57
+ this.report({ source: this.source, ...result, ...(chars !== undefined ? { chars } : {}) });
58
+ }
59
+ /** Follows a write's promise. */
60
+ follow(write) {
61
+ write.then(() => this.wrote({ ok: true }), (err) => this.wrote({ ok: false, reason: "refused", message: String(err) }));
62
+ }
63
+ }
64
+ /** A mounted terminal's copies. */
65
+ export class Copier {
66
+ host;
67
+ timeoutMs;
68
+ /** The releases' copies waiting for the worker's answer, by number. */
69
+ pending = new Map();
70
+ next = 1;
71
+ /** Every copy not yet reported or dropped, for `dispose`. */
72
+ live = new Set();
73
+ constructor(host, timeoutMs = COPY_TIMEOUT_MS) {
74
+ this.host = host;
75
+ this.timeoutMs = timeoutMs;
76
+ }
77
+ /** Starts the copy of what a release selected, and returns its number
78
+ * for the worker's answer. Safari writes to the clipboard only within
79
+ * the user's gesture, which is over before the worker answers: the
80
+ * write starts here with the text to come, and fails, leaving the
81
+ * clipboard alone, when nothing was selected. Where the browser has no
82
+ * `ClipboardItem` the text is written once it arrives. An earlier
83
+ * release's copy still waiting is dropped, so the clipboard gets the
84
+ * latest. */
85
+ start() {
86
+ for (const earlier of this.pending.values())
87
+ this.dropOne(earlier);
88
+ this.pending.clear();
89
+ const id = this.next++;
90
+ const pending = {
91
+ // At the timeout the write is left without its text.
92
+ attempt: this.attempt("release", () => {
93
+ this.pending.delete(id);
94
+ pending.text?.reject();
95
+ }),
96
+ };
97
+ this.pending.set(id, pending);
98
+ const clipboard = this.host.clipboard();
99
+ if (!this.host.item || !clipboard?.write)
100
+ return id;
101
+ const text = new Promise((resolve, reject) => {
102
+ pending.text = {
103
+ resolve: (value) => resolve(new Blob([value], { type: "text/plain" })),
104
+ reject: () => reject(new Error("nothing selected")),
105
+ };
106
+ });
107
+ // A browser that refuses the write never reads `text`, whose
108
+ // rejection would then go unhandled.
109
+ text.catch(() => { });
110
+ let item;
111
+ try {
112
+ item = this.host.item(text);
113
+ }
114
+ catch {
115
+ // One whose `ClipboardItem` takes no text to come: written once
116
+ // the text arrives.
117
+ delete pending.text;
118
+ return id;
119
+ }
120
+ pending.attempt.follow(clipboard.write([item]));
121
+ return id;
122
+ }
123
+ /** The worker's answer to release `id`: the selected text, or undefined
124
+ * when nothing was selected, which is no copy. An answer to a copy that
125
+ * was dropped or timed out is ignored. */
126
+ answer(id, text) {
127
+ const pending = this.pending.get(id);
128
+ if (!pending)
129
+ return;
130
+ this.pending.delete(id);
131
+ if (text === undefined)
132
+ return this.dropOne(pending);
133
+ pending.attempt.text(text);
134
+ if (pending.text)
135
+ pending.text.resolve(text);
136
+ else
137
+ this.writeText(pending.attempt, text);
138
+ }
139
+ /** Copies `text` known now. */
140
+ write(text, source) {
141
+ const attempt = this.attempt(source);
142
+ attempt.text(text);
143
+ this.writeText(attempt, text);
144
+ }
145
+ /** A copy the browser made itself (the `copy` event's data). */
146
+ copied(text, source) {
147
+ this.host.report({ ok: true, source, chars: length(text) });
148
+ }
149
+ /** Drops every copy under way, unreported. */
150
+ dispose() {
151
+ for (const pending of this.pending.values())
152
+ this.dropOne(pending);
153
+ this.pending.clear();
154
+ for (const attempt of this.live)
155
+ attempt.drop();
156
+ }
157
+ attempt(source, timedOut) {
158
+ const attempt = new Attempt(source, this.host.report, this.timeoutMs, () => this.live.delete(attempt), timedOut);
159
+ this.live.add(attempt);
160
+ return attempt;
161
+ }
162
+ dropOne(pending) {
163
+ pending.attempt.drop();
164
+ pending.text?.reject();
165
+ }
166
+ writeText(attempt, text) {
167
+ const clipboard = this.host.clipboard();
168
+ if (!clipboard?.writeText)
169
+ return attempt.wrote({ ok: false, reason: "unavailable" });
170
+ attempt.follow(clipboard.writeText(text));
171
+ }
172
+ }
@@ -4,6 +4,7 @@ export { Client, connect } from "./client.ts";
4
4
  export type { ConnectOptions, EventGap, SendInput, WaitOptions } from "./client.ts";
5
5
  export { ClientError, Connection, hello } from "./connection.ts";
6
6
  export type { ErrorKind, Transport } from "./connection.ts";
7
+ export type { Copy, CopyFailure, CopySource } from "./copy.ts";
7
8
  export { Osc52Scanner } from "./osc52.ts";
8
9
  export type { ClipboardWrite } from "./osc52.ts";
9
10
  export type { Colors, MountOptions, Terminal } from "./mount.ts";
@@ -1,4 +1,5 @@
1
1
  import type { Scheme } from "../proto/index.ts";
2
+ import { type Copy } from "./copy.ts";
2
3
  import type { Link, ToPage, ToWorker } from "./worker.ts";
3
4
  /** The terminal's default colours as CSS colours (hex or `rgb()`). */
4
5
  export interface Colors {
@@ -69,6 +70,10 @@ export interface Terminal {
69
70
  onScroll(listener: (rows: number) => void): () => void;
70
71
  /** A line of text for each change, for display; the latest at once. */
71
72
  onStatus(listener: (text: string) => void): () => void;
73
+ /** Each copy to the clipboard as it ends: copied, with the text's
74
+ * length, or failed and why. A release that selected nothing is no
75
+ * copy. A listener added later is not told of earlier copies. */
76
+ onCopy(listener: (copy: Copy) => void): () => void;
72
77
  }
73
78
  /** The client's side of a terminal: its messages to the worker, and the
74
79
  * worker's for it. */
@@ -1,4 +1,5 @@
1
1
  import { CanvasRenderer } from "../renderer/index.js";
2
+ import { Copier } from "./copy.js";
2
3
  import { Typing } from "./keyboard.js";
3
4
  import { profileFrom, schemeOf } from "./profile.js";
4
5
  /** Listeners of one kind, and the latest value they were told. */
@@ -89,43 +90,27 @@ export function mountOn(host, id, options) {
89
90
  const links = new Listeners();
90
91
  const scrolls = new Listeners();
91
92
  const statuses = new Listeners();
93
+ const copies = new Set();
92
94
  /** The selection's text, for a copy. */
93
95
  let selected;
94
- /** The clipboard write a release started, waiting for the worker to say
95
- * what was selected. */
96
- let copying;
97
- /** Starts the copy of what a release selected. Safari writes to the
98
- * clipboard only within the user's gesture, which is over before the
99
- * worker answers: the write starts here with the text to come, and
100
- * fails, leaving the clipboard alone, when nothing was selected. */
101
- function startCopy() {
102
- copying?.reject();
103
- copying = undefined;
104
- if (typeof ClipboardItem === "undefined" || !navigator.clipboard?.write)
105
- return;
106
- let settle;
107
- const text = new Promise((resolve, reject) => {
108
- settle = {
109
- resolve: (value) => resolve(new Blob([value], { type: "text/plain" })),
110
- reject: () => reject(new Error("nothing selected")),
111
- };
112
- });
113
- // A browser that refuses the write never reads `text`, whose
114
- // rejection would then go unhandled.
115
- text.catch(() => { });
116
- navigator.clipboard.write([new ClipboardItem({ "text/plain": text })]).catch(() => { });
117
- const timer = setTimeout(() => settle.reject(), 2000);
118
- copying = {
119
- resolve: (value) => {
120
- clearTimeout(timer);
121
- settle.resolve(value);
122
- },
123
- reject: () => {
124
- clearTimeout(timer);
125
- settle.reject();
126
- },
127
- };
128
- }
96
+ const copier = new Copier({
97
+ clipboard: () => navigator.clipboard,
98
+ item: typeof ClipboardItem === "undefined" ? undefined : (text) => new ClipboardItem({ "text/plain": text }),
99
+ // Told from a promise's callback: a listener's throw is reported as
100
+ // a listener's throw is elsewhere, and the rest are told.
101
+ report: (copy) => {
102
+ for (const listener of copies) {
103
+ try {
104
+ listener(copy);
105
+ }
106
+ catch (err) {
107
+ queueMicrotask(() => {
108
+ throw err;
109
+ });
110
+ }
111
+ }
112
+ },
113
+ });
129
114
  /** Rows the view is scrolled back, as `onScroll` last reported;
130
115
  * undefined before the first frame. */
131
116
  let scrolled;
@@ -157,22 +142,13 @@ export function mountOn(host, id, options) {
157
142
  break;
158
143
  }
159
144
  case "clipboard":
160
- navigator.clipboard?.writeText(message.text).catch(() => { });
145
+ copier.write(message.text, "program");
161
146
  break;
162
- case "selection": {
147
+ case "selection":
163
148
  selected = message.text;
164
- if (!message.copy)
165
- break;
166
- const copy = copying;
167
- copying = undefined;
168
- if (message.text === undefined)
169
- copy?.reject();
170
- else if (copy)
171
- copy.resolve(message.text);
172
- else
173
- navigator.clipboard?.writeText(message.text).catch(() => { });
149
+ if (message.copy !== undefined)
150
+ copier.answer(message.copy, message.text);
174
151
  break;
175
- }
176
152
  }
177
153
  });
178
154
  /** Where the input element goes for a composition: the cursor's cell,
@@ -255,7 +231,7 @@ export function mountOn(host, id, options) {
255
231
  // Ctrl+Shift+C copies a selection, where Ctrl+C is the terminal's;
256
232
  // without one it is a key.
257
233
  if (e.ctrlKey && e.shiftKey && !e.altKey && !e.metaKey && e.code === "KeyC" && selected) {
258
- navigator.clipboard?.writeText(selected).catch(() => { });
234
+ copier.write(selected, "keys");
259
235
  e.preventDefault();
260
236
  return;
261
237
  }
@@ -316,6 +292,7 @@ export function mountOn(host, id, options) {
316
292
  return;
317
293
  e.clipboardData.setData("text/plain", selected);
318
294
  e.preventDefault();
295
+ copier.copied(selected, "browser");
319
296
  }, { signal });
320
297
  /** The pointer in cells of the grid, fractions included, kept inside it. */
321
298
  function cellAt(e) {
@@ -352,8 +329,7 @@ export function mountOn(host, id, options) {
352
329
  window.removeEventListener("mousemove", move);
353
330
  window.removeEventListener("mouseup", up);
354
331
  endDrag = undefined;
355
- startCopy();
356
- send({ type: "select", action: "release", at, time });
332
+ send({ type: "select", action: "release", at, time, copy: copier.start() });
357
333
  };
358
334
  const move = (m) => {
359
335
  if ((m.buttons & 1) === 0)
@@ -462,9 +438,15 @@ export function mountOn(host, id, options) {
462
438
  links.clear();
463
439
  scrolls.clear();
464
440
  statuses.clear();
441
+ copier.dispose();
442
+ copies.clear();
465
443
  },
466
444
  onLink: (listener) => links.add(listener),
467
445
  onScroll: (listener) => scrolls.add(listener),
468
446
  onStatus: (listener) => statuses.add(listener),
447
+ onCopy: (listener) => {
448
+ copies.add(listener);
449
+ return () => void copies.delete(listener);
450
+ },
469
451
  };
470
452
  }
@@ -103,13 +103,21 @@ export type ToWorker = {
103
103
  text: string;
104
104
  }
105
105
  /** The mouse on the canvas, `at` in cells of the viewport, fractions
106
- * included; `time` in ms, for double and triple clicks. */
106
+ * included; `time` in ms, for double and triple clicks. A release has
107
+ * the page's number for its copy, which the answer carries. */
107
108
  | {
108
109
  type: "select";
109
110
  mount: number;
110
- action: "press" | "drag" | "release";
111
+ action: "press" | "drag";
111
112
  at: CellPoint;
112
113
  time: number;
114
+ } | {
115
+ type: "select";
116
+ mount: number;
117
+ action: "release";
118
+ at: CellPoint;
119
+ time: number;
120
+ copy: number;
113
121
  }
114
122
  /** Text as typed, which no key sent: a composition's, the emoji picker's. */
115
123
  | {
@@ -204,13 +212,14 @@ export type ToPage =
204
212
  mount: number;
205
213
  text: string;
206
214
  }
207
- /** The selection's text, or undefined once there is none; `copy`: the
208
- * answer to a release, so the text (if any) goes to the clipboard. */
215
+ /** The selection's text, or undefined once there is none; with `copy`,
216
+ * the answer to the release of that number, so the text (if any) goes
217
+ * to the clipboard. Every release is answered. */
209
218
  | {
210
219
  type: "selection";
211
220
  mount: number;
212
221
  text: string | undefined;
213
- copy: boolean;
222
+ copy?: number;
214
223
  };
215
224
  /** The worker's end of the page's messages: `self` in a dedicated worker. */
216
225
  export interface WorkerPort {
@@ -602,10 +602,14 @@ class Mount {
602
602
  }
603
603
  /** The mouse: a press clears the selection, or selects a word or a line
604
604
  * at once; a drag selects; the release copies what is selected. */
605
- select(action, at, time) {
605
+ select(action, at, time, copy) {
606
606
  const term = this.attachment?.term();
607
- if (!term)
607
+ if (!term) {
608
+ // Nothing to select: the page's copy waits for the answer even so.
609
+ if (copy !== undefined)
610
+ this.post({ type: "selection", mount: this.id, text: undefined, copy });
608
611
  return;
612
+ }
609
613
  switch (action) {
610
614
  case "press":
611
615
  this.claim();
@@ -625,7 +629,7 @@ class Mount {
625
629
  // (trimmed to nothing) are no text to copy.
626
630
  const text = term.selectionText() || undefined;
627
631
  this.selected = text !== undefined;
628
- this.post({ type: "selection", mount: this.id, text, copy: true });
632
+ this.post({ type: "selection", mount: this.id, text, ...(copy !== undefined ? { copy } : {}) });
629
633
  break;
630
634
  }
631
635
  }
@@ -643,7 +647,7 @@ class Mount {
643
647
  }
644
648
  unselected() {
645
649
  this.selected = false;
646
- this.post({ type: "selection", mount: this.id, text: undefined, copy: false });
650
+ this.post({ type: "selection", mount: this.id, text: undefined });
647
651
  }
648
652
  /** The terminal goes, and its selection and the gesture's anchor with it. */
649
653
  forgetSelection(term) {
@@ -933,7 +937,7 @@ export function runWorker(port) {
933
937
  mount.text(message.text);
934
938
  break;
935
939
  case "select":
936
- mount.select(message.action, message.at, message.time);
940
+ mount.select(message.action, message.at, message.time, message.action === "release" ? message.copy : undefined);
937
941
  break;
938
942
  case "wheel":
939
943
  mount.wheel(message.rows);
@@ -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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjermundgaraba/clankermux",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",