@gkoreli/ghostty-vt-js 0.3.2
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/LICENSE +21 -0
- package/README.md +89 -0
- package/THIRD_PARTY_NOTICES.md +45 -0
- package/dist/browser-terminal/buffer.d.ts +93 -0
- package/dist/browser-terminal/buffer.d.ts.map +1 -0
- package/dist/browser-terminal/buffer.js +337 -0
- package/dist/browser-terminal/buffer.js.map +1 -0
- package/dist/browser-terminal/encoding.d.ts +17 -0
- package/dist/browser-terminal/encoding.d.ts.map +1 -0
- package/dist/browser-terminal/encoding.js +22 -0
- package/dist/browser-terminal/encoding.js.map +1 -0
- package/dist/browser-terminal/event-emitter.d.ts +8 -0
- package/dist/browser-terminal/event-emitter.d.ts.map +1 -0
- package/dist/browser-terminal/event-emitter.js +27 -0
- package/dist/browser-terminal/event-emitter.js.map +1 -0
- package/dist/browser-terminal/frame-hold.d.ts +13 -0
- package/dist/browser-terminal/frame-hold.d.ts.map +1 -0
- package/dist/browser-terminal/frame-hold.js +17 -0
- package/dist/browser-terminal/frame-hold.js.map +1 -0
- package/dist/browser-terminal/geometry.d.ts +229 -0
- package/dist/browser-terminal/geometry.d.ts.map +1 -0
- package/dist/browser-terminal/geometry.js +299 -0
- package/dist/browser-terminal/geometry.js.map +1 -0
- package/dist/browser-terminal/ghostty.d.ts +478 -0
- package/dist/browser-terminal/ghostty.d.ts.map +1 -0
- package/dist/browser-terminal/ghostty.js +1212 -0
- package/dist/browser-terminal/ghostty.js.map +1 -0
- package/dist/browser-terminal/gpu/adapter.d.ts +70 -0
- package/dist/browser-terminal/gpu/adapter.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/adapter.js +260 -0
- package/dist/browser-terminal/gpu/adapter.js.map +1 -0
- package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.d.ts +33 -0
- package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.js +159 -0
- package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.js.map +1 -0
- package/dist/browser-terminal/gpu/core/context-loss-watchdog.d.ts +35 -0
- package/dist/browser-terminal/gpu/core/context-loss-watchdog.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/context-loss-watchdog.js +73 -0
- package/dist/browser-terminal/gpu/core/context-loss-watchdog.js.map +1 -0
- package/dist/browser-terminal/gpu/core/frame.d.ts +68 -0
- package/dist/browser-terminal/gpu/core/frame.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/frame.js +167 -0
- package/dist/browser-terminal/gpu/core/frame.js.map +1 -0
- package/dist/browser-terminal/gpu/core/glyph-atlas.d.ts +136 -0
- package/dist/browser-terminal/gpu/core/glyph-atlas.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/glyph-atlas.js +281 -0
- package/dist/browser-terminal/gpu/core/glyph-atlas.js.map +1 -0
- package/dist/browser-terminal/gpu/core/procedural-shapes.d.ts +24 -0
- package/dist/browser-terminal/gpu/core/procedural-shapes.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/procedural-shapes.js +316 -0
- package/dist/browser-terminal/gpu/core/procedural-shapes.js.map +1 -0
- package/dist/browser-terminal/gpu/core/webgl2-renderer.d.ts +107 -0
- package/dist/browser-terminal/gpu/core/webgl2-renderer.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/webgl2-renderer.js +698 -0
- package/dist/browser-terminal/gpu/core/webgl2-renderer.js.map +1 -0
- package/dist/browser-terminal/gpu/create-renderer.d.ts +15 -0
- package/dist/browser-terminal/gpu/create-renderer.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/create-renderer.js +33 -0
- package/dist/browser-terminal/gpu/create-renderer.js.map +1 -0
- package/dist/browser-terminal/index.d.ts +47 -0
- package/dist/browser-terminal/index.d.ts.map +1 -0
- package/dist/browser-terminal/index.js +75 -0
- package/dist/browser-terminal/index.js.map +1 -0
- package/dist/browser-terminal/input-handler.d.ts +262 -0
- package/dist/browser-terminal/input-handler.d.ts.map +1 -0
- package/dist/browser-terminal/input-handler.js +978 -0
- package/dist/browser-terminal/input-handler.js.map +1 -0
- package/dist/browser-terminal/interfaces.d.ts +230 -0
- package/dist/browser-terminal/interfaces.d.ts.map +1 -0
- package/dist/browser-terminal/interfaces.js +5 -0
- package/dist/browser-terminal/interfaces.js.map +1 -0
- package/dist/browser-terminal/link-detector.d.ts +76 -0
- package/dist/browser-terminal/link-detector.d.ts.map +1 -0
- package/dist/browser-terminal/link-detector.js +165 -0
- package/dist/browser-terminal/link-detector.js.map +1 -0
- package/dist/browser-terminal/providers/osc8-link-provider.d.ts +56 -0
- package/dist/browser-terminal/providers/osc8-link-provider.d.ts.map +1 -0
- package/dist/browser-terminal/providers/osc8-link-provider.js +204 -0
- package/dist/browser-terminal/providers/osc8-link-provider.js.map +1 -0
- package/dist/browser-terminal/providers/url-regex-provider.d.ts +68 -0
- package/dist/browser-terminal/providers/url-regex-provider.d.ts.map +1 -0
- package/dist/browser-terminal/providers/url-regex-provider.js +107 -0
- package/dist/browser-terminal/providers/url-regex-provider.js.map +1 -0
- package/dist/browser-terminal/renderer-interface.d.ts +59 -0
- package/dist/browser-terminal/renderer-interface.d.ts.map +1 -0
- package/dist/browser-terminal/renderer-interface.js +2 -0
- package/dist/browser-terminal/renderer-interface.js.map +1 -0
- package/dist/browser-terminal/renderer.d.ts +254 -0
- package/dist/browser-terminal/renderer.d.ts.map +1 -0
- package/dist/browser-terminal/renderer.js +905 -0
- package/dist/browser-terminal/renderer.js.map +1 -0
- package/dist/browser-terminal/scrollbar-overlay.d.ts +45 -0
- package/dist/browser-terminal/scrollbar-overlay.d.ts.map +1 -0
- package/dist/browser-terminal/scrollbar-overlay.js +89 -0
- package/dist/browser-terminal/scrollbar-overlay.js.map +1 -0
- package/dist/browser-terminal/selection-manager.d.ts +180 -0
- package/dist/browser-terminal/selection-manager.d.ts.map +1 -0
- package/dist/browser-terminal/selection-manager.js +913 -0
- package/dist/browser-terminal/selection-manager.js.map +1 -0
- package/dist/browser-terminal/terminal.d.ts +423 -0
- package/dist/browser-terminal/terminal.d.ts.map +1 -0
- package/dist/browser-terminal/terminal.js +1754 -0
- package/dist/browser-terminal/terminal.js.map +1 -0
- package/dist/browser-terminal/types.d.ts +595 -0
- package/dist/browser-terminal/types.d.ts.map +1 -0
- package/dist/browser-terminal/types.js +467 -0
- package/dist/browser-terminal/types.js.map +1 -0
- package/dist/browser-terminal/viewport-scroller.d.ts +94 -0
- package/dist/browser-terminal/viewport-scroller.d.ts.map +1 -0
- package/dist/browser-terminal/viewport-scroller.js +159 -0
- package/dist/browser-terminal/viewport-scroller.js.map +1 -0
- package/dist/browser-terminal/vt/effects.d.ts +114 -0
- package/dist/browser-terminal/vt/effects.d.ts.map +1 -0
- package/dist/browser-terminal/vt/effects.js +143 -0
- package/dist/browser-terminal/vt/effects.js.map +1 -0
- package/dist/browser-terminal/vt/enums.d.ts +155 -0
- package/dist/browser-terminal/vt/enums.d.ts.map +1 -0
- package/dist/browser-terminal/vt/enums.js +161 -0
- package/dist/browser-terminal/vt/enums.js.map +1 -0
- package/dist/browser-terminal/vt/mouse.d.ts +166 -0
- package/dist/browser-terminal/vt/mouse.d.ts.map +1 -0
- package/dist/browser-terminal/vt/mouse.js +259 -0
- package/dist/browser-terminal/vt/mouse.js.map +1 -0
- package/dist/browser-terminal/vt/paste.d.ts +53 -0
- package/dist/browser-terminal/vt/paste.d.ts.map +1 -0
- package/dist/browser-terminal/vt/paste.js +89 -0
- package/dist/browser-terminal/vt/paste.js.map +1 -0
- package/dist/browser-terminal/vt/render-state.d.ts +208 -0
- package/dist/browser-terminal/vt/render-state.d.ts.map +1 -0
- package/dist/browser-terminal/vt/render-state.js +776 -0
- package/dist/browser-terminal/vt/render-state.js.map +1 -0
- package/dist/browser-terminal/vt/style.d.ts +54 -0
- package/dist/browser-terminal/vt/style.d.ts.map +1 -0
- package/dist/browser-terminal/vt/style.js +92 -0
- package/dist/browser-terminal/vt/style.js.map +1 -0
- package/dist/browser-terminal/wasm-loader.d.ts +39 -0
- package/dist/browser-terminal/wasm-loader.d.ts.map +1 -0
- package/dist/browser-terminal/wasm-loader.js +73 -0
- package/dist/browser-terminal/wasm-loader.js.map +1 -0
- package/dist/browser-terminal/write-buffer.d.ts +40 -0
- package/dist/browser-terminal/write-buffer.d.ts.map +1 -0
- package/dist/browser-terminal/write-buffer.js +144 -0
- package/dist/browser-terminal/write-buffer.js.map +1 -0
- package/dist/protocol/index.d.ts +135 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/index.js +76 -0
- package/dist/protocol/index.js.map +1 -0
- package/dist/react/index.d.ts +59 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +146 -0
- package/dist/react/index.js.map +1 -0
- package/dist/server/index.d.ts +130 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +191 -0
- package/dist/server/index.js.map +1 -0
- package/dist/terminal-screen-emulator/emulator.d.ts +66 -0
- package/dist/terminal-screen-emulator/emulator.d.ts.map +1 -0
- package/dist/terminal-screen-emulator/emulator.js +286 -0
- package/dist/terminal-screen-emulator/emulator.js.map +1 -0
- package/dist/terminal-screen-emulator/index.d.ts +82 -0
- package/dist/terminal-screen-emulator/index.d.ts.map +1 -0
- package/dist/terminal-screen-emulator/index.js +92 -0
- package/dist/terminal-screen-emulator/index.js.map +1 -0
- package/dist/terminal-screen-emulator/types.d.ts +382 -0
- package/dist/terminal-screen-emulator/types.d.ts.map +1 -0
- package/dist/terminal-screen-emulator/types.js +91 -0
- package/dist/terminal-screen-emulator/types.js.map +1 -0
- package/dist/terminal-screen-emulator/wasm-loader.d.ts +71 -0
- package/dist/terminal-screen-emulator/wasm-loader.d.ts.map +1 -0
- package/dist/terminal-screen-emulator/wasm-loader.js +162 -0
- package/dist/terminal-screen-emulator/wasm-loader.js.map +1 -0
- package/dist/themes/index.d.ts +6 -0
- package/dist/themes/index.d.ts.map +1 -0
- package/dist/themes/index.js +51 -0
- package/dist/themes/index.js.map +1 -0
- package/dist/wasm/alloc.d.ts +170 -0
- package/dist/wasm/alloc.d.ts.map +1 -0
- package/dist/wasm/alloc.js +307 -0
- package/dist/wasm/alloc.js.map +1 -0
- package/dist/wasm/compile.d.ts +73 -0
- package/dist/wasm/compile.d.ts.map +1 -0
- package/dist/wasm/compile.js +82 -0
- package/dist/wasm/compile.js.map +1 -0
- package/dist/wasm/exports.d.ts +234 -0
- package/dist/wasm/exports.d.ts.map +1 -0
- package/dist/wasm/exports.js +15 -0
- package/dist/wasm/exports.js.map +1 -0
- package/dist/wasm/fn-table.d.ts +119 -0
- package/dist/wasm/fn-table.d.ts.map +1 -0
- package/dist/wasm/fn-table.js +168 -0
- package/dist/wasm/fn-table.js.map +1 -0
- package/dist/wasm/memory.d.ts +59 -0
- package/dist/wasm/memory.d.ts.map +1 -0
- package/dist/wasm/memory.js +89 -0
- package/dist/wasm/memory.js.map +1 -0
- package/dist/wasm/type-layouts.d.ts +61 -0
- package/dist/wasm/type-layouts.d.ts.map +1 -0
- package/dist/wasm/type-layouts.js +32 -0
- package/dist/wasm/type-layouts.js.map +1 -0
- package/package.json +94 -0
- package/wasm/ghostty-vt.wasm +0 -0
|
@@ -0,0 +1,1212 @@
|
|
|
1
|
+
// Portions originally derived from coder/ghostty-web (MIT — see ./LICENSE):
|
|
2
|
+
// https://github.com/coder/ghostty-web/blob/6a1a50df5b4f6b34d1b1de10fad3a0fc811bfbc0/lib/ghostty.ts
|
|
3
|
+
// Substantially rewritten since; this file is ours and does not track that project.
|
|
4
|
+
// Modified: rewrote against the Ghostty C ABI in vendor/ghostty/include/ghostty/vt/*.h.
|
|
5
|
+
// The upstream wrapper invented its own ABI (custom 16-byte cell, response queue,
|
|
6
|
+
// 7+ tiny render-state getters) and is now unmaintained. This file replaces it
|
|
7
|
+
// with a thin TS layer over the actual headers.
|
|
8
|
+
/**
|
|
9
|
+
* @module browser-terminal/ghostty
|
|
10
|
+
*
|
|
11
|
+
* TypeScript wrapper for the local `ghostty-vt.wasm` (built from Ghostty's
|
|
12
|
+
* first-party `-Demit-lib-vt` flag).
|
|
13
|
+
*
|
|
14
|
+
* The class hierarchy mirrors the C API:
|
|
15
|
+
*
|
|
16
|
+
* | TS class | Owns | C API surface |
|
|
17
|
+
* |----------------|-------------------------------|---------------------------|
|
|
18
|
+
* | `Ghostty` | The WASM instance | factory for the rest |
|
|
19
|
+
* | `Terminal` | a `GhosttyTerminal` handle | `terminal.h` |
|
|
20
|
+
* | `RenderState` | a `GhosttyRenderState` handle | `render.h` |
|
|
21
|
+
* | `KeyEncoder` | a `GhosttyKeyEncoder` handle | `key/encoder.h` |
|
|
22
|
+
* | `KeyEvent` | a `GhosttyKeyEvent` handle | `key/event.h` |
|
|
23
|
+
* | `GridRef` | a sized struct (no handle) | `grid_ref.h` accessors |
|
|
24
|
+
*
|
|
25
|
+
* Every method JSDoc cites the header and the specific C function it wraps.
|
|
26
|
+
* If a method has no header citation, it's a JS-side convenience built on
|
|
27
|
+
* top of one or more C calls — those are explicitly marked `VT_NOTE`.
|
|
28
|
+
*/
|
|
29
|
+
import { ScratchPool, allocZeroedBytes, withBytes } from "../wasm/alloc.js";
|
|
30
|
+
import { fieldOffset, readU8, readU16, readU32, readU64, structSize, writeU32, } from "../wasm/memory.js";
|
|
31
|
+
import { CellWide, GhosttyResult, PointTag, RenderStateDirty, VtModes, vtMode, styleToFlags, toGhosttyCell, } from "./types.js";
|
|
32
|
+
import { ScrollViewportTag, TerminalData, TerminalOption } from "./vt/enums.js";
|
|
33
|
+
import { MouseEncoder } from "./vt/mouse.js";
|
|
34
|
+
import { RenderState } from "./vt/render-state.js";
|
|
35
|
+
import { readStyle, resolveStyleColor } from "./vt/style.js";
|
|
36
|
+
/**
|
|
37
|
+
* Convenience: alloc a zero-filled byte block in WASM memory.
|
|
38
|
+
*
|
|
39
|
+
* Re-exported from `wasm/alloc.ts` so existing call sites stay terse.
|
|
40
|
+
* For per-frame churn use the `FrameArena` from `wasm/alloc.ts` instead.
|
|
41
|
+
*/
|
|
42
|
+
const allocZeroed = allocZeroedBytes;
|
|
43
|
+
function checkResult(name, result) {
|
|
44
|
+
if (result !== GhosttyResult.SUCCESS) {
|
|
45
|
+
throw new Error(`${name} failed with result code ${result}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
// =============================================================================
|
|
49
|
+
// Ghostty — module-level wrapper around a single WASM instance
|
|
50
|
+
// =============================================================================
|
|
51
|
+
/**
|
|
52
|
+
* Ghostty WASM module wrapper.
|
|
53
|
+
*
|
|
54
|
+
* Construct via `Ghostty.fromInstance()` (after loading via `wasm-loader.ts`).
|
|
55
|
+
* Use this object as a factory for {@link Terminal}, {@link KeyEncoder}, and
|
|
56
|
+
* other handles bound to the same WASM linear memory.
|
|
57
|
+
*/
|
|
58
|
+
export class Ghostty {
|
|
59
|
+
/** @internal Direct access to WASM exports. Avoid using outside this module. */
|
|
60
|
+
exports;
|
|
61
|
+
/** @internal Self-describing struct layouts parsed from `ghostty_type_json()`. */
|
|
62
|
+
typeLayouts;
|
|
63
|
+
constructor(exports, typeLayouts) {
|
|
64
|
+
this.exports = exports;
|
|
65
|
+
this.typeLayouts = typeLayouts;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Wrap a WASM instance produced by `loadGhosttyWasm()`.
|
|
69
|
+
* Use this when you want to share one WASM instance across multiple terminals.
|
|
70
|
+
*/
|
|
71
|
+
static fromInstance(opts) {
|
|
72
|
+
return new Ghostty(opts.exports, opts.typeLayouts);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Create a new {@link Terminal} bound to this WASM instance.
|
|
76
|
+
*
|
|
77
|
+
* Wraps `ghostty_terminal_new(allocator, &handle, options)` from `terminal.h`.
|
|
78
|
+
*/
|
|
79
|
+
createTerminal(opts = { cols: 80, rows: 24 }) {
|
|
80
|
+
return new Terminal(this, {
|
|
81
|
+
cols: opts.cols,
|
|
82
|
+
rows: opts.rows,
|
|
83
|
+
maxScrollback: opts.maxScrollback ?? 0,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Create a new {@link KeyEncoder} bound to this WASM instance.
|
|
88
|
+
*
|
|
89
|
+
* Wraps `ghostty_key_encoder_new(allocator, &handle)` from `key/encoder.h`.
|
|
90
|
+
*/
|
|
91
|
+
createKeyEncoder() {
|
|
92
|
+
return new KeyEncoder(this);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Create a new {@link MouseEncoder} bound to this WASM instance.
|
|
96
|
+
*
|
|
97
|
+
* Wraps `ghostty_mouse_encoder_new(allocator, &handle)` from
|
|
98
|
+
* `mouse/encoder.h`. One per terminal (the encoder is stateful); sync with
|
|
99
|
+
* `syncFromTerminal()` before each encode. See `vt/mouse.ts`.
|
|
100
|
+
*/
|
|
101
|
+
createMouseEncoder() {
|
|
102
|
+
return new MouseEncoder({ exports: this.exports, typeLayouts: this.typeLayouts });
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Create a new {@link RenderState} bound to this WASM instance.
|
|
106
|
+
*
|
|
107
|
+
* Wraps `ghostty_render_state_new(allocator, &handle)` from `render.h`.
|
|
108
|
+
* Most callers should use `Terminal.renderState` (lazy-created per terminal)
|
|
109
|
+
* rather than this factory.
|
|
110
|
+
*/
|
|
111
|
+
createRenderState() {
|
|
112
|
+
return new RenderState({ exports: this.exports, typeLayouts: this.typeLayouts });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// =============================================================================
|
|
116
|
+
// Terminal — wraps a `GhosttyTerminal` handle
|
|
117
|
+
// =============================================================================
|
|
118
|
+
/**
|
|
119
|
+
* Terminal instance — wraps a `GhosttyTerminal` opaque handle.
|
|
120
|
+
*
|
|
121
|
+
* One terminal owns:
|
|
122
|
+
* - the WASM-side terminal handle (freed by `dispose()`)
|
|
123
|
+
* - a lazily-allocated {@link RenderState} (created on first `update()`)
|
|
124
|
+
* - reusable scratch buffers for `terminal_get` out-pointers
|
|
125
|
+
*
|
|
126
|
+
* The class shape mirrors `terminal.h`: lifecycle methods, `vtWrite`,
|
|
127
|
+
* keyed `get` / `getMulti`, mode read/set, scroll viewport, grid-ref
|
|
128
|
+
* acquisition.
|
|
129
|
+
*/
|
|
130
|
+
export class Terminal {
|
|
131
|
+
ghostty;
|
|
132
|
+
/** @internal */
|
|
133
|
+
handle;
|
|
134
|
+
_disposed = false;
|
|
135
|
+
/** Cached cols/rows. Re-read from the terminal after `resize()`. */
|
|
136
|
+
_cols;
|
|
137
|
+
_rows;
|
|
138
|
+
/** Lazily created — see {@link renderState}. */
|
|
139
|
+
_renderState;
|
|
140
|
+
/**
|
|
141
|
+
* Pool of reusable scratch slots for keyed `terminal_get`/`mode_get` reads.
|
|
142
|
+
* Owns u8/u16/u32 slots backed by the C ABI allocators.
|
|
143
|
+
*/
|
|
144
|
+
scratch;
|
|
145
|
+
constructor(ghostty, opts) {
|
|
146
|
+
this.ghostty = ghostty;
|
|
147
|
+
this._cols = opts.cols;
|
|
148
|
+
this._rows = opts.rows;
|
|
149
|
+
this.scratch = new ScratchPool(ghostty.exports);
|
|
150
|
+
// ── ghostty_terminal_new ─────────────────────────────────────────────
|
|
151
|
+
// VT_NOTE: as of vendor/ghostty@6ad1fe7d8 the signature is
|
|
152
|
+
// `(allocator, &handle, uint16 cols, uint16 rows)`. The old
|
|
153
|
+
// `GhosttyTerminalOptions` struct was REMOVED upstream (libghostty-vt is
|
|
154
|
+
// pre-1.0 and `vt.h:25` warns the API is unstable), and scrollback moved to
|
|
155
|
+
// `ghostty_terminal_set(OPT_SCROLLBACK_MAX_LINES)` — applied below.
|
|
156
|
+
const memory = ghostty.exports.memory;
|
|
157
|
+
const outPtr = ghostty.exports.ghostty_wasm_alloc_opaque();
|
|
158
|
+
const result = ghostty.exports.ghostty_terminal_new(0, outPtr, opts.cols, opts.rows);
|
|
159
|
+
if (result !== GhosttyResult.SUCCESS) {
|
|
160
|
+
ghostty.exports.ghostty_wasm_free_opaque(outPtr);
|
|
161
|
+
throw new Error(`ghostty_terminal_new failed: ${result} (cols=${opts.cols}, rows=${opts.rows})`);
|
|
162
|
+
}
|
|
163
|
+
this.handle = readU32(memory, outPtr);
|
|
164
|
+
ghostty.exports.ghostty_wasm_free_opaque(outPtr);
|
|
165
|
+
// Scrollback is an option now, not a constructor field. `size_t*` input;
|
|
166
|
+
// 0 means "no history", which the engine treats as no limit removal — we
|
|
167
|
+
// only set it when a limit was asked for.
|
|
168
|
+
if (opts.maxScrollback > 0) {
|
|
169
|
+
withBytes(ghostty.exports, 4, (ptr) => {
|
|
170
|
+
writeU32(memory, ptr, opts.maxScrollback);
|
|
171
|
+
ghostty.exports.ghostty_terminal_set(this.handle, TerminalOption.SCROLLBACK_MAX_LINES, ptr);
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/** Viewport width in cells. Reflects the most recent constructor/resize call. */
|
|
176
|
+
get cols() {
|
|
177
|
+
return this._cols;
|
|
178
|
+
}
|
|
179
|
+
/** Viewport height in cells. Reflects the most recent constructor/resize call. */
|
|
180
|
+
get rows() {
|
|
181
|
+
return this._rows;
|
|
182
|
+
}
|
|
183
|
+
/** @internal Access the underlying Ghostty wrapper (used by sibling classes). */
|
|
184
|
+
get _ghostty() {
|
|
185
|
+
return this.ghostty;
|
|
186
|
+
}
|
|
187
|
+
// ─── Lifecycle ─────────────────────────────────────────────────────────
|
|
188
|
+
/**
|
|
189
|
+
* Reset terminal state to initial. Mirrors `ghostty_terminal_reset` from `terminal.h`.
|
|
190
|
+
*/
|
|
191
|
+
reset() {
|
|
192
|
+
this.assertAlive();
|
|
193
|
+
this.invalidateScrollbackCache();
|
|
194
|
+
this.ghostty.exports.ghostty_terminal_reset(this.handle);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Resize the terminal. Mirrors `ghostty_terminal_resize(term, cols, rows, cell_w_px, cell_h_px)`
|
|
198
|
+
* from `terminal.h`.
|
|
199
|
+
*
|
|
200
|
+
* VT_NOTE: pixel sizing is ignored on the JS side (the canvas renderer owns
|
|
201
|
+
* font metrics) — we always pass `0` for cell width/height.
|
|
202
|
+
*/
|
|
203
|
+
resize(cols, rows) {
|
|
204
|
+
this.assertAlive();
|
|
205
|
+
if (cols === this._cols && rows === this._rows)
|
|
206
|
+
return;
|
|
207
|
+
this._cols = cols;
|
|
208
|
+
this._rows = rows;
|
|
209
|
+
// Resize reflows history (rewrap changes what each row contains) and the
|
|
210
|
+
// cached rows carry the old width — drop the memo.
|
|
211
|
+
this.invalidateScrollbackCache();
|
|
212
|
+
this.ghostty.exports.ghostty_terminal_resize(this.handle, cols, rows, 0, 0);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Free the terminal handle and any cached scratch buffers.
|
|
216
|
+
* Mirrors `ghostty_terminal_free` from `terminal.h`.
|
|
217
|
+
*/
|
|
218
|
+
dispose() {
|
|
219
|
+
if (this._disposed)
|
|
220
|
+
return;
|
|
221
|
+
this._disposed = true;
|
|
222
|
+
if (this._renderState) {
|
|
223
|
+
this._renderState.dispose();
|
|
224
|
+
this._renderState = undefined;
|
|
225
|
+
}
|
|
226
|
+
this.scratch.dispose();
|
|
227
|
+
this.ghostty.exports.ghostty_terminal_free(this.handle);
|
|
228
|
+
}
|
|
229
|
+
// ─── Writing ───────────────────────────────────────────────────────────
|
|
230
|
+
/**
|
|
231
|
+
* Feed VT bytes to the terminal. Mirrors `ghostty_terminal_vt_write` from `terminal.h`.
|
|
232
|
+
*
|
|
233
|
+
* Strings are UTF-8 encoded before being written. Empty inputs are no-ops.
|
|
234
|
+
*/
|
|
235
|
+
write(data) {
|
|
236
|
+
this.assertAlive();
|
|
237
|
+
const buf = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
238
|
+
if (buf.length === 0)
|
|
239
|
+
return;
|
|
240
|
+
// Writes are the ONLY mutation of scrollback (push + eviction shifting
|
|
241
|
+
// offsets), so this is the one place the row memo must be dropped.
|
|
242
|
+
this.invalidateScrollbackCache();
|
|
243
|
+
withBytes(this.ghostty.exports, buf.length, (ptr) => {
|
|
244
|
+
new Uint8Array(this.ghostty.exports.memory.buffer).set(buf, ptr);
|
|
245
|
+
this.ghostty.exports.ghostty_terminal_vt_write(this.handle, ptr, buf.length);
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
getMode(modeOrValue, isAnsi) {
|
|
249
|
+
this.assertAlive();
|
|
250
|
+
const mode = isAnsi === undefined
|
|
251
|
+
? modeOrValue
|
|
252
|
+
: vtMode(modeOrValue, isAnsi);
|
|
253
|
+
const outPtr = this.scratchU8Ptr();
|
|
254
|
+
new Uint8Array(this.ghostty.exports.memory.buffer)[outPtr] = 0;
|
|
255
|
+
const result = this.ghostty.exports.ghostty_terminal_mode_get(this.handle, mode, outPtr);
|
|
256
|
+
if (result !== GhosttyResult.SUCCESS)
|
|
257
|
+
return false;
|
|
258
|
+
return readU8(this.ghostty.exports.memory, outPtr) !== 0;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Set a terminal mode. Mirrors `ghostty_terminal_mode_set(term, mode, value)`
|
|
262
|
+
* from `terminal.h` + `modes.h`.
|
|
263
|
+
*/
|
|
264
|
+
setMode(mode, value) {
|
|
265
|
+
this.assertAlive();
|
|
266
|
+
checkResult("ghostty_terminal_mode_set", this.ghostty.exports.ghostty_terminal_mode_set(this.handle, mode, value ? 1 : 0));
|
|
267
|
+
}
|
|
268
|
+
// ─── Keyed `get` ───────────────────────────────────────────────────────
|
|
269
|
+
/**
|
|
270
|
+
* Read a single u16 data kind. Mirrors `ghostty_terminal_get(term, kind, &outU16)`
|
|
271
|
+
* from `terminal.h`.
|
|
272
|
+
*/
|
|
273
|
+
getU16(kind) {
|
|
274
|
+
const outPtr = this.scratchU16Ptr();
|
|
275
|
+
writeU32(this.ghostty.exports.memory, outPtr, 0);
|
|
276
|
+
checkResult("ghostty_terminal_get", this.ghostty.exports.ghostty_terminal_get(this.handle, kind, outPtr));
|
|
277
|
+
return readU16(this.ghostty.exports.memory, outPtr);
|
|
278
|
+
}
|
|
279
|
+
/** Read a single u32 data kind. */
|
|
280
|
+
getU32(kind) {
|
|
281
|
+
const outPtr = this.scratchU32Ptr();
|
|
282
|
+
writeU32(this.ghostty.exports.memory, outPtr, 0);
|
|
283
|
+
checkResult("ghostty_terminal_get", this.ghostty.exports.ghostty_terminal_get(this.handle, kind, outPtr));
|
|
284
|
+
return readU32(this.ghostty.exports.memory, outPtr);
|
|
285
|
+
}
|
|
286
|
+
/** Read a single bool data kind. */
|
|
287
|
+
getBool(kind) {
|
|
288
|
+
const outPtr = this.scratchU8Ptr();
|
|
289
|
+
new Uint8Array(this.ghostty.exports.memory.buffer)[outPtr] = 0;
|
|
290
|
+
checkResult("ghostty_terminal_get", this.ghostty.exports.ghostty_terminal_get(this.handle, kind, outPtr));
|
|
291
|
+
return readU8(this.ghostty.exports.memory, outPtr) !== 0;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Read a `GhosttyString` data kind (TITLE, PWD).
|
|
295
|
+
*
|
|
296
|
+
* Mirrors `ghostty_terminal_get(term, kind, &outString)` from `terminal.h:697-717`.
|
|
297
|
+
* `GhosttyString` is `{ const uint8_t *ptr; size_t len; }` — see
|
|
298
|
+
* `vendor/ghostty/include/ghostty/vt/types.h:193-199`.
|
|
299
|
+
*
|
|
300
|
+
* VT_NOTE: the returned string is *borrowed* — its bytes live inside the
|
|
301
|
+
* terminal and are only valid until the next `vt_write`/`reset`. We copy
|
|
302
|
+
* eagerly into a JS string so callers don't need to think about lifetime.
|
|
303
|
+
* Returns `""` for `len == 0` (the API's "no value" signal for these kinds).
|
|
304
|
+
*/
|
|
305
|
+
getString(kind) {
|
|
306
|
+
const layouts = this.ghostty.typeLayouts;
|
|
307
|
+
const size = structSize(layouts, "GhosttyString");
|
|
308
|
+
const outPtr = allocZeroed(this.ghostty.exports, size);
|
|
309
|
+
try {
|
|
310
|
+
checkResult("ghostty_terminal_get", this.ghostty.exports.ghostty_terminal_get(this.handle, kind, outPtr));
|
|
311
|
+
const view = new DataView(this.ghostty.exports.memory.buffer, outPtr, size);
|
|
312
|
+
const ptr = view.getUint32(fieldOffset(layouts, "GhosttyString", "ptr"), true);
|
|
313
|
+
const len = view.getUint32(fieldOffset(layouts, "GhosttyString", "len"), true);
|
|
314
|
+
if (len === 0 || ptr === 0)
|
|
315
|
+
return "";
|
|
316
|
+
const bytes = new Uint8Array(this.ghostty.exports.memory.buffer, ptr, len);
|
|
317
|
+
return new TextDecoder("utf-8", { fatal: false }).decode(bytes);
|
|
318
|
+
}
|
|
319
|
+
finally {
|
|
320
|
+
this.ghostty.exports.ghostty_wasm_free_u8_array(outPtr, size);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
/** True if any mouse-tracking mode is enabled. */
|
|
324
|
+
hasMouseTracking() {
|
|
325
|
+
return this.getBool(TerminalData.MOUSE_TRACKING);
|
|
326
|
+
}
|
|
327
|
+
/** True if the active screen is the alternate (full-screen-app) screen. */
|
|
328
|
+
isAlternateScreen() {
|
|
329
|
+
return this.getU32(TerminalData.ACTIVE_SCREEN) === 1;
|
|
330
|
+
}
|
|
331
|
+
/** Total scrollback rows. Mirrors `GHOSTTY_TERMINAL_DATA_SCROLLBACK_ROWS`. */
|
|
332
|
+
scrollbackRows() {
|
|
333
|
+
return this.getU32(TerminalData.SCROLLBACK_ROWS);
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Current title set by escape sequences (OSC 0/2). Mirrors
|
|
337
|
+
* `GHOSTTY_TERMINAL_DATA_TITLE` from `terminal.h:697-705`. Returns `""` when
|
|
338
|
+
* no title has been set.
|
|
339
|
+
*/
|
|
340
|
+
getTitle() {
|
|
341
|
+
return this.getString(TerminalData.TITLE);
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Current working directory set by escape sequences (OSC 7). Mirrors
|
|
345
|
+
* `GHOSTTY_TERMINAL_DATA_PWD` from `terminal.h:707-717`. Returns `""` when
|
|
346
|
+
* no pwd has been set.
|
|
347
|
+
*/
|
|
348
|
+
getPwd() {
|
|
349
|
+
return this.getString(TerminalData.PWD);
|
|
350
|
+
}
|
|
351
|
+
// ─── Scroll viewport ───────────────────────────────────────────────────
|
|
352
|
+
/**
|
|
353
|
+
* Scroll the viewport by a delta. Mirrors `ghostty_terminal_scroll_viewport`
|
|
354
|
+
* with the DELTA tag. Negative is up, positive is down.
|
|
355
|
+
*/
|
|
356
|
+
scrollViewportDelta(delta) {
|
|
357
|
+
this.assertAlive();
|
|
358
|
+
const layouts = this.ghostty.typeLayouts;
|
|
359
|
+
const size = structSize(layouts, "GhosttyTerminalScrollViewport");
|
|
360
|
+
const ptr = allocZeroed(this.ghostty.exports, size);
|
|
361
|
+
const view = new DataView(this.ghostty.exports.memory.buffer, ptr, size);
|
|
362
|
+
view.setUint32(fieldOffset(layouts, "GhosttyTerminalScrollViewport", "tag"), ScrollViewportTag.DELTA, true);
|
|
363
|
+
// The value union has `delta: intptr_t` at offset 8 in the wasm32 ABI.
|
|
364
|
+
view.setInt32(fieldOffset(layouts, "GhosttyTerminalScrollViewport", "value"), delta, true);
|
|
365
|
+
this.ghostty.exports.ghostty_terminal_scroll_viewport(this.handle, ptr);
|
|
366
|
+
this.ghostty.exports.ghostty_wasm_free_u8_array(ptr, size);
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Scroll to an absolute row offset from the top of the scrollable area.
|
|
370
|
+
* Mirrors `ghostty_terminal_scroll_viewport` with the ROW tag.
|
|
371
|
+
*/
|
|
372
|
+
scrollViewportRow(row) {
|
|
373
|
+
this.assertAlive();
|
|
374
|
+
if (!Number.isInteger(row) || row < 0) {
|
|
375
|
+
throw new RangeError('scroll viewport row must be a non-negative integer');
|
|
376
|
+
}
|
|
377
|
+
this.scrollViewport(ScrollViewportTag.ROW, row);
|
|
378
|
+
}
|
|
379
|
+
/** Restore the viewport to the active bottom. */
|
|
380
|
+
scrollViewportBottom() {
|
|
381
|
+
this.assertAlive();
|
|
382
|
+
this.scrollViewport(ScrollViewportTag.BOTTOM, 0);
|
|
383
|
+
}
|
|
384
|
+
scrollViewport(tag, value) {
|
|
385
|
+
const layouts = this.ghostty.typeLayouts;
|
|
386
|
+
const size = structSize(layouts, "GhosttyTerminalScrollViewport");
|
|
387
|
+
const ptr = allocZeroed(this.ghostty.exports, size);
|
|
388
|
+
const view = new DataView(this.ghostty.exports.memory.buffer, ptr, size);
|
|
389
|
+
view.setUint32(fieldOffset(layouts, "GhosttyTerminalScrollViewport", "tag"), tag, true);
|
|
390
|
+
view.setUint32(fieldOffset(layouts, "GhosttyTerminalScrollViewport", "value"), value, true);
|
|
391
|
+
this.ghostty.exports.ghostty_terminal_scroll_viewport(this.handle, ptr);
|
|
392
|
+
this.ghostty.exports.ghostty_wasm_free_u8_array(ptr, size);
|
|
393
|
+
}
|
|
394
|
+
// ─── Grid Ref ──────────────────────────────────────────────────────────
|
|
395
|
+
/**
|
|
396
|
+
* Resolve a {@link Point} to a {@link GridRef} for random-access cell/row reads.
|
|
397
|
+
*
|
|
398
|
+
* Mirrors `ghostty_terminal_grid_ref(term, point, &outRef)` from `terminal.h`.
|
|
399
|
+
* Returns `null` if the point is out of bounds.
|
|
400
|
+
*
|
|
401
|
+
* Per `grid_ref.h`: the resulting ref is only valid until the next mutating
|
|
402
|
+
* terminal call. Callers must read everything they need immediately and
|
|
403
|
+
* call `gridRef.dispose()` when done (which frees the WASM-side struct).
|
|
404
|
+
*/
|
|
405
|
+
gridRef(point) {
|
|
406
|
+
this.assertAlive();
|
|
407
|
+
return GridRef.fromPoint(this, point);
|
|
408
|
+
}
|
|
409
|
+
// ─── Default color theme (terminal_set) ────────────────────────────────
|
|
410
|
+
/**
|
|
411
|
+
* Set the default foreground color. Mirrors
|
|
412
|
+
* `ghostty_terminal_set(term, GHOSTTY_TERMINAL_OPT_COLOR_FOREGROUND, &rgb)`
|
|
413
|
+
* from `terminal.h`. Pass `null` to clear.
|
|
414
|
+
*/
|
|
415
|
+
setForegroundColor(rgb) {
|
|
416
|
+
this.setColorOption(TerminalOption.COLOR_FOREGROUND, rgb);
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Set the default background color. Mirrors
|
|
420
|
+
* `ghostty_terminal_set(term, GHOSTTY_TERMINAL_OPT_COLOR_BACKGROUND, &rgb)`.
|
|
421
|
+
*/
|
|
422
|
+
setBackgroundColor(rgb) {
|
|
423
|
+
this.setColorOption(TerminalOption.COLOR_BACKGROUND, rgb);
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Set the default cursor color. Mirrors
|
|
427
|
+
* `ghostty_terminal_set(term, GHOSTTY_TERMINAL_OPT_COLOR_CURSOR, &rgb)`.
|
|
428
|
+
*/
|
|
429
|
+
setCursorColor(rgb) {
|
|
430
|
+
this.setColorOption(TerminalOption.COLOR_CURSOR, rgb);
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Set the default 256-color palette. Mirrors
|
|
434
|
+
* `ghostty_terminal_set(term, GHOSTTY_TERMINAL_OPT_COLOR_PALETTE, rgb[256])`.
|
|
435
|
+
* Pass `null` to reset to the built-in palette.
|
|
436
|
+
*
|
|
437
|
+
* Per the header: `null` resets while preserving any per-index OSC overrides;
|
|
438
|
+
* setting an array updates only unmodified indices.
|
|
439
|
+
*/
|
|
440
|
+
setPalette(palette) {
|
|
441
|
+
this.assertAlive();
|
|
442
|
+
if (palette === null) {
|
|
443
|
+
this.ghostty.exports.ghostty_terminal_set(this.handle, TerminalOption.COLOR_PALETTE, 0);
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
if (palette.length !== 256) {
|
|
447
|
+
throw new Error(`Palette must have 256 entries, got ${palette.length}`);
|
|
448
|
+
}
|
|
449
|
+
const ptr = this.ghostty.exports.ghostty_wasm_alloc_u8_array(256 * 3);
|
|
450
|
+
const bytes = new Uint8Array(this.ghostty.exports.memory.buffer);
|
|
451
|
+
for (let i = 0; i < 256; i++) {
|
|
452
|
+
const c = palette[i];
|
|
453
|
+
bytes[ptr + i * 3] = c.r & 0xff;
|
|
454
|
+
bytes[ptr + i * 3 + 1] = c.g & 0xff;
|
|
455
|
+
bytes[ptr + i * 3 + 2] = c.b & 0xff;
|
|
456
|
+
}
|
|
457
|
+
this.ghostty.exports.ghostty_terminal_set(this.handle, TerminalOption.COLOR_PALETTE, ptr);
|
|
458
|
+
this.ghostty.exports.ghostty_wasm_free_u8_array(ptr, 256 * 3);
|
|
459
|
+
}
|
|
460
|
+
setColorOption(option, rgb) {
|
|
461
|
+
this.assertAlive();
|
|
462
|
+
if (rgb === null) {
|
|
463
|
+
this.ghostty.exports.ghostty_terminal_set(this.handle, option, 0);
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
const ptr = this.ghostty.exports.ghostty_wasm_alloc_u8_array(3);
|
|
467
|
+
const bytes = new Uint8Array(this.ghostty.exports.memory.buffer);
|
|
468
|
+
bytes[ptr] = rgb.r & 0xff;
|
|
469
|
+
bytes[ptr + 1] = rgb.g & 0xff;
|
|
470
|
+
bytes[ptr + 2] = rgb.b & 0xff;
|
|
471
|
+
this.ghostty.exports.ghostty_terminal_set(this.handle, option, ptr);
|
|
472
|
+
this.ghostty.exports.ghostty_wasm_free_u8_array(ptr, 3);
|
|
473
|
+
}
|
|
474
|
+
// ─── RenderState (lazy) ────────────────────────────────────────────────
|
|
475
|
+
/**
|
|
476
|
+
* The render state bound to this terminal. Allocated on first access and
|
|
477
|
+
* freed when the terminal is disposed.
|
|
478
|
+
*/
|
|
479
|
+
get renderState() {
|
|
480
|
+
if (!this._renderState) {
|
|
481
|
+
this._renderState = new RenderState({
|
|
482
|
+
exports: this.ghostty.exports,
|
|
483
|
+
typeLayouts: this.ghostty.typeLayouts,
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
return this._renderState;
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Sync the render state from the terminal. Mirrors
|
|
490
|
+
* `ghostty_render_state_update(state, terminal)` from `render.h`. The render
|
|
491
|
+
* state is allocated lazily on first call.
|
|
492
|
+
*/
|
|
493
|
+
updateRenderState() {
|
|
494
|
+
return this.renderState.update(this.handle);
|
|
495
|
+
}
|
|
496
|
+
// ─── Renderer-facing convenience ─────────────────────────────────────────────
|
|
497
|
+
//
|
|
498
|
+
// These methods convert the Ghostty-native APIs (RenderState, GridRef, modes)
|
|
499
|
+
// into the flat shapes that the canvas renderer/buffer/selection-manager
|
|
500
|
+
// already speak (`GhosttyCell[]`, `{x, y, visible}`, etc.). They are NOT
|
|
501
|
+
// mirrors of any C function; each one cites the underlying calls.
|
|
502
|
+
/** Sync the render state and return its dirty kind. Wraps `updateRenderState`. */
|
|
503
|
+
update() {
|
|
504
|
+
return this.updateRenderState();
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* Begin a render frame. Re-snapshots the render state so subsequent reads
|
|
508
|
+
* (`getCursor`, `getColors`, `getLine`, `isRowDirty`) return values consistent
|
|
509
|
+
* with the same point in time.
|
|
510
|
+
*
|
|
511
|
+
* VT_NOTE: explicit framing replaces the previous arrangement where
|
|
512
|
+
* `getCursor()` carried a hidden `updateRenderState()` side effect — that
|
|
513
|
+
* coupled "I want the cursor" with "refresh the snapshot" and made the
|
|
514
|
+
* lifecycle invisible at the call site. Now the renderer's frame loop owns
|
|
515
|
+
* the contract: `beginFrame()` → reads → `endFrame()`.
|
|
516
|
+
*
|
|
517
|
+
* Mirrors `ghostty_render_state_update(state, term)` from `render.h`.
|
|
518
|
+
*/
|
|
519
|
+
beginFrame() {
|
|
520
|
+
return this.updateRenderState();
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* End a render frame. Clears the global + per-row dirty flags.
|
|
524
|
+
*
|
|
525
|
+
* Mirrors `ghostty_render_state_set(state, DIRTY, &NONE)` plus a per-row
|
|
526
|
+
* iterator walk that clears `GHOSTTY_RENDER_STATE_ROW_OPTION_DIRTY`.
|
|
527
|
+
*/
|
|
528
|
+
endFrame() {
|
|
529
|
+
if (!this._renderState)
|
|
530
|
+
return;
|
|
531
|
+
this._renderState.clearDirty();
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Cursor x/y/visible from the most recent render-state snapshot.
|
|
535
|
+
*
|
|
536
|
+
* VT_NOTE: pure read. Out-of-frame callers (e.g. one-off cursor probes
|
|
537
|
+
* after `write()`) get a stale snapshot if they haven't called
|
|
538
|
+
* `beginFrame()` yet — that's intentional. The renderer's frame loop
|
|
539
|
+
* is the single owner of snapshot freshness; sprinkling implicit
|
|
540
|
+
* `update()` calls inside getters is exactly the asymmetry we removed.
|
|
541
|
+
*/
|
|
542
|
+
getCursor() {
|
|
543
|
+
if (!this._renderState)
|
|
544
|
+
this.updateRenderState();
|
|
545
|
+
const c = this.renderState.cursor;
|
|
546
|
+
return { x: c.x, y: c.y, visible: c.visible && c.hasValue };
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Viewport `{ cols, rows }`.
|
|
550
|
+
*
|
|
551
|
+
* Prefers the render-state snapshot (the authoritative view at frame time)
|
|
552
|
+
* and falls back to the live terminal dims before the first frame.
|
|
553
|
+
*/
|
|
554
|
+
getDimensions() {
|
|
555
|
+
if (this._renderState && this._renderState.cols > 0) {
|
|
556
|
+
return { cols: this._renderState.cols, rows: this._renderState.rows };
|
|
557
|
+
}
|
|
558
|
+
return { cols: this._cols, rows: this._rows };
|
|
559
|
+
}
|
|
560
|
+
/** True if the render state's last update marked any dirty rows or full. */
|
|
561
|
+
isDirty() {
|
|
562
|
+
if (!this._renderState)
|
|
563
|
+
return true;
|
|
564
|
+
return this.renderState.dirty !== RenderStateDirty.NONE;
|
|
565
|
+
}
|
|
566
|
+
/** True if the render state's last update marked the whole frame dirty. */
|
|
567
|
+
needsFullRedraw() {
|
|
568
|
+
if (!this._renderState)
|
|
569
|
+
return true;
|
|
570
|
+
return this.renderState.dirty === RenderStateDirty.FULL;
|
|
571
|
+
}
|
|
572
|
+
/** True if row `y` is dirty (cached from last update). */
|
|
573
|
+
isRowDirty(y) {
|
|
574
|
+
if (!this._renderState)
|
|
575
|
+
return true;
|
|
576
|
+
return this.renderState.isRowDirty(y);
|
|
577
|
+
}
|
|
578
|
+
/** Clear all dirty flags. Call after the renderer has painted the frame. */
|
|
579
|
+
clearDirty() {
|
|
580
|
+
if (!this._renderState)
|
|
581
|
+
return;
|
|
582
|
+
this.renderState.clearDirty();
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* Get the cells of viewport row `y` in the legacy packed-RGB shape.
|
|
586
|
+
*
|
|
587
|
+
* VT_NOTE: caches the rows during the last `update()`. If the consumer
|
|
588
|
+
* calls `getLine()` before any `update()`, we trigger one to populate.
|
|
589
|
+
*/
|
|
590
|
+
getLine(y) {
|
|
591
|
+
// Bound by the snapshot's rows, not the live terminal handle's. Mid-resize
|
|
592
|
+
// they can disagree; the renderer paints snapshot-sized frames, so the
|
|
593
|
+
// bounds check has to use the snapshot too. Without this, a stale row at
|
|
594
|
+
// index `snapshot.rows ≤ y < live.rows` returns `null` and the renderer
|
|
595
|
+
// skips it, leaving previous-frame content visible (e.g. a dropdown that
|
|
596
|
+
// doesn't redraw when the agent changes its dropdown size).
|
|
597
|
+
if (!this._renderState)
|
|
598
|
+
this.updateRenderState();
|
|
599
|
+
if (y < 0 || y >= this.renderState.rows)
|
|
600
|
+
return null;
|
|
601
|
+
const cells = this.renderState.getRow(y);
|
|
602
|
+
if (!cells)
|
|
603
|
+
return null;
|
|
604
|
+
const colors = this.renderState.colors;
|
|
605
|
+
return cells.map((c) => toGhosttyCell(c, colors.foreground, colors.background));
|
|
606
|
+
}
|
|
607
|
+
/** Effective foreground/background/cursor colors from the render state. */
|
|
608
|
+
getColors() {
|
|
609
|
+
if (!this._renderState)
|
|
610
|
+
this.updateRenderState();
|
|
611
|
+
const c = this.renderState.colors;
|
|
612
|
+
return {
|
|
613
|
+
background: c.background,
|
|
614
|
+
foreground: c.foreground,
|
|
615
|
+
cursor: c.cursorHasValue ? c.cursor : null,
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
// ─── Mode shortcuts ──────────────────────────────────────────────────────────
|
|
619
|
+
/** Bracketed-paste mode (DEC 2004). */
|
|
620
|
+
hasBracketedPaste() {
|
|
621
|
+
return this.getMode(VtModes.BRACKETED_PASTE);
|
|
622
|
+
}
|
|
623
|
+
/** Focus-event reporting (DEC 1004). */
|
|
624
|
+
hasFocusEvents() {
|
|
625
|
+
return this.getMode(VtModes.FOCUS_EVENT);
|
|
626
|
+
}
|
|
627
|
+
// ─── Scrollback (gridRef-backed) ─────────────────────────────────────────────
|
|
628
|
+
/**
|
|
629
|
+
* Number of scrollback rows. Mirrors
|
|
630
|
+
* `ghostty_terminal_get(term, GHOSTTY_TERMINAL_DATA_SCROLLBACK_ROWS, &out)`.
|
|
631
|
+
*/
|
|
632
|
+
getScrollbackLength() {
|
|
633
|
+
return this.scrollbackRows();
|
|
634
|
+
}
|
|
635
|
+
/**
|
|
636
|
+
* Get cells for scrollback line `offset` (0 = oldest), in the legacy packed-RGB shape.
|
|
637
|
+
*
|
|
638
|
+
* Walks {@link gridRef} cell-by-cell — slower than the active-screen path but
|
|
639
|
+
* fine for the JS-side selection/link/copy code paths that need it.
|
|
640
|
+
*/
|
|
641
|
+
getScrollbackLine(offset) {
|
|
642
|
+
return this.readScrollbackRow(offset);
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* Whether viewport row `y` is soft-wrapped. Reads via the render state row
|
|
646
|
+
* iterator (`GHOSTTY_RENDER_STATE_ROW_DATA_RAW` → `ghostty_row_get(WRAP)`).
|
|
647
|
+
*/
|
|
648
|
+
isRowWrapped(_y) {
|
|
649
|
+
// VT_NOTE: row WRAP would require the row iterator to land on `y` and call
|
|
650
|
+
// `ghostty_row_get(rawRow, WRAP, &out)`. The render-state row iterator only
|
|
651
|
+
// walks forward; for now we return false (matches upstream's behavior for
|
|
652
|
+
// scrollback where they also returned false). A future pass can wire this
|
|
653
|
+
// up by extending RenderState's per-row cache to record the WRAP flag.
|
|
654
|
+
return false;
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* Get the full grapheme string at viewport (row, col). Wraps a {@link GridRef}.
|
|
658
|
+
*/
|
|
659
|
+
getGraphemeString(row, col) {
|
|
660
|
+
const ref = this.gridRef({ tag: PointTag.VIEWPORT, x: col, y: row });
|
|
661
|
+
if (!ref)
|
|
662
|
+
return ' ';
|
|
663
|
+
try {
|
|
664
|
+
const s = ref.graphemes();
|
|
665
|
+
return s.length === 0 ? ' ' : s;
|
|
666
|
+
}
|
|
667
|
+
finally {
|
|
668
|
+
ref.dispose();
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
/** Get the full grapheme string at scrollback (offset, col). */
|
|
672
|
+
getScrollbackGraphemeString(offset, col) {
|
|
673
|
+
const ref = this.gridRef({ tag: PointTag.HISTORY, x: col, y: offset });
|
|
674
|
+
if (!ref)
|
|
675
|
+
return ' ';
|
|
676
|
+
try {
|
|
677
|
+
const s = ref.graphemes();
|
|
678
|
+
return s.length === 0 ? ' ' : s;
|
|
679
|
+
}
|
|
680
|
+
finally {
|
|
681
|
+
ref.dispose();
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
/** Get the OSC 8 hyperlink URI at viewport (row, col), or `null`. */
|
|
685
|
+
getHyperlinkUri(row, col) {
|
|
686
|
+
const ref = this.gridRef({ tag: PointTag.VIEWPORT, x: col, y: row });
|
|
687
|
+
if (!ref)
|
|
688
|
+
return null;
|
|
689
|
+
try {
|
|
690
|
+
return ref.hyperlinkUri();
|
|
691
|
+
}
|
|
692
|
+
finally {
|
|
693
|
+
ref.dispose();
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
/** Get the OSC 8 hyperlink URI at scrollback (offset, col), or `null`. */
|
|
697
|
+
getScrollbackHyperlinkUri(offset, col) {
|
|
698
|
+
const ref = this.gridRef({ tag: PointTag.HISTORY, x: col, y: offset });
|
|
699
|
+
if (!ref)
|
|
700
|
+
return null;
|
|
701
|
+
try {
|
|
702
|
+
return ref.hyperlinkUri();
|
|
703
|
+
}
|
|
704
|
+
finally {
|
|
705
|
+
ref.dispose();
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
// ─── PTY response surface ─────────────────────────────────────────────────
|
|
709
|
+
//
|
|
710
|
+
// VT_NOTE: libghostty-vt delivers DSR / DA1 / DA2 / DA3 / XTVERSION /
|
|
711
|
+
// XTWINOPS-size / DECRPM responses *only* through the
|
|
712
|
+
// `GHOSTTY_TERMINAL_OPT_WRITE_PTY` effect callback (`terminal.h:60-95,
|
|
713
|
+
// 354-372`). We install that callback (and BELL / TITLE_CHANGED) from JS
|
|
714
|
+
// via the synthetic-module function-table trick — `WebAssembly.Function`
|
|
715
|
+
// is NOT required (see `../wasm/fn-table.ts` for the mechanism and
|
|
716
|
+
// `./vt/effects.ts` for the effect contract). Responses flow outward
|
|
717
|
+
// through the high-level Terminal's `onData` to the PTY.
|
|
718
|
+
//
|
|
719
|
+
// The polling getters above (`getTitle`/`getPwd`/`hasMouseTracking`) remain
|
|
720
|
+
// the read surface for state that has no effect callback at our pinned
|
|
721
|
+
// vendor commit (pwd, mouse tracking) and as the fetch mechanism for
|
|
722
|
+
// TITLE_CHANGED (the effect signals *that* the title changed; the getter
|
|
723
|
+
// reads it).
|
|
724
|
+
/** Alias for {@link dispose}. Kept for backwards compatibility with consumers. */
|
|
725
|
+
free() {
|
|
726
|
+
this.dispose();
|
|
727
|
+
}
|
|
728
|
+
// ─── Internal helpers used by the renderer-facing convenience methods ────────
|
|
729
|
+
/**
|
|
730
|
+
* Memo for {@link readScrollbackRow}, keyed by scrollback offset.
|
|
731
|
+
*
|
|
732
|
+
* Scrollback is immutable except when a WRITE occurs (new lines push, and at
|
|
733
|
+
* max capacity eviction shifts every offset) — so the whole cache is
|
|
734
|
+
* invalidated on write and on reset, and never anywhere else. Scrolling
|
|
735
|
+
* through static history (the common case) then costs zero wasm calls per
|
|
736
|
+
* frame; without this, a scrolled viewport re-walked every visible cell
|
|
737
|
+
* through ~5 FFI calls each, every frame (~30k calls/frame at 137×41).
|
|
738
|
+
*
|
|
739
|
+
* Bounded: cleared wholesale past {@link SCROLLBACK_CACHE_MAX_ROWS} rather
|
|
740
|
+
* than LRU-tracked — a full clear is cheap and the refill is one frame.
|
|
741
|
+
*/
|
|
742
|
+
scrollbackRowCache = new Map();
|
|
743
|
+
static SCROLLBACK_CACHE_MAX_ROWS = 2000;
|
|
744
|
+
/** @internal Called on any state-mutating write/reset — see {@link scrollbackRowCache}. */
|
|
745
|
+
invalidateScrollbackCache() {
|
|
746
|
+
this.scrollbackRowCache.clear();
|
|
747
|
+
}
|
|
748
|
+
readScrollbackRow(offset) {
|
|
749
|
+
const cached = this.scrollbackRowCache.get(offset);
|
|
750
|
+
if (cached !== undefined)
|
|
751
|
+
return cached;
|
|
752
|
+
const row = this.readScrollbackRowUncached(offset);
|
|
753
|
+
if (this.scrollbackRowCache.size >= Terminal.SCROLLBACK_CACHE_MAX_ROWS) {
|
|
754
|
+
this.scrollbackRowCache.clear();
|
|
755
|
+
}
|
|
756
|
+
this.scrollbackRowCache.set(offset, row);
|
|
757
|
+
return row;
|
|
758
|
+
}
|
|
759
|
+
readScrollbackRowUncached(offset) {
|
|
760
|
+
const cells = [];
|
|
761
|
+
// Resolved theme defaults — used for cells where the style doesn't pin an
|
|
762
|
+
// explicit color. Without this, scrollback rows would render as #cccccc on
|
|
763
|
+
// any theme that isn't VS Code's dark default (i.e. invisible on light).
|
|
764
|
+
if (!this._renderState)
|
|
765
|
+
this.updateRenderState();
|
|
766
|
+
const { foreground: defaultFg, background: defaultBg, palette } = this.renderState.colors;
|
|
767
|
+
for (let x = 0; x < this._cols; x++) {
|
|
768
|
+
const ref = this.gridRef({ tag: PointTag.HISTORY, x, y: offset });
|
|
769
|
+
if (!ref) {
|
|
770
|
+
if (x === 0)
|
|
771
|
+
return null;
|
|
772
|
+
// Past the row's last cell — pad with empty cells.
|
|
773
|
+
for (let i = x; i < this._cols; i++) {
|
|
774
|
+
cells.push({
|
|
775
|
+
codepoint: 0,
|
|
776
|
+
fg_r: defaultFg.r, fg_g: defaultFg.g, fg_b: defaultFg.b,
|
|
777
|
+
bg_r: defaultBg.r, bg_g: defaultBg.g, bg_b: defaultBg.b,
|
|
778
|
+
flags: 0, width: 1, hyperlink_id: 0, grapheme_len: 0,
|
|
779
|
+
});
|
|
780
|
+
}
|
|
781
|
+
return cells;
|
|
782
|
+
}
|
|
783
|
+
try {
|
|
784
|
+
const raw = ref.rawCell();
|
|
785
|
+
const style = ref.style();
|
|
786
|
+
const uri = ref.hyperlinkUri();
|
|
787
|
+
const gph = ref.graphemes();
|
|
788
|
+
let codepoint = 0;
|
|
789
|
+
let width = 1;
|
|
790
|
+
if (raw !== null) {
|
|
791
|
+
// Decode the raw u64 cell via cell_get for codepoint and wide.
|
|
792
|
+
const memory = this.ghostty.exports.memory;
|
|
793
|
+
const u32Ptr = this.scratchU32Ptr();
|
|
794
|
+
writeU32(memory, u32Ptr, 0);
|
|
795
|
+
if (this.ghostty.exports.ghostty_cell_get(raw, /* CODEPOINT */ 1, u32Ptr) === GhosttyResult.SUCCESS) {
|
|
796
|
+
codepoint = readU32(memory, u32Ptr);
|
|
797
|
+
}
|
|
798
|
+
writeU32(memory, u32Ptr, 0);
|
|
799
|
+
if (this.ghostty.exports.ghostty_cell_get(raw, /* WIDE */ 3, u32Ptr) === GhosttyResult.SUCCESS) {
|
|
800
|
+
const w = readU32(memory, u32Ptr);
|
|
801
|
+
width = w === CellWide.WIDE ? 2 : (w === CellWide.SPACER_TAIL || w === CellWide.SPACER_HEAD) ? 0 : 1;
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
const flags = style ? styleToFlags(style) : 0;
|
|
805
|
+
// Resolve the style's tagged colors exactly like the active-screen
|
|
806
|
+
// path: RGB directly, PALETTE via the render state's 256-entry
|
|
807
|
+
// palette, NONE falls back to the resolved theme defaults. This used
|
|
808
|
+
// to substitute theme defaults for EVERYTHING (the row reader was
|
|
809
|
+
// written for selection/copy, which only reads codepoints) — once the
|
|
810
|
+
// renderer started painting scrollback through it, all history lost
|
|
811
|
+
// its colors while the live screen kept them.
|
|
812
|
+
const fg = resolveStyleColor(style?.fgColor, palette, defaultFg);
|
|
813
|
+
const bg = resolveStyleColor(style?.bgColor, palette, defaultBg);
|
|
814
|
+
cells.push({
|
|
815
|
+
codepoint: codepoint,
|
|
816
|
+
fg_r: fg.r, fg_g: fg.g, fg_b: fg.b,
|
|
817
|
+
bg_r: bg.r, bg_g: bg.g, bg_b: bg.b,
|
|
818
|
+
flags,
|
|
819
|
+
width,
|
|
820
|
+
hyperlink_id: uri ? 1 : 0,
|
|
821
|
+
grapheme_len: gph.length > 1 ? gph.length - 1 : 0,
|
|
822
|
+
});
|
|
823
|
+
}
|
|
824
|
+
finally {
|
|
825
|
+
ref.dispose();
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
return cells;
|
|
829
|
+
}
|
|
830
|
+
// ─── Internal helpers ──────────────────────────────────────────────────
|
|
831
|
+
assertAlive() {
|
|
832
|
+
if (this._disposed)
|
|
833
|
+
throw new Error("Terminal has been disposed");
|
|
834
|
+
}
|
|
835
|
+
scratchU8Ptr() {
|
|
836
|
+
return this.scratch.u8();
|
|
837
|
+
}
|
|
838
|
+
scratchU16Ptr() {
|
|
839
|
+
return this.scratch.u16();
|
|
840
|
+
}
|
|
841
|
+
scratchU32Ptr() {
|
|
842
|
+
return this.scratch.u32();
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
// =============================================================================
|
|
846
|
+
// GridRef — wraps a sized `GhosttyGridRef` struct
|
|
847
|
+
// =============================================================================
|
|
848
|
+
/**
|
|
849
|
+
* A resolved reference to a cell in the terminal grid.
|
|
850
|
+
*
|
|
851
|
+
* Owns a sized `GhosttyGridRef` struct in WASM memory. Used for random-access
|
|
852
|
+
* reads outside the active viewport (e.g. scrollback inspection, hyperlink URI
|
|
853
|
+
* lookups). For per-frame rendering, prefer the {@link RenderState} API —
|
|
854
|
+
* grid refs are not tuned for high-frequency use.
|
|
855
|
+
*
|
|
856
|
+
* Per `grid_ref.h`: the ref is invalidated by ANY mutating terminal call.
|
|
857
|
+
* Read what you need immediately and call `dispose()` to free the struct.
|
|
858
|
+
*/
|
|
859
|
+
export class GridRef {
|
|
860
|
+
ghostty;
|
|
861
|
+
/** @internal Pointer to the WASM-side `GhosttyGridRef` struct. */
|
|
862
|
+
ptr;
|
|
863
|
+
/** @internal Allocated size of the struct (for the eventual free()). */
|
|
864
|
+
size;
|
|
865
|
+
_disposed = false;
|
|
866
|
+
constructor(ghostty, ptr, size) {
|
|
867
|
+
this.ghostty = ghostty;
|
|
868
|
+
this.ptr = ptr;
|
|
869
|
+
this.size = size;
|
|
870
|
+
}
|
|
871
|
+
/**
|
|
872
|
+
* Resolve a {@link Point} to a `GhosttyGridRef`. Mirrors `ghostty_terminal_grid_ref`.
|
|
873
|
+
*
|
|
874
|
+
* Returns `null` if the point is out of bounds (matches C `INVALID_VALUE`).
|
|
875
|
+
*/
|
|
876
|
+
static fromPoint(terminal, point) {
|
|
877
|
+
const ghostty = terminal._ghostty;
|
|
878
|
+
const layouts = ghostty.typeLayouts;
|
|
879
|
+
const exports = ghostty.exports;
|
|
880
|
+
// Build GhosttyPoint in WASM memory.
|
|
881
|
+
const pointSize = structSize(layouts, "GhosttyPoint");
|
|
882
|
+
const pointPtr = allocZeroed(exports, pointSize);
|
|
883
|
+
const view = new DataView(exports.memory.buffer);
|
|
884
|
+
view.setUint32(pointPtr + fieldOffset(layouts, "GhosttyPoint", "tag"), point.tag, true);
|
|
885
|
+
// GhosttyPointCoordinate is the first union member at the value offset.
|
|
886
|
+
const valueOff = fieldOffset(layouts, "GhosttyPoint", "value");
|
|
887
|
+
view.setUint16(pointPtr + valueOff + fieldOffset(layouts, "GhosttyPointCoordinate", "x"), point.x, true);
|
|
888
|
+
view.setUint32(pointPtr + valueOff + fieldOffset(layouts, "GhosttyPointCoordinate", "y"), point.y, true);
|
|
889
|
+
// Allocate output GhosttyGridRef and set its size field.
|
|
890
|
+
const refSize = structSize(layouts, "GhosttyGridRef");
|
|
891
|
+
const refPtr = allocZeroed(exports, refSize);
|
|
892
|
+
view.setUint32(refPtr + fieldOffset(layouts, "GhosttyGridRef", "size"), refSize, true);
|
|
893
|
+
const result = exports.ghostty_terminal_grid_ref(terminal.handle, pointPtr, refPtr);
|
|
894
|
+
exports.ghostty_wasm_free_u8_array(pointPtr, pointSize);
|
|
895
|
+
if (result !== GhosttyResult.SUCCESS) {
|
|
896
|
+
exports.ghostty_wasm_free_u8_array(refPtr, refSize);
|
|
897
|
+
return null;
|
|
898
|
+
}
|
|
899
|
+
return new GridRef(ghostty, refPtr, refSize);
|
|
900
|
+
}
|
|
901
|
+
/**
|
|
902
|
+
* Get the cell at this grid reference as a u64 opaque value, or `null` if
|
|
903
|
+
* the ref is unset. Mirrors `ghostty_grid_ref_cell` from `grid_ref.h`.
|
|
904
|
+
*/
|
|
905
|
+
rawCell() {
|
|
906
|
+
this.assertAlive();
|
|
907
|
+
const outPtr = this.ghostty.exports.ghostty_wasm_alloc_u8_array(8);
|
|
908
|
+
const result = this.ghostty.exports.ghostty_grid_ref_cell(this.ptr, outPtr);
|
|
909
|
+
if (result !== GhosttyResult.SUCCESS) {
|
|
910
|
+
this.ghostty.exports.ghostty_wasm_free_u8_array(outPtr, 8);
|
|
911
|
+
return null;
|
|
912
|
+
}
|
|
913
|
+
const value = readU64(this.ghostty.exports.memory, outPtr);
|
|
914
|
+
this.ghostty.exports.ghostty_wasm_free_u8_array(outPtr, 8);
|
|
915
|
+
return value;
|
|
916
|
+
}
|
|
917
|
+
/**
|
|
918
|
+
* Get the row at this grid reference as a u64 opaque value, or `null`.
|
|
919
|
+
* Mirrors `ghostty_grid_ref_row` from `grid_ref.h`.
|
|
920
|
+
*/
|
|
921
|
+
rawRow() {
|
|
922
|
+
this.assertAlive();
|
|
923
|
+
const outPtr = this.ghostty.exports.ghostty_wasm_alloc_u8_array(8);
|
|
924
|
+
const result = this.ghostty.exports.ghostty_grid_ref_row(this.ptr, outPtr);
|
|
925
|
+
if (result !== GhosttyResult.SUCCESS) {
|
|
926
|
+
this.ghostty.exports.ghostty_wasm_free_u8_array(outPtr, 8);
|
|
927
|
+
return null;
|
|
928
|
+
}
|
|
929
|
+
const value = readU64(this.ghostty.exports.memory, outPtr);
|
|
930
|
+
this.ghostty.exports.ghostty_wasm_free_u8_array(outPtr, 8);
|
|
931
|
+
return value;
|
|
932
|
+
}
|
|
933
|
+
/**
|
|
934
|
+
* Read all grapheme codepoints at this grid reference into a string.
|
|
935
|
+
*
|
|
936
|
+
* Mirrors `ghostty_grid_ref_graphemes(ref, buf, len, &out_len)` from
|
|
937
|
+
* `grid_ref.h`, with automatic buffer growth. Returns `""` for empty cells.
|
|
938
|
+
*/
|
|
939
|
+
graphemes() {
|
|
940
|
+
this.assertAlive();
|
|
941
|
+
return this.readGraphemesAsString();
|
|
942
|
+
}
|
|
943
|
+
/**
|
|
944
|
+
* Get the hyperlink URI at this grid reference, or `null` if no hyperlink.
|
|
945
|
+
*
|
|
946
|
+
* Mirrors `ghostty_grid_ref_hyperlink_uri(ref, buf, len, &out_len)` from
|
|
947
|
+
* `grid_ref.h`, with automatic buffer growth.
|
|
948
|
+
*/
|
|
949
|
+
hyperlinkUri() {
|
|
950
|
+
this.assertAlive();
|
|
951
|
+
const exports = this.ghostty.exports;
|
|
952
|
+
const memory = exports.memory;
|
|
953
|
+
let cap = 256;
|
|
954
|
+
for (let attempt = 0; attempt < 4; attempt++) {
|
|
955
|
+
const buf = exports.ghostty_wasm_alloc_u8_array(cap);
|
|
956
|
+
const lenPtr = exports.ghostty_wasm_alloc_usize();
|
|
957
|
+
writeU32(memory, lenPtr, 0);
|
|
958
|
+
const result = exports.ghostty_grid_ref_hyperlink_uri(this.ptr, buf, cap, lenPtr);
|
|
959
|
+
const len = readU32(memory, lenPtr);
|
|
960
|
+
exports.ghostty_wasm_free_usize(lenPtr);
|
|
961
|
+
if (result === GhosttyResult.SUCCESS) {
|
|
962
|
+
if (len === 0) {
|
|
963
|
+
exports.ghostty_wasm_free_u8_array(buf, cap);
|
|
964
|
+
return null;
|
|
965
|
+
}
|
|
966
|
+
const decoded = new TextDecoder().decode(new Uint8Array(memory.buffer, buf, len));
|
|
967
|
+
exports.ghostty_wasm_free_u8_array(buf, cap);
|
|
968
|
+
return decoded;
|
|
969
|
+
}
|
|
970
|
+
exports.ghostty_wasm_free_u8_array(buf, cap);
|
|
971
|
+
if (result === GhosttyResult.OUT_OF_SPACE && len > cap) {
|
|
972
|
+
cap = Math.max(cap * 2, len);
|
|
973
|
+
continue;
|
|
974
|
+
}
|
|
975
|
+
return null;
|
|
976
|
+
}
|
|
977
|
+
return null;
|
|
978
|
+
}
|
|
979
|
+
/**
|
|
980
|
+
* Read the cell style at this grid reference. Mirrors `ghostty_grid_ref_style`.
|
|
981
|
+
*/
|
|
982
|
+
style() {
|
|
983
|
+
this.assertAlive();
|
|
984
|
+
const layouts = this.ghostty.typeLayouts;
|
|
985
|
+
const size = structSize(layouts, "GhosttyStyle");
|
|
986
|
+
const ptr = allocZeroed(this.ghostty.exports, size);
|
|
987
|
+
writeU32(this.ghostty.exports.memory, ptr + fieldOffset(layouts, "GhosttyStyle", "size"), size);
|
|
988
|
+
const result = this.ghostty.exports.ghostty_grid_ref_style(this.ptr, ptr);
|
|
989
|
+
if (result !== GhosttyResult.SUCCESS) {
|
|
990
|
+
this.ghostty.exports.ghostty_wasm_free_u8_array(ptr, size);
|
|
991
|
+
return null;
|
|
992
|
+
}
|
|
993
|
+
const style = readStyle(this.ghostty.exports.memory, layouts, ptr);
|
|
994
|
+
this.ghostty.exports.ghostty_wasm_free_u8_array(ptr, size);
|
|
995
|
+
return style;
|
|
996
|
+
}
|
|
997
|
+
/**
|
|
998
|
+
* Free the underlying WASM struct. Idempotent. After dispose, the GridRef
|
|
999
|
+
* cannot be used.
|
|
1000
|
+
*/
|
|
1001
|
+
dispose() {
|
|
1002
|
+
if (this._disposed)
|
|
1003
|
+
return;
|
|
1004
|
+
this._disposed = true;
|
|
1005
|
+
this.ghostty.exports.ghostty_wasm_free_u8_array(this.ptr, this.size);
|
|
1006
|
+
}
|
|
1007
|
+
assertAlive() {
|
|
1008
|
+
if (this._disposed)
|
|
1009
|
+
throw new Error("GridRef has been disposed");
|
|
1010
|
+
}
|
|
1011
|
+
readGraphemesAsString() {
|
|
1012
|
+
const exports = this.ghostty.exports;
|
|
1013
|
+
const memory = exports.memory;
|
|
1014
|
+
let cap = 8;
|
|
1015
|
+
for (let attempt = 0; attempt < 4; attempt++) {
|
|
1016
|
+
const buf = exports.ghostty_wasm_alloc_u8_array(cap * 4);
|
|
1017
|
+
const lenPtr = exports.ghostty_wasm_alloc_usize();
|
|
1018
|
+
writeU32(memory, lenPtr, 0);
|
|
1019
|
+
const result = exports.ghostty_grid_ref_graphemes(this.ptr, buf, cap, lenPtr);
|
|
1020
|
+
const len = readU32(memory, lenPtr);
|
|
1021
|
+
exports.ghostty_wasm_free_usize(lenPtr);
|
|
1022
|
+
if (result === GhosttyResult.SUCCESS) {
|
|
1023
|
+
if (len === 0) {
|
|
1024
|
+
exports.ghostty_wasm_free_u8_array(buf, cap * 4);
|
|
1025
|
+
return "";
|
|
1026
|
+
}
|
|
1027
|
+
let s = "";
|
|
1028
|
+
const view = new DataView(memory.buffer);
|
|
1029
|
+
for (let i = 0; i < len; i++) {
|
|
1030
|
+
const cp = view.getUint32(buf + i * 4, true);
|
|
1031
|
+
if (cp > 0 && cp <= 0x10ffff && !(cp >= 0xd800 && cp <= 0xdfff)) {
|
|
1032
|
+
s += String.fromCodePoint(cp);
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
exports.ghostty_wasm_free_u8_array(buf, cap * 4);
|
|
1036
|
+
return s;
|
|
1037
|
+
}
|
|
1038
|
+
exports.ghostty_wasm_free_u8_array(buf, cap * 4);
|
|
1039
|
+
if (result === GhosttyResult.OUT_OF_SPACE && len > cap) {
|
|
1040
|
+
cap = len;
|
|
1041
|
+
continue;
|
|
1042
|
+
}
|
|
1043
|
+
return "";
|
|
1044
|
+
}
|
|
1045
|
+
return "";
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
// =============================================================================
|
|
1049
|
+
// KeyEncoder + KeyEvent — wrap `GhosttyKeyEncoder` / `GhosttyKeyEvent` handles
|
|
1050
|
+
// =============================================================================
|
|
1051
|
+
/**
|
|
1052
|
+
* Key encoder. Wraps a `GhosttyKeyEncoder` opaque handle.
|
|
1053
|
+
*
|
|
1054
|
+
* Mirrors `key/encoder.h`. The encoder owns options (cursor key application,
|
|
1055
|
+
* Kitty flags, etc.) and turns {@link KeyEvent} input into VT escape bytes.
|
|
1056
|
+
*
|
|
1057
|
+
* Recommended flow: configure once, optionally call `setoptFromTerminal` after
|
|
1058
|
+
* a terminal write to pick up mode changes, then `encode()` per key event.
|
|
1059
|
+
*/
|
|
1060
|
+
export class KeyEncoder {
|
|
1061
|
+
ghostty;
|
|
1062
|
+
handle;
|
|
1063
|
+
_disposed = false;
|
|
1064
|
+
/** Reusable scratch slots for setopt and the encode buffer. */
|
|
1065
|
+
optionScratch;
|
|
1066
|
+
encodeBuf;
|
|
1067
|
+
writtenScratch;
|
|
1068
|
+
constructor(ghostty) {
|
|
1069
|
+
this.ghostty = ghostty;
|
|
1070
|
+
const outPtr = ghostty.exports.ghostty_wasm_alloc_opaque();
|
|
1071
|
+
const result = ghostty.exports.ghostty_key_encoder_new(0, outPtr);
|
|
1072
|
+
if (result !== GhosttyResult.SUCCESS) {
|
|
1073
|
+
ghostty.exports.ghostty_wasm_free_opaque(outPtr);
|
|
1074
|
+
throw new Error(`ghostty_key_encoder_new failed: ${result}`);
|
|
1075
|
+
}
|
|
1076
|
+
this.handle = readU32(ghostty.exports.memory, outPtr);
|
|
1077
|
+
ghostty.exports.ghostty_wasm_free_opaque(outPtr);
|
|
1078
|
+
}
|
|
1079
|
+
/**
|
|
1080
|
+
* Set an encoder option. Mirrors `ghostty_key_encoder_setopt(encoder, opt, &value)`
|
|
1081
|
+
* from `key/encoder.h`.
|
|
1082
|
+
*
|
|
1083
|
+
* Booleans are passed as a single byte; numbers are passed as a single u8
|
|
1084
|
+
* (matches the typed options in the header — Kitty flags are u8, cursor
|
|
1085
|
+
* key application is bool, etc.).
|
|
1086
|
+
*/
|
|
1087
|
+
setOption(option, value) {
|
|
1088
|
+
this.assertAlive();
|
|
1089
|
+
const ptr = this.ensureOptionScratch();
|
|
1090
|
+
new Uint8Array(this.ghostty.exports.memory.buffer)[ptr] =
|
|
1091
|
+
typeof value === "boolean" ? (value ? 1 : 0) : value & 0xff;
|
|
1092
|
+
this.ghostty.exports.ghostty_key_encoder_setopt(this.handle, option, ptr);
|
|
1093
|
+
}
|
|
1094
|
+
/**
|
|
1095
|
+
* Sync this encoder's options from a live terminal's mode state.
|
|
1096
|
+
*
|
|
1097
|
+
* Mirrors `ghostty_key_encoder_setopt_from_terminal(encoder, terminal)` from
|
|
1098
|
+
* `key/encoder.h`. Useful when terminal modes change at runtime (cursor
|
|
1099
|
+
* keys, keypad app mode, Kitty flags).
|
|
1100
|
+
*/
|
|
1101
|
+
setoptFromTerminal(terminal) {
|
|
1102
|
+
this.assertAlive();
|
|
1103
|
+
this.ghostty.exports.ghostty_key_encoder_setopt_from_terminal(this.handle, terminal.handle);
|
|
1104
|
+
}
|
|
1105
|
+
/**
|
|
1106
|
+
* Encode a key event into bytes. Mirrors `ghostty_key_encoder_encode(...)`
|
|
1107
|
+
* from `key/encoder.h` plus the key-event setters from `key/event.h`.
|
|
1108
|
+
*
|
|
1109
|
+
* Returns a freshly-allocated `Uint8Array` (caller owns).
|
|
1110
|
+
*/
|
|
1111
|
+
encode(event) {
|
|
1112
|
+
this.assertAlive();
|
|
1113
|
+
const exports = this.ghostty.exports;
|
|
1114
|
+
const memory = exports.memory;
|
|
1115
|
+
// Allocate a key event handle, populate setters, encode, free.
|
|
1116
|
+
const evtOutPtr = exports.ghostty_wasm_alloc_opaque();
|
|
1117
|
+
const newResult = exports.ghostty_key_event_new(0, evtOutPtr);
|
|
1118
|
+
if (newResult !== GhosttyResult.SUCCESS) {
|
|
1119
|
+
exports.ghostty_wasm_free_opaque(evtOutPtr);
|
|
1120
|
+
throw new Error(`ghostty_key_event_new failed: ${newResult}`);
|
|
1121
|
+
}
|
|
1122
|
+
const evt = readU32(memory, evtOutPtr);
|
|
1123
|
+
exports.ghostty_wasm_free_opaque(evtOutPtr);
|
|
1124
|
+
try {
|
|
1125
|
+
exports.ghostty_key_event_set_action(evt, event.action);
|
|
1126
|
+
exports.ghostty_key_event_set_key(evt, event.key);
|
|
1127
|
+
exports.ghostty_key_event_set_mods(evt, event.mods);
|
|
1128
|
+
if (event.consumedMods !== undefined)
|
|
1129
|
+
exports.ghostty_key_event_set_consumed_mods(evt, event.consumedMods);
|
|
1130
|
+
if (event.composing !== undefined)
|
|
1131
|
+
exports.ghostty_key_event_set_composing(evt, event.composing ? 1 : 0);
|
|
1132
|
+
if (event.unshiftedCodepoint !== undefined)
|
|
1133
|
+
exports.ghostty_key_event_set_unshifted_codepoint(evt, event.unshiftedCodepoint);
|
|
1134
|
+
let utf8Ptr = 0;
|
|
1135
|
+
let utf8Len = 0;
|
|
1136
|
+
if (event.utf8) {
|
|
1137
|
+
const bytes = new TextEncoder().encode(event.utf8);
|
|
1138
|
+
utf8Ptr = exports.ghostty_wasm_alloc_u8_array(bytes.length);
|
|
1139
|
+
new Uint8Array(memory.buffer).set(bytes, utf8Ptr);
|
|
1140
|
+
utf8Len = bytes.length;
|
|
1141
|
+
exports.ghostty_key_event_set_utf8(evt, utf8Ptr, utf8Len);
|
|
1142
|
+
}
|
|
1143
|
+
// Encode loop with growing buffer.
|
|
1144
|
+
const writtenPtr = this.ensureWrittenScratch();
|
|
1145
|
+
let cap = 64;
|
|
1146
|
+
let buf = this.ensureEncodeBuf(cap);
|
|
1147
|
+
writeU32(memory, writtenPtr, 0);
|
|
1148
|
+
let result = exports.ghostty_key_encoder_encode(this.handle, evt, buf, cap, writtenPtr);
|
|
1149
|
+
if (result === GhosttyResult.OUT_OF_SPACE) {
|
|
1150
|
+
cap = readU32(memory, writtenPtr);
|
|
1151
|
+
buf = this.ensureEncodeBuf(cap);
|
|
1152
|
+
result = exports.ghostty_key_encoder_encode(this.handle, evt, buf, cap, writtenPtr);
|
|
1153
|
+
}
|
|
1154
|
+
if (utf8Ptr !== 0)
|
|
1155
|
+
exports.ghostty_wasm_free_u8_array(utf8Ptr, utf8Len);
|
|
1156
|
+
if (result !== GhosttyResult.SUCCESS) {
|
|
1157
|
+
throw new Error(`ghostty_key_encoder_encode failed: ${result}`);
|
|
1158
|
+
}
|
|
1159
|
+
const written = readU32(memory, writtenPtr);
|
|
1160
|
+
return new Uint8Array(memory.buffer, buf, written).slice();
|
|
1161
|
+
}
|
|
1162
|
+
finally {
|
|
1163
|
+
exports.ghostty_key_event_free(evt);
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
/** Free the WASM encoder handle and any scratch slots. */
|
|
1167
|
+
dispose() {
|
|
1168
|
+
if (this._disposed)
|
|
1169
|
+
return;
|
|
1170
|
+
this._disposed = true;
|
|
1171
|
+
if (this.optionScratch !== undefined)
|
|
1172
|
+
this.ghostty.exports.ghostty_wasm_free_u8(this.optionScratch);
|
|
1173
|
+
if (this.encodeBuf)
|
|
1174
|
+
this.ghostty.exports.ghostty_wasm_free_u8_array(this.encodeBuf.ptr, this.encodeBuf.capacity);
|
|
1175
|
+
if (this.writtenScratch !== undefined)
|
|
1176
|
+
this.ghostty.exports.ghostty_wasm_free_usize(this.writtenScratch);
|
|
1177
|
+
this.ghostty.exports.ghostty_key_encoder_free(this.handle);
|
|
1178
|
+
}
|
|
1179
|
+
assertAlive() {
|
|
1180
|
+
if (this._disposed)
|
|
1181
|
+
throw new Error("KeyEncoder has been disposed");
|
|
1182
|
+
}
|
|
1183
|
+
ensureOptionScratch() {
|
|
1184
|
+
if (this.optionScratch === undefined)
|
|
1185
|
+
this.optionScratch = this.ghostty.exports.ghostty_wasm_alloc_u8();
|
|
1186
|
+
return this.optionScratch;
|
|
1187
|
+
}
|
|
1188
|
+
ensureWrittenScratch() {
|
|
1189
|
+
if (this.writtenScratch === undefined)
|
|
1190
|
+
this.writtenScratch = this.ghostty.exports.ghostty_wasm_alloc_usize();
|
|
1191
|
+
return this.writtenScratch;
|
|
1192
|
+
}
|
|
1193
|
+
ensureEncodeBuf(capacity) {
|
|
1194
|
+
if (!this.encodeBuf || this.encodeBuf.capacity < capacity) {
|
|
1195
|
+
if (this.encodeBuf) {
|
|
1196
|
+
this.ghostty.exports.ghostty_wasm_free_u8_array(this.encodeBuf.ptr, this.encodeBuf.capacity);
|
|
1197
|
+
}
|
|
1198
|
+
const ptr = this.ghostty.exports.ghostty_wasm_alloc_u8_array(capacity);
|
|
1199
|
+
this.encodeBuf = { ptr, capacity };
|
|
1200
|
+
}
|
|
1201
|
+
return this.encodeBuf.ptr;
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
export { CellFlags } from "./types.js";
|
|
1205
|
+
// Re-export the per-header VT modules for back-compat with consumers that
|
|
1206
|
+
// imported `RenderState` from `./ghostty.js`. New code should reach for
|
|
1207
|
+
// `./vt/render-state.js` directly.
|
|
1208
|
+
export { RenderState } from "./vt/render-state.js";
|
|
1209
|
+
// Backwards-compat alias for upstream consumers that imported `GhosttyTerminal`.
|
|
1210
|
+
// New code should use `Terminal` directly.
|
|
1211
|
+
export { Terminal as GhosttyTerminal };
|
|
1212
|
+
//# sourceMappingURL=ghostty.js.map
|