The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | type GhosttyKeyboardLayoutMap, |
| 3 | ghosttyConsumedMods, |
| 4 | ghosttyKeyForEvent, |
| 5 | ghosttyUnshiftedCodepoint, |
| 6 | loadGhosttyKeyboardLayoutMap, |
| 7 | } from "./keyCodes"; |
| 8 | import { GhosttyRuntime, loadGhosttyRuntime } from "./runtime"; |
| 9 | |
| 10 | const GHOSTTY_SUCCESS = 0; |
| 11 | const GHOSTTY_OUT_OF_SPACE = -3; |
| 12 | // The pinned ABI measures max_scrollback in bytes despite its header saying lines. |
| 13 | // Match Ghostty's default scrollback-limit. |
| 14 | const MAX_SCROLLBACK_BYTES = 10_000_000; |
| 15 | // wasm32 GhosttyTerminalSelectionFormatOptions layout at the pinned libghostty-vt revision. |
| 16 | const SELECTION_FORMAT_OPTIONS_SIZE = 16; |
| 17 | const PALETTE_BYTES = 256 * 3; |
| 18 | |
| 19 | const RENDER_DATA = { |
| 20 | cols: 1, |
| 21 | rows: 2, |
| 22 | dirty: 3, |
| 23 | rowIterator: 4, |
| 24 | background: 5, |
| 25 | foreground: 6, |
| 26 | cursor: 7, |
| 27 | cursorHasValue: 8, |
| 28 | cursorStyle: 10, |
| 29 | cursorVisible: 11, |
| 30 | cursorBlinking: 12, |
| 31 | cursorInViewport: 14, |
| 32 | cursorX: 15, |
| 33 | cursorY: 16, |
| 34 | } as const; |
| 35 | |
| 36 | const ROW_DATA = { |
| 37 | dirty: 1, |
| 38 | raw: 2, |
| 39 | cells: 3, |
| 40 | } as const; |
| 41 | |
| 42 | const CELL_DATA = { |
| 43 | raw: 1, |
| 44 | style: 2, |
| 45 | graphemesLength: 3, |
| 46 | graphemes: 4, |
| 47 | background: 5, |
| 48 | foreground: 6, |
| 49 | selected: 7, |
| 50 | } as const; |
| 51 | |
| 52 | // Optional row_cells_get_multi colors must come last; an unset color stops the query. |
| 53 | const CELL_QUERY = { |
| 54 | keys: 0, |
| 55 | values: 20, |
| 56 | graphemesLength: 40, |
| 57 | selected: 44, |
| 58 | background: 48, |
| 59 | foreground: 52, |
| 60 | written: 56, |
| 61 | size: 60, |
| 62 | } as const; |
| 63 | |
| 64 | const RAW_CELL_DATA = { |
| 65 | wide: 3, |
| 66 | } as const; |
| 67 | |
| 68 | export const GHOSTTY_CELL_WIDE = { |
| 69 | narrow: 0, |
| 70 | wide: 1, |
| 71 | spacerTail: 2, |
| 72 | spacerHead: 3, |
| 73 | } as const; |
| 74 | |
| 75 | export interface GhosttyColor { |
| 76 | readonly r: number; |
| 77 | readonly g: number; |
| 78 | readonly b: number; |
| 79 | } |
| 80 | |
| 81 | export interface GhosttyTheme { |
| 82 | readonly foreground: GhosttyColor; |
| 83 | readonly background: GhosttyColor; |
| 84 | readonly cursor: GhosttyColor; |
| 85 | readonly palette?: readonly GhosttyColor[]; |
| 86 | readonly selectionBackground?: string; |
| 87 | } |
| 88 | |
| 89 | export interface GhosttyCell { |
| 90 | readonly text: string; |
| 91 | readonly wide: number; |
| 92 | readonly foreground: GhosttyColor; |
| 93 | readonly background: GhosttyColor; |
| 94 | readonly bold: boolean; |
| 95 | readonly italic: boolean; |
| 96 | readonly invisible: boolean; |
| 97 | readonly strikethrough: boolean; |
| 98 | readonly overline: boolean; |
| 99 | readonly underline: boolean; |
| 100 | readonly selected: boolean; |
| 101 | } |
| 102 | |
| 103 | export interface GhosttyRow { |
| 104 | readonly cells: readonly GhosttyCell[]; |
| 105 | readonly text: string; |
| 106 | readonly isWrapContinuation: boolean; |
| 107 | readonly wrapsToNext: boolean; |
| 108 | } |
| 109 | |
| 110 | export interface GhosttySnapshot { |
| 111 | readonly cols: number; |
| 112 | readonly rows: number; |
| 113 | readonly foreground: GhosttyColor; |
| 114 | readonly background: GhosttyColor; |
| 115 | readonly cursor: GhosttyColor; |
| 116 | readonly cursorX: number; |
| 117 | readonly cursorY: number; |
| 118 | readonly cursorVisible: boolean; |
| 119 | readonly cursorBlinking: boolean; |
| 120 | readonly cursorStyle: number; |
| 121 | readonly dirtyRows: ReadonlySet<number>; |
| 122 | readonly rowData: readonly GhosttyRow[]; |
| 123 | } |
| 124 | |
| 125 | export interface GhosttySelectionRange { |
| 126 | readonly viewport: { |
| 127 | readonly start: { readonly x: number; readonly y: number }; |
| 128 | readonly end: { readonly x: number; readonly y: number }; |
| 129 | }; |
| 130 | readonly screen: { |
| 131 | readonly start: { readonly x: number; readonly y: number }; |
| 132 | readonly end: { readonly x: number; readonly y: number }; |
| 133 | }; |
| 134 | } |
| 135 | |
| 136 | export interface GhosttyScrollbar { |
| 137 | readonly total: number; |
| 138 | readonly offset: number; |
| 139 | readonly len: number; |
| 140 | } |
| 141 | |
| 142 | export interface GhosttyPointInput { |
| 143 | readonly x: number; |
| 144 | readonly y: number; |
| 145 | readonly tag?: 1 | 2; |
| 146 | } |
| 147 | |
| 148 | export interface GhosttyMouseInput { |
| 149 | readonly action: "press" | "release" | "motion"; |
| 150 | readonly button: number | null; |
| 151 | readonly mods: number; |
| 152 | readonly x: number; |
| 153 | readonly y: number; |
| 154 | readonly screenWidth: number; |
| 155 | readonly screenHeight: number; |
| 156 | readonly cellWidth: number; |
| 157 | readonly cellHeight: number; |
| 158 | readonly paddingLeft: number; |
| 159 | readonly paddingRight: number; |
| 160 | readonly paddingTop: number; |
| 161 | readonly paddingBottom: number; |
| 162 | readonly anyButtonPressed: boolean; |
| 163 | } |
| 164 | |
| 165 | // Ghostty sends modified F3 as CSI 13;m ~ to stay clear of cursor position reports, but the PTY |
| 166 | // advertises xterm-256color, whose kf15/kf27/... are CSI 1;m R. Terminfo keys only apply in |
| 167 | // keypad-transmit mode (smkx), so that mode marks a terminfo reader; raw parsers never set it. |
| 168 | const MODIFIED_F3 = /^\x1b\[13;(\d+)~$/; |
| 169 | /** Mouse report input: UTF-8 text, or one UTF-16 code unit per byte when `binary`. */ |
| 170 | interface GhosttyMouseReport { |
| 171 | readonly data: string; |
| 172 | readonly binary: boolean; |
| 173 | } |
| 174 | |
| 175 | const decoder = new TextDecoder("utf-8", { ignoreBOM: true }); |
| 176 | const strictDecoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }); |
| 177 | const encoder = new TextEncoder(); |
| 178 | |
| 179 | // Legacy X10 reports can contain bytes above 0x7f that are not valid UTF-8. |
| 180 | function mouseReport(bytes: Uint8Array): GhosttyMouseReport { |
| 181 | try { |
| 182 | return { data: strictDecoder.decode(bytes), binary: false }; |
| 183 | } catch { |
| 184 | return { data: String.fromCharCode(...bytes), binary: true }; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // The encoder only takes whole-pixel cells, so cell reports rescale the pointer onto that grid. |
| 189 | function encoderAxis( |
| 190 | position: number, |
| 191 | screen: number, |
| 192 | cell: number, |
| 193 | paddingStart: number, |
| 194 | paddingEnd: number, |
| 195 | pixelReports: boolean, |
| 196 | ) { |
| 197 | const wholeCell = Math.max(1, Math.round(cell)); |
| 198 | if (pixelReports) { |
| 199 | return { position, screen, cell: wholeCell, paddingStart, paddingEnd }; |
| 200 | } |
| 201 | const scale = wholeCell / cell; |
| 202 | const start = Math.round(paddingStart); |
| 203 | const end = Math.round(paddingEnd); |
| 204 | return { |
| 205 | position: Math.max(0, start + (position - paddingStart) * scale), |
| 206 | screen: start + end + Math.floor((screen - paddingStart - paddingEnd) / cell) * wholeCell, |
| 207 | cell: wholeCell, |
| 208 | paddingStart: start, |
| 209 | paddingEnd: end, |
| 210 | }; |
| 211 | } |
| 212 | |
| 213 | function blend(foreground: GhosttyColor, background: GhosttyColor): GhosttyColor { |
| 214 | const channel = (front: number, back: number) => Math.floor((front * 155 + back * 100) / 255); |
| 215 | return { |
| 216 | r: channel(foreground.r, background.r), |
| 217 | g: channel(foreground.g, background.g), |
| 218 | b: channel(foreground.b, background.b), |
| 219 | }; |
| 220 | } |
| 221 | |
| 222 | function sameColor(left: GhosttyColor, right: GhosttyColor): boolean { |
| 223 | return left.r === right.r && left.g === right.g && left.b === right.b; |
| 224 | } |
| 225 | |
| 226 | /** Bounds String.fromCodePoint arguments for cells with enormous combining-mark sequences. */ |
| 227 | export function ghosttyCellText( |
| 228 | codepointView: DataView, |
| 229 | graphemeLength: number, |
| 230 | byteOffset = 0, |
| 231 | ): string { |
| 232 | if (graphemeLength === 1) return String.fromCodePoint(codepointView.getUint32(byteOffset, true)); |
| 233 | const CHUNK_SIZE = 4_096; |
| 234 | let text = ""; |
| 235 | for (let start = 0; start < graphemeLength; start += CHUNK_SIZE) { |
| 236 | const count = Math.min(CHUNK_SIZE, graphemeLength - start); |
| 237 | const codes = new Array<number>(count); |
| 238 | for (let index = 0; index < count; index += 1) { |
| 239 | codes[index] = codepointView.getUint32(byteOffset + (start + index) * 4, true); |
| 240 | } |
| 241 | text += String.fromCodePoint(...codes); |
| 242 | } |
| 243 | return text; |
| 244 | } |
| 245 | |
| 246 | export class GhosttyTerminalCore { |
| 247 | private readonly runtime: GhosttyRuntime; |
| 248 | private terminalSlot = 0; |
| 249 | private terminal = 0; |
| 250 | private renderStateSlot = 0; |
| 251 | private renderState = 0; |
| 252 | private rowIteratorSlot = 0; |
| 253 | private rowCellsSlot = 0; |
| 254 | private keyEncoderSlot = 0; |
| 255 | private keyEncoder = 0; |
| 256 | private keyEventSlot = 0; |
| 257 | private keyEvent = 0; |
| 258 | private mouseEncoderSlot = 0; |
| 259 | private mouseEncoder = 0; |
| 260 | private mouseEventSlot = 0; |
| 261 | private mouseEvent = 0; |
| 262 | private ptyWriterId = 0; |
| 263 | private ptyWriter: ((data: string) => void) | null = null; |
| 264 | private scratch = 0; |
| 265 | private graphemes = 0; |
| 266 | private graphemeCapacity = 0; |
| 267 | private style = 0; |
| 268 | private cellQuery = 0; |
| 269 | private scrollbar = 0; |
| 270 | private rows: GhosttyRow[] = []; |
| 271 | // Defaults the cached rows resolved; Ghostty marks no row dirty when OSC 10/11 change them. |
| 272 | private rowColors: Pick<GhosttySnapshot, "foreground" | "background"> | null = null; |
| 273 | private disposed = false; |
| 274 | private keyboardLayoutMap: GhosttyKeyboardLayoutMap | undefined; |
| 275 | |
| 276 | private constructor(runtime: GhosttyRuntime) { |
| 277 | this.runtime = runtime; |
| 278 | void loadGhosttyKeyboardLayoutMap().then((layoutMap) => { |
| 279 | if (!this.disposed) this.keyboardLayoutMap = layoutMap; |
| 280 | }); |
| 281 | } |
| 282 | |
| 283 | static async create( |
| 284 | cols: number, |
| 285 | rows: number, |
| 286 | cellWidth: number, |
| 287 | cellHeight: number, |
| 288 | theme: GhosttyTheme, |
| 289 | onPtyData: (data: string) => void, |
| 290 | ): Promise<GhosttyTerminalCore> { |
| 291 | const core = new GhosttyTerminalCore(await loadGhosttyRuntime()); |
| 292 | try { |
| 293 | core.initialize(cols, rows, cellWidth, cellHeight, theme, onPtyData); |
| 294 | return core; |
| 295 | } catch (error) { |
| 296 | core.dispose(); |
| 297 | throw error; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | private initialize( |
| 302 | cols: number, |
| 303 | rows: number, |
| 304 | cellWidth: number, |
| 305 | cellHeight: number, |
| 306 | theme: GhosttyTheme, |
| 307 | onPtyData: (data: string) => void, |
| 308 | ): void { |
| 309 | const optionsSize = this.runtime.layout("GhosttyTerminalOptions").size; |
| 310 | const options = this.runtime.alloc(optionsSize); |
| 311 | this.runtime.setField(options, "GhosttyTerminalOptions", "cols", cols); |
| 312 | this.runtime.setField(options, "GhosttyTerminalOptions", "rows", rows); |
| 313 | this.runtime.setField( |
| 314 | options, |
| 315 | "GhosttyTerminalOptions", |
| 316 | "max_scrollback", |
| 317 | MAX_SCROLLBACK_BYTES, |
| 318 | ); |
| 319 | this.terminalSlot = this.runtime.allocOpaque(); |
| 320 | const terminalResult = this.runtime.call("ghostty_terminal_new", 0, this.terminalSlot, options); |
| 321 | this.runtime.free(options, optionsSize); |
| 322 | this.assertSuccess("ghostty_terminal_new", terminalResult); |
| 323 | this.terminal = this.runtime.readPointer(this.terminalSlot); |
| 324 | this.applyDefaultCursorBlink(); |
| 325 | this.ptyWriter = onPtyData; |
| 326 | this.ptyWriterId = this.runtime.attachPtyWriter(this.terminal, onPtyData); |
| 327 | |
| 328 | this.renderStateSlot = this.runtime.allocOpaque(); |
| 329 | this.assertSuccess( |
| 330 | "ghostty_render_state_new", |
| 331 | this.runtime.call("ghostty_render_state_new", 0, this.renderStateSlot), |
| 332 | ); |
| 333 | this.renderState = this.runtime.readPointer(this.renderStateSlot); |
| 334 | |
| 335 | this.rowIteratorSlot = this.runtime.allocOpaque(); |
| 336 | this.assertSuccess( |
| 337 | "ghostty_render_state_row_iterator_new", |
| 338 | this.runtime.call("ghostty_render_state_row_iterator_new", 0, this.rowIteratorSlot), |
| 339 | ); |
| 340 | this.rowCellsSlot = this.runtime.allocOpaque(); |
| 341 | this.assertSuccess( |
| 342 | "ghostty_render_state_row_cells_new", |
| 343 | this.runtime.call("ghostty_render_state_row_cells_new", 0, this.rowCellsSlot), |
| 344 | ); |
| 345 | |
| 346 | this.keyEncoderSlot = this.runtime.allocOpaque(); |
| 347 | this.assertSuccess( |
| 348 | "ghostty_key_encoder_new", |
| 349 | this.runtime.call("ghostty_key_encoder_new", 0, this.keyEncoderSlot), |
| 350 | ); |
| 351 | this.keyEncoder = this.runtime.readPointer(this.keyEncoderSlot); |
| 352 | this.keyEventSlot = this.runtime.allocOpaque(); |
| 353 | this.assertSuccess( |
| 354 | "ghostty_key_event_new", |
| 355 | this.runtime.call("ghostty_key_event_new", 0, this.keyEventSlot), |
| 356 | ); |
| 357 | this.keyEvent = this.runtime.readPointer(this.keyEventSlot); |
| 358 | |
| 359 | this.mouseEncoderSlot = this.runtime.allocOpaque(); |
| 360 | this.assertSuccess( |
| 361 | "ghostty_mouse_encoder_new", |
| 362 | this.runtime.call("ghostty_mouse_encoder_new", 0, this.mouseEncoderSlot), |
| 363 | ); |
| 364 | this.mouseEncoder = this.runtime.readPointer(this.mouseEncoderSlot); |
| 365 | this.mouseEventSlot = this.runtime.allocOpaque(); |
| 366 | this.assertSuccess( |
| 367 | "ghostty_mouse_event_new", |
| 368 | this.runtime.call("ghostty_mouse_event_new", 0, this.mouseEventSlot), |
| 369 | ); |
| 370 | this.mouseEvent = this.runtime.readPointer(this.mouseEventSlot); |
| 371 | |
| 372 | this.scratch = this.runtime.alloc(16); |
| 373 | const styleSize = this.runtime.layout("GhosttyStyle").size; |
| 374 | this.style = this.runtime.alloc(styleSize); |
| 375 | this.runtime.setField(this.style, "GhosttyStyle", "size", styleSize); |
| 376 | this.cellQuery = this.runtime.alloc(CELL_QUERY.size); |
| 377 | const queryView = this.runtime.memoryDataView(); |
| 378 | const cellFields = [ |
| 379 | [CELL_DATA.style, this.style], |
| 380 | [CELL_DATA.graphemesLength, this.cellQuery + CELL_QUERY.graphemesLength], |
| 381 | [CELL_DATA.selected, this.cellQuery + CELL_QUERY.selected], |
| 382 | [CELL_DATA.background, this.cellQuery + CELL_QUERY.background], |
| 383 | [CELL_DATA.foreground, this.cellQuery + CELL_QUERY.foreground], |
| 384 | ] as const; |
| 385 | for (const [index, [key, pointer]] of cellFields.entries()) { |
| 386 | queryView.setUint32(this.cellQuery + CELL_QUERY.keys + index * 4, key, true); |
| 387 | queryView.setUint32(this.cellQuery + CELL_QUERY.values + index * 4, pointer, true); |
| 388 | } |
| 389 | this.scrollbar = this.runtime.alloc(this.runtime.layout("GhosttyTerminalScrollbar").size); |
| 390 | this.setTheme(theme); |
| 391 | this.resize(cols, rows, cellWidth, cellHeight); |
| 392 | } |
| 393 | |
| 394 | write(data: string | Uint8Array, onSynchronizedOutputEnd?: () => void): void { |
| 395 | this.ensureActive(); |
| 396 | const bytes = typeof data === "string" ? encoder.encode(data) : data; |
| 397 | if (bytes.length === 0) return; |
| 398 | const pointer = this.runtime.alloc(bytes.length); |
| 399 | this.runtime.bytes(pointer, bytes.length).set(bytes); |
| 400 | // An existing render deadline belongs to one group. Observe Ghostty before |
| 401 | // the next escape can reopen it; Ghostty still parses OSC/DCS and split VT |
| 402 | // sequences itself. Once that group ends, the remaining bytes can be batched. |
| 403 | let synchronized = onSynchronizedOutputEnd !== undefined && this.isSynchronizedOutput(); |
| 404 | try { |
| 405 | for (let start = 0; start < bytes.length;) { |
| 406 | const nextEscape = synchronized ? bytes.indexOf(0x1b, start + 1) : -1; |
| 407 | const end = nextEscape === -1 ? bytes.length : nextEscape; |
| 408 | this.runtime.call("ghostty_terminal_vt_write", this.terminal, pointer + start, end - start); |
| 409 | start = end; |
| 410 | if (synchronized && !this.isSynchronizedOutput()) { |
| 411 | synchronized = false; |
| 412 | onSynchronizedOutputEnd?.(); |
| 413 | } |
| 414 | } |
| 415 | } finally { |
| 416 | this.runtime.free(pointer, bytes.length); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | resetAndWrite(data: string): void { |
| 421 | this.ensureActive(); |
| 422 | this.runtime.call("ghostty_terminal_reset", this.terminal); |
| 423 | // RIS restores the built-in steady cursor; reapply the embedder default before replay. |
| 424 | this.applyDefaultCursorBlink(); |
| 425 | this.rows = []; |
| 426 | if (data.length === 0) return; |
| 427 | const writer = this.ptyWriter; |
| 428 | if (this.ptyWriterId !== 0) { |
| 429 | this.runtime.detachPtyWriter(this.terminal, this.ptyWriterId); |
| 430 | this.ptyWriterId = 0; |
| 431 | } |
| 432 | try { |
| 433 | this.write(data); |
| 434 | } finally { |
| 435 | if (writer !== null && !this.disposed) { |
| 436 | this.ptyWriterId = this.runtime.attachPtyWriter(this.terminal, writer); |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | resize(cols: number, rows: number, cellWidth: number, cellHeight: number): void { |
| 442 | this.ensureActive(); |
| 443 | this.assertSuccess( |
| 444 | "ghostty_terminal_resize", |
| 445 | this.runtime.call( |
| 446 | "ghostty_terminal_resize", |
| 447 | this.terminal, |
| 448 | Math.max(1, Math.min(65_535, cols)), |
| 449 | Math.max(1, Math.min(65_535, rows)), |
| 450 | Math.max(1, Math.round(cellWidth)), |
| 451 | Math.max(1, Math.round(cellHeight)), |
| 452 | ), |
| 453 | ); |
| 454 | } |
| 455 | |
| 456 | // Option 23 sets blink on startup and DECSCUSR reset; explicit DECSCUSR or DEC mode 12 still wins. |
| 457 | private applyDefaultCursorBlink(): void { |
| 458 | const blink = this.runtime.alloc(1); |
| 459 | this.runtime.bytes(blink, 1)[0] = 1; |
| 460 | this.runtime.call("ghostty_terminal_set", this.terminal, 23, blink); |
| 461 | this.runtime.free(blink, 1); |
| 462 | } |
| 463 | |
| 464 | setTheme(theme: GhosttyTheme): void { |
| 465 | this.ensureActive(); |
| 466 | const color = this.runtime.alloc(3); |
| 467 | for (const [option, value] of [ |
| 468 | [11, theme.foreground], |
| 469 | [12, theme.background], |
| 470 | [13, theme.cursor], |
| 471 | ] as const) { |
| 472 | this.runtime.bytes(color, 3).set([value.r, value.g, value.b]); |
| 473 | this.runtime.call("ghostty_terminal_set", this.terminal, option, color); |
| 474 | } |
| 475 | this.runtime.free(color, 3); |
| 476 | if (theme.palette) this.setPalette(theme.palette); |
| 477 | } |
| 478 | |
| 479 | private setPalette(colors: readonly GhosttyColor[]): void { |
| 480 | const palette = this.runtime.alloc(PALETTE_BYTES); |
| 481 | this.runtime.call("ghostty_terminal_get", this.terminal, 25, palette); |
| 482 | const bytes = this.runtime.bytes(palette, PALETTE_BYTES); |
| 483 | colors.slice(0, 16).forEach((color, index) => { |
| 484 | bytes.set([color.r, color.g, color.b], index * 3); |
| 485 | }); |
| 486 | this.runtime.call("ghostty_terminal_set", this.terminal, 14, palette); |
| 487 | this.runtime.free(palette, PALETTE_BYTES); |
| 488 | } |
| 489 | |
| 490 | scroll(deltaRows: number): void { |
| 491 | this.ensureActive(); |
| 492 | const layout = this.runtime.layout("GhosttyTerminalScrollViewport"); |
| 493 | const scroll = this.runtime.alloc(layout.size); |
| 494 | this.runtime.setField(scroll, "GhosttyTerminalScrollViewport", "tag", 2); |
| 495 | const value = layout.fields.value!; |
| 496 | this.runtime.view(scroll + value.offset, value.size).setInt32(0, deltaRows, true); |
| 497 | this.runtime.call("ghostty_terminal_scroll_viewport", this.terminal, scroll); |
| 498 | this.runtime.free(scroll, layout.size); |
| 499 | } |
| 500 | |
| 501 | scrollToBottom(): void { |
| 502 | this.ensureActive(); |
| 503 | const layout = this.runtime.layout("GhosttyTerminalScrollViewport"); |
| 504 | const scroll = this.runtime.alloc(layout.size); |
| 505 | this.runtime.setField(scroll, "GhosttyTerminalScrollViewport", "tag", 1); |
| 506 | this.runtime.call("ghostty_terminal_scroll_viewport", this.terminal, scroll); |
| 507 | this.runtime.free(scroll, layout.size); |
| 508 | } |
| 509 | |
| 510 | isViewportActive(): boolean { |
| 511 | this.ensureActive(); |
| 512 | this.runtime.bytes(this.scratch, 1)[0] = 0; |
| 513 | return ( |
| 514 | this.runtime.call("ghostty_terminal_get", this.terminal, 32, this.scratch) === |
| 515 | GHOSTTY_SUCCESS && this.runtime.bytes(this.scratch, 1)[0] !== 0 |
| 516 | ); |
| 517 | } |
| 518 | |
| 519 | scrollbarState(): GhosttyScrollbar | null { |
| 520 | this.ensureActive(); |
| 521 | const layout = this.runtime.layout("GhosttyTerminalScrollbar"); |
| 522 | this.runtime.bytes(this.scrollbar, layout.size).fill(0); |
| 523 | if ( |
| 524 | this.runtime.call("ghostty_terminal_get", this.terminal, 9, this.scrollbar) !== |
| 525 | GHOSTTY_SUCCESS |
| 526 | ) { |
| 527 | return null; |
| 528 | } |
| 529 | return { |
| 530 | total: this.runtime.readField(this.scrollbar, "GhosttyTerminalScrollbar", "total"), |
| 531 | offset: this.runtime.readField(this.scrollbar, "GhosttyTerminalScrollbar", "offset"), |
| 532 | len: this.runtime.readField(this.scrollbar, "GhosttyTerminalScrollbar", "len"), |
| 533 | }; |
| 534 | } |
| 535 | |
| 536 | isMouseTracking(): boolean { |
| 537 | this.ensureActive(); |
| 538 | this.runtime.bytes(this.scratch, 1)[0] = 0; |
| 539 | return ( |
| 540 | this.runtime.call("ghostty_terminal_get", this.terminal, 11, this.scratch) === |
| 541 | GHOSTTY_SUCCESS && this.runtime.bytes(this.scratch, 1)[0] !== 0 |
| 542 | ); |
| 543 | } |
| 544 | |
| 545 | isMouseAnyEventTracking(): boolean { |
| 546 | this.ensureActive(); |
| 547 | return this.modeEnabled(1003); |
| 548 | } |
| 549 | |
| 550 | isSynchronizedOutput(): boolean { |
| 551 | this.ensureActive(); |
| 552 | return this.modeEnabled(2026); |
| 553 | } |
| 554 | |
| 555 | endSynchronizedOutput(): void { |
| 556 | this.ensureActive(); |
| 557 | this.assertSuccess( |
| 558 | "ghostty_terminal_mode_set", |
| 559 | this.runtime.call("ghostty_terminal_mode_set", this.terminal, 2026, 0), |
| 560 | ); |
| 561 | } |
| 562 | |
| 563 | isAlternateScreen(): boolean { |
| 564 | this.ensureActive(); |
| 565 | this.runtime.bytes(this.scratch, 4).fill(0); |
| 566 | return ( |
| 567 | this.runtime.call("ghostty_terminal_get", this.terminal, 6, this.scratch) === |
| 568 | GHOSTTY_SUCCESS && this.runtime.view(this.scratch, 4).getUint32(0, true) === 1 |
| 569 | ); |
| 570 | } |
| 571 | |
| 572 | isApplicationCursorKeys(): boolean { |
| 573 | this.ensureActive(); |
| 574 | return this.modeEnabled(1); |
| 575 | } |
| 576 | |
| 577 | encodeKey(event: KeyboardEvent, action: "press" | "release" = "press"): string { |
| 578 | this.ensureActive(); |
| 579 | this.runtime.call("ghostty_key_encoder_setopt_from_terminal", this.keyEncoder, this.terminal); |
| 580 | this.runtime.call( |
| 581 | "ghostty_key_event_set_action", |
| 582 | this.keyEvent, |
| 583 | action === "release" ? 0 : event.repeat ? 2 : 1, |
| 584 | ); |
| 585 | this.runtime.call("ghostty_key_event_set_key", this.keyEvent, ghosttyKeyForEvent(event)); |
| 586 | const mods = |
| 587 | (event.shiftKey ? 1 : 0) | |
| 588 | (event.ctrlKey ? 1 << 1 : 0) | |
| 589 | (event.altKey ? 1 << 2 : 0) | |
| 590 | (event.metaKey ? 1 << 3 : 0) | |
| 591 | (event.getModifierState("CapsLock") ? 1 << 4 : 0) | |
| 592 | (event.getModifierState("NumLock") ? 1 << 5 : 0); |
| 593 | this.runtime.call("ghostty_key_event_set_mods", this.keyEvent, mods); |
| 594 | this.runtime.call( |
| 595 | "ghostty_key_event_set_consumed_mods", |
| 596 | this.keyEvent, |
| 597 | ghosttyConsumedMods(event), |
| 598 | ); |
| 599 | this.runtime.call("ghostty_key_event_set_composing", this.keyEvent, event.isComposing ? 1 : 0); |
| 600 | this.runtime.call( |
| 601 | "ghostty_key_event_set_unshifted_codepoint", |
| 602 | this.keyEvent, |
| 603 | ghosttyUnshiftedCodepoint(event, this.keyboardLayoutMap), |
| 604 | ); |
| 605 | |
| 606 | const text = event.key.length === 1 ? event.key : ""; |
| 607 | const textBytes = encoder.encode(text); |
| 608 | const textPointer = textBytes.length === 0 ? 0 : this.runtime.alloc(textBytes.length); |
| 609 | if (textPointer !== 0) this.runtime.bytes(textPointer, textBytes.length).set(textBytes); |
| 610 | this.runtime.call("ghostty_key_event_set_utf8", this.keyEvent, textPointer, textBytes.length); |
| 611 | |
| 612 | const written = this.runtime.call("ghostty_wasm_alloc_usize"); |
| 613 | const encoded = decoder.decode( |
| 614 | this.encodeOutput(written, (output, outputSize) => |
| 615 | this.runtime.call( |
| 616 | "ghostty_key_encoder_encode", |
| 617 | this.keyEncoder, |
| 618 | this.keyEvent, |
| 619 | output, |
| 620 | outputSize, |
| 621 | written, |
| 622 | ), |
| 623 | ), |
| 624 | ); |
| 625 | this.runtime.call("ghostty_wasm_free_usize", written); |
| 626 | if (textPointer !== 0) this.runtime.free(textPointer, textBytes.length); |
| 627 | return MODIFIED_F3.test(encoded) && this.modeEnabled(66) && this.kittyKeyboardFlags() === 0 |
| 628 | ? encoded.replace(MODIFIED_F3, "\x1b[1;$1R") |
| 629 | : encoded; |
| 630 | } |
| 631 | |
| 632 | encodePaste(data: string): string { |
| 633 | this.ensureActive(); |
| 634 | const input = encoder.encode(data); |
| 635 | if (input.length === 0) return ""; |
| 636 | const inputPointer = this.runtime.alloc(input.length); |
| 637 | this.runtime.bytes(inputPointer, input.length).set(input); |
| 638 | const bracketed = this.modeEnabled(2004); |
| 639 | const written = this.runtime.call("ghostty_wasm_alloc_usize"); |
| 640 | let encoded = ""; |
| 641 | const sizeResult = this.runtime.call( |
| 642 | "ghostty_paste_encode", |
| 643 | inputPointer, |
| 644 | input.length, |
| 645 | bracketed ? 1 : 0, |
| 646 | 0, |
| 647 | 0, |
| 648 | written, |
| 649 | ); |
| 650 | const outputSize = this.runtime.view(written, 4).getUint32(0, true); |
| 651 | if (sizeResult === GHOSTTY_OUT_OF_SPACE && outputSize > 0) { |
| 652 | const output = this.runtime.alloc(outputSize); |
| 653 | const result = this.runtime.call( |
| 654 | "ghostty_paste_encode", |
| 655 | inputPointer, |
| 656 | input.length, |
| 657 | bracketed ? 1 : 0, |
| 658 | output, |
| 659 | outputSize, |
| 660 | written, |
| 661 | ); |
| 662 | const outputLength = this.runtime.view(written, 4).getUint32(0, true); |
| 663 | encoded = |
| 664 | result === GHOSTTY_SUCCESS ? decoder.decode(this.runtime.bytes(output, outputLength)) : ""; |
| 665 | this.runtime.free(output, outputSize); |
| 666 | } |
| 667 | this.runtime.call("ghostty_wasm_free_usize", written); |
| 668 | this.runtime.free(inputPointer, input.length); |
| 669 | return encoded; |
| 670 | } |
| 671 | |
| 672 | /** Empty unless the program enabled DEC 1004 focus reporting. */ |
| 673 | encodeFocus(focused: boolean): string { |
| 674 | this.ensureActive(); |
| 675 | if (!this.modeEnabled(1004)) return ""; |
| 676 | const written = this.runtime.call("ghostty_wasm_alloc_usize"); |
| 677 | const encoded = decoder.decode( |
| 678 | this.encodeOutput(written, (output, outputSize) => |
| 679 | this.runtime.call("ghostty_focus_encode", focused ? 0 : 1, output, outputSize, written), |
| 680 | ), |
| 681 | ); |
| 682 | this.runtime.call("ghostty_wasm_free_usize", written); |
| 683 | return encoded; |
| 684 | } |
| 685 | |
| 686 | encodeMouse(input: GhosttyMouseInput): GhosttyMouseReport { |
| 687 | this.ensureActive(); |
| 688 | this.runtime.call( |
| 689 | "ghostty_mouse_encoder_setopt_from_terminal", |
| 690 | this.mouseEncoder, |
| 691 | this.terminal, |
| 692 | ); |
| 693 | |
| 694 | // SGR pixel reports (DEC 1016) must name surface pixels, so they skip the rescale. |
| 695 | const pixelReports = this.modeEnabled(1016); |
| 696 | const x = encoderAxis( |
| 697 | input.x, |
| 698 | input.screenWidth, |
| 699 | input.cellWidth, |
| 700 | input.paddingLeft, |
| 701 | input.paddingRight, |
| 702 | pixelReports, |
| 703 | ); |
| 704 | const y = encoderAxis( |
| 705 | input.y, |
| 706 | input.screenHeight, |
| 707 | input.cellHeight, |
| 708 | input.paddingTop, |
| 709 | input.paddingBottom, |
| 710 | pixelReports, |
| 711 | ); |
| 712 | |
| 713 | const sizeLayout = this.runtime.layout("GhosttyMouseEncoderSize"); |
| 714 | const size = this.runtime.alloc(sizeLayout.size); |
| 715 | for (const [field, value] of [ |
| 716 | ["size", sizeLayout.size], |
| 717 | ["screen_width", x.screen], |
| 718 | ["screen_height", y.screen], |
| 719 | ["cell_width", x.cell], |
| 720 | ["cell_height", y.cell], |
| 721 | ["padding_top", y.paddingStart], |
| 722 | ["padding_bottom", y.paddingEnd], |
| 723 | ["padding_right", x.paddingEnd], |
| 724 | ["padding_left", x.paddingStart], |
| 725 | ] as const) { |
| 726 | this.runtime.setField(size, "GhosttyMouseEncoderSize", field, Math.max(0, Math.round(value))); |
| 727 | } |
| 728 | this.runtime.call("ghostty_mouse_encoder_setopt", this.mouseEncoder, 2, size); |
| 729 | this.runtime.free(size, sizeLayout.size); |
| 730 | |
| 731 | this.runtime.bytes(this.scratch, 1)[0] = input.anyButtonPressed ? 1 : 0; |
| 732 | this.runtime.call("ghostty_mouse_encoder_setopt", this.mouseEncoder, 3, this.scratch); |
| 733 | this.runtime.bytes(this.scratch, 1)[0] = 1; |
| 734 | this.runtime.call("ghostty_mouse_encoder_setopt", this.mouseEncoder, 4, this.scratch); |
| 735 | |
| 736 | this.runtime.call( |
| 737 | "ghostty_mouse_event_set_action", |
| 738 | this.mouseEvent, |
| 739 | input.action === "press" ? 0 : input.action === "release" ? 1 : 2, |
| 740 | ); |
| 741 | if (input.button === null) { |
| 742 | this.runtime.call("ghostty_mouse_event_clear_button", this.mouseEvent); |
| 743 | } else { |
| 744 | this.runtime.call("ghostty_mouse_event_set_button", this.mouseEvent, input.button); |
| 745 | } |
| 746 | this.runtime.call("ghostty_mouse_event_set_mods", this.mouseEvent, input.mods); |
| 747 | const positionLayout = this.runtime.layout("GhosttyMousePosition"); |
| 748 | const position = this.runtime.alloc(positionLayout.size); |
| 749 | const positionView = this.runtime.view(position, positionLayout.size); |
| 750 | positionView.setFloat32(positionLayout.fields.x!.offset, x.position, true); |
| 751 | positionView.setFloat32(positionLayout.fields.y!.offset, y.position, true); |
| 752 | this.runtime.call("ghostty_mouse_event_set_position", this.mouseEvent, position); |
| 753 | this.runtime.free(position, positionLayout.size); |
| 754 | |
| 755 | const written = this.runtime.call("ghostty_wasm_alloc_usize"); |
| 756 | const encoded = this.encodeOutput(written, (output, outputSize) => |
| 757 | this.runtime.call( |
| 758 | "ghostty_mouse_encoder_encode", |
| 759 | this.mouseEncoder, |
| 760 | this.mouseEvent, |
| 761 | output, |
| 762 | outputSize, |
| 763 | written, |
| 764 | ), |
| 765 | ); |
| 766 | this.runtime.call("ghostty_wasm_free_usize", written); |
| 767 | return mouseReport(encoded); |
| 768 | } |
| 769 | |
| 770 | setSelection(anchor: GhosttyPointInput, end: GhosttyPointInput): void { |
| 771 | this.ensureActive(); |
| 772 | const selectionLayout = this.runtime.layout("GhosttySelection"); |
| 773 | const gridRefSize = this.runtime.layout("GhosttyGridRef").size; |
| 774 | const selection = this.runtime.alloc(selectionLayout.size); |
| 775 | let start = 0; |
| 776 | let endRef = 0; |
| 777 | try { |
| 778 | this.runtime.setField(selection, "GhosttySelection", "size", selectionLayout.size); |
| 779 | start = this.gridRef(anchor.x, anchor.y, anchor.tag ?? 1); |
| 780 | endRef = this.gridRef(end.x, end.y, end.tag ?? 1); |
| 781 | const startField = selectionLayout.fields.start!; |
| 782 | const endField = selectionLayout.fields.end!; |
| 783 | this.runtime |
| 784 | .bytes(selection + startField.offset, startField.size) |
| 785 | .set(this.runtime.bytes(start, startField.size)); |
| 786 | this.runtime |
| 787 | .bytes(selection + endField.offset, endField.size) |
| 788 | .set(this.runtime.bytes(endRef, endField.size)); |
| 789 | this.runtime.call("ghostty_terminal_set", this.terminal, 21, selection); |
| 790 | } finally { |
| 791 | this.runtime.free(start, gridRefSize); |
| 792 | this.runtime.free(endRef, gridRefSize); |
| 793 | this.runtime.free(selection, selectionLayout.size); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | selectAll(): GhosttySelectionRange["screen"] | null { |
| 798 | this.ensureActive(); |
| 799 | const layout = this.runtime.layout("GhosttySelection"); |
| 800 | const selection = this.runtime.alloc(layout.size); |
| 801 | try { |
| 802 | this.runtime.setField(selection, "GhosttySelection", "size", layout.size); |
| 803 | if ( |
| 804 | this.runtime.call("ghostty_terminal_select_all", this.terminal, selection) !== |
| 805 | GHOSTTY_SUCCESS |
| 806 | ) { |
| 807 | return null; |
| 808 | } |
| 809 | const start = this.pointFromGridRef(selection + layout.fields.start!.offset, 2); |
| 810 | const end = this.pointFromGridRef(selection + layout.fields.end!.offset, 2); |
| 811 | if (!start || !end) return null; |
| 812 | this.runtime.call("ghostty_terminal_set", this.terminal, 21, selection); |
| 813 | return { start, end }; |
| 814 | } finally { |
| 815 | this.runtime.free(selection, layout.size); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | selectionRange(): GhosttySelectionRange["screen"] | null { |
| 820 | this.ensureActive(); |
| 821 | const layout = this.runtime.layout("GhosttySelection"); |
| 822 | const selection = this.runtime.alloc(layout.size); |
| 823 | try { |
| 824 | this.runtime.setField(selection, "GhosttySelection", "size", layout.size); |
| 825 | if ( |
| 826 | this.runtime.call("ghostty_terminal_get", this.terminal, 31, selection) !== GHOSTTY_SUCCESS |
| 827 | ) { |
| 828 | return null; |
| 829 | } |
| 830 | const start = this.pointFromGridRef(selection + layout.fields.start!.offset, 2); |
| 831 | const end = this.pointFromGridRef(selection + layout.fields.end!.offset, 2); |
| 832 | return start && end ? { start, end } : null; |
| 833 | } finally { |
| 834 | this.runtime.free(selection, layout.size); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | selectWord(col: number, row: number): GhosttySelectionRange | null { |
| 839 | return this.selectAt( |
| 840 | "GhosttyTerminalSelectWordOptions", |
| 841 | "ghostty_terminal_select_word", |
| 842 | col, |
| 843 | row, |
| 844 | ); |
| 845 | } |
| 846 | |
| 847 | selectLine(col: number, row: number): GhosttySelectionRange | null { |
| 848 | return this.selectAt( |
| 849 | "GhosttyTerminalSelectLineOptions", |
| 850 | "ghostty_terminal_select_line", |
| 851 | col, |
| 852 | row, |
| 853 | ); |
| 854 | } |
| 855 | |
| 856 | hyperlinkAt(col: number, row: number): string | null { |
| 857 | this.ensureActive(); |
| 858 | const ref = this.gridRef(col, row); |
| 859 | const written = this.runtime.call("ghostty_wasm_alloc_usize"); |
| 860 | const sizeResult = this.runtime.call("ghostty_grid_ref_hyperlink_uri", ref, 0, 0, written); |
| 861 | const outputSize = this.runtime.view(written, 4).getUint32(0, true); |
| 862 | let hyperlink: string | null = null; |
| 863 | if (sizeResult === GHOSTTY_OUT_OF_SPACE && outputSize > 0) { |
| 864 | const output = this.runtime.alloc(outputSize); |
| 865 | const result = this.runtime.call( |
| 866 | "ghostty_grid_ref_hyperlink_uri", |
| 867 | ref, |
| 868 | output, |
| 869 | outputSize, |
| 870 | written, |
| 871 | ); |
| 872 | const outputLength = this.runtime.view(written, 4).getUint32(0, true); |
| 873 | if (result === GHOSTTY_SUCCESS && outputLength > 0) { |
| 874 | hyperlink = decoder.decode(this.runtime.bytes(output, outputLength)); |
| 875 | } |
| 876 | this.runtime.free(output, outputSize); |
| 877 | } |
| 878 | this.runtime.call("ghostty_wasm_free_usize", written); |
| 879 | this.runtime.free(ref, this.runtime.layout("GhosttyGridRef").size); |
| 880 | return hyperlink; |
| 881 | } |
| 882 | |
| 883 | clearSelection(): void { |
| 884 | this.ensureActive(); |
| 885 | this.runtime.call("ghostty_terminal_set", this.terminal, 21, 0); |
| 886 | } |
| 887 | |
| 888 | snapshot(): GhosttySnapshot { |
| 889 | this.ensureActive(); |
| 890 | this.assertSuccess( |
| 891 | "ghostty_render_state_update", |
| 892 | this.runtime.call("ghostty_render_state_update", this.renderState, this.terminal), |
| 893 | ); |
| 894 | const cols = this.getU16(RENDER_DATA.cols); |
| 895 | const rowCount = this.getU16(RENDER_DATA.rows); |
| 896 | const foreground = this.getColor(RENDER_DATA.foreground, { r: 229, g: 231, b: 235 }); |
| 897 | const background = this.getColor(RENDER_DATA.background, { r: 0, g: 0, b: 0 }); |
| 898 | const colorsChanged = |
| 899 | this.rowColors !== null && |
| 900 | (!sameColor(this.rowColors.foreground, foreground) || |
| 901 | !sameColor(this.rowColors.background, background)); |
| 902 | this.rowColors = { foreground, background }; |
| 903 | const dirty = colorsChanged ? 2 : this.getU32(RENDER_DATA.dirty); |
| 904 | const cursorHasValue = this.getBool(RENDER_DATA.cursorHasValue); |
| 905 | const cursor = cursorHasValue ? this.getColor(RENDER_DATA.cursor, foreground) : foreground; |
| 906 | const cursorInViewport = this.getBool(RENDER_DATA.cursorInViewport); |
| 907 | const cursorVisible = this.getBool(RENDER_DATA.cursorVisible) && cursorInViewport; |
| 908 | const cursorX = cursorInViewport ? this.getU16(RENDER_DATA.cursorX) : -1; |
| 909 | const cursorY = cursorInViewport ? this.getU16(RENDER_DATA.cursorY) : -1; |
| 910 | |
| 911 | if (this.rows.length !== rowCount || this.rows.some((row) => row.cells.length !== cols)) { |
| 912 | this.rows = Array.from({ length: rowCount }, () => ({ |
| 913 | cells: Array.from({ length: cols }, () => this.emptyCell(foreground, background)), |
| 914 | text: "", |
| 915 | isWrapContinuation: false, |
| 916 | wrapsToNext: false, |
| 917 | })); |
| 918 | } |
| 919 | |
| 920 | const dirtyRows = new Set<number>(); |
| 921 | if (dirty !== 0) { |
| 922 | this.assertSuccess( |
| 923 | "ghostty_render_state_get(row iterator)", |
| 924 | this.runtime.call( |
| 925 | "ghostty_render_state_get", |
| 926 | this.renderState, |
| 927 | RENDER_DATA.rowIterator, |
| 928 | this.rowIteratorSlot, |
| 929 | ), |
| 930 | ); |
| 931 | const iterator = this.runtime.readPointer(this.rowIteratorSlot); |
| 932 | let rowIndex = 0; |
| 933 | while ( |
| 934 | rowIndex < rowCount && |
| 935 | this.runtime.call("ghostty_render_state_row_iterator_next", iterator) !== 0 |
| 936 | ) { |
| 937 | const rowDirty = dirty === 2 || this.getRowBool(iterator, ROW_DATA.dirty); |
| 938 | if (rowDirty) { |
| 939 | this.rows[rowIndex] = this.readRow(iterator, cols, foreground, background); |
| 940 | dirtyRows.add(rowIndex); |
| 941 | this.runtime.memoryBytes()[this.scratch] = 0; |
| 942 | this.runtime.call("ghostty_render_state_row_set", iterator, 0, this.scratch); |
| 943 | } |
| 944 | rowIndex += 1; |
| 945 | } |
| 946 | this.runtime.memoryDataView().setUint32(this.scratch, 0, true); |
| 947 | this.runtime.call("ghostty_render_state_set", this.renderState, 0, this.scratch); |
| 948 | } |
| 949 | |
| 950 | return { |
| 951 | cols, |
| 952 | rows: rowCount, |
| 953 | foreground, |
| 954 | background, |
| 955 | cursor, |
| 956 | cursorX, |
| 957 | cursorY, |
| 958 | cursorVisible, |
| 959 | cursorBlinking: this.getBool(RENDER_DATA.cursorBlinking), |
| 960 | cursorStyle: this.getU32(RENDER_DATA.cursorStyle), |
| 961 | dirtyRows, |
| 962 | rowData: this.rows, |
| 963 | }; |
| 964 | } |
| 965 | |
| 966 | selectionText(): string { |
| 967 | this.ensureActive(); |
| 968 | const options = this.runtime.alloc(SELECTION_FORMAT_OPTIONS_SIZE); |
| 969 | const optionsView = this.runtime.view(options, SELECTION_FORMAT_OPTIONS_SIZE); |
| 970 | optionsView.setUint32(0, SELECTION_FORMAT_OPTIONS_SIZE, true); |
| 971 | optionsView.setUint32(4, 0, true); |
| 972 | optionsView.setUint8(8, 1); |
| 973 | optionsView.setUint8(9, 1); |
| 974 | optionsView.setUint32(12, 0, true); |
| 975 | const written = this.runtime.call("ghostty_wasm_alloc_usize"); |
| 976 | const sizeResult = this.runtime.call( |
| 977 | "ghostty_terminal_selection_format_buf", |
| 978 | this.terminal, |
| 979 | options, |
| 980 | 0, |
| 981 | 0, |
| 982 | written, |
| 983 | ); |
| 984 | const outputSize = this.runtime.view(written, 4).getUint32(0, true); |
| 985 | let text = ""; |
| 986 | if (sizeResult === GHOSTTY_OUT_OF_SPACE && outputSize > 0) { |
| 987 | const output = this.runtime.alloc(outputSize); |
| 988 | const result = this.runtime.call( |
| 989 | "ghostty_terminal_selection_format_buf", |
| 990 | this.terminal, |
| 991 | options, |
| 992 | output, |
| 993 | outputSize, |
| 994 | written, |
| 995 | ); |
| 996 | const outputLength = this.runtime.view(written, 4).getUint32(0, true); |
| 997 | if (result === GHOSTTY_SUCCESS) { |
| 998 | text = decoder.decode(this.runtime.bytes(output, outputLength)); |
| 999 | } |
| 1000 | this.runtime.free(output, outputSize); |
| 1001 | } |
| 1002 | this.runtime.call("ghostty_wasm_free_usize", written); |
| 1003 | this.runtime.free(options, SELECTION_FORMAT_OPTIONS_SIZE); |
| 1004 | return text; |
| 1005 | } |
| 1006 | |
| 1007 | viewportPointToScreen(col: number, row: number): { x: number; y: number } | null { |
| 1008 | return this.convertPoint(col, row, 1, 2); |
| 1009 | } |
| 1010 | |
| 1011 | screenPointToViewport(col: number, row: number): { x: number; y: number } | null { |
| 1012 | return this.convertPoint(col, row, 2, 1); |
| 1013 | } |
| 1014 | |
| 1015 | private convertPoint( |
| 1016 | col: number, |
| 1017 | row: number, |
| 1018 | fromTag: 1 | 2, |
| 1019 | toTag: 1 | 2, |
| 1020 | ): { x: number; y: number } | null { |
| 1021 | this.ensureActive(); |
| 1022 | const ref = this.gridRef(col, row, fromTag); |
| 1023 | const point = this.pointFromGridRef(ref, toTag); |
| 1024 | this.runtime.free(ref, this.runtime.layout("GhosttyGridRef").size); |
| 1025 | return point; |
| 1026 | } |
| 1027 | |
| 1028 | dispose(): void { |
| 1029 | if (this.disposed) return; |
| 1030 | this.disposed = true; |
| 1031 | if (this.mouseEvent) this.runtime.call("ghostty_mouse_event_free", this.mouseEvent); |
| 1032 | if (this.mouseEncoder) this.runtime.call("ghostty_mouse_encoder_free", this.mouseEncoder); |
| 1033 | if (this.keyEvent) this.runtime.call("ghostty_key_event_free", this.keyEvent); |
| 1034 | if (this.keyEncoder) this.runtime.call("ghostty_key_encoder_free", this.keyEncoder); |
| 1035 | if (this.rowCellsSlot) { |
| 1036 | const cells = this.runtime.readPointer(this.rowCellsSlot); |
| 1037 | if (cells) this.runtime.call("ghostty_render_state_row_cells_free", cells); |
| 1038 | } |
| 1039 | if (this.rowIteratorSlot) { |
| 1040 | const iterator = this.runtime.readPointer(this.rowIteratorSlot); |
| 1041 | if (iterator) this.runtime.call("ghostty_render_state_row_iterator_free", iterator); |
| 1042 | } |
| 1043 | if (this.renderState) this.runtime.call("ghostty_render_state_free", this.renderState); |
| 1044 | if (this.terminal) { |
| 1045 | if (this.ptyWriterId) this.runtime.detachPtyWriter(this.terminal, this.ptyWriterId); |
| 1046 | this.runtime.call("ghostty_terminal_free", this.terminal); |
| 1047 | } |
| 1048 | if (this.style) this.runtime.free(this.style, this.runtime.layout("GhosttyStyle").size); |
| 1049 | if (this.cellQuery) this.runtime.free(this.cellQuery, CELL_QUERY.size); |
| 1050 | if (this.scrollbar) { |
| 1051 | this.runtime.free(this.scrollbar, this.runtime.layout("GhosttyTerminalScrollbar").size); |
| 1052 | } |
| 1053 | if (this.scratch) this.runtime.free(this.scratch, 16); |
| 1054 | if (this.graphemes) this.runtime.free(this.graphemes, this.graphemeCapacity); |
| 1055 | for (const slot of [ |
| 1056 | this.mouseEventSlot, |
| 1057 | this.mouseEncoderSlot, |
| 1058 | this.keyEventSlot, |
| 1059 | this.keyEncoderSlot, |
| 1060 | this.rowCellsSlot, |
| 1061 | this.rowIteratorSlot, |
| 1062 | this.renderStateSlot, |
| 1063 | this.terminalSlot, |
| 1064 | ]) { |
| 1065 | this.runtime.freeOpaque(slot); |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | private kittyKeyboardFlags(): number { |
| 1070 | this.runtime.bytes(this.scratch, 1)[0] = 0; |
| 1071 | this.runtime.call("ghostty_terminal_get", this.terminal, 8, this.scratch); |
| 1072 | return this.runtime.bytes(this.scratch, 1)[0]!; |
| 1073 | } |
| 1074 | |
| 1075 | /** Reads a DEC private mode; an unknown mode reads as off. */ |
| 1076 | private modeEnabled(mode: number): boolean { |
| 1077 | this.runtime.bytes(this.scratch, 1)[0] = 0; |
| 1078 | return ( |
| 1079 | this.runtime.call("ghostty_terminal_mode_get", this.terminal, mode, this.scratch) === |
| 1080 | GHOSTTY_SUCCESS && this.runtime.bytes(this.scratch, 1)[0] !== 0 |
| 1081 | ); |
| 1082 | } |
| 1083 | |
| 1084 | private encodeOutput( |
| 1085 | written: number, |
| 1086 | encode: (output: number, outputSize: number) => number, |
| 1087 | ): Uint8Array { |
| 1088 | const sizeResult = encode(0, 0); |
| 1089 | const outputSize = this.runtime.view(written, 4).getUint32(0, true); |
| 1090 | if (sizeResult === GHOSTTY_SUCCESS && outputSize === 0) return new Uint8Array(); |
| 1091 | if (sizeResult !== GHOSTTY_OUT_OF_SPACE || outputSize === 0) return new Uint8Array(); |
| 1092 | |
| 1093 | const output = this.runtime.alloc(outputSize); |
| 1094 | const result = encode(output, outputSize); |
| 1095 | const outputLength = this.runtime.view(written, 4).getUint32(0, true); |
| 1096 | const encoded = |
| 1097 | result === GHOSTTY_SUCCESS |
| 1098 | ? this.runtime.bytes(output, outputLength).slice() |
| 1099 | : new Uint8Array(); |
| 1100 | this.runtime.free(output, outputSize); |
| 1101 | return encoded; |
| 1102 | } |
| 1103 | |
| 1104 | private readRow( |
| 1105 | iterator: number, |
| 1106 | cols: number, |
| 1107 | defaultForeground: GhosttyColor, |
| 1108 | defaultBackground: GhosttyColor, |
| 1109 | ): GhosttyRow { |
| 1110 | this.assertSuccess( |
| 1111 | "ghostty_render_state_row_get(raw)", |
| 1112 | this.runtime.call("ghostty_render_state_row_get", iterator, ROW_DATA.raw, this.scratch), |
| 1113 | ); |
| 1114 | const rawRow = this.runtime.memoryDataView().getBigUint64(this.scratch, true); |
| 1115 | this.runtime.memoryBytes()[this.scratch + 8] = 0; |
| 1116 | this.assertSuccess( |
| 1117 | "ghostty_row_get(wrap continuation)", |
| 1118 | this.runtime.call("ghostty_row_get", rawRow, 2, this.scratch + 8), |
| 1119 | ); |
| 1120 | const isWrapContinuation = this.runtime.memoryBytes()[this.scratch + 8] !== 0; |
| 1121 | this.runtime.memoryBytes()[this.scratch + 8] = 0; |
| 1122 | this.assertSuccess( |
| 1123 | "ghostty_row_get(wrap)", |
| 1124 | this.runtime.call("ghostty_row_get", rawRow, 1, this.scratch + 8), |
| 1125 | ); |
| 1126 | const wrapsToNext = this.runtime.memoryBytes()[this.scratch + 8] !== 0; |
| 1127 | |
| 1128 | this.assertSuccess( |
| 1129 | "ghostty_render_state_row_get(cells)", |
| 1130 | this.runtime.call( |
| 1131 | "ghostty_render_state_row_get", |
| 1132 | iterator, |
| 1133 | ROW_DATA.cells, |
| 1134 | this.rowCellsSlot, |
| 1135 | ), |
| 1136 | ); |
| 1137 | const cellsIterator = this.runtime.readPointer(this.rowCellsSlot); |
| 1138 | const { size: styleSize, fields: styleFields } = this.runtime.layout("GhosttyStyle"); |
| 1139 | const cellsNext = this.runtime.fn("ghostty_render_state_row_cells_next"); |
| 1140 | const cellsGetMulti = this.runtime.fn("ghostty_render_state_row_cells_get_multi"); |
| 1141 | const cells: GhosttyCell[] = []; |
| 1142 | while (cells.length < cols && cellsNext(cellsIterator) !== 0) { |
| 1143 | this.runtime.memoryBytes().fill(0, this.style, this.style + styleSize); |
| 1144 | this.runtime.setField(this.style, "GhosttyStyle", "size", styleSize); |
| 1145 | const result = cellsGetMulti( |
| 1146 | cellsIterator, |
| 1147 | 5, |
| 1148 | this.cellQuery + CELL_QUERY.keys, |
| 1149 | this.cellQuery + CELL_QUERY.values, |
| 1150 | this.cellQuery + CELL_QUERY.written, |
| 1151 | ); |
| 1152 | const queryView = this.runtime.memoryDataView(); |
| 1153 | const written = queryView.getUint32(this.cellQuery + CELL_QUERY.written, true); |
| 1154 | if (written < 3) this.assertSuccess("ghostty_render_state_row_cells_get_multi", result); |
| 1155 | const graphemeLength = queryView.getUint32(this.cellQuery + CELL_QUERY.graphemesLength, true); |
| 1156 | const selected = queryView.getUint8(this.cellQuery + CELL_QUERY.selected) !== 0; |
| 1157 | let background = |
| 1158 | written >= 4 ? this.readColor(this.cellQuery + CELL_QUERY.background) : defaultBackground; |
| 1159 | let foreground = |
| 1160 | written === 5 |
| 1161 | ? this.readColor(this.cellQuery + CELL_QUERY.foreground) |
| 1162 | : written === 3 |
| 1163 | ? this.getCellColor(cellsIterator, CELL_DATA.foreground, defaultForeground) |
| 1164 | : defaultForeground; |
| 1165 | let text = ""; |
| 1166 | if (graphemeLength > 0) { |
| 1167 | const bufferSize = graphemeLength * 4; |
| 1168 | if (bufferSize > this.graphemeCapacity) { |
| 1169 | const capacity = Math.max(bufferSize, this.graphemeCapacity * 2); |
| 1170 | const buffer = this.runtime.alloc(capacity); |
| 1171 | this.runtime.free(this.graphemes, this.graphemeCapacity); |
| 1172 | this.graphemes = buffer; |
| 1173 | this.graphemeCapacity = capacity; |
| 1174 | } |
| 1175 | if ( |
| 1176 | this.runtime.call( |
| 1177 | "ghostty_render_state_row_cells_get", |
| 1178 | cellsIterator, |
| 1179 | CELL_DATA.graphemes, |
| 1180 | this.graphemes, |
| 1181 | ) === GHOSTTY_SUCCESS |
| 1182 | ) { |
| 1183 | // Byte allocations need not be 4-byte aligned; use DataView instead of Uint32Array. |
| 1184 | text = ghosttyCellText(this.runtime.memoryDataView(), graphemeLength, this.graphemes); |
| 1185 | } |
| 1186 | } |
| 1187 | let wide = 0; |
| 1188 | // A wrap spacer in the final column can follow a spacer tail. |
| 1189 | if (text.length === 0 && (cells.at(-1)?.text.length || cells.length === cols - 1)) { |
| 1190 | this.assertSuccess( |
| 1191 | "ghostty_render_state_row_cells_get(raw)", |
| 1192 | this.runtime.call( |
| 1193 | "ghostty_render_state_row_cells_get", |
| 1194 | cellsIterator, |
| 1195 | CELL_DATA.raw, |
| 1196 | this.scratch, |
| 1197 | ), |
| 1198 | ); |
| 1199 | const rawCell = this.runtime.memoryDataView().getBigUint64(this.scratch, true); |
| 1200 | this.runtime.memoryDataView().setUint32(this.scratch + 8, 0, true); |
| 1201 | this.assertSuccess( |
| 1202 | "ghostty_cell_get(wide)", |
| 1203 | this.runtime.call("ghostty_cell_get", rawCell, RAW_CELL_DATA.wide, this.scratch + 8), |
| 1204 | ); |
| 1205 | wide = this.runtime.memoryDataView().getUint32(this.scratch + 8, true); |
| 1206 | } |
| 1207 | // Read the style after allocation and ABI calls, which can grow WASM memory. |
| 1208 | const styleView = this.runtime.memoryDataView(); |
| 1209 | if (styleView.getUint8(this.style + styleFields.inverse!.offset) !== 0) { |
| 1210 | [foreground, background] = [background, foreground]; |
| 1211 | } |
| 1212 | if (styleView.getUint8(this.style + styleFields.faint!.offset) !== 0) { |
| 1213 | foreground = blend(foreground, background); |
| 1214 | } |
| 1215 | cells.push({ |
| 1216 | text, |
| 1217 | wide, |
| 1218 | foreground, |
| 1219 | background, |
| 1220 | bold: styleView.getUint8(this.style + styleFields.bold!.offset) !== 0, |
| 1221 | italic: styleView.getUint8(this.style + styleFields.italic!.offset) !== 0, |
| 1222 | invisible: styleView.getUint8(this.style + styleFields.invisible!.offset) !== 0, |
| 1223 | strikethrough: styleView.getUint8(this.style + styleFields.strikethrough!.offset) !== 0, |
| 1224 | overline: styleView.getUint8(this.style + styleFields.overline!.offset) !== 0, |
| 1225 | underline: styleView.getInt32(this.style + styleFields.underline!.offset, true) !== 0, |
| 1226 | selected, |
| 1227 | }); |
| 1228 | } |
| 1229 | while (cells.length < cols) cells.push(this.emptyCell(defaultForeground, defaultBackground)); |
| 1230 | return { |
| 1231 | cells, |
| 1232 | text: cells |
| 1233 | .map((cell) => cell.text || " ") |
| 1234 | .join("") |
| 1235 | .trimEnd(), |
| 1236 | isWrapContinuation, |
| 1237 | wrapsToNext, |
| 1238 | }; |
| 1239 | } |
| 1240 | |
| 1241 | private gridRef(col: number, row: number, tag: 1 | 2 = 1): number { |
| 1242 | const pointLayout = this.runtime.layout("GhosttyPoint"); |
| 1243 | const point = this.runtime.alloc(pointLayout.size); |
| 1244 | this.runtime.setField(point, "GhosttyPoint", "tag", tag); |
| 1245 | const pointValue = pointLayout.fields.value!; |
| 1246 | const valueOffset = pointValue.offset; |
| 1247 | const view = this.runtime.view(point + valueOffset, pointValue.size); |
| 1248 | view.setUint16(0, Math.max(0, col), true); |
| 1249 | view.setUint32(4, Math.max(0, row), true); |
| 1250 | const gridRefSize = this.runtime.layout("GhosttyGridRef").size; |
| 1251 | const gridRef = this.runtime.alloc(gridRefSize); |
| 1252 | this.runtime.setField(gridRef, "GhosttyGridRef", "size", gridRefSize); |
| 1253 | const result = this.runtime.call("ghostty_terminal_grid_ref", this.terminal, point, gridRef); |
| 1254 | this.runtime.free(point, pointLayout.size); |
| 1255 | if (result !== GHOSTTY_SUCCESS) { |
| 1256 | this.runtime.free(gridRef, gridRefSize); |
| 1257 | this.assertSuccess("ghostty_terminal_grid_ref", result); |
| 1258 | } |
| 1259 | return gridRef; |
| 1260 | } |
| 1261 | |
| 1262 | private selectAt( |
| 1263 | optionsName: "GhosttyTerminalSelectWordOptions" | "GhosttyTerminalSelectLineOptions", |
| 1264 | operation: "ghostty_terminal_select_word" | "ghostty_terminal_select_line", |
| 1265 | col: number, |
| 1266 | row: number, |
| 1267 | ): GhosttySelectionRange | null { |
| 1268 | this.ensureActive(); |
| 1269 | const optionsLayout = this.runtime.layout(optionsName); |
| 1270 | const selectionLayout = this.runtime.layout("GhosttySelection"); |
| 1271 | const options = this.runtime.alloc(optionsLayout.size); |
| 1272 | let ref = 0; |
| 1273 | let selection = 0; |
| 1274 | let range: GhosttySelectionRange | null = null; |
| 1275 | try { |
| 1276 | this.runtime.setField(options, optionsName, "size", optionsLayout.size); |
| 1277 | ref = this.gridRef(col, row); |
| 1278 | const refField = optionsLayout.fields.ref!; |
| 1279 | this.runtime |
| 1280 | .bytes(options + refField.offset, refField.size) |
| 1281 | .set(this.runtime.bytes(ref, refField.size)); |
| 1282 | selection = this.runtime.alloc(selectionLayout.size); |
| 1283 | this.runtime.setField(selection, "GhosttySelection", "size", selectionLayout.size); |
| 1284 | const result = this.runtime.call(operation, this.terminal, options, selection); |
| 1285 | if (result === GHOSTTY_SUCCESS) { |
| 1286 | const start = selection + selectionLayout.fields.start!.offset; |
| 1287 | const end = selection + selectionLayout.fields.end!.offset; |
| 1288 | const viewportStart = this.pointFromGridRef(start, 1); |
| 1289 | const viewportEnd = this.pointFromGridRef(end, 1); |
| 1290 | const screenStart = this.pointFromGridRef(start, 2); |
| 1291 | const screenEnd = this.pointFromGridRef(end, 2); |
| 1292 | if (viewportStart && viewportEnd && screenStart && screenEnd) { |
| 1293 | range = { |
| 1294 | viewport: { start: viewportStart, end: viewportEnd }, |
| 1295 | screen: { start: screenStart, end: screenEnd }, |
| 1296 | }; |
| 1297 | } |
| 1298 | this.runtime.call("ghostty_terminal_set", this.terminal, 21, selection); |
| 1299 | } |
| 1300 | } finally { |
| 1301 | this.runtime.free(selection, selectionLayout.size); |
| 1302 | this.runtime.free(ref, this.runtime.layout("GhosttyGridRef").size); |
| 1303 | this.runtime.free(options, optionsLayout.size); |
| 1304 | } |
| 1305 | return range; |
| 1306 | } |
| 1307 | |
| 1308 | private pointFromGridRef(ref: number, tag: 1 | 2): { x: number; y: number } | null { |
| 1309 | const coordinateLayout = this.runtime.layout("GhosttyPointCoordinate"); |
| 1310 | const coordinate = this.runtime.alloc(coordinateLayout.size); |
| 1311 | const result = this.runtime.call( |
| 1312 | "ghostty_terminal_point_from_grid_ref", |
| 1313 | this.terminal, |
| 1314 | ref, |
| 1315 | tag, |
| 1316 | coordinate, |
| 1317 | ); |
| 1318 | const point = |
| 1319 | result === GHOSTTY_SUCCESS |
| 1320 | ? { |
| 1321 | x: this.runtime.readField(coordinate, "GhosttyPointCoordinate", "x"), |
| 1322 | y: this.runtime.readField(coordinate, "GhosttyPointCoordinate", "y"), |
| 1323 | } |
| 1324 | : null; |
| 1325 | this.runtime.free(coordinate, coordinateLayout.size); |
| 1326 | return point; |
| 1327 | } |
| 1328 | |
| 1329 | private getU16(data: number): number { |
| 1330 | this.runtime.memoryDataView().setUint16(this.scratch, 0, true); |
| 1331 | this.assertSuccess( |
| 1332 | "ghostty_render_state_get", |
| 1333 | this.runtime.call("ghostty_render_state_get", this.renderState, data, this.scratch), |
| 1334 | ); |
| 1335 | return this.runtime.memoryDataView().getUint16(this.scratch, true); |
| 1336 | } |
| 1337 | |
| 1338 | private getU32(data: number): number { |
| 1339 | this.runtime.memoryDataView().setUint32(this.scratch, 0, true); |
| 1340 | this.assertSuccess( |
| 1341 | "ghostty_render_state_get", |
| 1342 | this.runtime.call("ghostty_render_state_get", this.renderState, data, this.scratch), |
| 1343 | ); |
| 1344 | return this.runtime.memoryDataView().getUint32(this.scratch, true); |
| 1345 | } |
| 1346 | |
| 1347 | private getBool(data: number): boolean { |
| 1348 | this.runtime.memoryBytes()[this.scratch] = 0; |
| 1349 | this.assertSuccess( |
| 1350 | "ghostty_render_state_get", |
| 1351 | this.runtime.call("ghostty_render_state_get", this.renderState, data, this.scratch), |
| 1352 | ); |
| 1353 | return this.runtime.memoryBytes()[this.scratch] !== 0; |
| 1354 | } |
| 1355 | |
| 1356 | private getColor(data: number, fallback: GhosttyColor): GhosttyColor { |
| 1357 | this.runtime.memoryBytes().fill(0, this.scratch, this.scratch + 3); |
| 1358 | const result = this.runtime.call( |
| 1359 | "ghostty_render_state_get", |
| 1360 | this.renderState, |
| 1361 | data, |
| 1362 | this.scratch, |
| 1363 | ); |
| 1364 | return result === GHOSTTY_SUCCESS ? this.readColor(this.scratch) : fallback; |
| 1365 | } |
| 1366 | |
| 1367 | private getRowBool(iterator: number, data: number): boolean { |
| 1368 | this.runtime.memoryBytes()[this.scratch] = 0; |
| 1369 | return ( |
| 1370 | this.runtime.call("ghostty_render_state_row_get", iterator, data, this.scratch) === |
| 1371 | GHOSTTY_SUCCESS && this.runtime.memoryBytes()[this.scratch] !== 0 |
| 1372 | ); |
| 1373 | } |
| 1374 | |
| 1375 | private getCellColor(iterator: number, data: number, fallback: GhosttyColor): GhosttyColor { |
| 1376 | this.runtime.memoryBytes().fill(0, this.scratch, this.scratch + 3); |
| 1377 | const result = this.runtime.call( |
| 1378 | "ghostty_render_state_row_cells_get", |
| 1379 | iterator, |
| 1380 | data, |
| 1381 | this.scratch, |
| 1382 | ); |
| 1383 | return result === GHOSTTY_SUCCESS ? this.readColor(this.scratch) : fallback; |
| 1384 | } |
| 1385 | |
| 1386 | private readColor(pointer: number): GhosttyColor { |
| 1387 | const view = this.runtime.memoryDataView(); |
| 1388 | return { |
| 1389 | r: view.getUint8(pointer), |
| 1390 | g: view.getUint8(pointer + 1), |
| 1391 | b: view.getUint8(pointer + 2), |
| 1392 | }; |
| 1393 | } |
| 1394 | |
| 1395 | private emptyCell(foreground: GhosttyColor, background: GhosttyColor): GhosttyCell { |
| 1396 | return { |
| 1397 | text: "", |
| 1398 | wide: 0, |
| 1399 | foreground, |
| 1400 | background, |
| 1401 | bold: false, |
| 1402 | italic: false, |
| 1403 | invisible: false, |
| 1404 | strikethrough: false, |
| 1405 | overline: false, |
| 1406 | underline: false, |
| 1407 | selected: false, |
| 1408 | }; |
| 1409 | } |
| 1410 | |
| 1411 | private assertSuccess(operation: string, result: number): void { |
| 1412 | if (result !== GHOSTTY_SUCCESS) throw new Error(`${operation} failed with result ${result}`); |
| 1413 | } |
| 1414 | |
| 1415 | private ensureActive(): void { |
| 1416 | if (this.disposed) throw new Error("libghostty-vt terminal has been disposed"); |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | export function ghosttyColorsEqual(left: GhosttyColor, right: GhosttyColor): boolean { |
| 1421 | return sameColor(left, right); |
| 1422 | } |