ethanholz / freeze.nvim

A Neovim plugin for freeze
MIT License
28 stars 5 forks source link

feat(#5): add copying to clipboard support #7

Open AlejandroSuero opened 4 months ago

AlejandroSuero commented 4 months ago

powershell script demo from Windows11 VM:

Image pasted from Windows11 VM: image from windows wm

osascript demo from MacOS:

osascript -e 'set the clipboard to (read (POSIX file "/Users/aome/dev/nvim_plugins/freeze.nvim/freeze.png") as JPEG picture)'

Image from the script: image from MacOS script

I didn't test it yet in Ubuntu, but the script looks fine from what I use to copy images myself. If xclip is not the way to do it, I will appreciate some suggestions.

Closes #5

ethanholz commented 4 months ago

The unix implementation will likely fail in the near feature as xclip will not work on systems using Wayland. This issue might best be resolved by providing users with the ability to override this internal function with their own and we can provide a few defaults for the most common ones.

AlejandroSuero commented 4 months ago

@ethanholz What would you think should be the best way to approach it?

This is what first comes to my mind.

https://github.com/ethanholz/freeze.nvim/blob/8512cebb305e50f4acca6e71bc3061149a5c61eb/lua/freeze/init.lua#L4-L13

Adding an option such as:

local freeze = {
  opts = {
    -- ...
    copy_cmd = ""
  }
  output = nil
}
-- ...
function freeze.copy(filename)
  if freeze.opts.copy_cmd ~= "" then
    -- use freeze.opts.copy_cmd
  end
  -- otherwise use the defaults?
end

I don't use Wayland so I was not familiar with those type of changes, sorry.

ethanholz commented 4 months ago

No worries on Wayland, I still appreciate the changes! let me think about implementation around the copy_cmd this weekend and I can get back to you on that.

AlejandroSuero commented 4 months ago

@ethanholz I made some changes for the copy command to work properly.

For some reason it won't work unless outside a job or spawn, it will trigger it but ignore it. Since they are terminal commands I called them in os.execute and it works fine now.

Tested it with both open and copy actions active (true) and it will trigger both.

AlejandroSuero commented 4 months ago

@ethanholz I added the Wayland support, but reading into Wayland here I discovered that echo $XDG_SESSION_TYPE could result in x11 if using Wayland under X11 system.

What do you think is the best way to show if using Wayland if this not enough to tell? Maybe using echo $WAYLAND_DISPLAY and check if not nil?

ethanholz commented 3 months ago

What do you think is the best way to show if using Wayland if this not enough to tell? Maybe using echo $WAYLAND_DISPLAY and check if not nil?

I think this is good idea for a first pass and is good enough for review.

AlejandroSuero commented 3 months ago

If they merge this PR https://github.com/charmbracelet/freeze/pull/97 it could be much easier, don't know exactly how it will work on Wayland but is working on Windows, MacOS and the Manjaro that I have.

It is using gclip in case you want to test it on your machine.

ethanholz commented 3 months ago

If we can get this merged upstream I would much prefer to lean on that. Let's give it some time to see if your PR gets merged and if it does (it was reviewed so I'm hopeful), we can use that.