fix(terminal): address ghostty refresh review
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
2bc4029e81
commit
e2309c1afc
5 files changed
+191
-16
No files matched your search
@@ -0,0 +1,76 @@
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import {
|
||||
ghosttyThemeSearchPaths,
|
||||
parseGhosttyConfig,
|
||||
splitThemeSelection,
|
||||
} from "./ghosttyStyle.ts";
|
||||
|
||||
const posixPath = {
|
||||
isAbsolute: (value: string) => value.startsWith("/"),
|
||||
join: (...segments: ReadonlyArray<string>) =>
|
||||
segments
|
||||
.map((segment, index) =>
|
||||
index === 0 ? segment.replace(/\/$/, "") : segment.replace(/^\//, ""),
|
||||
)
|
||||
.join("/"),
|
||||
};
|
||||
|
||||
const windowsPath = {
|
||||
isAbsolute: (value: string) => /^[A-Za-z]:[\\/]/.test(value),
|
||||
join: (...segments: ReadonlyArray<string>) => segments.join("\\"),
|
||||
};
|
||||
|
||||
describe("parseGhosttyConfig", () => {
|
||||
it("omits empty scalar color assignments", () => {
|
||||
const config = parseGhosttyConfig(`
|
||||
background =
|
||||
foreground = ""
|
||||
cursor-color = #c0ffee
|
||||
`);
|
||||
|
||||
expect(config.colors.background).toBeUndefined();
|
||||
expect(config.colors.foreground).toBeUndefined();
|
||||
expect(config.colors.cursor).toBe("#c0ffee");
|
||||
});
|
||||
});
|
||||
|
||||
describe("splitThemeSelection", () => {
|
||||
it("keeps a Windows absolute theme path as a bare selection", () => {
|
||||
expect(splitThemeSelection("C:/Users/Alex/Ghostty Themes/t3code")).toEqual({
|
||||
light: "C:/Users/Alex/Ghostty Themes/t3code",
|
||||
dark: "C:/Users/Alex/Ghostty Themes/t3code",
|
||||
});
|
||||
});
|
||||
|
||||
it("still parses explicit light and dark theme selections", () => {
|
||||
expect(splitThemeSelection("light:Day,dark:Night")).toEqual({
|
||||
light: "Day",
|
||||
dark: "Night",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("ghosttyThemeSearchPaths", () => {
|
||||
it("searches beside the macOS Application Support config", () => {
|
||||
const candidates = ghosttyThemeSearchPaths(posixPath, {
|
||||
home: "/Users/alex",
|
||||
xdgConfigHome: "/Users/alex/.config",
|
||||
themeName: "t3code",
|
||||
});
|
||||
|
||||
expect(candidates).toContain(
|
||||
"/Users/alex/Library/Application Support/com.mitchellh.ghostty/themes/t3code",
|
||||
);
|
||||
});
|
||||
|
||||
it("tries an absolute theme file before named-theme directories", () => {
|
||||
const candidates = ghosttyThemeSearchPaths(windowsPath, {
|
||||
home: "C:\\Users\\alex",
|
||||
xdgConfigHome: "C:\\Users\\alex\\.config",
|
||||
themeName: "D:\\themes\\t3code",
|
||||
});
|
||||
|
||||
expect(candidates[0]).toBe("D:\\themes\\t3code");
|
||||
});
|
||||
});
|
||||
@@ -35,6 +35,10 @@ function stripQuotes(value: string): string {
|
||||
}
|
||||
|
||||
function applyColorEntry(colors: MutableThemeColors, key: string, value: string): void {
|
||||
// Empty assignments are resets, not colors. Keeping an empty scalar here
|
||||
// would later violate ServerConfig's TrimmedNonEmptyString schema and make
|
||||
// the whole server.getConfig response fail to encode.
|
||||
if (value.length === 0) return;
|
||||
switch (key) {
|
||||
case "background":
|
||||
colors.background = value;
|
||||
@@ -109,20 +113,22 @@ export function parseGhosttyConfig(source: string): GhosttyConfigValues {
|
||||
|
||||
/** Split `theme = light:A,dark:B` into per-mode names; a bare name applies to both. */
|
||||
export function splitThemeSelection(theme: string): { light?: string; dark?: string } {
|
||||
if (!theme.includes(":")) {
|
||||
return { light: theme, dark: theme };
|
||||
}
|
||||
const result: { light?: string; dark?: string } = {};
|
||||
let hasModeSelection = false;
|
||||
for (const part of theme.split(",")) {
|
||||
const separatorIndex = part.indexOf(":");
|
||||
if (separatorIndex <= 0) continue;
|
||||
const mode = part.slice(0, separatorIndex).trim();
|
||||
const name = part.slice(separatorIndex + 1).trim();
|
||||
const match = /^\s*(light|dark):(.*)$/.exec(part);
|
||||
if (!match) continue;
|
||||
const mode = match[1];
|
||||
const name = (match[2] ?? "").trim();
|
||||
if (name.length === 0) continue;
|
||||
hasModeSelection = true;
|
||||
if (mode === "light") result.light = name;
|
||||
if (mode === "dark") result.dark = name;
|
||||
}
|
||||
return result;
|
||||
// A colon is also part of a Windows drive path (C:/theme), and may be part
|
||||
// of another valid bare theme identifier. Only enter per-mode parsing when
|
||||
// an explicit light: or dark: selection was recognized.
|
||||
return hasModeSelection ? result : { light: theme, dark: theme };
|
||||
}
|
||||
|
||||
function hasAnyColor(colors: MutableThemeColors): boolean {
|
||||
@@ -174,6 +180,25 @@ const GHOSTTY_SYSTEM_THEME_DIRS = [
|
||||
"/usr/local/share/ghostty/themes",
|
||||
];
|
||||
|
||||
export function ghosttyThemeSearchPaths(
|
||||
path: Pick<Path.Path, "isAbsolute" | "join">,
|
||||
input: { readonly home: string; readonly xdgConfigHome: string; readonly themeName: string },
|
||||
): ReadonlyArray<string> {
|
||||
return [
|
||||
...(path.isAbsolute(input.themeName) ? [input.themeName] : []),
|
||||
path.join(input.xdgConfigHome, "ghostty", "themes", input.themeName),
|
||||
path.join(
|
||||
input.home,
|
||||
"Library",
|
||||
"Application Support",
|
||||
"com.mitchellh.ghostty",
|
||||
"themes",
|
||||
input.themeName,
|
||||
),
|
||||
...GHOSTTY_SYSTEM_THEME_DIRS.map((dir) => path.join(dir, input.themeName)),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Load terminal font and theme colors from the user's local Ghostty config.
|
||||
* Best effort by design: any missing file or parse problem yields `undefined`
|
||||
@@ -230,10 +255,9 @@ export const loadGhosttyTerminalStyle: Effect.Effect<
|
||||
|
||||
const loadThemeColors = (themeName: string) =>
|
||||
Effect.gen(function* () {
|
||||
const themeSource = yield* readFirstExisting([
|
||||
path.join(xdgConfigHome, "ghostty", "themes", themeName),
|
||||
...GHOSTTY_SYSTEM_THEME_DIRS.map((dir) => dir + "/" + themeName),
|
||||
]);
|
||||
const themeSource = yield* readFirstExisting(
|
||||
ghosttyThemeSearchPaths(path, { home, xdgConfigHome, themeName }),
|
||||
);
|
||||
if (themeSource === undefined) return emptyColors();
|
||||
return parseGhosttyConfig(themeSource).colors;
|
||||
});
|
||||
|
||||
@@ -35,6 +35,7 @@ import { writeTextToClipboard } from "~/hooks/useCopyToClipboard";
|
||||
import { cn } from "~/lib/utils";
|
||||
import { type TerminalContextSelection } from "~/lib/terminalContext";
|
||||
import { useOpenInPreferredEditor } from "../editorPreferences";
|
||||
import { shouldFocusTerminalAfterSetup } from "../terminal-focus";
|
||||
import {
|
||||
collectWrappedTerminalLinkLine,
|
||||
extractTerminalLinks,
|
||||
@@ -445,7 +446,7 @@ export function TerminalViewport({
|
||||
const environmentId = threadRef.environmentId;
|
||||
const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId));
|
||||
const terminalStyle = serverConfig?.terminalStyle;
|
||||
const terminalStyleKey = useMemo(() => JSON.stringify(terminalStyle ?? null), [terminalStyle]);
|
||||
const terminalStyleKey = JSON.stringify(terminalStyle ?? null);
|
||||
const readTerminalStyle = useEffectEvent(() => terminalStyle);
|
||||
const openInPreferredEditor = useOpenInPreferredEditor(
|
||||
environmentId,
|
||||
@@ -462,6 +463,9 @@ export function TerminalViewport({
|
||||
reportFailure: false,
|
||||
});
|
||||
const hasHandledExitRef = useRef(false);
|
||||
const hasMountedTerminalRef = useRef(false);
|
||||
const restoreTerminalFocusOnSetupRef = useRef(false);
|
||||
const focusOnInitialSessionSyncRef = useRef(false);
|
||||
// Bumped when the async WASM gate finishes creating a terminal, so the
|
||||
// session sync effect re-runs against the live terminal even when no
|
||||
// session field changed while the WASM module was loading.
|
||||
@@ -545,6 +549,18 @@ export function TerminalViewport({
|
||||
};
|
||||
applyMountBackground(initialTheme);
|
||||
const fitAddon = new FitAddon();
|
||||
const previouslyFocusedElement =
|
||||
document.activeElement instanceof HTMLElement && document.activeElement !== document.body
|
||||
? document.activeElement
|
||||
: null;
|
||||
const shouldFocusAfterSetup = shouldFocusTerminalAfterSetup({
|
||||
autoFocus: readAutoFocus(),
|
||||
hasMounted: hasMountedTerminalRef.current,
|
||||
restorePreviousTerminalFocus: restoreTerminalFocusOnSetupRef.current,
|
||||
});
|
||||
restoreTerminalFocusOnSetupRef.current = false;
|
||||
hasMountedTerminalRef.current = true;
|
||||
focusOnInitialSessionSyncRef.current = shouldFocusAfterSetup;
|
||||
const terminal = new Terminal({
|
||||
cursorBlink: true,
|
||||
// Ghostty font-size is in points; treat it as CSS px, which reads
|
||||
@@ -560,10 +576,15 @@ export function TerminalViewport({
|
||||
fitTerminalSafely(fitAddon);
|
||||
// ghostty-web's open() focuses unconditionally; in split view the
|
||||
// last-mounted terminal would steal focus from the active one.
|
||||
if (readAutoFocus()) {
|
||||
if (shouldFocusAfterSetup) {
|
||||
terminal.focus();
|
||||
} else {
|
||||
terminal.blur();
|
||||
// open() focuses unconditionally. Restore whatever external control
|
||||
// (commonly the chat composer) had focus before a style-driven remount.
|
||||
if (previouslyFocusedElement?.isConnected) {
|
||||
previouslyFocusedElement.focus({ preventScroll: true });
|
||||
}
|
||||
}
|
||||
|
||||
terminalRef.current = terminal;
|
||||
@@ -912,6 +933,7 @@ export function TerminalViewport({
|
||||
}, 30);
|
||||
|
||||
return () => {
|
||||
restoreTerminalFocusOnSetupRef.current = mount.contains(document.activeElement);
|
||||
window.clearTimeout(fitTimer);
|
||||
inputDisposable.dispose();
|
||||
selectionDisposable.dispose();
|
||||
@@ -945,7 +967,17 @@ export function TerminalViewport({
|
||||
effectDisposed = true;
|
||||
teardown?.();
|
||||
};
|
||||
}, [cwd, environmentId, runtimeEnvKey, terminalId, terminalStyleKey, threadId, worktreePath]);
|
||||
}, [
|
||||
cwd,
|
||||
environmentId,
|
||||
openPreview,
|
||||
runtimeEnvKey,
|
||||
terminalId,
|
||||
terminalStyleKey,
|
||||
threadId,
|
||||
threadRef,
|
||||
worktreePath,
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
const terminal = terminalRef.current;
|
||||
@@ -1003,11 +1035,12 @@ export function TerminalViewport({
|
||||
}, 0);
|
||||
}
|
||||
|
||||
if (previous.version === 0 && autoFocus) {
|
||||
if (previous.version === 0 && autoFocus && focusOnInitialSessionSyncRef.current) {
|
||||
window.requestAnimationFrame(() => {
|
||||
terminal.focus();
|
||||
});
|
||||
}
|
||||
focusOnInitialSessionSyncRef.current = false;
|
||||
previousSessionRef.current = current;
|
||||
}, [autoFocus, terminalBuffer, terminalEpoch, terminalError, terminalStatus, terminalVersion]);
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { shouldFocusTerminalAfterSetup } from "./terminal-focus";
|
||||
|
||||
describe("shouldFocusTerminalAfterSetup", () => {
|
||||
it("focuses the active terminal on its first setup", () => {
|
||||
expect(
|
||||
shouldFocusTerminalAfterSetup({
|
||||
autoFocus: true,
|
||||
hasMounted: false,
|
||||
restorePreviousTerminalFocus: false,
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("does not steal focus when terminal style causes a remount", () => {
|
||||
expect(
|
||||
shouldFocusTerminalAfterSetup({
|
||||
autoFocus: true,
|
||||
hasMounted: true,
|
||||
restorePreviousTerminalFocus: false,
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("restores focus when the terminal itself was focused before remount", () => {
|
||||
expect(
|
||||
shouldFocusTerminalAfterSetup({
|
||||
autoFocus: false,
|
||||
hasMounted: true,
|
||||
restorePreviousTerminalFocus: true,
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,7 @@
|
||||
export function shouldFocusTerminalAfterSetup(input: {
|
||||
readonly autoFocus: boolean;
|
||||
readonly hasMounted: boolean;
|
||||
readonly restorePreviousTerminalFocus: boolean;
|
||||
}): boolean {
|
||||
return input.restorePreviousTerminalFocus || (!input.hasMounted && input.autoFocus);
|
||||
}
|
||||
Reference in new issue
Block a user