diff --git a/Cargo.toml b/Cargo.toml index 7109f45..89e8e50 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "rime_ls" version = "0.3.0" edition = "2021" authors = ["ZilcH40 "] -rust-version = "1.63" +rust-version = "1.64" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/config.rs b/src/config.rs index b43ff99..848c856 100644 --- a/src/config.rs +++ b/src/config.rs @@ -101,7 +101,7 @@ impl Default for Config { always_incomplete: default_always_incomplete(), preselect_first: default_preselect_first(), long_filter_text: default_long_filter_text(), - show_filter_text_in_label: default_show_filter_text_in_label() + show_filter_text_in_label: default_show_filter_text_in_label(), } } } @@ -131,7 +131,7 @@ fn default_paging_characters() -> Vec { } fn default_shared_data_dir() -> PathBuf { - PathBuf::from("/usr/share/rime-data") + PathBuf::from(crate::utils::rime_default_shared_data_dir()) } fn default_user_data_dir() -> PathBuf { diff --git a/src/rime.rs b/src/rime.rs index 384d64a..dd4ae48 100644 --- a/src/rime.rs +++ b/src/rime.rs @@ -1,7 +1,7 @@ use crate::consts::{APP_NAME, KEY_BACKSPACE, KEY_ESCAPE, RAW_RE}; use librime_sys as librime; use once_cell::sync::OnceCell; -use std::ffi::{CStr, CString, NulError, c_char}; +use std::ffi::{c_char, CStr, CString, NulError}; use std::sync::Mutex; use thiserror::Error; @@ -298,14 +298,12 @@ impl Rime { #[test] fn test_get_candidates() { - let shared_data_dir = "/usr/share/rime-data/"; - let base_dir = directories::BaseDirs::new().unwrap(); - let data_dir = base_dir.data_dir().join("rime-ls-test"); - let user_data_dir = data_dir.to_str().unwrap(); - let log_dir = "/tmp"; + let shared_data_dir = crate::utils::rime_default_shared_data_dir(); + let temp_dir = std::env::temp_dir(); + let temp_dir = temp_dir.to_str().unwrap(); // init - Rime::init(shared_data_dir, user_data_dir, log_dir).unwrap(); + Rime::init(shared_data_dir, temp_dir, temp_dir).unwrap(); let rime = Rime::global(); // simulate typing let keys = vec![b'w', b'l', b'h']; diff --git a/src/utils.rs b/src/utils.rs index 215e2b7..f5eab56 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -80,6 +80,20 @@ pub fn expand_tilde(path: impl AsRef) -> PathBuf { home_dir.join(path.as_ref().strip_prefix("~").unwrap()) } +/// rime's default shared data dir +pub fn rime_default_shared_data_dir() -> &'static str { + // read environment variable first + if let Some(var) = option_env!("RIME_DEFAULT_DATA_DIR") { + var + } else if cfg!(target_os = "macos") { + "/Library/Input Methods/Squirrel.app/Contents/SharedSupport" + } else { + // cannot determine shared data dir on windows + // Ref: https://github.com/rime/home/wiki/SharedData + "/usr/share/rime-data" + } +} + #[inline] fn char_is_word(ch: char) -> bool { ch.is_alphanumeric() || ch == '_'