Skip to content

Commit 2f1a0e7

Browse files
authored
fix(tui): open clicked transcript links, including wrapped rows (#53903)
Fixes #44442 Refs #51727, #35649
1 parent 434f7b2 commit 2f1a0e7

5 files changed

Lines changed: 211 additions & 5 deletions

File tree

‎packages/tui/src/app.tsx‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import { SessionTabs } from "./component/session-tabs"
7373
import { clampSessionTabsWidth, sessionTabsFitVertically, SESSION_SIDEBAR_WIDTH } from "./ui/layout"
7474
import { createPaneResize } from "./ui/pane-resize"
7575
import { PaneResizeHandle } from "./ui/pane-resize-handle"
76+
import { clickedLink } from "./ui/link"
7677
import { ThemeErrorToast } from "./component/theme-error-toast"
7778
import { createThemeSource, ThemeProvider, useTheme, useThemes } from "./context/theme"
7879
import { Home } from "./routes/home"
@@ -1331,9 +1332,11 @@ function App() {
13311332
evt.preventDefault()
13321333
evt.stopPropagation()
13331334
}}
1334-
onMouseUp={
1335-
copyOnSelectEnabled() ? (event) => Selection.copyOnSelectRelease(event, renderer, toast, clipboard) : undefined
1336-
}
1335+
onMouseUp={(event) => {
1336+
const url = clickedLink(renderer, event)
1337+
if (url) return void openUrl(url).catch(toast.error)
1338+
if (copyOnSelectEnabled()) Selection.copyOnSelectRelease(event, renderer, toast, clipboard)
1339+
}}
13371340
>
13381341
<box
13391342
flexGrow={1}

‎packages/tui/src/feature-plugins/system/storybook/index.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { mermanLayoutsStory } from "./merman-layouts"
66
import { sessionTabsStory } from "./session-tabs"
77
import { sessionLocationMissingStory } from "./session-location-missing"
88
import { oneCellSpinnerStory } from "./one-cell-spinner"
9+
import { linkClicksStory } from "./link-clicks"
910

1011
/**
1112
* A story is a full-screen, fixture-driven simulation of a real production component. Stories own
@@ -17,7 +18,13 @@ export type Story = {
1718
render: (context: Plugin.Context) => JSX.Element
1819
}
1920

20-
const stories: Story[] = [mermanLayoutsStory, sessionTabsStory, sessionLocationMissingStory, oneCellSpinnerStory]
21+
const stories: Story[] = [
22+
mermanLayoutsStory,
23+
sessionTabsStory,
24+
sessionLocationMissingStory,
25+
oneCellSpinnerStory,
26+
linkClicksStory,
27+
]
2128

2229
function Commands(props: { context: Plugin.Context }) {
2330
props.context.keymap.layer(() => ({
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import type { Plugin } from "@opencode/plugin/tui"
2+
import type { SessionMessageAssistant, SessionMessageAssistantText } from "@opencode/client"
3+
import { useTerminalDimensions } from "@opentui/solid"
4+
import { createSignal } from "solid-js"
5+
import { useConfig } from "../../../config"
6+
import { createTimelineAnchors } from "../../../routes/session/anchors"
7+
import { TextPart } from "../../../routes/session/message-parts"
8+
import { context } from "../../../routes/session/render-context"
9+
import { StoryFooter } from "./footer"
10+
import type { Story } from "./index"
11+
12+
const text = `Click, double-click, drag, right-click, or Ctrl/Shift+click the links below.
13+
14+
**Long bare URL:** https://www.rockauto.com/en/catalog/dodge,2004,ram+1500,4.7l+v8,1432463,brake+&+wheel+hub,brake+pad,1684
15+
16+
**Named link:** read [the OpenTUI terminal capabilities guide](https://github.com/anomalyco/opentui/blob/main/packages/core/docs/terminal-capabilities.mdx) for details.
17+
18+
**URL in inline code (not linked):** \`https://github.com/anomalyco/opencode/issues/51727#issuecomment-wrapped-link\`
19+
20+
**Relative link (not http):** see [link.tsx](packages/tui/src/ui/link.tsx).
21+
22+
| Issue | Link |
23+
| --- | --- |
24+
| Wrapped links | https://github.com/anomalyco/opencode/issues/51727 |
25+
| Ctrl+click | https://github.com/anomalyco/opencode/issues/44442 |
26+
27+
- A list item ending in https://example.com/a/very/long/path/that/keeps/going/until/it/wraps/around`
28+
29+
const part: SessionMessageAssistantText = { type: "text", text }
30+
const message: SessionMessageAssistant = {
31+
id: "link-clicks",
32+
type: "assistant",
33+
agent: "build",
34+
model: { providerID: "fixture", id: "fixture" },
35+
time: { created: 0, completed: 1 },
36+
content: [part],
37+
}
38+
39+
function LinkClicksStory(props: { context: Plugin.Context }) {
40+
const dimensions = useTerminalDimensions()
41+
const theme = props.context.theme
42+
const config = useConfig()
43+
const [narrow, setNarrow] = createSignal(true)
44+
const [markdownMode, setMarkdownMode] = createSignal<"source" | "rendered">("rendered")
45+
const width = () => (narrow() ? Math.min(48, dimensions().width) : dimensions().width)
46+
47+
props.context.keymap.layer(() => ({
48+
commands: [
49+
{
50+
bind: "escape",
51+
title: "Back to storybook",
52+
group: "Storybook",
53+
run: () => props.context.ui.router.navigate({ type: "plugin", name: "storybook" }),
54+
},
55+
{
56+
bind: "w",
57+
title: "Toggle narrow width",
58+
group: "Storybook",
59+
run: () => setNarrow((value) => !value),
60+
},
61+
{
62+
bind: "m",
63+
title: "Toggle markdown mode",
64+
group: "Storybook",
65+
run: () => setMarkdownMode((mode) => (mode === "rendered" ? "source" : "rendered")),
66+
},
67+
],
68+
}))
69+
70+
return (
71+
<box
72+
width={dimensions().width}
73+
height={dimensions().height}
74+
flexDirection="column"
75+
backgroundColor={theme.background.base}
76+
>
77+
<context.Provider
78+
value={{
79+
get width() {
80+
return width()
81+
},
82+
get terminal() {
83+
return dimensions()
84+
},
85+
sessionID: "link-clicks",
86+
anchors: createTimelineAnchors(),
87+
groupExpanded: () => true,
88+
setGroupExpanded: () => {},
89+
thinkingMode: () => "show",
90+
markdownMode,
91+
groupExploration: () => false,
92+
diffWrapMode: () => "word",
93+
models: () => [],
94+
messageIndex: () => undefined,
95+
legacyTurns: () => false,
96+
config: config.data,
97+
mutatePending: async () => false,
98+
pendingDelivery: () => undefined,
99+
}}
100+
>
101+
<scrollbox flexGrow={1} minHeight={0}>
102+
<box width={width()} paddingTop={1} paddingRight={2}>
103+
<TextPart part={part} message={message} last />
104+
</box>
105+
</scrollbox>
106+
</context.Provider>
107+
<StoryFooter
108+
context={props.context}
109+
title="storybook / Link clicks"
110+
details={[`${width()} cols`, markdownMode()]}
111+
controls={[
112+
{ shortcut: "w", label: "narrow/full width" },
113+
{ shortcut: "m", label: "rendered/source" },
114+
{ shortcut: "esc", label: "back" },
115+
]}
116+
/>
117+
</box>
118+
)
119+
}
120+
121+
export const linkClicksStory: Story = {
122+
id: "link-clicks",
123+
title: "Link clicks",
124+
render: (context) => <LinkClicksStory context={context} />,
125+
}

‎packages/tui/src/ui/link.tsx‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { JSX } from "solid-js"
2-
import type { RGBA } from "@opentui/core"
2+
import type { CliRenderer, MouseEvent, RGBA } from "@opentui/core"
33
import { openUrl } from "@opencode/util/open"
44

55
export interface LinkProps {
@@ -32,3 +32,16 @@ export function Link(props: LinkProps) {
3232
</text>
3333
)
3434
}
35+
36+
/**
37+
* Returns the hyperlink under a plain left click. opentui marks the release that ends a selection gesture with
38+
* `isDragging`, and a fresh "cell" selection means the pointer never moved; other releases, such as the end of a drag
39+
* that started outside text, are not clicks. Each rendered cell stores its full URL, so a click on any row of a wrapped
40+
* link resolves the whole link. Modified clicks are left to the terminal's own hyperlink handling.
41+
*/
42+
export function clickedLink(renderer: CliRenderer, event: MouseEvent) {
43+
if (!event.isDragging || event.modifiers.ctrl || event.modifiers.alt || event.modifiers.shift) return
44+
const selection = renderer.getSelection()
45+
if (!selection?.isStart || selection.behavior !== "cell") return
46+
return renderer.getLinkAt(event.x, event.y) ?? undefined
47+
}

‎packages/tui/test/ui/link.test.tsx‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/** @jsxImportSource @opentui/solid */
2+
import { expect, test } from "bun:test"
3+
import { SyntaxStyle } from "@opentui/core"
4+
import { ManualClock } from "@opentui/core/testing"
5+
import { testRender, useRenderer } from "@opentui/solid"
6+
import { clickedLink } from "../../src/ui/link"
7+
8+
const url = "https://example.com/catalog/moparts,2004,dodge,ram+1500,4.7l+v8,1432463,brake+pad"
9+
const syntaxStyle = SyntaxStyle.fromStyles({ default: { fg: "#ffffff" } })
10+
11+
test("plain clicks on any row of a wrapped Markdown link resolve the full URL", async () => {
12+
const opened: string[] = []
13+
function Transcript() {
14+
const renderer = useRenderer()
15+
return (
16+
<box
17+
onMouseUp={(event) => {
18+
const link = clickedLink(renderer, event)
19+
if (link) opened.push(link)
20+
}}
21+
>
22+
<box height={1} />
23+
<markdown syntaxStyle={syntaxStyle} content={`See ${url} now.`} />
24+
</box>
25+
)
26+
}
27+
const app = await testRender(() => <Transcript />, { width: 32, height: 6, clock: new ManualClock() })
28+
29+
try {
30+
for (let attempt = 0; attempt < 50 && !app.captureCharFrame().replace(/\s/g, "").includes(url); attempt++) {
31+
await app.renderOnce()
32+
await Bun.sleep(10)
33+
}
34+
const rows = app.captureCharFrame().split("\n")
35+
const head = rows.findIndex((row) => row.includes("https"))
36+
const tail = rows.findIndex((row) => row.includes("brake+pad"))
37+
const x = rows[head]!.indexOf("https")
38+
expect(tail).toBeGreaterThan(head)
39+
40+
await app.mockMouse.click(x, head)
41+
await app.mockMouse.click(rows[tail]!.indexOf("brake"), tail)
42+
expect(opened).toEqual([url, url])
43+
44+
await app.mockMouse.click(0, head)
45+
await app.mockMouse.click(x, head, 0, { modifiers: { ctrl: true } })
46+
await app.mockMouse.click(x, head, 0, { modifiers: { shift: true } })
47+
await app.mockMouse.click(x, head, 2)
48+
await app.mockMouse.drag(0, head, x + 2, head)
49+
await app.mockMouse.drag(0, 0, x + 2, head)
50+
expect(opened).toEqual([url, url])
51+
52+
await app.mockMouse.click(rows[tail]!.indexOf("brake"), tail)
53+
await app.mockMouse.click(rows[tail]!.indexOf("brake"), tail)
54+
expect(opened).toEqual([url, url, url])
55+
} finally {
56+
app.renderer.destroy()
57+
}
58+
})

0 commit comments

Comments
 (0)