diff --git a/README.md b/README.md index d565a30..e23db59 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,14 @@ Copy screenshots directly into the clipboard is cool, however, it doesn't work w If you using CodeSnap.nvim on wl-clipboard, you can refer [wl-clip-persist](https://github.com/Linus789/wl-clip-persist), it reads all the clipboard data into memory and then overwrites the clipboard with the data from our memory to persist copied data. + +#### Copy into clipboard on WSL +currently copy to clipboard in WSL works, but it has some potencial issues, inside of wsl normally is not possible to copy an image to the clipboard, +how this works in wsl at this moment is the following, we create an image in the /tmp directory, then we try to execute powershell commands +to copy the image to the clipboard +> [!WARNING] +> If the WSL distro name is not the default or the pretty_name/name in the /etc/os-release this feature will fai + ### Save the snapshot Of course, you can use `CodeSnapSave` command to save the snapshot to path where you defined it in `config.save_path` @@ -181,6 +189,8 @@ require("codesnap").setup({ -- parsed: "~/Pictures/CodeSnap_y-m-d_at_h:m:s.png" -- save_path = "~/Pictures/foo.png" -- parsed: "~/Pictures/foo.png" + -- if you are on wsl and do you want to use a windows path + -- save_path="/mnt/c/Users/user/Pictures/" save_path = ... }) ``` diff --git a/generator/Cargo.lock b/generator/Cargo.lock index 9cb50a2..90603f3 100644 --- a/generator/Cargo.lock +++ b/generator/Cargo.lock @@ -492,9 +492,11 @@ dependencies = [ "regex", "serde", "syntect", + "sys-info", "thiserror", "tiny-skia", "two-face", + "wsl", ] [[package]] @@ -1256,6 +1258,16 @@ dependencies = [ "yaml-rust", ] +[[package]] +name = "sys-info" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b3a0d0aba8bf96a0e1ddfdc352fc53b3df7f39318c71854910c3c4b024ae52c" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "sys-locale" version = "0.3.1" @@ -1737,6 +1749,12 @@ dependencies = [ "wayland-protocols-wlr", ] +[[package]] +name = "wsl" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dab7ac864710bdea6594becbea5b5050333cf34fefb0dc319567eb347950d4" + [[package]] name = "x11rb" version = "0.13.0" diff --git a/generator/Cargo.toml b/generator/Cargo.toml index d174ec1..8ddc8d8 100644 --- a/generator/Cargo.toml +++ b/generator/Cargo.toml @@ -14,6 +14,8 @@ thiserror = "1.0.58" regex = "1.10.3" two-face = "0.3.0" cached = "0.49.3" +wsl = "0.1.0" +sys-info = "0.9.1" [lib] crate-type = ["cdylib"] diff --git a/generator/src/copy.rs b/generator/src/copy.rs index 6f5a388..420ffda 100644 --- a/generator/src/copy.rs +++ b/generator/src/copy.rs @@ -2,8 +2,7 @@ use crate::{config::TakeSnapshotParams, snapshot::take_snapshot}; #[cfg(target_os = "linux")] use arboard::SetExtLinux; use arboard::{Clipboard, ImageData}; - -use nvim_oxi::Result; +use nvim_oxi::{lua::Error::RuntimeError, Error, Result}; pub fn copy_into_clipboard(config: TakeSnapshotParams) -> Result<()> { let pixmap = take_snapshot(config.clone())?; @@ -28,17 +27,92 @@ pub fn copy_into_clipboard(config: TakeSnapshotParams) -> Result<()> { }; #[cfg(target_os = "linux")] - std::thread::spawn(move || { - Clipboard::new() - .unwrap() - .set() - .wait() - .image(img_data) - .unwrap(); - }); + if wsl::is_wsl() { + let temp_dir = std::env::temp_dir(); + let filename = generate_random_filename(); + + let path = format!("{}/{}", String::from(temp_dir.to_str().unwrap()), filename); + let _ = pixmap + .save_png(path.clone()) + .map_err(|err| Error::Lua(RuntimeError(err.to_string()))); + + //getting mounted vdisk location of linux install + let os_linux_release = sys_info::linux_os_release().unwrap(); + let mut wsl_path = format!( + "\\\\wsl$\\{}", + os_linux_release.pretty_name() + ); + if !powershell_folder_exist(wsl_path.clone()) { + wsl_path = format!( + "\\\\wsl$\\{}", + os_linux_release.name() + ); + } + let src_path = format!( + "{}\\tmp\\{}", + wsl_path, + filename + ); + let _ = copy_to_wsl_clipboard(&src_path); + //delete the file when done? + } else { + std::thread::spawn(move || { + Clipboard::new() + .unwrap() + .set() + .wait() + .image(img_data) + .unwrap(); + }); + } #[cfg(not(target_os = "linux"))] Clipboard::new().unwrap().set_image(img_data).unwrap(); Ok(()) } + +fn copy_to_wsl_clipboard(src_path: &str) -> Result<()> { + println!("{}", src_path); + let powershell = Command::new("/mnt/c/Windows//System32/WindowsPowerShell/v1.0/powershell.exe") + .arg("-NoProfile") + .arg("-Command") + .arg(&format!("Get-ChildItem \"{}\" | Set-Clipboard", src_path)) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn(); + + // Wait until the powershell process is finished before returning + let _ = powershell.unwrap().wait().unwrap(); + + Ok(()) +} + +use std::{ + process::{Command, Stdio}, time::Instant +}; + +fn powershell_folder_exist(src_path: String) -> bool { + let powershell = Command::new("/mnt/c/Windows//System32/WindowsPowerShell/v1.0/powershell.exe") + .arg("-NoProfile") + .arg("-Command") + .arg(&format!("Test-Path -path \"{}\"", src_path)) + .stdout(Stdio::piped()) + .stderr(Stdio::null()) + .output(); + + let stdout = String::from_utf8(powershell.unwrap().stdout).unwrap(); + + let result = stdout == "True\r\n"; + + return result; +} + +fn generate_random_filename() -> String { + // Get nanoseconds since epoch for randomness + let now = Instant::now(); + let random_part = format!("{:016x}", now.elapsed().as_nanos() % u128::MAX); + + // Combine prefix, random part, and extension + format!("codesnap_{}.png", random_part) +} diff --git a/lua/linux-x86_64generator.so b/lua/linux-x86_64generator.so index 275c31c..3aebfef 100755 Binary files a/lua/linux-x86_64generator.so and b/lua/linux-x86_64generator.so differ diff --git a/lua/mac-aarch64generator.so b/lua/mac-aarch64generator.so index 96f1be3..11da972 100755 Binary files a/lua/mac-aarch64generator.so and b/lua/mac-aarch64generator.so differ diff --git a/lua/mac-x86_64generator.so b/lua/mac-x86_64generator.so index c23fb75..6e988a5 100755 Binary files a/lua/mac-x86_64generator.so and b/lua/mac-x86_64generator.so differ