@gjermundgaraba/clankermux 0.1.0 → 0.2.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.
- package/README.md +161 -0
- package/dist/client/index.d.ts +3 -2
- package/dist/client/keyboard.d.ts +41 -0
- package/dist/client/keyboard.js +60 -0
- package/dist/client/mount.d.ts +41 -10
- package/dist/client/mount.js +326 -16
- package/dist/client/profile.d.ts +4 -0
- package/dist/client/profile.js +11 -0
- package/dist/client/worker.d.ts +99 -5
- package/dist/client/worker.js +298 -24
- package/dist/engine/index.d.ts +34 -0
- package/dist/engine/index.js +102 -0
- package/dist/engine/render.d.ts +8 -1
- package/dist/engine/render.js +24 -3
- package/dist/engine/selection.d.ts +39 -0
- package/dist/engine/selection.js +114 -0
- package/dist/renderer/index.d.ts +8 -2
- package/dist/renderer/index.js +57 -16
- package/package.json +1 -1
package/dist/engine/index.js
CHANGED
|
@@ -2,6 +2,7 @@ export { Decoder, readyEnd } from "./decoder.js";
|
|
|
2
2
|
export { KeyEncoder, isText, keyName } from "./keys.js";
|
|
3
3
|
export { FLAG, NO_COLOR, RenderState } from "./render.js";
|
|
4
4
|
export { ENGINE } from "./pin.js";
|
|
5
|
+
export { SelectionGesture } from "./selection.js";
|
|
5
6
|
export class EngineError extends Error {
|
|
6
7
|
call;
|
|
7
8
|
code;
|
|
@@ -317,6 +318,35 @@ export class Terminal {
|
|
|
317
318
|
get scrollbackRows() {
|
|
318
319
|
return this.get("SCROLLBACK_ROWS", 4);
|
|
319
320
|
}
|
|
321
|
+
/** Whether the alternate screen (full-screen programs) is active; it
|
|
322
|
+
* has no history to scroll. */
|
|
323
|
+
get alternate() {
|
|
324
|
+
return this.get("ACTIVE_SCREEN", 4) === this.engine.enumValue("GhosttyTerminalScreen", "ALTERNATE");
|
|
325
|
+
}
|
|
326
|
+
get scrollbar() {
|
|
327
|
+
const e = this.engine;
|
|
328
|
+
return e.scratch(e.sizeOf("GhosttyTerminalScrollbar"), (out) => {
|
|
329
|
+
e.check("ghostty_terminal_get", this.handle, e.enumValue("GhosttyTerminalData", "SCROLLBAR"), out);
|
|
330
|
+
const u64 = (key) => Number(e.view.getBigUint64(out + e.offsetOf("GhosttyTerminalScrollbar", key), true));
|
|
331
|
+
return { total: u64("total"), offset: u64("offset"), len: u64("len") };
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
/** Moves the viewport `rows` rows (up when negative) through the
|
|
335
|
+
* history, or to its top or the live screen at its bottom. Output does
|
|
336
|
+
* not move a viewport scrolled into the history. */
|
|
337
|
+
scroll(to) {
|
|
338
|
+
const e = this.engine;
|
|
339
|
+
const size = e.sizeOf("GhosttyTerminalScrollViewport");
|
|
340
|
+
e.scratch(size, (ptr) => {
|
|
341
|
+
e.memory.fill(0, ptr, ptr + size);
|
|
342
|
+
const tag = typeof to === "number" ? "DELTA" : to.toUpperCase();
|
|
343
|
+
e.view.setInt32(ptr + e.offsetOf("GhosttyTerminalScrollViewport", "tag"), e.enumValue("GhosttyTerminalScrollViewportTag", tag), true);
|
|
344
|
+
if (typeof to === "number")
|
|
345
|
+
e.view.setInt32(ptr + e.offsetOf("GhosttyTerminalScrollViewport", "value"), to, true);
|
|
346
|
+
// The tagged union is passed by value, which the wasm ABI passes by address.
|
|
347
|
+
e.call("ghostty_terminal_scroll_viewport", this.handle, ptr);
|
|
348
|
+
});
|
|
349
|
+
}
|
|
320
350
|
get title() {
|
|
321
351
|
const size = this.engine.sizeOf("GhosttyString");
|
|
322
352
|
return this.engine.scratch(size, (out) => {
|
|
@@ -328,6 +358,78 @@ export class Terminal {
|
|
|
328
358
|
return this.engine.readString(out);
|
|
329
359
|
});
|
|
330
360
|
}
|
|
361
|
+
/** An untracked `GhosttyGridRef` to the viewport cell at `at`, valid
|
|
362
|
+
* until the terminal changes. */
|
|
363
|
+
gridRef(at) {
|
|
364
|
+
const e = this.engine;
|
|
365
|
+
const point = e.sizeOf("GhosttyPoint");
|
|
366
|
+
const ref = e.sizeOf("GhosttyGridRef");
|
|
367
|
+
const x = Math.min(Math.max(0, Math.floor(at.x)), this.cols - 1);
|
|
368
|
+
const y = Math.min(Math.max(0, Math.floor(at.y)), this.rows - 1);
|
|
369
|
+
return e.scratch(point + ref, (ptr) => {
|
|
370
|
+
e.memory.fill(0, ptr, ptr + point + ref);
|
|
371
|
+
e.view.setInt32(ptr + e.offsetOf("GhosttyPoint", "tag"), e.enumValue("GhosttyPointTag", "VIEWPORT"), true);
|
|
372
|
+
const coordinate = ptr + e.offsetOf("GhosttyPoint", "value");
|
|
373
|
+
e.view.setUint16(coordinate + e.offsetOf("GhosttyPointCoordinate", "x"), x, true);
|
|
374
|
+
e.view.setUint32(coordinate + e.offsetOf("GhosttyPointCoordinate", "y"), y, true);
|
|
375
|
+
e.view.setUint32(ptr + point, ref, true);
|
|
376
|
+
// The point is passed by value, which the wasm ABI passes by address.
|
|
377
|
+
e.check("ghostty_terminal_grid_ref", this.handle, ptr, ptr + point);
|
|
378
|
+
return e.memory.slice(ptr + point, ptr + point + ref);
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
/** Installs `selection` (a `GhosttySelection`) as the terminal's, which
|
|
382
|
+
* the terminal then tracks through output, scrolling and resizes, or
|
|
383
|
+
* clears it. */
|
|
384
|
+
select(selection) {
|
|
385
|
+
const option = this.engine.enumValue("GhosttyTerminalOption", "SELECTION");
|
|
386
|
+
if (!selection)
|
|
387
|
+
this.engine.check("ghostty_terminal_set", this.handle, option, 0);
|
|
388
|
+
else
|
|
389
|
+
this.engine.withBytes(selection, (ptr) => this.engine.check("ghostty_terminal_set", this.handle, option, ptr));
|
|
390
|
+
}
|
|
391
|
+
/** The selection's text: soft-wrapped lines joined, trailing blanks
|
|
392
|
+
* trimmed, as Ghostty copies it; undefined with no selection. */
|
|
393
|
+
selectionText() {
|
|
394
|
+
const e = this.engine;
|
|
395
|
+
const options = e.pack("GhosttyTerminalSelectionFormatOptions", {
|
|
396
|
+
emit: e.enumValue("GhosttyFormatterFormat", "PLAIN"),
|
|
397
|
+
unwrap: 1,
|
|
398
|
+
trim: 1,
|
|
399
|
+
});
|
|
400
|
+
return e.withBytes(options, (optionsPtr) => e.scratch(8, (out) => {
|
|
401
|
+
e.view.setUint32(out, 0, true);
|
|
402
|
+
e.view.setUint32(out + 4, 0, true);
|
|
403
|
+
// 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)
|
|
406
|
+
return undefined;
|
|
407
|
+
if (code !== e.result.SUCCESS)
|
|
408
|
+
throw new EngineError("ghostty_terminal_selection_format_alloc", code);
|
|
409
|
+
const ptr = e.view.getUint32(out, true);
|
|
410
|
+
const len = e.view.getUint32(out + 4, true);
|
|
411
|
+
try {
|
|
412
|
+
return new TextDecoder().decode(e.memory.slice(ptr, ptr + len));
|
|
413
|
+
}
|
|
414
|
+
finally {
|
|
415
|
+
e.call("ghostty_free", 0, ptr, len);
|
|
416
|
+
}
|
|
417
|
+
}));
|
|
418
|
+
}
|
|
419
|
+
/** Whether the active screen has a selection. */
|
|
420
|
+
get selected() {
|
|
421
|
+
const e = this.engine;
|
|
422
|
+
const size = e.sizeOf("GhosttySelection");
|
|
423
|
+
return e.scratch(size, (out) => {
|
|
424
|
+
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;
|
|
431
|
+
});
|
|
432
|
+
}
|
|
331
433
|
/** Whether DEC private mode `number` is set. */
|
|
332
434
|
mode(number) {
|
|
333
435
|
const config = this.engine.pack("GhosttyTerminalModeConfig", { mode: number });
|
package/dist/engine/render.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Engine, type Terminal } from "./index.ts";
|
|
1
|
+
import { type Engine, type Scrollbar, type Terminal } from "./index.ts";
|
|
2
2
|
/** Style flag bits of a [`Cell`]. */
|
|
3
3
|
export declare const FLAG: {
|
|
4
4
|
readonly BOLD: 1;
|
|
@@ -32,6 +32,8 @@ type CursorStyle = (typeof CURSOR_STYLES)[number];
|
|
|
32
32
|
export interface FrameRow {
|
|
33
33
|
y: number;
|
|
34
34
|
cells: Cell[];
|
|
35
|
+
/** The selected cells of the row, first and last inclusive. */
|
|
36
|
+
selection?: [number, number];
|
|
35
37
|
}
|
|
36
38
|
export interface Frame {
|
|
37
39
|
cols: number;
|
|
@@ -50,6 +52,9 @@ export interface Frame {
|
|
|
50
52
|
} | undefined;
|
|
51
53
|
/** The rows that changed, or every row when `full`. */
|
|
52
54
|
lines: FrameRow[];
|
|
55
|
+
/** Where the viewport is in the history: scrolled back when it ends
|
|
56
|
+
* above the last row (`offset + len < total`). */
|
|
57
|
+
scrollbar: Scrollbar;
|
|
53
58
|
}
|
|
54
59
|
/**
|
|
55
60
|
* The engine's render state: updated from a terminal, read into a
|
|
@@ -69,6 +74,8 @@ export declare class RenderState {
|
|
|
69
74
|
constructor(engine: Engine);
|
|
70
75
|
/** Captures the terminal and returns the rows that changed since the last `clean`. */
|
|
71
76
|
update(term: Terminal): Frame;
|
|
77
|
+
/** The row's selected cells, or undefined when the selection misses it. */
|
|
78
|
+
private rowSelection;
|
|
72
79
|
private readRow;
|
|
73
80
|
private color;
|
|
74
81
|
private graphemes;
|
package/dist/engine/render.js
CHANGED
|
@@ -40,7 +40,7 @@ export class RenderState {
|
|
|
40
40
|
const values = (name, members) => Object.fromEntries(members.map((m) => [m, engine.enumValue(name, m)]));
|
|
41
41
|
this.keys = {
|
|
42
42
|
data: values("GhosttyRenderStateData", ["COLS", "ROWS", "DIRTY", "ROW_ITERATOR", "COLORS", "CURSOR"]),
|
|
43
|
-
row: values("GhosttyRenderStateRowData", ["CELLS"]),
|
|
43
|
+
row: values("GhosttyRenderStateRowData", ["CELLS", "SELECTION"]),
|
|
44
44
|
cell: values("GhosttyRenderStateRowCellsData", [
|
|
45
45
|
"RAW",
|
|
46
46
|
"STYLE",
|
|
@@ -69,6 +69,8 @@ export class RenderState {
|
|
|
69
69
|
update(term) {
|
|
70
70
|
const e = this.engine;
|
|
71
71
|
e.check("ghostty_render_state_update", this.state, term.handle);
|
|
72
|
+
// Read with the update: the render state does not keep the scroll position.
|
|
73
|
+
const scrollbar = term.scrollbar;
|
|
72
74
|
const u16 = (key) => e.scratch(4, (out) => {
|
|
73
75
|
e.check("ghostty_render_state_get", this.state, key, out);
|
|
74
76
|
return e.view.getUint16(out, true);
|
|
@@ -110,10 +112,29 @@ export class RenderState {
|
|
|
110
112
|
e.check("ghostty_render_state_get", this.state, this.keys.data["ROW_ITERATOR"], out);
|
|
111
113
|
while (e.call("ghostty_render_state_row_iterator_next_dirty", this.iterator, out + 4)) {
|
|
112
114
|
const y = e.view.getUint16(out + 4, true);
|
|
113
|
-
|
|
115
|
+
const row = { y, cells: this.readRow(cols) };
|
|
116
|
+
const selection = this.rowSelection();
|
|
117
|
+
if (selection)
|
|
118
|
+
row.selection = selection;
|
|
119
|
+
lines.push(row);
|
|
114
120
|
}
|
|
115
121
|
});
|
|
116
|
-
return { cols, rows, full, background, foreground, cursor, lines };
|
|
122
|
+
return { cols, rows, full, background, foreground, cursor, lines, scrollbar };
|
|
123
|
+
}
|
|
124
|
+
/** The row's selected cells, or undefined when the selection misses it. */
|
|
125
|
+
rowSelection() {
|
|
126
|
+
const e = this.engine;
|
|
127
|
+
const size = e.sizeOf("GhosttyRenderStateRowSelection");
|
|
128
|
+
return e.scratch(size, (out) => {
|
|
129
|
+
e.view.setUint32(out, size, true);
|
|
130
|
+
const code = e.call("ghostty_render_state_row_get", this.iterator, this.keys.row["SELECTION"], out);
|
|
131
|
+
if (code === e.result.NO_VALUE)
|
|
132
|
+
return undefined;
|
|
133
|
+
if (code !== e.result.SUCCESS)
|
|
134
|
+
throw new Error(`row selection: ${code}`);
|
|
135
|
+
const field = (key) => e.field("GhosttyRenderStateRowSelection", out, key);
|
|
136
|
+
return [field("start_x"), field("end_x")];
|
|
137
|
+
});
|
|
117
138
|
}
|
|
118
139
|
readRow(cols) {
|
|
119
140
|
const e = this.engine;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Engine, Terminal } from "./index.ts";
|
|
2
|
+
/** A point in the viewport in cells, fractions included: the pointer's
|
|
3
|
+
* place within its cell decides which cells a drag covers. */
|
|
4
|
+
export interface CellPoint {
|
|
5
|
+
x: number;
|
|
6
|
+
y: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Mouse selection through the engine's gesture: a press, then drags, then
|
|
10
|
+
* a release. A single press selects nothing; a drag selects from it by
|
|
11
|
+
* cells, a double press a word and a triple press a line, dragging by
|
|
12
|
+
* words or lines. The gesture tracks its anchor in the terminal, so
|
|
13
|
+
* output between press and drag does not move it.
|
|
14
|
+
*/
|
|
15
|
+
export declare class SelectionGesture {
|
|
16
|
+
readonly engine: Engine;
|
|
17
|
+
private gesture;
|
|
18
|
+
private readonly events;
|
|
19
|
+
/** The terminal the gesture tracks its anchor in. */
|
|
20
|
+
private term;
|
|
21
|
+
constructor(engine: Engine);
|
|
22
|
+
/** Presses at `at` (`time` in ms); the selection to install, or
|
|
23
|
+
* undefined for none (a single press). */
|
|
24
|
+
press(term: Terminal, at: CellPoint, time: number): Uint8Array | undefined;
|
|
25
|
+
/** Drags to `at`; the selection from the press, or undefined when the
|
|
26
|
+
* pointer has not left the pressed cell. */
|
|
27
|
+
drag(term: Terminal, at: CellPoint): Uint8Array | undefined;
|
|
28
|
+
release(term: Terminal, at: CellPoint): void;
|
|
29
|
+
/** Starts afresh, for a terminal freed or about to be: `term` when it
|
|
30
|
+
* is still alive, to release the tracked anchor in it, else none. The
|
|
31
|
+
* caller says so, since a new terminal may have the handle of a freed
|
|
32
|
+
* one. */
|
|
33
|
+
forget(term?: Terminal): void;
|
|
34
|
+
/** A gesture is in one terminal. */
|
|
35
|
+
private follow;
|
|
36
|
+
private set;
|
|
37
|
+
private apply;
|
|
38
|
+
free(): void;
|
|
39
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/** Pixels per cell in the gesture's geometry: positions are in cells,
|
|
2
|
+
* scaled to whole numbers. */
|
|
3
|
+
const SCALE = 1000;
|
|
4
|
+
/** Clicks closer together than this, and within a cell of each other,
|
|
5
|
+
* count up to a word (two) and a line (three). */
|
|
6
|
+
const REPEAT_MS = 500;
|
|
7
|
+
/**
|
|
8
|
+
* Mouse selection through the engine's gesture: a press, then drags, then
|
|
9
|
+
* a release. A single press selects nothing; a drag selects from it by
|
|
10
|
+
* cells, a double press a word and a triple press a line, dragging by
|
|
11
|
+
* words or lines. The gesture tracks its anchor in the terminal, so
|
|
12
|
+
* output between press and drag does not move it.
|
|
13
|
+
*/
|
|
14
|
+
export class SelectionGesture {
|
|
15
|
+
engine;
|
|
16
|
+
gesture;
|
|
17
|
+
events;
|
|
18
|
+
/** The terminal the gesture tracks its anchor in. */
|
|
19
|
+
term;
|
|
20
|
+
constructor(engine) {
|
|
21
|
+
this.engine = engine;
|
|
22
|
+
this.gesture = engine.construct("ghostty_selection_gesture_new");
|
|
23
|
+
const event = (type) => engine.construct("ghostty_selection_gesture_event_new", engine.enumValue("GhosttySelectionGestureEventType", type));
|
|
24
|
+
this.events = { press: event("PRESS"), drag: event("DRAG"), release: event("RELEASE") };
|
|
25
|
+
const f64 = new Uint8Array(8);
|
|
26
|
+
new DataView(f64.buffer).setFloat64(0, SCALE, true);
|
|
27
|
+
this.set(this.events.press, "REPEAT_DISTANCE", f64);
|
|
28
|
+
this.set(this.events.press, "REPEAT_INTERVAL_NS", u64(REPEAT_MS * 1e6));
|
|
29
|
+
}
|
|
30
|
+
/** Presses at `at` (`time` in ms); the selection to install, or
|
|
31
|
+
* undefined for none (a single press). */
|
|
32
|
+
press(term, at, time) {
|
|
33
|
+
this.follow(term);
|
|
34
|
+
this.set(this.events.press, "REF", term.gridRef(at));
|
|
35
|
+
this.set(this.events.press, "POSITION", position(at));
|
|
36
|
+
this.set(this.events.press, "TIME_NS", u64(time * 1e6));
|
|
37
|
+
return this.apply(term, this.events.press);
|
|
38
|
+
}
|
|
39
|
+
/** Drags to `at`; the selection from the press, or undefined when the
|
|
40
|
+
* pointer has not left the pressed cell. */
|
|
41
|
+
drag(term, at) {
|
|
42
|
+
if (this.term !== term.handle)
|
|
43
|
+
return undefined;
|
|
44
|
+
const geometry = new Uint8Array(16);
|
|
45
|
+
const view = new DataView(geometry.buffer);
|
|
46
|
+
view.setUint32(0, term.cols, true);
|
|
47
|
+
view.setUint32(4, SCALE, true);
|
|
48
|
+
view.setUint32(8, 0, true);
|
|
49
|
+
view.setUint32(12, term.rows * SCALE, true);
|
|
50
|
+
this.set(this.events.drag, "REF", term.gridRef(at));
|
|
51
|
+
this.set(this.events.drag, "POSITION", position(at));
|
|
52
|
+
this.set(this.events.drag, "GEOMETRY", geometry);
|
|
53
|
+
return this.apply(term, this.events.drag);
|
|
54
|
+
}
|
|
55
|
+
release(term, at) {
|
|
56
|
+
if (this.term !== term.handle)
|
|
57
|
+
return;
|
|
58
|
+
this.set(this.events.release, "REF", term.gridRef(at));
|
|
59
|
+
this.apply(term, this.events.release);
|
|
60
|
+
}
|
|
61
|
+
/** Starts afresh, for a terminal freed or about to be: `term` when it
|
|
62
|
+
* is still alive, to release the tracked anchor in it, else none. The
|
|
63
|
+
* caller says so, since a new terminal may have the handle of a freed
|
|
64
|
+
* one. */
|
|
65
|
+
forget(term) {
|
|
66
|
+
if (this.term === undefined)
|
|
67
|
+
return;
|
|
68
|
+
this.engine.call("ghostty_selection_gesture_free", this.gesture, term?.handle ?? 0);
|
|
69
|
+
this.gesture = this.engine.construct("ghostty_selection_gesture_new");
|
|
70
|
+
this.term = undefined;
|
|
71
|
+
}
|
|
72
|
+
/** A gesture is in one terminal. */
|
|
73
|
+
follow(term) {
|
|
74
|
+
if (this.term !== term.handle)
|
|
75
|
+
this.forget();
|
|
76
|
+
this.term = term.handle;
|
|
77
|
+
}
|
|
78
|
+
set(event, option, value) {
|
|
79
|
+
const e = this.engine;
|
|
80
|
+
e.withBytes(value, (ptr) => e.check("ghostty_selection_gesture_event_set", event, e.enumValue("GhosttySelectionGestureEventOption", option), ptr));
|
|
81
|
+
}
|
|
82
|
+
apply(term, event) {
|
|
83
|
+
const e = this.engine;
|
|
84
|
+
const size = e.sizeOf("GhosttySelection");
|
|
85
|
+
return e.scratch(size, (out) => {
|
|
86
|
+
e.memory.fill(0, out, out + size);
|
|
87
|
+
e.view.setUint32(out, size, true);
|
|
88
|
+
const code = e.call("ghostty_selection_gesture_event", this.gesture, term.handle, event, out);
|
|
89
|
+
if (code === e.result.NO_VALUE)
|
|
90
|
+
return undefined;
|
|
91
|
+
if (code !== e.result.SUCCESS)
|
|
92
|
+
throw new Error(`selection gesture: ${code}`);
|
|
93
|
+
return e.memory.slice(out, out + size);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
free() {
|
|
97
|
+
for (const event of Object.values(this.events))
|
|
98
|
+
this.engine.call("ghostty_selection_gesture_event_free", event);
|
|
99
|
+
this.engine.call("ghostty_selection_gesture_free", this.gesture, 0);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function u64(value) {
|
|
103
|
+
const bytes = new Uint8Array(8);
|
|
104
|
+
new DataView(bytes.buffer).setBigUint64(0, BigInt(Math.round(value)), true);
|
|
105
|
+
return bytes;
|
|
106
|
+
}
|
|
107
|
+
/** A `GhosttySurfacePosition` for `at`, in the gesture's scaled pixels. */
|
|
108
|
+
function position(at) {
|
|
109
|
+
const bytes = new Uint8Array(16);
|
|
110
|
+
const view = new DataView(bytes.buffer);
|
|
111
|
+
view.setFloat64(0, at.x * SCALE, true);
|
|
112
|
+
view.setFloat64(8, at.y * SCALE, true);
|
|
113
|
+
return bytes;
|
|
114
|
+
}
|
package/dist/renderer/index.d.ts
CHANGED
|
@@ -24,8 +24,9 @@ export interface Context2D {
|
|
|
24
24
|
fillText(text: string, x: number, y: number): void;
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
|
-
* Draws frames onto a 2D context: changed rows only, then the cursor
|
|
28
|
-
* Rows are cached so the
|
|
27
|
+
* Draws frames onto a 2D context: changed rows only, then the cursor, then
|
|
28
|
+
* the scrollback mark. Rows are cached so the rows the cursor or the mark
|
|
29
|
+
* leaves can be redrawn.
|
|
29
30
|
*/
|
|
30
31
|
export declare class CanvasRenderer {
|
|
31
32
|
private readonly ctx;
|
|
@@ -33,11 +34,16 @@ export declare class CanvasRenderer {
|
|
|
33
34
|
private readonly palette;
|
|
34
35
|
private rows;
|
|
35
36
|
private cursorRow;
|
|
37
|
+
/** The scrollback mark drawn last, in pixels from the top. */
|
|
38
|
+
private mark;
|
|
36
39
|
private cols;
|
|
37
40
|
private background;
|
|
38
41
|
private foreground;
|
|
39
42
|
constructor(ctx: Context2D, metrics: Metrics, palette: Palette);
|
|
40
43
|
draw(frame: Frame): void;
|
|
44
|
+
/** The scrollback mark for `frame`: the viewport's share of the history
|
|
45
|
+
* at the right edge, while the view is scrolled back. */
|
|
46
|
+
private markOf;
|
|
41
47
|
private drawRow;
|
|
42
48
|
private font;
|
|
43
49
|
private drawCursor;
|
package/dist/renderer/index.js
CHANGED
|
@@ -2,9 +2,13 @@ import { FLAG, NO_COLOR } from "../engine/index.js";
|
|
|
2
2
|
const hex = (color) => `#${color.toString(16).padStart(6, "0")}`;
|
|
3
3
|
/** A cell or cursor colour, or `fallback` when it has none. */
|
|
4
4
|
const css = (color, fallback) => (color === NO_COLOR ? fallback : hex(color));
|
|
5
|
+
/** Width in CSS pixels of the mark at the right edge that shows the view
|
|
6
|
+
* scrolled back, and where it is in the history. */
|
|
7
|
+
const MARK_WIDTH = 3;
|
|
5
8
|
/**
|
|
6
|
-
* Draws frames onto a 2D context: changed rows only, then the cursor
|
|
7
|
-
* Rows are cached so the
|
|
9
|
+
* Draws frames onto a 2D context: changed rows only, then the cursor, then
|
|
10
|
+
* the scrollback mark. Rows are cached so the rows the cursor or the mark
|
|
11
|
+
* leaves can be redrawn.
|
|
8
12
|
*/
|
|
9
13
|
export class CanvasRenderer {
|
|
10
14
|
ctx;
|
|
@@ -12,6 +16,8 @@ export class CanvasRenderer {
|
|
|
12
16
|
palette;
|
|
13
17
|
rows = new Map();
|
|
14
18
|
cursorRow;
|
|
19
|
+
/** The scrollback mark drawn last, in pixels from the top. */
|
|
20
|
+
mark;
|
|
15
21
|
cols = 0;
|
|
16
22
|
background = "";
|
|
17
23
|
foreground = "";
|
|
@@ -27,34 +33,67 @@ export class CanvasRenderer {
|
|
|
27
33
|
if (frame.full || frame.cols !== this.cols) {
|
|
28
34
|
this.cols = frame.cols;
|
|
29
35
|
this.rows.clear();
|
|
36
|
+
this.mark = undefined;
|
|
30
37
|
this.ctx.globalAlpha = 1;
|
|
31
38
|
this.ctx.fillStyle = this.background;
|
|
32
39
|
this.ctx.fillRect(0, 0, frame.cols * this.metrics.cellWidth, frame.rows * this.metrics.cellHeight);
|
|
33
40
|
}
|
|
34
41
|
const drawn = new Set();
|
|
35
42
|
for (const row of frame.lines) {
|
|
36
|
-
this.rows.set(row.y, row
|
|
43
|
+
this.rows.set(row.y, row);
|
|
37
44
|
this.drawRow(row);
|
|
38
45
|
drawn.add(row.y);
|
|
39
46
|
}
|
|
47
|
+
// The last mark is erased with the rows under it, every frame: it is
|
|
48
|
+
// translucent, so one drawn over itself would darken.
|
|
49
|
+
const mark = this.markOf(frame);
|
|
50
|
+
if (this.mark) {
|
|
51
|
+
const { cellHeight } = this.metrics;
|
|
52
|
+
const last = Math.ceil((this.mark.top + this.mark.height) / cellHeight);
|
|
53
|
+
for (let y = Math.floor(this.mark.top / cellHeight); y < last; y++) {
|
|
54
|
+
const row = this.rows.get(y);
|
|
55
|
+
if (row && !drawn.has(y)) {
|
|
56
|
+
this.drawRow(row);
|
|
57
|
+
drawn.add(y);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
40
61
|
// The cursor's old row is redrawn from the cache when the cursor
|
|
41
62
|
// moved or went away; the engine marks no row dirty for either.
|
|
42
63
|
const cursor = frame.cursor?.visible ? frame.cursor : undefined;
|
|
43
64
|
if (this.cursorRow !== undefined && this.cursorRow !== cursor?.y && !drawn.has(this.cursorRow)) {
|
|
44
|
-
const
|
|
45
|
-
if (
|
|
46
|
-
this.drawRow(
|
|
65
|
+
const row = this.rows.get(this.cursorRow);
|
|
66
|
+
if (row)
|
|
67
|
+
this.drawRow(row);
|
|
47
68
|
}
|
|
48
69
|
this.cursorRow = undefined;
|
|
49
|
-
if (
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
70
|
+
if (cursor) {
|
|
71
|
+
if (!drawn.has(cursor.y)) {
|
|
72
|
+
const row = this.rows.get(cursor.y);
|
|
73
|
+
if (row)
|
|
74
|
+
this.drawRow(row);
|
|
75
|
+
}
|
|
76
|
+
this.drawCursor(cursor);
|
|
77
|
+
this.cursorRow = cursor.y;
|
|
55
78
|
}
|
|
56
|
-
|
|
57
|
-
this.
|
|
79
|
+
// Over everything, on rows just drawn.
|
|
80
|
+
this.mark = mark;
|
|
81
|
+
if (mark) {
|
|
82
|
+
this.ctx.globalAlpha = 0.5;
|
|
83
|
+
this.ctx.fillStyle = this.foreground;
|
|
84
|
+
this.ctx.fillRect(this.cols * this.metrics.cellWidth - MARK_WIDTH, mark.top, MARK_WIDTH, mark.height);
|
|
85
|
+
this.ctx.globalAlpha = 1;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/** The scrollback mark for `frame`: the viewport's share of the history
|
|
89
|
+
* at the right edge, while the view is scrolled back. */
|
|
90
|
+
markOf(frame) {
|
|
91
|
+
const { total, offset, len } = frame.scrollbar;
|
|
92
|
+
if (offset + len >= total)
|
|
93
|
+
return undefined;
|
|
94
|
+
const height = frame.rows * this.metrics.cellHeight;
|
|
95
|
+
const size = Math.max(Math.round((height * len) / total), Math.min(height, this.metrics.cellHeight));
|
|
96
|
+
return { top: Math.min(Math.round((height * offset) / total), height - size), height: size };
|
|
58
97
|
}
|
|
59
98
|
drawRow(row) {
|
|
60
99
|
const { cellWidth, cellHeight, baseline } = this.metrics;
|
|
@@ -69,7 +108,9 @@ export class CanvasRenderer {
|
|
|
69
108
|
const width = cell.flags & FLAG.WIDE ? 2 : 1;
|
|
70
109
|
let fg = css(cell.fg, this.foreground);
|
|
71
110
|
let bg = css(cell.bg, this.background);
|
|
72
|
-
|
|
111
|
+
// A selected cell is drawn inverted, an inverse one the other way.
|
|
112
|
+
const selected = row.selection !== undefined && x >= row.selection[0] && x <= row.selection[1];
|
|
113
|
+
if (((cell.flags & FLAG.INVERSE) !== 0) !== selected)
|
|
73
114
|
[fg, bg] = [bg, fg];
|
|
74
115
|
if (bg !== this.background) {
|
|
75
116
|
this.ctx.fillStyle = bg;
|
|
@@ -126,7 +167,7 @@ export class CanvasRenderer {
|
|
|
126
167
|
default: {
|
|
127
168
|
this.ctx.fillRect(x, y, cellWidth, cellHeight);
|
|
128
169
|
// The cell's text in the background colour, over the block.
|
|
129
|
-
const cell = this.rows.get(cursor.y)?.[cursor.x];
|
|
170
|
+
const cell = this.rows.get(cursor.y)?.cells[cursor.x];
|
|
130
171
|
if (cell && cell.text !== "" && cell.text !== " ") {
|
|
131
172
|
this.ctx.fillStyle = this.background;
|
|
132
173
|
this.ctx.font = this.font(cell.flags);
|