From 4ab442fc90b0e793ff077e09341dd14764fe0acf Mon Sep 17 00:00:00 2001 From: zilcH40 Date: Wed, 8 Feb 2023 16:28:43 +0800 Subject: [PATCH] fix: destruct rime when Ctrl-C pressed --- Cargo.toml | 3 ++- src/lib.rs | 2 +- src/main.rs | 12 ++++++++++++ src/rime.rs | 7 +++++-- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0aac195..084908e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ librime-sys = { version = "0.1.0", git = "https://github.com/lotem/librime-sys" dashmap = "5.4.0" regex = "1.7.1" ropey = "1.5.1" -tokio = { version = "1.17", features = ["io-util", "io-std", "macros", "rt-multi-thread", "net"] } +tokio = { version = "1", features = ["io-util", "io-std", "macros", "rt-multi-thread", "net"] } tower-lsp = "0.17.0" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.91" @@ -20,6 +20,7 @@ directories = "4.0.1" ouroboros = "0.15.5" thiserror = "1.0.38" once_cell = "1.17.0" +ctrlc = "3.2.5" [features] default = [] diff --git a/src/lib.rs b/src/lib.rs index 8a3d341..f263c34 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ mod consts; /// handle user input mod input; /// librime C FFI -mod rime; +pub mod rime; /// helper functions mod utils; diff --git a/src/main.rs b/src/main.rs index 235bb44..d398422 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use std::{net::SocketAddr, str::FromStr}; use rime_ls::lsp::Backend; +use rime_ls::rime::Rime; use tokio::net::{TcpListener, TcpStream}; use tower_lsp::{LspService, Server}; @@ -35,6 +36,17 @@ fn usage() { #[tokio::main] async fn main() { + // set handler to finalize rime + // TODO: it is ugly + ctrlc::set_handler(move || { + println!("Ctrl-C pressed."); + if Rime::is_initialized() { + Rime::global().destroy(); + } + std::process::exit(0); // 0? + }) + .expect("Error setting Ctrl-C handler"); + let mut args = std::env::args(); match args.nth(1).as_deref() { None => run_stdio().await, diff --git a/src/rime.rs b/src/rime.rs index 992678f..3a82a93 100644 --- a/src/rime.rs +++ b/src/rime.rs @@ -50,9 +50,8 @@ pub struct RimeResponse { impl Drop for Rime { fn drop(&mut self) { - // FIXME: it seems that staic variables will not be dorpped? + // FIXME: it seems that static once_cell variables will not be dropped? self.destroy(); - println!("rime exit"); } } @@ -62,6 +61,10 @@ impl Rime { RIME.get().expect("Rime is not initialized") } + pub fn is_initialized() -> bool { + RIME.get().is_some() + } + pub fn init( shared_data_dir: &str, user_data_dir: &str,