copypasta icon indicating copy to clipboard operation
copypasta copied to clipboard

Add support for non-text clipboard content

Open liquidev opened this issue 2 years ago • 12 comments

Some data that goes through the clipboard is not necessarily text, one example is images. Currently copypasta uses Strings to transfer all clipboard data, which all guarantee that the data inside is valid UTF-8, and panic if it's not. This prevents users from transferring non-text data through the clipboard.

liquidev avatar Nov 13 '21 22:11 liquidev

While non-text clipboard support is a valid feature request, that should really never be in the form of just some arbitrary Vec<u8>.

That said, this is not something I have any usecase for at all. So if someone is interested in this feature you'll likely have to send a PR yourself.

chrisduerr avatar Nov 14 '21 03:11 chrisduerr

Yeah, I've researched a bit more on how system clipboards work and it's not just as simple as putting a PNG file into the clipboard after all… Different systems handle image files differently. On Linux (X11 and Wayland) you have to set the MIME type to image/png; on Windows you have to specify that the clipboard should be a BMP file and put one in there, I don't know about macOS.

There seem to be other forks of rust-clipboard like arboard, but there the readme says the author is not interested in maintaining the library and will be archiving the repository soon. Copypasta seems to be the most supported one (thanks to it being used in alacritty), so introducing support for other formats to this library would make sense from a support perspective.

liquidev avatar Nov 14 '21 11:11 liquidev

Copypasta seems to be the most supported one

I'm definitely happy to maintain copypasta and fix issues popping up. But it does everything I want both for Alacritty and any other application I might want to write. But as stated previously, I'm happy to put in the time to review PRs related to a more sophisticated API which includes image support.

chrisduerr avatar Nov 15 '21 01:11 chrisduerr

One feature that I really would like to implement in alacritty is better support for the "file" clipboard type on macOS. If I drag a file into macOS Terminal from Finder, it pastes the full path to the file and properly escapes any spaces. In alacritty, only the base name is pasted. macOS has multiple representations for a given item, and so the default string representation of a file is just the base name.

There are a few approaches that I can see to bring this to alacritty:

  1. Adding this behavior to copypasta
  2. Exposing macOS-specific functionality for alacritty to implement the behavior
  3. Building a cross-platform API to encapsulate being able to get files from the clipboard and using that from alacritty

I'm currently leaning towards 2, but I'd love to get your thoughts @chrisduerr on which approach would be best

conradev avatar Apr 19 '23 22:04 conradev

One feature that I really would like to implement in alacritty is better support for the "file" clipboard type on macOS. If I drag a file into macOS Terminal from Finder, it pastes the full path to the file and properly escapes any spaces. In alacritty, only the base name is pasted. macOS has multiple representations for a given item, and so the default string representation of a file is just the base name.

That is intentional. We do not want to escape file paths in Alacritty.

chrisduerr avatar Apr 20 '23 00:04 chrisduerr

Why is that? Is there a security/correctness concern, or is the ability to copy a file in a file manager and paste it as a path in the terminal window not desirable to you?

conradev avatar Apr 28 '23 20:04 conradev

Why is that? Is there a security/correctness concern, or is the ability to copy a file in a file manager and paste it as a path in the terminal window not desirable to you?

Because it is so much easier to just quote something you want escaped than to remove the escape characters from something you don't want escaped.

chrisduerr avatar Apr 29 '23 11:04 chrisduerr

@chrisduerr oh, that makes sense! My use of the term escaping was incorrect, then, because I do not care how the path gets into the terminal, as long as I have some way of getting it in there. Escaping/quoting is a fine exercise to leave up to the reader, because I know how to use quotes.

If the change was only so that files get pasted as their full file paths and not base names, would that be a change that you'd consider?

conradev avatar Apr 29 '23 17:04 conradev

If the change was only so that files get pasted as their full file paths and not base names, would that be a change that you'd consider?

I kinda assumed that how it works already. I don't really use it myself. So I'd be open to the idea I guess.

chrisduerr avatar Apr 29 '23 19:04 chrisduerr

FYI I made a PR to rust-clipboard years ago where this was also addressed.. it's not very trivial especially considering multiple platforms. There are some thoughts on that in the PR. https://github.com/aweinstock314/rust-clipboard/pull/60

muja avatar Jun 25 '23 19:06 muja

I'm not sure major changes to copypasta are worthwhile considering any non-trivial application should make use of a different clipboard library anyway.

Focusing on text-only makes sense for something that is completely separate, but if you actually want proper clipboard management the better solution is proper integration with your window management loop.

chrisduerr avatar Jun 25 '23 23:06 chrisduerr