clipboard dir

This commit is contained in:
2026-03-07 17:09:52 +01:00
parent f85007ebcd
commit ab2c25f8dc
4 changed files with 313 additions and 33 deletions

View File

@ -1,8 +1,10 @@
use arboard::{Clipboard, ImageData};
use image::codecs::png::PngEncoder;
use image::{ExtendedColorType, ImageEncoder};
use image::{ExtendedColorType, ImageEncoder, ImageReader};
use rand::{RngExt, distr::Alphanumeric};
use serde::{Deserialize, Serialize};
use std::fs::{self, File};
use std::io::Cursor;
use std::path::Path;
use std::result::Result;
use std::sync::mpsc::Sender;
@ -40,9 +42,11 @@ impl ClipboardHandler for Handler {
CallbackResult::Next
}
}
// X11 end
// Wayland
// Wayland end
mod base64_vec {
use base64::{Engine as _, engine::general_purpose::STANDARD};
use serde::{Deserialize, Deserializer, Serializer};
@ -64,6 +68,7 @@ mod base64_vec {
pub trait ImageDataExt {
fn to_png(&self) -> Result<Vec<u8>, Box<dyn Error>>;
fn save_img(bytes: Vec<u8>) -> Result<(), Box<dyn Error>>;
}
impl ImageDataExt for ImageData<'_> {
@ -78,6 +83,19 @@ impl ImageDataExt for ImageData<'_> {
)?;
Ok(buffer)
}
fn save_img(bytes: Vec<u8>) -> Result<(), Box<dyn Error>> {
let img = ImageReader::new(Cursor::new(&bytes))
.with_guessed_format()?
.decode()?;
let name: String = rand::rng()
.sample_iter(&Alphanumeric)
.take(16)
.map(char::from)
.collect();
img.save(format!("clipboard/{}.png", name))?;
Ok(())
}
}
impl ClipboardData {
@ -111,6 +129,17 @@ impl ClipboardEntry {
})
}
pub fn init(path: &str) -> Result<(), Box<dyn Error>> {
if !std::fs::exists(path)? {
fs::create_dir(path)?;
} else {
println!("Dir already exists.");
}
let file = Path::new(path).join("clipboard.json");
ClipboardEntry::new_json(file.to_str().unwrap())?;
Ok(())
}
pub fn new_json(path: &str) -> Result<(), Box<dyn Error>> {
if Path::new(path).exists() {
Err("File already exists.".into())
@ -148,17 +177,4 @@ impl ClipboardEntry {
let entries: Vec<ClipboardEntry> = serde_json::from_str(&data)?;
Ok(entries)
}
// pub fn write_json(&self, path: &str) -> Result<(), Box<dyn Error>> {
// let json = serde_json::to_string_pretty(self)?;
// let mut file = File::create(path)?;
// file.write_all(json.as_bytes())?;
// Ok(())
// }
// pub fn read_json(path: &str) -> Result<Self, Box<dyn Error>> {
// let data = fs::read_to_string(path)?;
// let entry: ClipboardEntry = serde_json::from_str(&data)?;
// Ok(entry)
// }
}

View File

@ -8,8 +8,9 @@ use std::sync::mpsc::channel;
#[cfg(feature = "x11")]
fn main() -> Result<(), Box<dyn Error>> {
let mut clipboard = Clipboard::new()?;
let path = "clipboard.json";
ClipboardEntry::new_json(path).unwrap_or(());
let dir_path = "clipboard";
let file_path = "clipboard/clipboard.json";
ClipboardEntry::init(dir_path).unwrap_or(());
let (tx, rx) = channel();
@ -26,7 +27,7 @@ fn main() -> Result<(), Box<dyn Error>> {
for _ in rx {
println!("Clipboard changed!");
if let Ok(entry) = ClipboardEntry::new(&mut clipboard) {
if let Err(e) = entry.append_json(path) {
if let Err(e) = entry.append_json(file_path) {
eprintln!("JSON writing error: {}", e);
} else {
println!("JSON edited!");