lcode_config/
global.rs

1use std::{collections::HashMap, fs::create_dir_all, path::PathBuf, sync::LazyLock};
2
3use crate::{config::LcodeConfig, theme::Theme};
4
5pub const G_APP_NAME: &str = "lcode";
6pub const LOG_FILE: &str = "lcode.log";
7
8pub static G_THEME: LazyLock<Theme> = LazyLock::new(Theme::default);
9
10/// # Get dir path and create dir
11///
12/// ~/.cache/lcode/
13pub static G_CACHE_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
14    let mut log_dir = dirs::cache_dir().expect("new cache dir failed");
15    log_dir.push(G_APP_NAME);
16    create_dir_all(&log_dir).expect("create cache dir failed");
17    log_dir
18});
19
20pub static G_LOG_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
21    let mut log_path = G_CACHE_DIR.clone();
22    log_path.push(LOG_FILE);
23    log_path
24});
25
26/// global user config
27pub static G_USER_CONFIG: LazyLock<LcodeConfig> =
28    LazyLock::new(|| LcodeConfig::get_user_conf().expect("get G_USER_CONFIG falied"));
29
30/// "~/.cache/lcode/leetcode-<cn/com>.db"
31pub static G_DATABASE_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
32    let mut db_path = G_CACHE_DIR.clone();
33    db_path.push(format!("leetcode-{}.db", G_USER_CONFIG.config.url_suffix));
34    db_path
35});
36
37/// # Initialize the config directory create dir if not exists
38/// "~/.config/lcode/"
39static G_CONF_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
40    #[cfg(not(target_os = "macos"))]
41    let mut config_dir = dirs::config_dir().expect("new config dir failed");
42
43    #[cfg(target_os = "macos")]
44    let mut config_dir = {
45        let mut dir = PathBuf::from(std::env::var("HOME").expect("get $HOME failed"));
46        dir.push(".config/");
47        dir
48    };
49
50    config_dir.push(G_APP_NAME);
51    create_dir_all(&config_dir).expect("G_CONF_DIR create_dir_all failed");
52    config_dir
53});
54
55/// # get the config path
56/// "~/.config/lcode/config.toml"
57pub static G_CONFIG_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
58    let mut dir = G_CONF_DIR.clone();
59    dir.push("config.toml");
60    dir
61});
62
63/// # get the cookies config path
64/// "~/.config/lcode/cookies.toml"
65pub static G_COOKIES_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
66    let mut dir = G_CONF_DIR.clone();
67    dir.push("cookies.toml");
68    dir
69});
70
71/// # get the lang config path
72/// "~/.config/lcode/langs.toml"
73pub static G_LANGS_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
74    let mut dir = G_CONF_DIR.clone();
75    dir.push("langs.toml");
76    dir
77});
78
79/// # get the keymap config path
80/// "~/.config/lcode/keymap.toml"
81pub static G_KEYMAP_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
82    let mut dir = G_CONF_DIR.clone();
83    dir.push("keymap.toml");
84    dir
85});
86
87pub static G_SUPPORT_LANGS: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
88    HashMap::from([
89        ("rust", ".rs"),
90        ("bash", ".sh"),
91        ("c", ".c"),
92        ("cpp", ".cpp"),
93        ("csharp", ".cs"),
94        ("golang", ".go"),
95        ("java", ".java"),
96        ("javascript", ".js"),
97        ("kotlin", ".kt"),
98        ("mysql", ".sql"),
99        ("php", ".php"),
100        ("python", ".py"),
101        ("python3", ".py"),
102        ("ruby", ".rb"),
103        ("scala", ".scala"),
104        ("swift", ".swift"),
105        ("typescript", ".ts"),
106        ("racket", ".rkt"),
107        ("erlang", ".erl"),
108        ("elixir", ".x"),
109        ("dart", ".dart"),
110        ("react", ".jsx"),
111        ("Postgresql", ".sql"),
112        ("oraclesql", ".sql"),
113        ("mysql", ".sql"),
114        ("mssql", ".sql"),
115    ])
116});