Skip to content

Commit ccffa65

Browse files
author
Brendan DeBeasi
committed
feat: add OSC8 hyperlink support for clickable URLs and file paths
- Update Link component to use native <a> element with OSC8 escape sequences - Add FilePathLink component for file:// URLs (Cmd/Ctrl+click to open) - Update tool displays (Read, Write, Edit, Glob, Grep, List, WebFetch) to use clickable links - Update BlockTool to support JSX titles for clickable file paths in headers Enables Cmd+click (macOS) or Ctrl+click (Linux/Windows) on URLs and file paths in terminals that support OSC8 hyperlinks (iTerm2, Windows Terminal, etc.) Closes anomalyco#1168
1 parent 2e4fe97 commit ccffa65

2 files changed

Lines changed: 65 additions & 29 deletions

File tree

‎packages/opencode/src/cli/cmd/tui/routes/session/index.tsx‎

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import { Filesystem } from "@/util/filesystem"
7171
import { PermissionPrompt } from "./permission"
7272
import { DialogExportOptions } from "../../ui/dialog-export-options"
7373
import { formatTranscript } from "../../util/transcript"
74+
import { FilePathLink } from "../../ui/link"
7475

7576
addDefaultParsers(parsers.parsers)
7677

@@ -1483,7 +1484,12 @@ function InlineTool(props: {
14831484
)
14841485
}
14851486

1486-
function BlockTool(props: { title: string; children: JSX.Element; onClick?: () => void; part?: ToolPart }) {
1487+
function BlockTool(props: {
1488+
title: string | JSX.Element
1489+
children: JSX.Element
1490+
onClick?: () => void
1491+
part?: ToolPart
1492+
}) {
14871493
const { theme } = useTheme()
14881494
const renderer = useRenderer()
14891495
const [hover, setHover] = createSignal(false)
@@ -1554,7 +1560,14 @@ function Write(props: ToolProps<typeof WriteTool>) {
15541560
return (
15551561
<Switch>
15561562
<Match when={props.metadata.diagnostics !== undefined}>
1557-
<BlockTool title={"# Wrote " + normalizePath(props.input.filePath!)} part={props.part}>
1563+
<BlockTool
1564+
title={
1565+
<>
1566+
# Wrote <FilePathLink path={props.input.filePath!}>{normalizePath(props.input.filePath!)}</FilePathLink>
1567+
</>
1568+
}
1569+
part={props.part}
1570+
>
15581571
<line_number fg={theme.textMuted} minWidth={3} paddingRight={1}>
15591572
<code
15601573
conceal={false}
@@ -1577,7 +1590,7 @@ function Write(props: ToolProps<typeof WriteTool>) {
15771590
</Match>
15781591
<Match when={true}>
15791592
<InlineTool icon="←" pending="Preparing write..." complete={props.input.filePath} part={props.part}>
1580-
Write {normalizePath(props.input.filePath!)}
1593+
Write <FilePathLink path={props.input.filePath!}>{normalizePath(props.input.filePath!)}</FilePathLink>
15811594
</InlineTool>
15821595
</Match>
15831596
</Switch>
@@ -1587,7 +1600,10 @@ function Write(props: ToolProps<typeof WriteTool>) {
15871600
function Glob(props: ToolProps<typeof GlobTool>) {
15881601
return (
15891602
<InlineTool icon="✱" pending="Finding files..." complete={props.input.pattern} part={props.part}>
1590-
Glob "{props.input.pattern}" <Show when={props.input.path}>in {normalizePath(props.input.path)} </Show>
1603+
Glob "{props.input.pattern}"{" "}
1604+
<Show when={props.input.path}>
1605+
in <FilePathLink path={props.input.path!}>{normalizePath(props.input.path)}</FilePathLink>{" "}
1606+
</Show>
15911607
<Show when={props.metadata.count}>({props.metadata.count} matches)</Show>
15921608
</InlineTool>
15931609
)
@@ -1596,38 +1612,40 @@ function Glob(props: ToolProps<typeof GlobTool>) {
15961612
function Read(props: ToolProps<typeof ReadTool>) {
15971613
return (
15981614
<InlineTool icon="→" pending="Reading file..." complete={props.input.filePath} part={props.part}>
1599-
Read {normalizePath(props.input.filePath!)} {input(props.input, ["filePath"])}
1615+
Read <FilePathLink path={props.input.filePath!}>{normalizePath(props.input.filePath!)}</FilePathLink>{" "}
1616+
{input(props.input, ["filePath"])}
16001617
</InlineTool>
16011618
)
16021619
}
16031620

16041621
function Grep(props: ToolProps<typeof GrepTool>) {
16051622
return (
16061623
<InlineTool icon="✱" pending="Searching content..." complete={props.input.pattern} part={props.part}>
1607-
Grep "{props.input.pattern}" <Show when={props.input.path}>in {normalizePath(props.input.path)} </Show>
1624+
Grep "{props.input.pattern}"{" "}
1625+
<Show when={props.input.path}>
1626+
in <FilePathLink path={props.input.path!}>{normalizePath(props.input.path)}</FilePathLink>{" "}
1627+
</Show>
16081628
<Show when={props.metadata.matches}>({props.metadata.matches} matches)</Show>
16091629
</InlineTool>
16101630
)
16111631
}
16121632

16131633
function List(props: ToolProps<typeof ListTool>) {
1614-
const dir = createMemo(() => {
1615-
if (props.input.path) {
1616-
return normalizePath(props.input.path)
1617-
}
1618-
return ""
1619-
})
16201634
return (
16211635
<InlineTool icon="→" pending="Listing directory..." complete={props.input.path !== undefined} part={props.part}>
1622-
List {dir()}
1636+
List{" "}
1637+
<Show when={props.input.path}>
1638+
<FilePathLink path={props.input.path!}>{normalizePath(props.input.path)}</FilePathLink>
1639+
</Show>
16231640
</InlineTool>
16241641
)
16251642
}
16261643

16271644
function WebFetch(props: ToolProps<typeof WebFetchTool>) {
1645+
const url = () => (props.input as any).url
16281646
return (
1629-
<InlineTool icon="%" pending="Fetching from the web..." complete={(props.input as any).url} part={props.part}>
1630-
WebFetch {(props.input as any).url}
1647+
<InlineTool icon="%" pending="Fetching from the web..." complete={url()} part={props.part}>
1648+
WebFetch <a href={url()}>{url()}</a>
16311649
</InlineTool>
16321650
)
16331651
}
@@ -1730,7 +1748,15 @@ function Edit(props: ToolProps<typeof EditTool>) {
17301748
return (
17311749
<Switch>
17321750
<Match when={props.metadata.diff !== undefined}>
1733-
<BlockTool title={"← Edit " + normalizePath(props.input.filePath!)} part={props.part}>
1751+
<BlockTool
1752+
title={
1753+
<>
1754+
{"← Edit "}
1755+
<FilePathLink path={props.input.filePath!}>{normalizePath(props.input.filePath!)}</FilePathLink>
1756+
</>
1757+
}
1758+
part={props.part}
1759+
>
17341760
<box paddingLeft={1}>
17351761
<diff
17361762
diff={diffContent()}
@@ -1768,7 +1794,8 @@ function Edit(props: ToolProps<typeof EditTool>) {
17681794
</Match>
17691795
<Match when={true}>
17701796
<InlineTool icon="←" pending="Preparing edit..." complete={props.input.filePath} part={props.part}>
1771-
Edit {normalizePath(props.input.filePath!)} {input({ replaceAll: props.input.replaceAll })}
1797+
Edit <FilePathLink path={props.input.filePath!}>{normalizePath(props.input.filePath!)}</FilePathLink>{" "}
1798+
{input({ replaceAll: props.input.replaceAll })}
17721799
</InlineTool>
17731800
</Match>
17741801
</Switch>
Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
import type { JSX } from "solid-js"
22
import type { RGBA } from "@opentui/core"
3-
import open from "open"
3+
import path from "path"
44

55
export interface LinkProps {
66
href: string
77
children?: JSX.Element | string
88
fg?: RGBA
99
}
1010

11-
/**
12-
* Link component that renders clickable hyperlinks.
13-
* Clicking anywhere on the link text opens the URL in the default browser.
14-
*/
1511
export function Link(props: LinkProps) {
1612
const displayText = props.children ?? props.href
1713

1814
return (
19-
<text
20-
fg={props.fg}
21-
onMouseUp={() => {
22-
open(props.href).catch(() => {})
23-
}}
24-
>
15+
<a href={props.href} style={{ fg: props.fg }}>
2516
{displayText}
26-
</text>
17+
</a>
18+
)
19+
}
20+
21+
export interface FilePathLinkProps {
22+
path: string
23+
children?: JSX.Element | string
24+
fg?: RGBA
25+
}
26+
27+
export function FilePathLink(props: FilePathLinkProps) {
28+
const displayText = props.children ?? props.path
29+
const absolutePath = path.isAbsolute(props.path) ? props.path : path.resolve(process.cwd(), props.path)
30+
const fileUrl = `file://${absolutePath}`
31+
32+
return (
33+
<a href={fileUrl} style={{ fg: props.fg }}>
34+
{displayText}
35+
</a>
2736
)
2837
}

0 commit comments

Comments
 (0)