Address second-round review findings

- Preserve a scrolled-back viewport when session output arrives:
  ghostty-web snaps to the bottom on every write, so re-anchor the
  viewport by the number of lines the write pushed into scrollback
- Read both config and config.ghostty when they coexist in a Ghostty
  config directory instead of letting the first shadow the second
- Linkify mailto/ftp/ssh/git/tel/magnet/gemini/gopher/news URLs in the
  terminal and open non-http schemes with the OS handler, keeping the
  in-app preview flow for http(s)
This commit is contained in:
Pixel Perfect authored and Julius Marminge committed 2026-07-20 18:05:01 +02:00
1 parent 36d1cc6578
commit 4f4a2ede59
4 files changed
+55 -10

No files matched your search

+6 -5
View File
@@ -217,11 +217,12 @@ export const loadGhosttyTerminalStyle: Effect.Effect<
];
const configSources: Array<string> = [];
for (const dir of configDirs) {
const source = yield* readFirstExisting([
path.join(dir, "config"),
path.join(dir, "config.ghostty"),
]);
if (source !== undefined) configSources.push(source);
// Read both file names when they coexist so a stale empty `config` does
// not shadow a populated `config.ghostty` alongside it.
for (const candidate of [path.join(dir, "config"), path.join(dir, "config.ghostty")]) {
const source = yield* readFirstExisting([candidate]);
if (source !== undefined) configSources.push(source);
}
}
if (configSources.length === 0) return undefined;
@@ -85,7 +85,8 @@ function ensureGhosttyReady(): Promise<void> {
// ghostty-web registers built-in OSC8/URL link providers on open() whose activate()
// calls window.open directly, bypassing the app's preview/editor flow. There is no
// public API to remove them, so clear the provider list before installing ours.
// public API to remove them, so clear the provider list before installing ours,
// which covers the same URL schemes and routes them through the app.
function clearBuiltInLinkProviders(terminal: Terminal): void {
const detector = (terminal as unknown as { linkDetector?: { providers?: unknown[] } })
.linkDetector;
@@ -120,6 +121,22 @@ function writeTerminalBuffer(terminal: Terminal, buffer: string): void {
}
}
// ghostty-web snaps the viewport to the bottom on every write, unlike
// xterm.js which keeps a scrolled-back viewport anchored. Restore the anchor
// by advancing viewportY by however many lines the write pushed into
// scrollback (viewportY counts lines back from the bottom).
function writePreservingScrollback(terminal: Terminal, data: string): void {
const viewportY = terminal.getViewportY();
if (viewportY <= 0) {
terminal.write(data);
return;
}
const scrollbackBefore = terminal.getScrollbackLength();
terminal.write(data);
const scrollbackAdded = terminal.getScrollbackLength() - scrollbackBefore;
terminal.scrollToLine(Math.round(viewportY) + scrollbackAdded);
}
function fitTerminalSafely(fitAddon: FitAddon): boolean {
try {
fitAddon.fit();
@@ -768,7 +785,7 @@ export function TerminalViewport({
);
return;
}
const fallbackToBrowser = () => {
const openExternally = () => {
void localApi.shell.openExternal(match.text).catch((error: unknown) => {
writeSystemMessage(
latestTerminal,
@@ -776,13 +793,19 @@ export function TerminalViewport({
);
});
};
// Only http(s) renders in the preview panel; hand other
// schemes (mailto, ssh, tel, ...) straight to the OS.
if (!/^https?:\/\//i.test(match.text)) {
openExternally();
return;
}
void openTerminalLinkInPreview({
url: match.text,
position: { x: event.clientX, y: event.clientY },
threadRef,
openPreview,
localApi,
fallbackToBrowser,
fallbackToBrowser: openExternally,
});
return;
}
@@ -939,7 +962,7 @@ export function TerminalViewport({
// ghostty-web 0.4.0 crashes on zero-length writes (alloc(0) returns an
// out-of-bounds pointer), so skip no-op updates such as status-only bumps.
if (appended.length > 0) {
terminal.write(appended);
writePreservingScrollback(terminal, appended);
}
} else {
writeTerminalBuffer(terminal, current.buffer);
+18
View File
@@ -37,6 +37,24 @@ describe("extractTerminalLinks", () => {
]);
});
it("finds non-http scheme urls", () => {
const line = "email mailto:[email protected] or clone ssh://[email protected]/t3/t3code.git now";
expect(extractTerminalLinks(line)).toEqual([
{
kind: "url",
text: "mailto:[email protected]",
start: 6,
end: 28,
},
{
kind: "url",
text: "ssh://[email protected]/t3/t3code.git",
start: 38,
end: 72,
},
]);
});
it("trims trailing punctuation from links", () => {
const line = "(https://example.com/docs), ./src/main.ts:12.";
expect(extractTerminalLinks(line)).toEqual([
+4 -1
View File
@@ -36,7 +36,10 @@ export interface WrappedTerminalLinkLine {
segments: ReadonlyArray<WrappedTerminalLinkLineSegment>;
}
const URL_PATTERN = /https?:\/\/[^\s"'`<>]+/g;
// Also matches the non-http schemes ghostty and other terminals linkify;
// authority-style schemes require // while opaque ones take a bare payload.
const URL_PATTERN =
/(?:https?|ftp|ssh|git|gemini|gopher):\/\/[^\s"'`<>]+|(?:mailto|tel|news|magnet):[^\s"'`<>:][^\s"'`<>]*/g;
const FILE_PATH_PATTERN =
/(?:~\/|\.{1,2}\/|\/|[A-Za-z]:[\\/]|\\\\)[^\s"'`<>]+|[A-Za-z0-9._-]+(?:\/[A-Za-z0-9._-]+)+(?::\d+){0,2}/g;
const TRAILING_PUNCTUATION_PATTERN = /[.,;!?]+$/;