Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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"]
94 changes: 84 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,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);
Comment thread
mistricky marked this conversation as resolved.
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)
}
Comment thread
mistricky marked this conversation as resolved.
Binary file modified lua/linux-x86_64generator.so

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These .so files should not be committed, because them is created by CI, I'll add the them into .gitignore

Binary file not shown.
Binary file modified lua/mac-aarch64generator.so
Binary file not shown.
Binary file modified lua/mac-x86_64generator.so
Binary file not shown.