Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] adding support to copy to clipboard in WSL #83

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Next Next commit
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
  • Loading branch information
jhonnyV-V committed Apr 11, 2024
commit 1e10ca6cea5e772709adb2d6f09923f286fd3dee
18 changes: 18 additions & 0 deletions generator/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
69 changes: 59 additions & 10 deletions generator/src/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())?;
Expand All @@ -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);
mistricky marked this conversation as resolved.
Show resolved Hide resolved
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)
}
mistricky marked this conversation as resolved.
Show resolved Hide resolved