Skip to content

feat(session): drag over the conversation to select and copy its text - #173

Merged
kalil0321 merged 4 commits into
mainfrom
kalil0321/session-copy
Nov 14, 2026
Merged

kalil0321 merged 4 commits into
mainfrom
kalil0321/session-copy

Conversation

@kalil0321

@kalil0321 kalil0321 commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Why: the session takes the mouse for the wheel and for clicking files and links, so the terminal cannot select its text: nothing in the conversation could be copied.

What: dragging over the conversation highlights the text and copies it when you let go, and the status row says what was copied. A drag can end over the input bar or status row; it then copies to the end of the last line on screen. The copy runs off the app's loop with pbcopy, wl-copy, xclip or xsel, and otherwise, or over SSH, asks the terminal to copy with OSC 52. A click without a drag still opens a link, and the wheel still scrolls. Typing, /clear or a new terminal width drops the selection.

Verification:

  • Unit tests for the selection, the highlight keeping link handlers, a release under the conversation or missed by it, the copy leaving the loop, and the clipboard on each platform and over SSH.
  • Wide characters and blank lines through prompt_toolkit's own window, which hands the control character indexes.
  • Each piece of the logic broken in turn: every break fails a test.
  • In a pty read through a terminal emulator (pyte), the real session with SGR mouse events: a drag from the right half of 本 over 日本 ok copies 本 ok and reverses its 5 cells, a drag let go on the input bar copies to the end of the conversation, the status row says copied, and a plain click copies nothing.

Out of scope: copying the input bar or menus; a word or line on double or triple click.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-14T18:44:03.827505Z 37474b8 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.


def drag(self, event: MouseEvent) -> bool:
"""A drag over the conversation selects text, copied on release; a click without one still opens a link."""
at = (self.view_start + event.position.y, event.position.x)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Wide characters copy incorrectly

drag treats event.position.x as a screen column, but prompt_toolkit passes a character index. cells then compares it with screen columns. For 日本 ok, dragging from 本 through k selects 日本 instead of 本 ok.

Convert the mouse position to screen columns, or use character indexes throughout. Add a test that sends wide-character input through the window's mouse handler.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/reverse_api/repl/app.py
Line: 986

Comment:
**Wide characters copy incorrectly**

`drag` treats `event.position.x` as a screen column, but prompt_toolkit passes a character index. `cells` then compares it with screen columns. For `日本 ok`, dragging from `本` through `k` selects `日本` instead of `本 ok`.

Convert the mouse position to screen columns, or use character indexes throughout. Add a test that sends wide-character input through the window's mouse handler.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Comment thread src/reverse_api/repl/app.py Outdated
Comment on lines +996 to +999
# A release here, or a move with no button held: it was let go over another part of the screen.
if kind == MouseEventType.MOUSE_UP:
sel.head = at
sel.dragging = False

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Release outside loses the copy

Releasing a drag over the input bar does not copy the selection. That window receives the release, and drag only finishes the copy after another mouse event reaches the conversation. If the user starts typing instead, _drop_selection discards the pending copy.

Handle the release across the whole layout rather than waiting for the pointer to return. The new test only passes because it adds that later return event.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/reverse_api/repl/app.py
Line: 996-999

Comment:
**Release outside loses the copy**

Releasing a drag over the input bar does not copy the selection. That window receives the release, and `drag` only finishes the copy after another mouse event reaches the conversation. If the user starts typing instead, `_drop_selection` discards the pending copy.

Handle the release across the whole layout rather than waiting for the pointer to return. The new test only passes because it adds that later return event.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Comment thread src/reverse_api/repl/clipboard.py Outdated
# pbcopy reads its input in the locale's encoding.
env = {**os.environ, "LC_CTYPE": "UTF-8"} if command[0] == "pbcopy" else None
try:
subprocess.run(command, input=text.encode(), env=env, check=True, capture_output=True, timeout=2)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Copying can freeze the session

subprocess.run runs directly inside the mouse callback. If a clipboard helper stalls, the two-second timeout blocks drawing, typing and cancellation for that whole interval.

Run the helper outside the app's event loop, then send any terminal fallback and status update back to that loop.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/reverse_api/repl/clipboard.py
Line: 37

Comment:
**Copying can freeze the session**

`subprocess.run` runs directly inside the mouse callback. If a clipboard helper stalls, the two-second timeout blocks drawing, typing and cancellation for that whole interval.

Run the helper outside the app's event loop, then send any terminal fallback and status update back to that loop.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4007a62c74

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".


def drag(self, event: MouseEvent) -> bool:
"""A drag over the conversation selects text, copied on release; a click without one still opens a link."""
at = (self.view_start + event.position.y, event.position.x)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Convert mouse character indexes before using them as cell columns

When a transcript row contains wide characters, event.position.x is not a terminal-cell column: prompt-toolkit's Window mouse wrapper converts screen coordinates into character indexes before dispatching to the control. Passing that index directly into Selection, whose extraction and highlighting use cell widths, selects the wrong text. For example, dragging over ok in 日本 ok produces character indexes 3–4, but selected_text interprets those as cells and copies 本. Convert the index to a cell offset using the corresponding row, or consistently use character indexes throughout selection handling; the existing wide-character test bypasses this event conversion.

Useful? React with 👍 / 👎.

Comment thread src/reverse_api/repl/clipboard.py Outdated
# pbcopy reads its input in the locale's encoding.
env = {**os.environ, "LC_CTYPE": "UTF-8"} if command[0] == "pbcopy" else None
try:
subprocess.run(command, input=text.encode(), env=env, check=True, capture_output=True, timeout=2)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid capturing the background clipboard owner's output pipes

On a local Linux X11 session using xclip, this synchronous call can freeze the session for the full two-second timeout on every copy even when copying succeeds. The xclip manual describes its default mode as “fork into the background to wait for requests”; its implementation leaves stdout and stderr open in that background clipboard owner. With capture_output=True, subprocess.run waits for EOF on those inherited pipes, times out, and unnecessarily falls back to OSC 52. Redirect the unused output streams to DEVNULL rather than pipes so the completed parent process can return immediately.

Useful? React with 👍 / 👎.


transcript_window: Window = Window(wrap_lines=False)
transcript_window.content = TranscriptControl(lambda: self.transcript_view(transcript_window), self.scroll)
transcript_window.content = TranscriptControl(lambda: self.transcript_view(transcript_window), self.scroll, self.drag)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Handle selection release outside the transcript window

If a user starts selecting in the conversation and releases over the input bar or status row, nothing is copied on release: self.drag is registered only on TranscriptControl, so the other window receives that mouse-up event. The no-button-move fallback only copies after the pointer returns to the conversation; switching directly to another application to paste leaves the previous clipboard contents, and typing in the input bar drops the selection without copying it. The existing outside-release test explicitly injects a subsequent move back over the transcript, so it does not verify copying at release. Capture the release at the application level or forward it from other windows while a selection is dragging.

Useful? React with 👍 / 👎.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 501185dbcf

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/reverse_api/repl/app.py Outdated
Comment on lines +1046 to +1051
def copy() -> None:
# Off the app's loop, so a slow clipboard command never holds up the screen.
clipboard.copy(text, lambda data: self.call_soon(lambda: self.write_raw(data)))
self.call_soon(lambda: self.flash(done))

threading.Thread(target=copy, name="rae-copy", daemon=True).start()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve clipboard request order across background copies

If a user makes another selection while the previous clipboard helper is still running, the independent threads can overwrite the newer clipboard contents with the older selection. For example, if the first helper stalls and the second succeeds, the first request's eventual timeout sends an OSC 52 fallback containing the old text, even though the newer selection remains highlighted. This ordering is reproducible with the current copy_selection and clipboard fallback code. Serialize clipboard requests or otherwise prevent superseded requests from writing clipboard contents and posting stale confirmations.

Useful? React with 👍 / 👎.

@kalil0321
kalil0321 merged commit 403faab into main Nov 14, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant