use base64::Engine;
use std::io::Cursor;
use crate::session::ImageAttachment;
fn is_ssh_or_headless() -> bool {
std::env::var("SSH_CONNECTION").is_ok()
|| std::env::var("SSH_TTY").is_ok()
|| (std::env::var("TERM")
.ok()
.map_or(false, |t| t.starts_with("xterm"))
&& std::env::var("DISPLAY").is_err()
&& std::env::var("WAYLAND_DISPLAY").is_err())
}
pub fn get_clipboard_image() -> Option<ImageAttachment> {
if is_ssh_or_headless() {
return None;
}
let mut clipboard = arboard::Clipboard::new().ok()?;
let img_data = clipboard.get_image().ok()?;
let width = img_data.width;
let height = img_data.height;
let raw_bytes = img_data.bytes.into_owned();
let buf: image::ImageBuffer<image::Rgba<u8>, Vec<u8>> =
image::ImageBuffer::from_raw(width as u32, height as u32, raw_bytes)?;
let mut png_bytes = Vec::new();
buf.write_to(&mut Cursor::new(&mut png_bytes), image::ImageFormat::Png)
.ok()?;
let b64 = base64::engine::general_purpose::STANDARD.encode(&png_bytes);
Some(ImageAttachment {
data_url: format!("data:image/png;base64,{b64}"),
mime_type: Some("image/png".to_string()),
})
}
pub fn get_clipboard_text() -> Option<String> {
if is_ssh_or_headless() {
return None;
}
let mut clipboard = arboard::Clipboard::new().ok()?;
clipboard.get_text().ok().filter(|t| !t.is_empty())
}