uds ipc working
This commit is contained in:
@ -112,4 +112,10 @@ impl Database {
|
||||
|
||||
Ok(entries)
|
||||
}
|
||||
|
||||
pub fn delete_entry_by_content(&self, content: &str) -> Result<(), Box<dyn Error>> {
|
||||
self.conn
|
||||
.execute("DELETE FROM history WHERE content = ?1", [content])?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,6 +11,7 @@ use std::sync::{Arc, Mutex};
|
||||
pub enum IpcRequest {
|
||||
GetHistory { limit: usize },
|
||||
SetClipboard { content: String },
|
||||
DeleteEntry { content: String },
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
@ -90,6 +91,29 @@ pub fn start_server(db: Arc<Mutex<Database>>, socket_path: &Path) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
IpcRequest::DeleteEntry { content } => {
|
||||
{
|
||||
let db_lock = db_clone.lock().unwrap();
|
||||
let _ = db_lock.delete_entry_by_content(&content);
|
||||
}
|
||||
|
||||
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 img_path.exists() {
|
||||
if let Err(e) = std::fs::remove_file(&img_path) {
|
||||
eprintln!("Error while deleting image: {}", e);
|
||||
} else {
|
||||
println!("Image deleted : {}", content);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@ pub enum Mode {
|
||||
Normal,
|
||||
Command,
|
||||
Search,
|
||||
ConfirmDelete,
|
||||
}
|
||||
|
||||
pub struct App {
|
||||
|
||||
14
src/ipc.rs
14
src/ipc.rs
@ -6,6 +6,7 @@ use std::os::unix::net::UnixStream;
|
||||
pub enum IpcRequest {
|
||||
GetHistory { limit: usize },
|
||||
SetClipboard { content: String },
|
||||
DeleteEntry { content: String },
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
@ -49,3 +50,16 @@ pub fn set_clipboard(content: String) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete_entry(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::DeleteEntry { 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
15
src/main.rs
15
src/main.rs
@ -60,7 +60,7 @@ fn run_app(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>, app: &mut App)
|
||||
}
|
||||
KeyCode::Char('d') => {
|
||||
if last_key_was_d {
|
||||
app.delete_selected();
|
||||
app.mode = Mode::ConfirmDelete;
|
||||
last_key_was_d = false;
|
||||
} else {
|
||||
last_key_was_d = true;
|
||||
@ -150,6 +150,19 @@ fn run_app(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>, app: &mut App)
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Mode::ConfirmDelete => match key.code {
|
||||
KeyCode::Char('y') | KeyCode::Char('Y') | KeyCode::Enter => {
|
||||
if let Some(selected) = app.get_selected_item() {
|
||||
crate::ipc::delete_entry(selected.clone());
|
||||
app.delete_selected();
|
||||
}
|
||||
app.mode = Mode::Normal;
|
||||
}
|
||||
KeyCode::Char('n') | KeyCode::Char('N') | KeyCode::Esc => {
|
||||
app.mode = Mode::Normal;
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
@ -21,7 +21,7 @@ pub fn render(f: &mut Frame, app: &mut App) {
|
||||
|
||||
let content_chunks = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
|
||||
.constraints([Constraint::Length(50), Constraint::Min(0)])
|
||||
.split(main_chunks[0]);
|
||||
|
||||
let items: Vec<ListItem> = app
|
||||
@ -72,6 +72,10 @@ pub fn render(f: &mut Frame, app: &mut App) {
|
||||
Span::styled("/", Style::default().fg(Color::Cyan)),
|
||||
Span::raw(&app.input_buffer),
|
||||
]),
|
||||
Mode::ConfirmDelete => Line::from(vec![Span::styled(
|
||||
"Delete ? (y/n)",
|
||||
Style::default().fg(Color::Red).add_modifier(Modifier::BOLD),
|
||||
)]),
|
||||
};
|
||||
|
||||
let bottom_bar = Paragraph::new(bottom_text).block(Block::default().borders(Borders::ALL));
|
||||
|
||||
Reference in New Issue
Block a user