-
Notifications
You must be signed in to change notification settings - Fork 46.3k
tui: make URLs clickable + hover-highlight in any terminal #25071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4d86dfe
tui: make URLs clickable + hover-highlight in any terminal
OutThisLife 046d226
tui: address Copilot review — explorer.exe on win32 + comment fixes
OutThisLife d702d8d
tui: address Copilot review #2 — async spawn errors + hover scope + docs
OutThisLife 1b896bd
tui: address Copilot review #3 — hover damage, alt-screen cleanup, op…
OutThisLife 078042c
tui: address Copilot review #4 — doc comment accuracy
OutThisLife f972755
tui: address Copilot review #5 — lint fixes
OutThisLife File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,38 @@ | ||
| import type { ReactNode } from 'react' | ||
| import React from 'react' | ||
| import { c as _c } from 'react/compiler-runtime' | ||
|
|
||
| import { supportsHyperlinks } from '../supports-hyperlinks.js' | ||
|
|
||
| import Text from './Text.js' | ||
| export type Props = { | ||
| readonly children?: ReactNode | ||
| readonly url: string | ||
| // Kept for backwards-compat: prior versions rendered `fallback` instead of | ||
| // the linked content on terminals where supportsHyperlinks() was false. We | ||
| // now always emit the hyperlink metadata so the in-process click/hover | ||
| // dispatcher can act on it regardless of the terminal's own OSC 8 support | ||
| // (see comment in the function body), so `fallback` is no longer wired up. | ||
| // Leaving the prop on the interface keeps existing call sites compiling. | ||
| readonly fallback?: ReactNode | ||
| } | ||
|
|
||
| export default function Link(t0: Props) { | ||
| const $ = _c(5) | ||
|
|
||
| const { children, url, fallback } = t0 | ||
|
|
||
| export default function Link({ children, url }: Props): React.ReactNode { | ||
| // Always emit <ink-link>: the renderer stores `hyperlink` per cell in the | ||
| // screen buffer, which the click dispatcher (Ink.getHyperlinkAt → | ||
| // onHyperlinkClick) reads on mouseup to open URLs externally. Gating this | ||
| // on supportsHyperlinks() broke clicks in Apple Terminal / any terminal | ||
| // not on the OSC 8 allowlist — the cell's hyperlink field stayed empty, | ||
| // so the click pipeline had nothing to open. | ||
| // | ||
| // The OSC 8 escape itself is emitted unconditionally by the renderer | ||
| // (wrapWithOsc8Link in render-node-to-output.ts, oscLink in log-update.ts). | ||
| // Terminals that don't understand OSC 8 silently strip it — including | ||
| // Apple Terminal, which is why hover/click affordance has to come from | ||
| // the in-process overlay (applyHyperlinkHoverHighlight) and not from the | ||
| // terminal's own link rendering. | ||
| const content = children ?? url | ||
|
|
||
| if (supportsHyperlinks()) { | ||
| let t1 | ||
|
|
||
| if ($[0] !== content || $[1] !== url) { | ||
| t1 = ( | ||
| <Text> | ||
| <ink-link href={url}>{content}</ink-link> | ||
| </Text> | ||
| ) | ||
| $[0] = content | ||
| $[1] = url | ||
| $[2] = t1 | ||
| } else { | ||
| t1 = $[2] | ||
| } | ||
|
|
||
| return t1 | ||
| } | ||
|
|
||
| const t1 = fallback ?? content | ||
| let t2 | ||
|
|
||
| if ($[3] !== t1) { | ||
| t2 = <Text>{t1}</Text> | ||
| $[3] = t1 | ||
| $[4] = t2 | ||
| } else { | ||
| t2 = $[4] | ||
| } | ||
|
|
||
| return t2 | ||
| return ( | ||
| <Text> | ||
| <ink-link href={url}>{content}</ink-link> | ||
| </Text> | ||
| ) | ||
| } | ||
| //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdE5vZGUiLCJSZWFjdCIsInN1cHBvcnRzSHlwZXJsaW5rcyIsIlRleHQiLCJQcm9wcyIsImNoaWxkcmVuIiwidXJsIiwiZmF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sUUFBbUIsSUFBbkJLLE9BQW1CO0VBQUEsSUFBQUUsRUFBQTtFQUFBLElBQUFKLENBQUEsUUFBQUcsRUFBQTtJQUExQkMsRUFBQSxJQUFDLElBQUksQ0FBRSxDQUFBRCxFQUFrQixDQUFFLEVBQTFCLElBQUksQ0FBNkI7SUFBQUgsQ0FBQSxNQUFBRyxFQUFBO0lBQUFILENBQUEsTUFBQUksRUFBQTtFQUFBO0lBQUFBLEVBQUEsR0FBQUosQ0FBQTtFQUFBO0VBQUEsT0FBbENJLEVBQWtDO0FBQUEiLCJpZ25vcmVMaXN0IjpbXX0= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { cellAtIndex, CellWidth, type Screen, setCellStyleId, type StylePool } from './screen.js' | ||
|
|
||
| /** | ||
| * Highlight every cell whose OSC 8 hyperlink matches `hoveredUrl` by inverting | ||
| * its style. This is the cursor-hover affordance for clickable links: terminal | ||
| * applications can't change the system mouse cursor, so we light up the link | ||
| * itself when the pointer is over it. Same overlay machinery as | ||
| * applySearchHighlight — post-layout, pure SGR, picked up by the diff. | ||
| * | ||
| * Returns true if any cell was highlighted. The caller decides whether to | ||
| * promote that into a full-frame damage request — for hover specifically, | ||
| * full damage is only useful on enter/leave/change transitions (so the | ||
| * previous frame's inverted cells get re-emitted), not on every steady-state | ||
| * frame the pointer sits on the link. | ||
| */ | ||
| export function applyHyperlinkHoverHighlight( | ||
| screen: Screen, | ||
| hoveredUrl: string | undefined, | ||
| stylePool: StylePool | ||
| ): boolean { | ||
| if (!hoveredUrl) { | ||
| return false | ||
| } | ||
|
|
||
| const w = screen.width | ||
| const height = screen.height | ||
| let applied = false | ||
|
|
||
| for (let row = 0; row < height; row++) { | ||
| const rowOff = row * w | ||
|
|
||
| for (let col = 0; col < w; col++) { | ||
| const cell = cellAtIndex(screen, rowOff + col) | ||
|
|
||
| // Skip SpacerTail — the head cell at col-1 owns the hyperlink, and | ||
| // setCellStyleId on the tail would split the styling of a wide-char | ||
| // glyph mid-cell. The head's restyle covers both halves. | ||
| if (cell.width === CellWidth.SpacerTail) { | ||
| continue | ||
| } | ||
|
|
||
| if (cell.hyperlink !== hoveredUrl) { | ||
| continue | ||
| } | ||
|
|
||
| applied = true | ||
| setCellStyleId(screen, col, row, stylePool.withInverse(cell.styleId)) | ||
| } | ||
| } | ||
|
|
||
| return applied | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.