summaryrefslogtreecommitdiff
path: root/utils/src/legacy/env.rs
blob: 1834cd314dbf9813bc48de9d5f41765266947ab8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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)
        }
    })
}