diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-03-12 15:54:59 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-03-12 15:54:59 +0800 |
| commit | 9d812580557cdc343378816cd65678b8aa75d944 (patch) | |
| tree | b1a3397e38d9620a487aed409fc94310f101bc27 /utils/src/env | |
| parent | 0a95bae451c1847f4f0b9601e60959f4e8e6b669 (diff) | |
Add lang field to command context and reorganize utils modules
Diffstat (limited to 'utils/src/env')
| -rw-r--r-- | utils/src/env/editor.rs | 19 | ||||
| -rw-r--r-- | utils/src/env/locales.rs | 28 | ||||
| -rw-r--r-- | utils/src/env/pager.rs | 15 |
3 files changed, 62 insertions, 0 deletions
diff --git a/utils/src/env/editor.rs b/utils/src/env/editor.rs new file mode 100644 index 0000000..c7bd446 --- /dev/null +++ b/utils/src/env/editor.rs @@ -0,0 +1,19 @@ +/// 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() +} diff --git a/utils/src/env/locales.rs b/utils/src/env/locales.rs new file mode 100644 index 0000000..302c874 --- /dev/null +++ b/utils/src/env/locales.rs @@ -0,0 +1,28 @@ +/// 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() +} diff --git a/utils/src/env/pager.rs b/utils/src/env/pager.rs new file mode 100644 index 0000000..3fdb1c3 --- /dev/null +++ b/utils/src/env/pager.rs @@ -0,0 +1,15 @@ +/// Gets the default pager based on environment variables. +/// +/// The function checks the JV_PAGER environment variable +/// and returns its value if it is set. If the variable is not set, +/// it returns "less" as the default pager. +/// +/// # Returns +/// A String containing the default pager +pub async fn get_default_pager() -> String { + if let Ok(pager) = std::env::var("JV_PAGER") { + return pager; + } + + "less".to_string() +} |
