leetcode_tui_config/
constants.rs

1use std::env;
2use std::path::Path;
3use std::sync::OnceLock;
4
5pub static PROJECT_NAME: OnceLock<String> = OnceLock::new();
6pub static GIT_COMMIT_HASH: OnceLock<String> = OnceLock::new();
7pub static LOG_ENV: OnceLock<String> = OnceLock::new();
8pub static LOG_FILE: OnceLock<String> = OnceLock::new();
9pub static EDITOR: OnceLock<String> = OnceLock::new();
10
11pub(crate) fn init() {
12    let project_name = env!("CARGO_CRATE_NAME").to_uppercase().to_string();
13
14    PROJECT_NAME.get_or_init(|| project_name.clone());
15
16    GIT_COMMIT_HASH.get_or_init(|| {
17        std::env::var(format!("{}_GIT_INFO", project_name.clone()))
18            .unwrap_or_else(|_| String::from("UNKNOWN"))
19    });
20
21    LOG_ENV.get_or_init(|| format!("{}_LOGLEVEL", project_name.clone()));
22
23    LOG_FILE.get_or_init(|| format!("{}.log", env!("CARGO_PKG_NAME")));
24
25    EDITOR.get_or_init(|| {
26        if let Ok(env_editor) = std::env::var("EDITOR") {
27            env_editor
28        } else if is_executable_in_path("nvim") {
29            "nvim".into()
30        } else if is_executable_in_path("vim") {
31            "vim".into()
32        } else if is_executable_in_path("nano") {
33            "nano".into()
34        } else {
35            "code".into()
36        }
37    });
38}
39
40fn is_executable_in_path(executable_name: &str) -> bool {
41    if let Some(path_var) = env::var_os("PATH") {
42        if let Some(paths) = env::split_paths(&path_var).collect::<Vec<_>>().first() {
43            for path_dir in paths.iter() {
44                let path_dir: &Path = path_dir.as_ref();
45                let executable_path = path_dir.join(executable_name);
46                if executable_path.exists() && executable_path.is_file() {
47                    return true;
48                }
49            }
50        }
51    }
52    false
53}