chwp 1.239.71

Change your wallpaper from the command line interface.
use std::path::Path;

use rand::Rng;

/// Holds the application configuration
pub struct Config {
    pub span: bool,
    pub query: String,
    pub api_key: Option<String>,
}

/// Parse the CLI arguments
/// # Arguments
/// * `args` - The CLI arguments
/// # Returns
/// The parsed configuration
pub fn parse_cli_args(args: Vec<String>) -> Config {
    let span_string = "span".to_string();
    let span = args.contains(&span_string);

    let mut keywords = args;
    remove_element(&mut keywords, span_string);

    let keyword = if keywords.is_empty() {
        "".to_string()
    } else {
        choose_random_keyword(keywords)
    };

    Config {
        span,
        query: keyword,
        api_key: std::env::var("WALLHAVEN_API_KEY").ok(),
    }
}

/// Choose a random keyword from a list of keywords
/// # Arguments
/// * `keywords` - The list of keywords
/// # Returns
/// A random keyword
fn choose_random_keyword(keywords: Vec<String>) -> String {
    if keywords.len() > 1 {
        let random_index = rand::rng().random_range(0..keywords.len());
        keywords.get(random_index).unwrap().to_string()
    } else {
        keywords.first().unwrap().to_string()
    }
}

/// Remove an element from a vector
/// # Arguments
/// * `keywords` - The list of keywords
/// * `term` - The element to remove
/// # Returns
/// The list of keywords without the removed element
fn remove_element(keywords: &mut Vec<String>, term: String) {
    let index = keywords.iter().position(|item| *item == term);

    if let Some(index) = index {
        keywords.remove(index);
    }
}

/// Check if a string is a URL
/// # Arguments
/// * `to_check` - The string to check
/// # Returns
/// True if the string is a URL
pub fn is_url(to_check: &str) -> bool {
    to_check.starts_with("http") && to_check.contains("://")
}

use crate::utils::expand_tilde;

/// Check if a string is a local path
/// # Arguments
/// * `to_check` - The string to check
/// # Returns
/// True if the string is a local path
pub fn is_local_path(to_check: &str) -> bool {
    let expanded = expand_tilde(to_check);
    Path::new(&expanded).exists()
}