Skip to content

Commit

Permalink
fix: destruct rime when Ctrl-C pressed
Browse files Browse the repository at this point in the history
  • Loading branch information
wlh320 committed Feb 8, 2023
1 parent 07882ba commit 4ab442f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ 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"
directories = "4.0.1"
ouroboros = "0.15.5"
thiserror = "1.0.38"
once_cell = "1.17.0"
ctrlc = "3.2.5"

[features]
default = []
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod consts;
/// handle user input
mod input;
/// librime C FFI
mod rime;
pub mod rime;
/// helper functions
mod utils;

Expand Down
12 changes: 12 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions src/rime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand All @@ -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,
Expand Down

0 comments on commit 4ab442f

Please sign in to comment.