@gjermundgaraba/clankermux 0.3.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 +113 -31
- package/dist/client/attachment.d.ts +1 -0
- package/dist/client/attachment.js +1 -1
- package/dist/client/client.d.ts +123 -0
- package/dist/client/client.js +268 -0
- package/dist/client/connection.d.ts +17 -2
- package/dist/client/connection.js +21 -3
- package/dist/client/copy.d.ts +69 -0
- package/dist/client/copy.js +172 -0
- package/dist/client/index.d.ts +5 -3
- package/dist/client/index.js +1 -1
- package/dist/client/mount.d.ts +50 -42
- package/dist/client/mount.js +107 -104
- package/dist/client/worker.d.ts +129 -51
- package/dist/client/worker.js +462 -219
- package/dist/engine/keys.js +8 -2
- package/dist/proto/messages.d.ts +287 -5
- package/package.json +4 -3
package/dist/client/mount.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type
|
|
1
|
+
import type { Scheme } from "../proto/index.ts";
|
|
2
|
+
import { type Copy } from "./copy.ts";
|
|
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 {
|
|
5
6
|
fg: string;
|
|
@@ -7,18 +8,11 @@ export interface Colors {
|
|
|
7
8
|
cursor: string;
|
|
8
9
|
}
|
|
9
10
|
export interface MountOptions {
|
|
10
|
-
/** A module worker whose script calls `runWorker(self)`; the terminal
|
|
11
|
-
* owns it from here. */
|
|
12
|
-
worker: Worker;
|
|
13
11
|
/** Drawn on. Focusing it focuses the terminal's input element, a
|
|
14
12
|
* textarea placed after it, which takes keys, paste and composed text. */
|
|
15
13
|
canvas: HTMLCanvasElement;
|
|
16
14
|
/** The element whose size sets the grid. */
|
|
17
15
|
container: HTMLElement;
|
|
18
|
-
/** ghostty-vt.wasm, resolved against the page. */
|
|
19
|
-
wasm: string;
|
|
20
|
-
/** The server's WebSocket carrier, `wss://<node>:<port>/`. */
|
|
21
|
-
url: string;
|
|
22
16
|
/** The session to attach at once. */
|
|
23
17
|
session?: string | undefined;
|
|
24
18
|
/** A CSS font shorthand with its size in px. */
|
|
@@ -30,48 +24,62 @@ export interface MountOptions {
|
|
|
30
24
|
* of `light` and `dark`, else the page's preferred scheme. Read again
|
|
31
25
|
* on every report. */
|
|
32
26
|
scheme?: () => Scheme;
|
|
33
|
-
/**
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
/** The rows between the bottom of the view and the live screen's, when
|
|
39
|
-
* that changes: above 0 while the view is scrolled back into the
|
|
40
|
-
* history (and growing with output meanwhile), 0 when it shows the
|
|
41
|
-
* live screen again. The canvas draws a mark at its right edge too. */
|
|
42
|
-
onScroll?: (rows: number) => void;
|
|
27
|
+
/** Attach read-only: the session's screen, with no input to it. */
|
|
28
|
+
readOnly?: boolean;
|
|
29
|
+
/** Mark the session's `done` and `error` records seen (`ack`) while the
|
|
30
|
+
* terminal has the keyboard and its window the focus. */
|
|
31
|
+
ackOnFocus?: boolean;
|
|
43
32
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
export interface MountedTerminal {
|
|
51
|
-
/** Detaches the current session and attaches `session`. */
|
|
33
|
+
/** One mounted terminal: an attachment on its client's connection, drawn
|
|
34
|
+
* on a canvas. */
|
|
35
|
+
export interface Terminal {
|
|
36
|
+
/** Detaches the current session and attaches `session`; after the
|
|
37
|
+
* client's connection ended for good, connects again first. */
|
|
52
38
|
attach(session: string): void;
|
|
53
|
-
/**
|
|
54
|
-
|
|
55
|
-
* call it after a `connected` link. */
|
|
56
|
-
create(options?: CreateOptions): Promise<Session>;
|
|
57
|
-
/** Hangs up the attached session's processes (SIGHUP); its exit
|
|
58
|
-
* follows as a link `ended` with reason `exited`. Resolves too when the
|
|
59
|
-
* session is not running (`exited` or `restored`). */
|
|
60
|
-
end(): Promise<void>;
|
|
61
|
-
/** Removes the attached session, which must be `exited` or `restored`
|
|
62
|
-
* (else `running`); the link ends with `removed`. */
|
|
63
|
-
remove(): Promise<void>;
|
|
39
|
+
/** Detaches the session; the terminal stays, attached to none. */
|
|
40
|
+
detach(): void;
|
|
64
41
|
/** Focuses the terminal's input element, as focusing the canvas does. */
|
|
65
42
|
focus(): void;
|
|
66
43
|
/** The selected text, as the last selection made copied it; undefined
|
|
67
44
|
* when nothing is selected. */
|
|
68
45
|
selection(): string | undefined;
|
|
46
|
+
/** Pastes, bracketed when the program asked for it. */
|
|
47
|
+
paste(text: string): void;
|
|
48
|
+
/** Scrolls the view through the history by `rows`, up when negative. */
|
|
49
|
+
scroll(rows: number): void;
|
|
50
|
+
/** Back to the live screen. */
|
|
51
|
+
scrollToLive(): void;
|
|
69
52
|
/** Reports the container's size, the colours and the scheme again;
|
|
70
53
|
* to the session only while the page is visible and focused. */
|
|
71
54
|
report(): void;
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
55
|
+
/** The grid the container holds now, for a `create` at this size. */
|
|
56
|
+
size(): {
|
|
57
|
+
cols: number;
|
|
58
|
+
rows: number;
|
|
59
|
+
};
|
|
60
|
+
/** Detaches, removes the listeners and the input element. */
|
|
75
61
|
dispose(): void;
|
|
62
|
+
/** Each change of the link to the session, to switch on; a listener
|
|
63
|
+
* added later gets the latest at once. */
|
|
64
|
+
onLink(listener: (link: Link) => void): () => void;
|
|
65
|
+
/** The rows between the bottom of the view and the live screen's: on
|
|
66
|
+
* the first frame, and at each change after. Above 0 while the view is
|
|
67
|
+
* scrolled back into the history (and growing with output meanwhile),
|
|
68
|
+
* 0 when it shows the live screen. The canvas draws a mark at its
|
|
69
|
+
* right edge too. A listener added later gets the latest at once. */
|
|
70
|
+
onScroll(listener: (rows: number) => void): () => void;
|
|
71
|
+
/** A line of text for each change, for display; the latest at once. */
|
|
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;
|
|
77
|
+
}
|
|
78
|
+
/** The client's side of a terminal: its messages to the worker, and the
|
|
79
|
+
* worker's for it. */
|
|
80
|
+
export interface MountHost {
|
|
81
|
+
send(message: ToWorker): void;
|
|
82
|
+
receive(handler: ((message: ToPage) => void) | undefined): void;
|
|
76
83
|
}
|
|
77
|
-
|
|
84
|
+
/** Mounts a terminal as `id` of the client `host` serves. */
|
|
85
|
+
export declare function mountOn(host: MountHost, id: number, options: MountOptions): Terminal;
|
package/dist/client/mount.js
CHANGED
|
@@ -1,7 +1,26 @@
|
|
|
1
1
|
import { CanvasRenderer } from "../renderer/index.js";
|
|
2
|
-
import {
|
|
2
|
+
import { Copier } from "./copy.js";
|
|
3
3
|
import { Typing } from "./keyboard.js";
|
|
4
4
|
import { profileFrom, schemeOf } from "./profile.js";
|
|
5
|
+
/** Listeners of one kind, and the latest value they were told. */
|
|
6
|
+
class Listeners {
|
|
7
|
+
all = new Set();
|
|
8
|
+
latest;
|
|
9
|
+
add(listener) {
|
|
10
|
+
this.all.add(listener);
|
|
11
|
+
if (this.latest)
|
|
12
|
+
listener(this.latest.value);
|
|
13
|
+
return () => void this.all.delete(listener);
|
|
14
|
+
}
|
|
15
|
+
emit(value) {
|
|
16
|
+
this.latest = { value };
|
|
17
|
+
for (const listener of this.all)
|
|
18
|
+
listener(value);
|
|
19
|
+
}
|
|
20
|
+
clear() {
|
|
21
|
+
this.all.clear();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
5
24
|
/** Cell metrics from the font. */
|
|
6
25
|
function measure(font) {
|
|
7
26
|
const ctx = document.createElement("canvas").getContext("2d");
|
|
@@ -20,8 +39,9 @@ function measure(font) {
|
|
|
20
39
|
boldItalicFont: `bold italic ${font}`,
|
|
21
40
|
};
|
|
22
41
|
}
|
|
23
|
-
|
|
24
|
-
|
|
42
|
+
/** Mounts a terminal as `id` of the client `host` serves. */
|
|
43
|
+
export function mountOn(host, id, options) {
|
|
44
|
+
const { canvas, container, colors } = options;
|
|
25
45
|
const light = matchMedia("(prefers-color-scheme: light)");
|
|
26
46
|
const scheme = options.scheme ?? (() => schemeOf(getComputedStyle(canvas).colorScheme, light.matches));
|
|
27
47
|
const metrics = measure(options.font);
|
|
@@ -62,84 +82,53 @@ export function mountTerminal(options) {
|
|
|
62
82
|
?.getLayoutMap()
|
|
63
83
|
.then((map) => (keyboard.layout = map))
|
|
64
84
|
.catch(() => { });
|
|
65
|
-
const send = (message) => worker.postMessage(message);
|
|
66
|
-
/** Requests waiting for their `done`, by id. They reject with a
|
|
67
|
-
* `ClientError` for a server's refusal, a lost connection or a
|
|
68
|
-
* disposed terminal, or an `Error` when no session is attached. */
|
|
69
|
-
const pending = new Map();
|
|
70
|
-
let nextId = 1;
|
|
71
85
|
let disposed = false;
|
|
72
|
-
|
|
73
|
-
if (disposed)
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
const failure = (error) => error.kind ? new ClientError(error.kind, error.message, error.server) : new Error(error.message);
|
|
86
|
+
const send = (message) => {
|
|
87
|
+
if (!disposed)
|
|
88
|
+
host.send({ ...message, mount: id });
|
|
89
|
+
};
|
|
90
|
+
const links = new Listeners();
|
|
91
|
+
const scrolls = new Listeners();
|
|
92
|
+
const statuses = new Listeners();
|
|
93
|
+
const copies = new Set();
|
|
82
94
|
/** The selection's text, for a copy. */
|
|
83
95
|
let selected;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
resolve: (value) => {
|
|
107
|
-
clearTimeout(timer);
|
|
108
|
-
settle.resolve(value);
|
|
109
|
-
},
|
|
110
|
-
reject: () => {
|
|
111
|
-
clearTimeout(timer);
|
|
112
|
-
settle.reject();
|
|
113
|
-
},
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
/** Rows the view is scrolled back, as `onScroll` last reported. */
|
|
117
|
-
let scrolled = 0;
|
|
118
|
-
worker.onmessage = (e) => {
|
|
119
|
-
const message = e.data;
|
|
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
|
+
});
|
|
114
|
+
/** Rows the view is scrolled back, as `onScroll` last reported;
|
|
115
|
+
* undefined before the first frame. */
|
|
116
|
+
let scrolled;
|
|
117
|
+
host.receive((message) => {
|
|
120
118
|
switch (message.type) {
|
|
121
119
|
case "status":
|
|
122
|
-
|
|
120
|
+
statuses.emit(message.text);
|
|
123
121
|
break;
|
|
124
122
|
case "link":
|
|
125
|
-
|
|
123
|
+
links.emit(message.link);
|
|
126
124
|
break;
|
|
127
|
-
case "done": {
|
|
128
|
-
const waiter = pending.get(message.id);
|
|
129
|
-
pending.delete(message.id);
|
|
130
|
-
if (message.error)
|
|
131
|
-
waiter?.reject(failure(message.error));
|
|
132
|
-
else
|
|
133
|
-
waiter?.resolve(message.session);
|
|
134
|
-
break;
|
|
135
|
-
}
|
|
136
125
|
case "frame": {
|
|
137
126
|
if (message.frame.cursor)
|
|
138
127
|
cursorAt = { x: message.frame.cursor.x, y: message.frame.cursor.y };
|
|
139
128
|
const { total, offset, len } = message.frame.scrollbar;
|
|
140
129
|
if (total - offset - len !== scrolled) {
|
|
141
130
|
scrolled = total - offset - len;
|
|
142
|
-
|
|
131
|
+
scrolls.emit(scrolled);
|
|
143
132
|
}
|
|
144
133
|
fitCanvas(message.frame.cols, message.frame.rows);
|
|
145
134
|
if (fullNext && !message.frame.full) {
|
|
@@ -152,28 +141,16 @@ export function mountTerminal(options) {
|
|
|
152
141
|
}
|
|
153
142
|
break;
|
|
154
143
|
}
|
|
155
|
-
case "sessions":
|
|
156
|
-
options.onSessions?.(message.sessions, message.current);
|
|
157
|
-
break;
|
|
158
144
|
case "clipboard":
|
|
159
|
-
|
|
145
|
+
copier.write(message.text, "program");
|
|
160
146
|
break;
|
|
161
|
-
case "selection":
|
|
147
|
+
case "selection":
|
|
162
148
|
selected = message.text;
|
|
163
|
-
if (
|
|
164
|
-
|
|
165
|
-
const copy = copying;
|
|
166
|
-
copying = undefined;
|
|
167
|
-
if (message.text === undefined)
|
|
168
|
-
copy?.reject();
|
|
169
|
-
else if (copy)
|
|
170
|
-
copy.resolve(message.text);
|
|
171
|
-
else
|
|
172
|
-
navigator.clipboard?.writeText(message.text).catch(() => { });
|
|
149
|
+
if (message.copy !== undefined)
|
|
150
|
+
copier.answer(message.copy, message.text);
|
|
173
151
|
break;
|
|
174
|
-
}
|
|
175
152
|
}
|
|
176
|
-
};
|
|
153
|
+
});
|
|
177
154
|
/** Where the input element goes for a composition: the cursor's cell,
|
|
178
155
|
* as the latest frame that had the cursor in view placed it. */
|
|
179
156
|
let cursorAt = { x: 0, y: 0 };
|
|
@@ -234,13 +211,27 @@ export function mountTerminal(options) {
|
|
|
234
211
|
});
|
|
235
212
|
}
|
|
236
213
|
canvas.addEventListener("focus", () => input.focus({ preventScroll: true }), { signal });
|
|
214
|
+
// Whether the terminal has the keyboard and its window the focus, for
|
|
215
|
+
// `ackOnFocus`.
|
|
216
|
+
let focused = false;
|
|
217
|
+
const watchFocus = () => {
|
|
218
|
+
const now = document.activeElement === input && document.hasFocus();
|
|
219
|
+
if (now === focused)
|
|
220
|
+
return;
|
|
221
|
+
focused = now;
|
|
222
|
+
send({ type: "focus", focused });
|
|
223
|
+
};
|
|
224
|
+
input.addEventListener("focus", watchFocus, { signal });
|
|
225
|
+
input.addEventListener("blur", watchFocus, { signal });
|
|
226
|
+
window.addEventListener("focus", watchFocus, { signal });
|
|
227
|
+
window.addEventListener("blur", watchFocus, { signal });
|
|
237
228
|
const typing = new Typing(keyboard);
|
|
238
229
|
const typed = (what) => send("text" in what ? { type: "text", text: what.text } : { type: "key", input: what.key });
|
|
239
230
|
input.addEventListener("keydown", (e) => {
|
|
240
231
|
// Ctrl+Shift+C copies a selection, where Ctrl+C is the terminal's;
|
|
241
232
|
// without one it is a key.
|
|
242
233
|
if (e.ctrlKey && e.shiftKey && !e.altKey && !e.metaKey && e.code === "KeyC" && selected) {
|
|
243
|
-
|
|
234
|
+
copier.write(selected, "keys");
|
|
244
235
|
e.preventDefault();
|
|
245
236
|
return;
|
|
246
237
|
}
|
|
@@ -301,6 +292,7 @@ export function mountTerminal(options) {
|
|
|
301
292
|
return;
|
|
302
293
|
e.clipboardData.setData("text/plain", selected);
|
|
303
294
|
e.preventDefault();
|
|
295
|
+
copier.copied(selected, "browser");
|
|
304
296
|
}, { signal });
|
|
305
297
|
/** The pointer in cells of the grid, fractions included, kept inside it. */
|
|
306
298
|
function cellAt(e) {
|
|
@@ -337,8 +329,7 @@ export function mountTerminal(options) {
|
|
|
337
329
|
window.removeEventListener("mousemove", move);
|
|
338
330
|
window.removeEventListener("mouseup", up);
|
|
339
331
|
endDrag = undefined;
|
|
340
|
-
|
|
341
|
-
send({ type: "select", action: "release", at, time });
|
|
332
|
+
send({ type: "select", action: "release", at, time, copy: copier.start() });
|
|
342
333
|
};
|
|
343
334
|
const move = (m) => {
|
|
344
335
|
if ((m.buttons & 1) === 0)
|
|
@@ -371,7 +362,7 @@ export function mountTerminal(options) {
|
|
|
371
362
|
if (whole === 0)
|
|
372
363
|
return;
|
|
373
364
|
wheeled -= whole;
|
|
374
|
-
send({ type: "
|
|
365
|
+
send({ type: "wheel", rows: whole });
|
|
375
366
|
}, { passive: false, signal });
|
|
376
367
|
/** The profile last reported, to tell when it changed. */
|
|
377
368
|
let reported = "";
|
|
@@ -413,37 +404,49 @@ export function mountTerminal(options) {
|
|
|
413
404
|
};
|
|
414
405
|
watchRatio();
|
|
415
406
|
send({
|
|
416
|
-
type: "
|
|
417
|
-
|
|
418
|
-
url: options.url,
|
|
407
|
+
type: "mount",
|
|
408
|
+
session: options.session,
|
|
419
409
|
viewport: viewport(),
|
|
420
410
|
profile: profile(),
|
|
421
411
|
active: active(),
|
|
422
|
-
|
|
412
|
+
readOnly: options.readOnly ?? false,
|
|
413
|
+
ackOnFocus: options.ackOnFocus ?? false,
|
|
423
414
|
});
|
|
424
415
|
return {
|
|
425
416
|
attach: (session) => send({ type: "attach", session }),
|
|
426
|
-
|
|
427
|
-
const session = await request((id) => ({ type: "create", id, ...(create?.onExit && { onExit: create.onExit }) }));
|
|
428
|
-
return session;
|
|
429
|
-
},
|
|
430
|
-
end: async () => void (await request((id) => ({ type: "end", id }))),
|
|
431
|
-
remove: async () => void (await request((id) => ({ type: "remove", id }))),
|
|
417
|
+
detach: () => send({ type: "detach" }),
|
|
432
418
|
focus: () => input.focus({ preventScroll: true }),
|
|
433
419
|
selection: () => selected,
|
|
434
|
-
report,
|
|
435
420
|
paste: (text) => send({ type: "paste", text }),
|
|
421
|
+
scroll: (rows) => send({ type: "view", rows }),
|
|
422
|
+
scrollToLive: () => send({ type: "view", rows: "live" }),
|
|
423
|
+
report,
|
|
424
|
+
size: () => {
|
|
425
|
+
const { cols, rows } = viewport();
|
|
426
|
+
return { cols, rows };
|
|
427
|
+
},
|
|
436
428
|
dispose: () => {
|
|
429
|
+
if (disposed)
|
|
430
|
+
return;
|
|
431
|
+
host.send({ type: "unmount", mount: id });
|
|
437
432
|
disposed = true;
|
|
433
|
+
host.receive(undefined);
|
|
438
434
|
listening.abort();
|
|
439
435
|
resizes.disconnect();
|
|
440
436
|
restyles.disconnect();
|
|
441
437
|
input.remove();
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
438
|
+
links.clear();
|
|
439
|
+
scrolls.clear();
|
|
440
|
+
statuses.clear();
|
|
441
|
+
copier.dispose();
|
|
442
|
+
copies.clear();
|
|
443
|
+
},
|
|
444
|
+
onLink: (listener) => links.add(listener),
|
|
445
|
+
onScroll: (listener) => scrolls.add(listener),
|
|
446
|
+
onStatus: (listener) => statuses.add(listener),
|
|
447
|
+
onCopy: (listener) => {
|
|
448
|
+
copies.add(listener);
|
|
449
|
+
return () => void copies.delete(listener);
|
|
447
450
|
},
|
|
448
451
|
};
|
|
449
452
|
}
|