fix(web): make terminal links appear clickable only when clickable (#7488)
This commit is contained in:
1 parent
8287f2c3a7
commit
883e1a3cd9
5 files changed
+28
-23
No files matched your search
@@ -43,7 +43,7 @@ import {
|
||||
} from "~/terminal/ghostty/surface";
|
||||
import { type GhosttyColor, type GhosttyTheme } from "~/terminal/ghostty/core";
|
||||
import { useOpenInPreferredEditor } from "../editorPreferences";
|
||||
import { isTerminalLinkActivation, resolvePathLinkTarget } from "../terminal-links";
|
||||
import { isTerminalLinkActivation, isTerminalUrl, resolvePathLinkTarget } from "../terminal-links";
|
||||
import {
|
||||
isDiffToggleShortcut,
|
||||
isTerminalClearShortcut,
|
||||
@@ -750,7 +750,7 @@ export function TerminalViewport({
|
||||
if (!isTerminalLinkActivation(event)) return;
|
||||
const latestTerminal = terminalRef.current;
|
||||
if (!latestTerminal) return;
|
||||
if (/^https?:\/\//u.test(text)) {
|
||||
if (isTerminalUrl(text)) {
|
||||
if (!localApi) {
|
||||
writeSystemMessage(latestTerminal, "Opening links is unavailable in this browser.");
|
||||
return;
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
collectWrappedTerminalLinkLine,
|
||||
extractTerminalLinks,
|
||||
isTerminalLinkActivation,
|
||||
isTerminalUrl,
|
||||
resolvePathLinkTarget,
|
||||
resolveWrappedTerminalLinkRange,
|
||||
wrappedTerminalLinkRangeIntersectsBufferLine,
|
||||
@@ -37,6 +38,24 @@ describe("extractTerminalLinks", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("classifies uppercase schemes as URLs at activation time too", () => {
|
||||
expect(isTerminalUrl("HTTPS://example.com/docs")).toBe(true);
|
||||
expect(isTerminalUrl("Http://example.com")).toBe(true);
|
||||
expect(isTerminalUrl("src/components/main.ts")).toBe(false);
|
||||
expect(isTerminalUrl("httpsdocs/readme.md")).toBe(false);
|
||||
});
|
||||
|
||||
it("finds URLs regardless of scheme casing", () => {
|
||||
expect(extractTerminalLinks("open HTTPS://example.com/docs")).toEqual([
|
||||
{
|
||||
kind: "url",
|
||||
text: "HTTPS://example.com/docs",
|
||||
start: 5,
|
||||
end: 29,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("trims trailing punctuation from links", () => {
|
||||
const line = "(https://example.com/docs), ./src/main.ts:12.";
|
||||
expect(extractTerminalLinks(line)).toEqual([
|
||||
|
||||
@@ -36,7 +36,7 @@ export interface WrappedTerminalLinkLine {
|
||||
segments: ReadonlyArray<WrappedTerminalLinkLineSegment>;
|
||||
}
|
||||
|
||||
const URL_PATTERN = /https?:\/\/[^\s"'`<>]+/g;
|
||||
const URL_PATTERN = /https?:\/\/[^\s"'`<>]+/giu;
|
||||
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 = /[.,;!?]+$/;
|
||||
@@ -80,7 +80,7 @@ function collectMatches(
|
||||
|
||||
const trimmed = trimClosingDelimiters(raw);
|
||||
if (trimmed.length === 0) continue;
|
||||
if (kind === "path" && /^https?:\/\//i.test(trimmed)) continue;
|
||||
if (kind === "path" && isTerminalUrl(trimmed)) continue;
|
||||
|
||||
const candidate: TerminalLinkMatch = {
|
||||
kind,
|
||||
@@ -172,6 +172,10 @@ export function extractTerminalLinks(line: string): TerminalLinkMatch[] {
|
||||
return [...urlMatches, ...pathMatches].toSorted((a, b) => a.start - b.start);
|
||||
}
|
||||
|
||||
export function isTerminalUrl(value: string): boolean {
|
||||
return /^https?:\/\//iu.test(value);
|
||||
}
|
||||
|
||||
export function collectWrappedTerminalLinkLine(
|
||||
bufferLineNumber: number,
|
||||
getLine: (bufferLineIndex: number) => TerminalBufferLineLike | null | undefined,
|
||||
|
||||
@@ -20,7 +20,6 @@ import {
|
||||
resolveTerminalMouseTrackingState,
|
||||
shouldBlinkTerminalCursor,
|
||||
shouldReportTerminalMouse,
|
||||
shouldShowTerminalLinkHover,
|
||||
terminalGridCellAt,
|
||||
terminalScrollbarGeometry,
|
||||
terminalScrollbarOffsetAtPointer,
|
||||
@@ -438,13 +437,6 @@ describe("application mouse reporting", () => {
|
||||
motionData: "\u001b[<35;8;4M",
|
||||
});
|
||||
});
|
||||
|
||||
it("only shows link hover during mouse tracking when the link modifier is held", () => {
|
||||
expect(shouldShowTerminalLinkHover(false, false)).toBe(true);
|
||||
expect(shouldShowTerminalLinkHover(false, true)).toBe(true);
|
||||
expect(shouldShowTerminalLinkHover(true, false)).toBe(false);
|
||||
expect(shouldShowTerminalLinkHover(true, true)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("terminal font resolution", () => {
|
||||
|
||||
@@ -483,13 +483,6 @@ export function isTerminalLinkPointerGesture(
|
||||
: event.ctrlKey && !event.metaKey;
|
||||
}
|
||||
|
||||
export function shouldShowTerminalLinkHover(
|
||||
mouseTracking: boolean,
|
||||
linkModifierActive: boolean,
|
||||
): boolean {
|
||||
return !mouseTracking || linkModifierActive;
|
||||
}
|
||||
|
||||
export function ghosttyMouseButton(button: number): number | null {
|
||||
switch (button) {
|
||||
case 0:
|
||||
@@ -1409,10 +1402,7 @@ export class GhosttyTerminalSurface {
|
||||
|
||||
private refreshHoveredLink(): void {
|
||||
const pointer = this.hoverPointer;
|
||||
const link =
|
||||
pointer && shouldShowTerminalLinkHover(this.core.isMouseTracking(), this.linkModifierActive)
|
||||
? this.linkAt(pointer.x, pointer.y)
|
||||
: null;
|
||||
const link = pointer && this.linkModifierActive ? this.linkAt(pointer.x, pointer.y) : null;
|
||||
this.setHoveredLink(link);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user