diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-03-12 14:28:08 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-03-12 14:28:08 +0800 |
| commit | 0a95bae451c1847f4f0b9601e60959f4e8e6b669 (patch) | |
| tree | 9e1cfad4f86a73176a4d738b28e7732b66fe5f97 /utils/src/env.rs | |
| parent | 8564c8f2177dec0c2c0c031d156347fa6b4485bc (diff) | |
Refactor display utilities
Diffstat (limited to 'utils/src/env.rs')
| -rw-r--r-- | utils/src/env.rs | 107 |
1 files changed, 0 insertions, 107 deletions
diff --git a/utils/src/env.rs b/utils/src/env.rs deleted file mode 100644 index 1834cd3..0000000 --- a/utils/src/env.rs +++ /dev/null @@ -1,107 +0,0 @@ -use std::path::PathBuf; - -/// Returns the current locale string based on environment variables. -/// -/// The function checks for locale settings in the following order: -/// 1. JV_LANG environment variable -/// 2. APP_LANG environment variable -/// 3. LANG environment variable (extracts base language before dot and replaces underscores with hyphens) -/// 4. Defaults to "en" if no locale environment variables are found -/// -/// # Returns -/// A String containing the detected locale code -pub fn current_locales() -> String { - if let Ok(lang) = std::env::var("JV_LANG") { - return lang; - } - - if let Ok(lang) = std::env::var("APP_LANG") { - return lang; - } - - if let Ok(lang) = std::env::var("LANG") { - if let Some(base_lang) = lang.split('.').next() { - return base_lang.replace('_', "-"); - } - return lang; - } - - "en".to_string() -} - -/// Checks if auto update is enabled based on environment variables. -/// -/// The function checks the JV_AUTO_UPDATE environment variable and compares -/// its value (after trimming and converting to lowercase) against known -/// positive and negative values. -/// -/// # Returns -/// `true` if the value matches "yes", "y", or "true" -/// `false` if the value matches "no", "n", or "false", or if the variable is not set -pub fn enable_auto_update() -> bool { - if let Ok(auto_update) = std::env::var("JV_AUTO_UPDATE") { - let normalized = auto_update.trim().to_lowercase(); - match normalized.as_str() { - "yes" | "y" | "true" => return true, - "no" | "n" | "false" => return false, - _ => {} - } - } - false -} - -/// Gets the auto update expiration time based on environment variables. -/// -/// The function checks the JV_OUTDATED_MINUTES environment variable. -/// Requires JV_AUTO_UPDATE to be enabled. -/// Next time the `jv` command is used, if the content is outdated, `jv update` will be automatically executed. -/// -/// # Returns -/// - When the set number is < 0, timeout-based update is disabled -/// - When the set number = 0, update runs every time (not recommended) -/// - When the set number > 0, update according to the specified time -/// - If not set or conversion error occurs, the default is -1 -pub fn auto_update_outdate() -> i64 { - if !enable_auto_update() { - return -1; - } - - match std::env::var("JV_OUTDATED_MINUTES") { - Ok(value) => match value.trim().parse::<i64>() { - Ok(num) => num, - Err(_) => -1, - }, - Err(_) => -1, - } -} - -/// Gets the default text editor based on environment variables. -/// -/// The function checks the JV_TEXT_EDITOR and EDITOR environment variables -/// and returns their values if they are set. If neither variable is set, -/// it returns "jvii" as the default editor. -/// -/// # Returns -/// A String containing the default text editor -pub async fn get_default_editor() -> String { - if let Ok(editor) = std::env::var("JV_TEXT_EDITOR") { - return editor; - } - - if let Ok(editor) = std::env::var("EDITOR") { - return editor; - } - - "jvii".to_string() -} - -/// Get temporary file path -pub fn current_tempfile_path(name: &str) -> Option<PathBuf> { - dirs::config_local_dir().map(|path| { - if cfg!(target_os = "linux") { - path.join("jvcs").join(".temp").join(name) - } else { - path.join("JustEnoughVCS").join(".temp").join(name) - } - }) -} |
