Address review findings on the ghostty-web terminal

- Retry WASM init on a later mount instead of caching a rejected
  promise until page reload
- Blur non-active terminals after open(): ghostty-web focuses
  unconditionally, letting the last-mounted split steal focus
- Seed the session snapshot with version 0 and a closed status at mount
  so an already-exited session still surfaces its exit and closes the
  viewport
- Keep CSS generic font keywords (monospace, system-ui, ...) unquoted
  when building the terminal font stack
- Resolve named Ghostty themes from the Linux system dirs
  (/usr/share/ghostty/themes, /usr/local/share/ghostty/themes) too
This commit is contained in:
Pixel Perfect authored and Julius Marminge committed 2026-07-20 18:05:01 +02:00
1 parent a6ff5725cc
commit 7266fafbf6
2 files changed
+47 -4

No files matched your search

+8 -2
View File
@@ -166,7 +166,13 @@ function toThemeColors(colors: MutableThemeColors): ServerTerminalThemeColors |
};
}
const GHOSTTY_MACOS_APP_THEME_DIR = "/Applications/Ghostty.app/Contents/Resources/ghostty/themes";
// Bundled theme locations by platform: the macOS app bundle and the
// Linux system resource dirs documented by Ghostty.
const GHOSTTY_SYSTEM_THEME_DIRS = [
"/Applications/Ghostty.app/Contents/Resources/ghostty/themes",
"/usr/share/ghostty/themes",
"/usr/local/share/ghostty/themes",
];
/**
* Load terminal font and theme colors from the user's local Ghostty config.
@@ -208,7 +214,7 @@ export const loadGhosttyTerminalStyle: Effect.Effect<
Effect.gen(function* () {
const themeSource = yield* readFirstExisting([
path.join(xdgConfigHome, "ghostty", "themes", themeName),
GHOSTTY_MACOS_APP_THEME_DIR + "/" + themeName,
...GHOSTTY_SYSTEM_THEME_DIRS.map((dir) => dir + "/" + themeName),
]);
if (themeSource === undefined) return emptyColors();
return parseGhosttyConfig(themeSource).colors;
@@ -74,7 +74,12 @@ const MULTI_CLICK_SELECTION_ACTION_DELAY_MS = 260;
// ghostty-web needs its WASM module loaded once before any Terminal is constructed.
let ghosttyReadyPromise: Promise<void> | null = null;
function ensureGhosttyReady(): Promise<void> {
ghosttyReadyPromise ??= initGhostty();
// Drop a failed init from the cache so a later mount can retry instead of
// reusing the rejected promise until the page reloads.
ghosttyReadyPromise ??= initGhostty().catch((error: unknown) => {
ghosttyReadyPromise = null;
throw error;
});
return ghosttyReadyPromise;
}
@@ -241,7 +246,23 @@ const NERD_FONT_FALLBACK_STACK =
const DEFAULT_TERMINAL_FONT_STACK = `"SF Mono", "SFMono-Regular", "JetBrains Mono", Consolas, "Liberation Mono", Menlo, ${NERD_FONT_FALLBACK_STACK}, monospace`;
const DEFAULT_TERMINAL_FONT_SIZE = 12;
const CSS_GENERIC_FONT_FAMILIES = new Set([
"serif",
"sans-serif",
"monospace",
"cursive",
"fantasy",
"system-ui",
"ui-serif",
"ui-sans-serif",
"ui-monospace",
"ui-rounded",
]);
function quoteFontFamily(family: string): string {
// Generic families must stay unquoted; quoting turns them into literal font
// name lookups (e.g. a font named "monospace") instead of CSS keywords.
if (CSS_GENERIC_FONT_FAMILIES.has(family.toLowerCase())) return family;
return family.includes('"') ? family : `"${family}"`;
}
@@ -427,6 +448,7 @@ export function TerminalViewport({
onAddTerminalContext(selection);
});
const readTerminalLabel = useEffectEvent(() => terminalLabel);
const readAutoFocus = useEffectEvent(() => autoFocus);
const terminalSession = useAttachedTerminalSession({
environmentId,
terminal: {
@@ -502,6 +524,13 @@ export function TerminalViewport({
terminal.open(mount);
clearBuiltInLinkProviders(terminal);
fitTerminalSafely(fitAddon);
// ghostty-web's open() focuses unconditionally; in split view the
// last-mounted terminal would steal focus from the active one.
if (readAutoFocus()) {
terminal.focus();
} else {
terminal.blur();
}
terminalRef.current = terminal;
fitAddonRef.current = fitAddon;
@@ -509,7 +538,15 @@ export function TerminalViewport({
if (initialSession.version > 0 && initialSession.buffer.length > 0) {
writeTerminalBuffer(terminal, initialSession.buffer);
}
previousSessionRef.current = initialSession;
// Seed with version 0 and a closed status (keeping the already-written
// buffer) so the sync effect still surfaces an exited/errored session
// that predates the mount instead of early-returning on equal versions.
previousSessionRef.current = {
buffer: initialSession.buffer,
error: null,
status: "closed",
version: 0,
};
const clearSelectionAction = () => {
selectionActionRequestIdRef.current += 1;