uds ipc working
This commit is contained in:
@ -7,10 +7,10 @@ use std::os::unix::net::UnixListener;
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
// --- LE CONTRAT (Protocole) ---
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub enum IpcRequest {
|
||||
GetHistory { limit: usize },
|
||||
SetClipboard { content: String },
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
@ -63,6 +63,33 @@ pub fn start_server(db: Arc<Mutex<Database>>, socket_path: &Path) {
|
||||
let response_json = serde_json::to_string(&response).unwrap();
|
||||
let _ = stream.write_all(response_json.as_bytes());
|
||||
}
|
||||
IpcRequest::SetClipboard { content } => {
|
||||
if let Ok(mut clipboard) = arboard::Clipboard::new() {
|
||||
if content.ends_with(".jpg") || content.ends_with(".png") {
|
||||
if let Some(proj_dirs) = directories::ProjectDirs::from(
|
||||
"com", "zefad", "rklipd",
|
||||
) {
|
||||
let img_path = proj_dirs
|
||||
.data_dir()
|
||||
.join("images")
|
||||
.join(&content);
|
||||
if let Ok(img) = image::open(&img_path) {
|
||||
let rgba = img.into_rgba8();
|
||||
let img_data = arboard::ImageData {
|
||||
width: rgba.width() as usize,
|
||||
height: rgba.height() as usize,
|
||||
bytes: std::borrow::Cow::Borrowed(
|
||||
rgba.as_raw(),
|
||||
),
|
||||
};
|
||||
let _ = clipboard.set_image(img_data);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let _ = clipboard.set_text(content);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
23
src/app.rs
23
src/app.rs
@ -28,8 +28,14 @@ impl App {
|
||||
let mut list_state = ListState::default();
|
||||
list_state.select(Some(0));
|
||||
|
||||
let items = ipc::fetch_history(100)
|
||||
.unwrap_or_else(|| vec!["rklipd deamon unaccessible".to_string()]);
|
||||
let items = ipc::fetch_history(100).unwrap_or_default();
|
||||
|
||||
let mut list_state = ListState::default();
|
||||
if items.is_empty() {
|
||||
list_state.select(None);
|
||||
} else {
|
||||
list_state.select(Some(0));
|
||||
}
|
||||
|
||||
let picker = Picker::from_query_stdio().unwrap_or_else(|_| Picker::halfblocks());
|
||||
|
||||
@ -175,6 +181,17 @@ impl App {
|
||||
}
|
||||
|
||||
pub fn get_selected_item(&self) -> Option<&String> {
|
||||
self.list_state.selected().map(|i| &self.filtered_items[i])
|
||||
self.list_state
|
||||
.selected()
|
||||
.and_then(|i| self.filtered_items.get(i))
|
||||
}
|
||||
|
||||
pub fn sync_with_daemon(&mut self) {
|
||||
if let Some(new_history) = crate::ipc::fetch_history(100) {
|
||||
if self.all_items != new_history {
|
||||
self.all_items = new_history;
|
||||
self.update_search();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
14
src/ipc.rs
14
src/ipc.rs
@ -5,6 +5,7 @@ use std::os::unix::net::UnixStream;
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub enum IpcRequest {
|
||||
GetHistory { limit: usize },
|
||||
SetClipboard { content: String },
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
@ -35,3 +36,16 @@ pub fn fetch_history(limit: usize) -> Option<Vec<String>> {
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn set_clipboard(content: String) {
|
||||
if let Some(base_dir) = directories::ProjectDirs::from("com", "zefad", "rklipd") {
|
||||
let socket_path = base_dir.data_dir().join("rklip.sock");
|
||||
if let Ok(mut stream) = UnixStream::connect(&socket_path) {
|
||||
let req = IpcRequest::SetClipboard { content };
|
||||
if let Ok(req_json) = serde_json::to_string(&req) {
|
||||
let _ = stream.write_all(req_json.as_bytes());
|
||||
let _ = stream.shutdown(std::net::Shutdown::Write);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
20
src/main.rs
20
src/main.rs
@ -11,6 +11,7 @@ use crossterm::{
|
||||
};
|
||||
use ratatui::{Terminal, backend::CrosstermBackend};
|
||||
use std::io;
|
||||
use std::time::Duration;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
enable_raw_mode()?;
|
||||
@ -39,9 +40,16 @@ fn run_app(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>, app: &mut App)
|
||||
loop {
|
||||
terminal.draw(|f| ui::render(f, app))?;
|
||||
|
||||
if event::poll(Duration::from_millis(500))? {
|
||||
if let Event::Key(key) = event::read()? {
|
||||
match app.mode {
|
||||
Mode::Normal => match key.code {
|
||||
KeyCode::Enter => {
|
||||
if let Some(selected) = app.get_selected_item() {
|
||||
crate::ipc::set_clipboard(selected.clone());
|
||||
app.should_quit = true;
|
||||
}
|
||||
}
|
||||
KeyCode::Char('j') => {
|
||||
app.next();
|
||||
last_key_was_d = false;
|
||||
@ -121,12 +129,17 @@ fn run_app(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>, app: &mut App)
|
||||
},
|
||||
|
||||
Mode::Search => match key.code {
|
||||
// ... ton code de recherche ...
|
||||
KeyCode::Esc | KeyCode::Enter => {
|
||||
KeyCode::Esc => {
|
||||
app.mode = Mode::Normal;
|
||||
app.input_buffer.clear();
|
||||
app.update_search();
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
if let Some(selected) = app.get_selected_item() {
|
||||
crate::ipc::set_clipboard(selected.clone());
|
||||
app.should_quit = true;
|
||||
}
|
||||
}
|
||||
KeyCode::Char(c) => {
|
||||
app.input_buffer.push(c);
|
||||
app.update_search();
|
||||
@ -139,6 +152,9 @@ fn run_app(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>, app: &mut App)
|
||||
},
|
||||
}
|
||||
}
|
||||
} else {
|
||||
app.sync_with_daemon();
|
||||
}
|
||||
|
||||
if app.should_quit {
|
||||
return Ok(());
|
||||
|
||||
Reference in New Issue
Block a user