HakonHarnes / img-clip.nvim

Effortlessly embed images into any markup language, like LaTeX, Markdown or Typst
MIT License
499 stars 8 forks source link

doesn't work in Windows #13

Closed hutterm closed 10 months ago

hutterm commented 10 months ago

it always says "Clipboard content is not an image"

I'm suspecting it's the windows powershell command not working.

PS> Get-Clipboard -Format Image returns Get-Clipboard : A parameter cannot be found that matches parameter name 'Format' both in pwsh.exe (7.3.10) and powershell.exe (PSVersion 5.1.22621.2506)

here is a linked issue about the missing format: https://github.com/PowerShell/PowerShell/issues/12290

HakonHarnes commented 10 months ago

Thanks for reporting the issue.

Seems like using Get-Clipboard -Format Image is indeed not a reliable method. I've changed the code to use .NET commands instead, which should be available on most Windows systems. Can you see if fix/clipboard-not-working-on-windows resolves your issues? If you're using Lazy, just set the branch option accordingly.

I will probably add support for win32yank as an optional dependency as well. I suspect it provides better performance and reliability.

hutterm commented 10 months ago

ok, seems like the clipboard can be read with the fix branch, but I still get the message, "Could not save image to disk."

hutterm commented 10 months ago

did some more tests with the new powershell commands you changed. I can successfully execute them in the console, but only if I have write access and the directories exist. a '~' is not recognized by the command as a valid directory it seems filename without a path is also not working, so it seems it needs a full path, that exists and has write access

HakonHarnes commented 10 months ago

Can you please try to execute the following command:

powershell.exe "Add-Type -AssemblyName System.Windows.Forms;Add-Type -AssemblyName System.Drawing;$img = [System.Windows.Forms.Clipboard]::GetImage();$img.Save('image.png', [System.Drawing.Imaging.ImageFormat]::Png);"

In the following environments:

  1. Command prompt
  2. Powershell 5.1
  3. Powershell 7.3

I suspect it will work in the command prompt, and not the latter options. Remember to copy an image to the clipboard before executing the command.

I can successfully execute them in the console, but only if I have write access and the directories exist.

That's taken care of by a different function, see: https://github.com/HakonHarnes/img-clip.nvim/blob/1aef484fc2302f8e81d9595f2a972bb635b81715/lua/img-clip/fs.lua#L79

hutterm commented 10 months ago

further tests: with the added base64 option I get: "Could not get base64 string." while still opening the File name input. pressing enter after that it says "Could not save image to disk."

hutterm commented 10 months ago
  1. Command prompt

prompt copied works

  1. Powershell 5.1

prompt fails with the whole powershell.exe in the front but works with just the stuff in the quotes

  1. Powershell 7.3

prompt fails, even with just the stuff in the quotes:

PS> Add-Type -AssemblyName System.Windows.Forms;Add-Type -AssemblyName System.Drawing;$img = [System.Windows.Forms.Clipboard]::GetImage();$img.Save('image.png', [System.Drawing.Imaging.ImageFormat]::Png);
MethodInvocationException: Exception calling "Save" with "2" argument(s): "A generic error occurred in GDI+."
HakonHarnes commented 10 months ago

I think the issue is related to what the default command-line shell is on your system. What is the output of :set shell? within neovim?

hutterm commented 10 months ago

ah, that could be it! I set mine to pwsh

HakonHarnes commented 10 months ago

Should be able to account for that, I'll ping you when I have a (hopefully) working solution!

HakonHarnes commented 10 months ago

@hutterm Can you see if it works now? If not, set debug = true and paste the output of :messages please.

hutterm commented 10 months ago

didn't work with message clipboard content is not an image. :messages:


Shell: pwsh
Command: Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.Clipboard]::GetImage()
Exit code: 0
Output: 
Tag                  : 
PhysicalDimension    : {Width=93, Height=189}
Size                 : {Width=93, Height=189}
Width                : 93
Height               : 189
HorizontalResolution : 96
VerticalResolution   : 96
Flags                : 335888
RawFormat            : MemoryBMP
PixelFormat          : Format32bppRgb
PropertyIdList       : {}
PropertyItems        : {}
Palette              : System.Drawing.Imaging.ColorPalette
FrameDimensionsList  : {7462dc86-6180-4c7e-8e3f-ee7333a7a483}
HakonHarnes commented 10 months ago

Ah, I see. Try again with the latest changes.

hutterm commented 10 months ago

awesome! that worked, thanks for your dedication!