zed icon indicating copy to clipboard operation
zed copied to clipboard

terminal: Events are delayed (e.g. copy to clipboard)

Open N8th8n8el opened this issue 1 year ago • 1 comments

Check for existing issues

  • [X] Completed

Describe the bug / provide steps to reproduce it

How I noticed: When copying terminal output and then immediately pasting it again into the terminal I almost always get what I had in the clipboard before. If I wait a bit between the copy and the paste it works.

Now that Zed is open source 🥳 I investigated the issue myself and I think I know what is going on. If I press Cmd+C to copy, all the terminal does is put a "copy request" into a queue

self.events.push_back(InternalEvent::Copy)

(terminal.rs:912). Then the next time GPUI paints the terminal it calls terminal.sync which goes through mentioned queue and actually puts the currently selected terminal text into the system clipboard.

InternalEvent::Copy => {
    if let Some(txt) = term.selection_to_string() {
        cx.write_to_clipboard(ClipboardItem::new(txt))
    }
}

(terminal.rs:713)

It seems like paint is called with a fairly low frequency and so there is enough time for me to paste before the copy actually happenes. I also see other race conditions here, e.g. the selected text could theoretically change between the copy "request" and the actual copying into the system clipboard.

I feel like the render loop isn't the right place to handle such kind of events, but I actually have not much of an idea what I'm talking about so happy to be corrected.

I have a fix for copy to clipboard and will open a PR shortly.

Environment

Zed: v0.121.2 (Zed Preview) OS: macOS 14.2.1 Memory: 16 GiB Architecture: aarch64

If applicable, add mockups / screenshots to help explain present your vision of the feature

No response

If applicable, attach your ~/Library/Logs/Zed/Zed.log file to this issue.

If you only need the most recent lines, you can run the zed: open log command palette action to see the last 1000.

No response

N8th8n8el avatar Feb 03 '24 13:02 N8th8n8el

Just noticed that I already reported this early last year here: #4790 I'll close that issue because this one has more information.

N8th8n8el avatar Feb 03 '24 14:02 N8th8n8el