File size: 13,582 Bytes
50fe3c9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | import { detectMonoFontFamily } from "@/lib/fonts";
import { usePreferencesStore } from "@/modules/settings/preferences";
import { buildTerminalTheme } from "@/styles/terminalTheme";
import { openUrl } from "@tauri-apps/plugin-opener";
import { FitAddon } from "@xterm/addon-fit";
import { SearchAddon } from "@xterm/addon-search";
import { WebLinksAddon } from "@xterm/addon-web-links";
import { WebglAddon } from "@xterm/addon-webgl";
import { Terminal } from "@xterm/xterm";
import { useCallback, useEffect, useLayoutEffect, useRef } from "react";
import {
registerCwdHandler,
registerPromptTracker,
} from "./osc-handlers";
import { openPty, type PtySession } from "./pty-bridge";
const BACKWARD_KILL_WORD = "\x17";
type Callbacks = {
onSearchReady?: (addon: SearchAddon) => void;
onExit?: (code: number) => void;
onCwd?: (cwd: string) => void;
};
// Lives outside React so split/unsplit re-parent the DOM without tearing
// down the term or PTY. Real disposal: `disposeSession`.
type Session = {
term: Terminal;
fitAddon: FitAddon;
searchAddon: SearchAddon;
pty: PtySession | null;
cleanups: (() => void)[];
callbacks: Callbacks;
observer: ResizeObserver | null;
fitTimer: ReturnType<typeof setTimeout> | null;
ptyTimer: ReturnType<typeof setTimeout> | null;
lastSentCols: number;
lastSentRows: number;
lastW: number;
lastH: number;
lastCwd: string | null;
pendingExit: number | null;
webglEnabled: boolean;
webglAddon: WebglAddon | null;
ready: Promise<void>;
disposed: boolean;
initialCwd: string | undefined;
ptyOpening: boolean;
};
const sessions = new Map<number, Session>();
function ensureSession(leafId: number, initialCwd?: string): Session {
const existing = sessions.get(leafId);
if (existing) return existing;
const prefs = usePreferencesStore.getState();
const webglEnabled = prefs.terminalWebglEnabled;
const fontSize = prefs.terminalFontSize;
const term = new Terminal({
fontFamily: detectMonoFontFamily(),
fontSize,
theme: buildTerminalTheme(),
cursorBlink: true,
cursorStyle: "bar",
cursorInactiveStyle: "outline",
// 5k lines × 80 cols × ~16 B per cell ≈ 6 MB per leaf. 10k doubled
// that for output almost no one scrolls back to. Keep this knob in
// mind if/when we add a "scrollback" preference.
scrollback: 5_000,
allowProposedApi: true,
});
const fitAddon = new FitAddon();
term.loadAddon(fitAddon);
const searchAddon = new SearchAddon();
term.loadAddon(searchAddon);
term.loadAddon(
new WebLinksAddon((_e, uri) => openUrl(uri).catch(console.error)),
);
const session: Session = {
term,
fitAddon,
searchAddon,
pty: null,
cleanups: [],
callbacks: {},
observer: null,
fitTimer: null,
ptyTimer: null,
lastSentCols: 0,
lastSentRows: 0,
lastW: 0,
lastH: 0,
lastCwd: null,
pendingExit: null,
webglEnabled,
webglAddon: null,
ready: Promise.resolve(),
disposed: false,
initialCwd,
ptyOpening: false,
};
sessions.set(leafId, session);
term.attachCustomKeyEventHandler((event) => {
if (!isCtrlBackspace(event)) return true;
const pty = session.pty;
if (!pty) return true;
event.preventDefault();
event.stopPropagation();
pty.write(BACKWARD_KILL_WORD);
return false;
});
// Routes through session.pty so respawn doesn't need to rebind.
term.onData((data) => session.pty?.write(data));
// PTY is opened lazily in attachSession after the first fit, so the shell
// starts with the real terminal size and never flushes a 80x24-sized
// prompt into scrollback.
session.ready = (async () => {
await document.fonts.ready;
if (session.disposed) return;
const prompt = registerPromptTracker(term);
session.cleanups.push(prompt.dispose);
session.cleanups.push(
registerCwdHandler(term, (cwd) => {
session.lastCwd = cwd;
session.callbacks.onCwd?.(cwd);
}),
);
})();
return session;
}
function openPtyForSession(
s: Session,
cwd: string | undefined,
): Promise<PtySession> {
return openPty(
s.term.cols,
s.term.rows,
{
// Hot path — keep this callback to a single statement. URL detection
// runs out-of-band against the xterm buffer (see ensureSession's
// scan interval), so no UTF-8 decode or regex happens per chunk.
onData: (bytes) => s.term.write(bytes),
onExit: (code) => {
s.term.options.disableStdin = true;
if (s.callbacks.onExit) s.callbacks.onExit(code);
else s.pendingExit = code;
},
},
cwd,
);
}
export async function respawnSession(
leafId: number,
cwd?: string,
): Promise<void> {
const s = sessions.get(leafId);
if (!s || s.disposed) return;
s.pty?.close();
s.pty = null;
s.term.reset();
s.term.options.disableStdin = false;
s.lastSentCols = 0;
s.lastSentRows = 0;
s.pendingExit = null;
// Hold the flag so attachSession can't open a second PTY while we await.
s.ptyOpening = true;
let pty: PtySession;
try {
pty = await openPtyForSession(s, cwd);
} catch (e) {
s.ptyOpening = false;
console.error("respawnSession: openPty failed:", e);
return;
}
s.ptyOpening = false;
if (s.disposed) {
pty.close();
return;
}
s.pty = pty;
if (s.observer) {
pty.resize(s.term.cols, s.term.rows);
s.lastSentCols = s.term.cols;
s.lastSentRows = s.term.rows;
}
}
function attachSession(
leafId: number,
container: HTMLDivElement,
callbacks: Callbacks,
): void {
const s = sessions.get(leafId);
if (!s || s.disposed) return;
s.callbacks = callbacks;
const firstAttach = !s.term.element;
if (firstAttach) {
s.term.open(container);
} else if (s.term.element && s.term.element.parentNode !== container) {
container.appendChild(s.term.element);
}
// Sync fit before WebGL load and PTY open so the renderer measures the
// real container and the shell starts at the right cols/rows.
s.fitAddon.fit();
s.lastW = container.clientWidth;
s.lastH = container.clientHeight;
if (firstAttach && !s.webglAddon && s.webglEnabled) {
try {
const webgl = new WebglAddon();
webgl.onContextLoss(() => {
webgl.dispose();
if (s.webglAddon === webgl) s.webglAddon = null;
});
s.term.loadAddon(webgl);
s.webglAddon = webgl;
} catch (e) {
console.warn("WebGL renderer unavailable:", e);
}
}
if (!s.pty && !s.ptyOpening) {
s.ptyOpening = true;
s.lastSentCols = s.term.cols;
s.lastSentRows = s.term.rows;
openPtyForSession(s, s.initialCwd)
.then((pty) => {
s.ptyOpening = false;
if (s.disposed) {
pty.close();
return;
}
s.pty = pty;
if (s.term.cols !== s.lastSentCols || s.term.rows !== s.lastSentRows) {
s.lastSentCols = s.term.cols;
s.lastSentRows = s.term.rows;
pty.resize(s.term.cols, s.term.rows);
}
})
.catch((e) => {
s.ptyOpening = false;
console.error("openPty failed:", e);
});
} else if (
s.pty &&
(s.term.cols !== s.lastSentCols || s.term.rows !== s.lastSentRows)
) {
s.lastSentCols = s.term.cols;
s.lastSentRows = s.term.rows;
s.pty.resize(s.term.cols, s.term.rows);
}
s.observer?.disconnect();
s.observer = null;
if (s.fitTimer) {
clearTimeout(s.fitTimer);
s.fitTimer = null;
}
if (s.ptyTimer) {
clearTimeout(s.ptyTimer);
s.ptyTimer = null;
}
// Two-stage debounce:
// - FIT runs frequently (~one frame) so xterm visually keeps up with
// the window during drag. Local, no IPC.
// - PTY_RESIZE only fires on the trailing edge of the drag, because
// SIGWINCH is what causes shells / fancy prompts (powerlevel10k,
// starship) to redraw mid-resize, which the user perceives as
// blinking. The shell only cares about the FINAL size.
const FIT_DEBOUNCE_MS = 8;
const PTY_RESIZE_DEBOUNCE_MS = 256;
const flushPtyResize = () => {
s.ptyTimer = null;
if (!s.pty || s.disposed) return;
if (s.term.cols === s.lastSentCols && s.term.rows === s.lastSentRows)
return;
s.lastSentCols = s.term.cols;
s.lastSentRows = s.term.rows;
s.pty.resize(s.term.cols, s.term.rows);
};
s.observer = new ResizeObserver(() => {
if (s.fitTimer) clearTimeout(s.fitTimer);
s.fitTimer = setTimeout(() => {
s.fitTimer = null;
const w = container.clientWidth;
const h = container.clientHeight;
if (w === s.lastW && h === s.lastH) return;
s.lastW = w;
s.lastH = h;
s.fitAddon.fit();
if (s.ptyTimer) clearTimeout(s.ptyTimer);
s.ptyTimer = setTimeout(flushPtyResize, PTY_RESIZE_DEBOUNCE_MS);
}, FIT_DEBOUNCE_MS);
});
s.observer.observe(container);
// Re-sync App state after re-attach (prior detach cleared callbacks).
if (s.lastCwd !== null) callbacks.onCwd?.(s.lastCwd);
callbacks.onSearchReady?.(s.searchAddon);
if (s.pendingExit !== null) {
const code = s.pendingExit;
s.pendingExit = null;
callbacks.onExit?.(code);
}
}
function detachSession(leafId: number): void {
const s = sessions.get(leafId);
if (!s) return;
s.observer?.disconnect();
s.observer = null;
if (s.fitTimer) {
clearTimeout(s.fitTimer);
s.fitTimer = null;
}
if (s.ptyTimer) {
clearTimeout(s.ptyTimer);
s.ptyTimer = null;
}
s.callbacks = {};
}
export function disposeSession(leafId: number): void {
const s = sessions.get(leafId);
if (!s) return;
s.disposed = true;
s.cleanups.forEach((fn) => fn());
s.observer?.disconnect();
if (s.fitTimer) clearTimeout(s.fitTimer);
if (s.ptyTimer) clearTimeout(s.ptyTimer);
s.pty?.close();
s.term.dispose();
sessions.delete(leafId);
}
type Options = {
leafId: number;
container: React.RefObject<HTMLDivElement | null>;
visible: boolean;
focused?: boolean;
initialCwd?: string;
onSearchReady?: (addon: SearchAddon) => void;
onExit?: (code: number) => void;
onCwd?: (cwd: string) => void;
};
export function useTerminalSession({
leafId,
container,
visible,
focused = true,
initialCwd,
onSearchReady,
onExit,
onCwd,
}: Options) {
const cbRef = useRef({
onSearchReady,
onExit,
onCwd,
});
cbRef.current = {
onSearchReady,
onExit,
onCwd,
};
useEffect(() => {
let cancelled = false;
const s = ensureSession(leafId, initialCwd);
s.ready.then(() => {
if (cancelled || !container.current) return;
attachSession(leafId, container.current, {
onSearchReady: (a) => cbRef.current.onSearchReady?.(a),
onExit: (c) => cbRef.current.onExit?.(c),
onCwd: (c) => cbRef.current.onCwd?.(c),
});
if (visible && focused) s.term.focus();
});
return () => {
cancelled = true;
detachSession(leafId);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [leafId]);
const fontSize = usePreferencesStore((p) => p.terminalFontSize);
useEffect(() => {
const s = sessions.get(leafId);
if (!s) return;
if (s.term.options.fontSize === fontSize) return;
s.term.options.fontSize = fontSize;
s.fitAddon.fit();
}, [leafId, fontSize]);
const webglPref = usePreferencesStore((p) => p.terminalWebglEnabled);
useEffect(() => {
const s = sessions.get(leafId);
if (!s) return;
s.webglEnabled = webglPref;
if (!s.term.element) return;
if (webglPref && !s.webglAddon) {
try {
const webgl = new WebglAddon();
webgl.onContextLoss(() => {
webgl.dispose();
if (s.webglAddon === webgl) s.webglAddon = null;
});
s.term.loadAddon(webgl);
s.webglAddon = webgl;
} catch (e) {
console.warn("WebGL renderer unavailable:", e);
}
} else if (!webglPref && s.webglAddon) {
s.webglAddon.dispose();
s.webglAddon = null;
}
}, [leafId, webglPref]);
useLayoutEffect(() => {
if (!visible) return;
const s = sessions.get(leafId);
if (!s) return;
s.fitAddon.fit();
if (focused) s.term.focus();
}, [leafId, visible, focused]);
const write = useCallback(
(data: string) => sessions.get(leafId)?.pty?.write(data),
[leafId],
);
const focus = useCallback(() => {
sessions.get(leafId)?.term.focus();
}, [leafId]);
const getBuffer = useCallback(
(maxLines = 200): string | null => {
const s = sessions.get(leafId);
if (!s) return null;
const buf = s.term.buffer.active;
const total = buf.length;
const lines: string[] = [];
const start = Math.max(0, total - maxLines);
for (let i = start; i < total; i++) {
lines.push(buf.getLine(i)?.translateToString(true) ?? "");
}
while (lines.length && lines[lines.length - 1] === "") lines.pop();
return lines.join("\n");
},
[leafId],
);
const getSelection = useCallback((): string | null => {
const sel = sessions.get(leafId)?.term.getSelection() ?? "";
return sel.length > 0 ? sel : null;
}, [leafId]);
const applyTheme = useCallback(() => {
const s = sessions.get(leafId);
if (!s) return;
s.term.options.theme = buildTerminalTheme();
}, [leafId]);
return { write, focus, getBuffer, getSelection, applyTheme };
}
function isCtrlBackspace(event: KeyboardEvent): boolean {
return (
event.type === "keydown" &&
event.key === "Backspace" &&
event.ctrlKey &&
!event.altKey &&
!event.metaKey
);
}
|