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
pull/83/head
JhonnyV-V 2024-04-11 09:41:27 -04:00
parent 6f5544774c
commit 1e10ca6cea
3 changed files with 79 additions and 10 deletions

18
generator/Cargo.lock generated
View File

@ -405,9 +405,11 @@ dependencies = [
"regex", "regex",
"serde", "serde",
"syntect", "syntect",
"sys-info",
"thiserror", "thiserror",
"tiny-skia", "tiny-skia",
"two-face", "two-face",
"wsl",
] ]
[[package]] [[package]]
@ -1133,6 +1135,16 @@ dependencies = [
"yaml-rust", "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]] [[package]]
name = "sys-locale" name = "sys-locale"
version = "0.3.1" version = "0.3.1"
@ -1614,6 +1626,12 @@ dependencies = [
"wayland-protocols-wlr", "wayland-protocols-wlr",
] ]
[[package]]
name = "wsl"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8dab7ac864710bdea6594becbea5b5050333cf34fefb0dc319567eb347950d4"
[[package]] [[package]]
name = "x11rb" name = "x11rb"
version = "0.13.0" version = "0.13.0"

View File

@ -13,6 +13,8 @@ arboard = {features = ["wayland-data-control"], version = "3.3.2"}
thiserror = "1.0.58" thiserror = "1.0.58"
regex = "1.10.3" regex = "1.10.3"
two-face = "0.3.0" two-face = "0.3.0"
wsl = "0.1.0"
sys-info = "0.9.1"
[lib] [lib]
crate-type = ["cdylib"] crate-type = ["cdylib"]

View File

@ -2,8 +2,7 @@ use crate::{config::TakeSnapshotParams, snapshot::take_snapshot};
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
use arboard::SetExtLinux; use arboard::SetExtLinux;
use arboard::{Clipboard, ImageData}; use arboard::{Clipboard, ImageData};
use nvim_oxi::{lua::Error::RuntimeError, Error, Result};
use nvim_oxi::Result;
pub fn copy_into_clipboard(config: TakeSnapshotParams) -> Result<()> { pub fn copy_into_clipboard(config: TakeSnapshotParams) -> Result<()> {
let pixmap = take_snapshot(config.clone())?; let pixmap = take_snapshot(config.clone())?;
@ -28,6 +27,26 @@ pub fn copy_into_clipboard(config: TakeSnapshotParams) -> Result<()> {
}; };
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
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 || { std::thread::spawn(move || {
Clipboard::new() Clipboard::new()
.unwrap() .unwrap()
@ -36,9 +55,39 @@ pub fn copy_into_clipboard(config: TakeSnapshotParams) -> Result<()> {
.image(img_data) .image(img_data)
.unwrap(); .unwrap();
}); });
}
#[cfg(not(target_os = "linux"))] #[cfg(not(target_os = "linux"))]
Clipboard::new().unwrap().set_image(img_data).unwrap(); Clipboard::new().unwrap().set_image(img_data).unwrap();
Ok(()) 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)
}