From 1e10ca6cea5e772709adb2d6f09923f286fd3dee Mon Sep 17 00:00:00 2001 From: JhonnyV-V Date: Thu, 11 Apr 2024 09:41:27 -0400 Subject: [PATCH] adding suppot to copy in wsl add to dependencies to get system info, to check if is wsl and the current pretty name of the distro. if wsl is getting used, this should create the image as a temp file inside of wsl, then use powershell to copy it to the clipboard and then it deletes the image from the tmp directory --- generator/Cargo.lock | 18 +++++++++++ generator/Cargo.toml | 2 ++ generator/src/copy.rs | 69 ++++++++++++++++++++++++++++++++++++------- 3 files changed, 79 insertions(+), 10 deletions(-) diff --git a/generator/Cargo.lock b/generator/Cargo.lock index 680fa02..06a04d1 100644 --- a/generator/Cargo.lock +++ b/generator/Cargo.lock @@ -405,9 +405,11 @@ dependencies = [ "regex", "serde", "syntect", + "sys-info", "thiserror", "tiny-skia", "two-face", + "wsl", ] [[package]] @@ -1133,6 +1135,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" @@ -1614,6 +1626,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 becf5eb..1d298fa 100644 --- a/generator/Cargo.toml +++ b/generator/Cargo.toml @@ -13,6 +13,8 @@ arboard = {features = ["wayland-data-control"], version = "3.3.2"} thiserror = "1.0.58" regex = "1.10.3" two-face = "0.3.0" +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..bd5cd41 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,67 @@ 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()))); + + let os_linux_release = sys_info::linux_os_release().unwrap(); + let src_path = format!( + "\\\\wsl$\\{}\\tmp\\{}", + os_linux_release.pretty_name(), + filename + ); + + let _ = copy_to_wsl_clipboard(&src_path); + //delete the file when done + std::fs::remove_file(path).unwrap(); + } 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 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) +}