diff --git a/docs/reference.md b/docs/reference.md index 4da3f5e..864fb2c 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -114,7 +114,7 @@ The layout follows T3 Code's web app, and much of it uses the web app's own code ## Mouse -- **Select and copy:** drag across any text to select it; it's copied when you let go (through the terminal with OSC 52, and the desktop clipboard locally). A key press clears the highlight. Your terminal's own selection still works with shift held. +- **Select and copy:** drag across any text, in the conversation or the terminal pane, to select it; it's copied when you let go (through the terminal with OSC 52, and the desktop clipboard locally) and `Copied` shows at the bottom. A program in the pane that takes the mouse itself, like an editor with mouse support, gets the drag instead. A single click still does what it did. A key press clears the highlight. Your terminal's own selection still works with shift held. - **Buttons:** approvals, questions (click an option), secret requests, confirmations, queued messages (click one to send it now, edit or remove it), attached files (✕ removes one) and the PR view have buttons, each with its key beside it. Every hint along the bottom that names one key is clickable, and so is `commands` at the top. - **Conversation:** a click on a prompt selects that turn, as `[`/`]` do; a click on a folded turn or work group opens just that one; a click on a command shows its output; "Load earlier turns" loads them. In a diff, a click on a file selects it and a second click shows the whole file. In the PR view, the URL opens in the browser. - **Right-click menus:** on a thread (its actions), a prompt (copy, view its changes, revert), a reply (copy), a command (copy the command or its output) and the PR view (its actions). diff --git a/src/ui/App.tsx b/src/ui/App.tsx index 7d299e6..b883116 100644 --- a/src/ui/App.tsx +++ b/src/ui/App.tsx @@ -47,7 +47,7 @@ import { SettingsView } from "./SettingsView.tsx"; import { useWorkspace } from "./useWorkspace.ts"; import { usePullRequests } from "./usePullRequests.ts"; import { rendererClipboard, type Clipboard } from "./clipboard.ts"; -import { copyText, desktopNotify, ring } from "./terminalOut.ts"; +import { desktopNotify, ring } from "./terminalOut.ts"; import { colors, statusColor } from "./theme.ts"; import { BAR, NO_BORDER } from "./edges.ts"; import { ThreadList } from "./ThreadList.tsx"; @@ -356,8 +356,7 @@ export function App(props: { readonly session: T3Session; readonly config: T3tui const copySelection = useEvent((selection: Selection) => { const text = selection.getSelectedText(); if (!text.trim()) return; - copyText(renderer, text); - say(`Copied ${text.length === 1 ? "1 character" : `${text.length} characters`}`); + void clipboard.copy(text).then((copied) => (copied ? say("Copied") : say("Could not reach the clipboard.", true))); }); useEffect(() => { renderer.on("selection", copySelection); @@ -505,7 +504,7 @@ export function App(props: { readonly session: T3Session; readonly config: T3tui const runtimeMode = models.runtimeMode; const threadMenu = useThreadMenu({ session, - renderer, + clipboard, snapshot: shell.snapshot, config: providerConfig, timestampFormat: config.timestampFormat, @@ -725,6 +724,7 @@ export function App(props: { readonly session: T3Session; readonly config: T3tui const pullRequests = usePullRequests({ session, renderer, + clipboard, threadShell, project: draft ? draftProject : project, projects: shell.snapshot?.projects ?? [], @@ -1027,7 +1027,7 @@ export function App(props: { readonly session: T3Session; readonly config: T3tui openTurnDiff: useEvent((checkpointTurnCount: number) => turns.openTurnDiff(checkpointTurnCount)), }; const copy = (label: string, text: string) => { - copyText(renderer, text); + void clipboard.copy(text); say(`Copied ${label}`); }; /** Long output stays on the server until it's asked for. */ diff --git a/src/ui/terminalOut.ts b/src/ui/terminalOut.ts index d14e9f7..db25366 100644 --- a/src/ui/terminalOut.ts +++ b/src/ui/terminalOut.ts @@ -1,5 +1,4 @@ -/** Escape sequences t3tui writes past the renderer: the bell, desktop notifications, the clipboard. */ -import type { CliRenderer } from "@opentui/core"; +/** Escape sequences t3tui writes past the renderer: the bell and desktop notifications. */ /** Inside tmux, escape sequences other than the bell need passthrough to reach the outer terminal. */ function passthrough(sequence: string): string { @@ -25,26 +24,3 @@ export function desktopNotify(title: string, body: string): void { : `\x1b]9;${clean(title)}: ${clean(body)}\x07`; process.stdout.write(passthrough(sequence)); } - -/** - * Copies text: through the terminal (OSC 52, which also works over SSH), and on a local - * desktop through its clipboard tool too, for terminals that ignore OSC 52. - */ -export function copyText(renderer: CliRenderer, text: string): void { - try { - renderer.copyToClipboardOSC52(text); - } catch { - // OpenTUI refuses when it isn't drawing (suspended, or a test renderer); the tool may still copy. - } - if (process.env.SSH_CONNECTION) return; - const tool = - process.platform === "darwin" ? ["pbcopy"] : process.env.WAYLAND_DISPLAY ? ["wl-copy"] : process.env.DISPLAY ? ["xclip", "-selection", "clipboard"] : null; - if (!tool || !Bun.which(tool[0]!)) return; - try { - const child = Bun.spawn(tool, { stdin: "pipe", stdout: "ignore", stderr: "ignore" }); - child.stdin.write(text); - void child.stdin.end(); - } catch { - // OSC 52 already went out; the tool is a fallback. - } -} diff --git a/src/ui/usePullRequests.ts b/src/ui/usePullRequests.ts index 4e46758..da914e0 100644 --- a/src/ui/usePullRequests.ts +++ b/src/ui/usePullRequests.ts @@ -39,13 +39,14 @@ import { } from "../model/pullRequests.ts"; import type { Confirmation } from "./ConfirmBar.tsx"; import { editInEditor } from "./editor.ts"; -import { copyText } from "./terminalOut.ts"; +import type { Clipboard } from "./clipboard.ts"; import { firstLine } from "./text.ts"; import type { PullRequestViewState } from "./PullRequestView.tsx"; export interface PullRequestDeps { readonly session: T3Session; readonly renderer: CliRenderer; + readonly clipboard: Clipboard; readonly threadShell: OrchestrationV2ThreadShell | undefined; readonly project: OrchestrationProjectShell | undefined; readonly projects: readonly OrchestrationProjectShell[]; @@ -162,7 +163,7 @@ export function usePullRequests(deps: PullRequestDeps) { if (!url) return; // Over SSH or without a desktop there may be no browser to open: copy the link instead. const fallBack = (reason: string) => { - copyText(deps.renderer, url); + void deps.clipboard.copy(url); deps.say(`Could not open the browser (${reason}); link copied: ${url}`, true); }; try { diff --git a/src/ui/useThreadMenu.ts b/src/ui/useThreadMenu.ts index 39a91c2..620fd0f 100644 --- a/src/ui/useThreadMenu.ts +++ b/src/ui/useThreadMenu.ts @@ -1,4 +1,3 @@ -import type { CliRenderer } from "@opentui/core"; import { threadChipCopyTarget } from "@t3tools/client-runtime/thread-reference"; import type { ContextMenuItem, OrchestrationV2ShellSnapshot, OrchestrationV2ThreadShell, ServerConfig, ThreadId } from "@t3tools/contracts"; @@ -8,14 +7,14 @@ import type { Binding } from "../model/keymap.ts"; import type { Picker, PickerItem } from "../model/palette.ts"; import { parseSnooze, pickSnooze, threadHasRuntime, threadMenu, topPinOrderKey, type ThreadActionMenuId } from "../model/threadMenu.ts"; import type { Confirmation } from "./ConfirmBar.tsx"; -import { copyText } from "./terminalOut.ts"; +import type { Clipboard } from "./clipboard.ts"; type SnoozePresets = ReturnType["snoozePresets"]; /** The web sidebar's thread menu as a picker: separators become groups, submenus open their own picker. */ export function useThreadMenu(deps: { readonly session: T3Session; - readonly renderer: CliRenderer; + readonly clipboard: Clipboard; readonly snapshot: OrchestrationV2ShellSnapshot | null; readonly config: ServerConfig | null; readonly timestampFormat: T3tuiConfig["timestampFormat"]; @@ -34,10 +33,10 @@ export function useThreadMenu(deps: { readonly keepUnread: (id: ThreadId) => void; readonly startDraftOn: (target: OrchestrationV2ThreadShell) => void; }) { - const { session, renderer, config, run, say } = deps; + const { session, clipboard, config, run, say } = deps; const copy = (text: string, message: string) => { - copyText(renderer, text); + void clipboard.copy(text); say(message); }; diff --git a/test/app/select.test.tsx b/test/app/select.test.tsx new file mode 100644 index 0000000..b238000 --- /dev/null +++ b/test/app/select.test.tsx @@ -0,0 +1,43 @@ +import { afterEach, describe, expect, test } from "bun:test"; + +import { withCompletedTurn } from "./fixtures.ts"; +import { fakeSession, renderApp, v2ThreadId } from "./harness.tsx"; + +let app: Awaited> | null = null; +const open = async (...args: Parameters) => (app = await renderApp(...args)); +afterEach(() => { + app?.destroy(); + app = null; +}); + +describe("select and copy", () => { + test("dragging across the conversation copies what it covers on release", async () => { + const app = await open({ session: fakeSession({ projection: withCompletedTurn() }) }); + const reply = app.find("Done — the thing"); + await app.mockMouse.drag(reply.x, reply.y, reply.x + 8, reply.y); + await app.settle(); + expect(app.calls("copy").map((call) => call.args[0])).toEqual(["Done — th"]); + expect(app.hints()).toBe("Copied"); + }); + + test("a single click copies nothing and still selects the turn", async () => { + const app = await open({ session: fakeSession({ projection: withCompletedTurn() }) }); + const prompt = app.find("Do the thing"); + await app.click(prompt.x, prompt.y); + expect(app.calls("copy")).toHaveLength(0); + expect(app.hints()).not.toBe("Copied"); + }); + + test("the terminal pane's text can be selected and copied too", async () => { + const terminal = { threadId: v2ThreadId, terminalId: "term-1", cwd: "/workspace/project", worktreePath: null, status: "running", pid: 42, exitCode: null, exitSignal: null, hasRunningSubprocess: false, label: "", updatedAt: "2026-06-20T00:00:00.000Z" }; + const snapshot = { type: "snapshot", threadId: v2ThreadId, terminalId: "term-1", createdAt: "2026-06-20T00:00:00.000Z", snapshot: { history: "build finished in 3s\r\n$ " } }; + const app = await open({ width: 200, session: fakeSession({ terminals: [terminal], terminalEvents: [snapshot] }) }); + await app.press("j", { ctrl: true }); + const line = app.find("build finished"); + await app.mockMouse.drag(line.x, line.y, line.x + 13, line.y); + await app.settle(); + expect(app.calls("copy").map((call) => call.args[0])).toEqual(["build finished"]); + // Dragging isn't typing: nothing went to the shell. + expect(app.calls("writeTerminal")).toHaveLength(0); + }); +});