Pi66

walrs
pywal but if its Fast-Minimal-Simple
git clone https://git.pi66.xyz/walrs

src/utils.rs

  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
108
use std::env;
use std::path::{Path, PathBuf};
use std::process::{exit, Stdio};
use std::{fs, process::Command};

pub fn share_files() -> PathBuf {
    PathBuf::from("/usr").join("share").join("walrs")
}

pub fn image_path(image: Option<String>, send: bool) -> String {
    match image {
        Some(ref v) if Path::new(v).exists() => match get_absolute_path(v) {
            Some(p) => {
                if Path::new(&p).is_file() {
                    p
                } else {
                    std::str::from_utf8(
                        &std::process::Command::new("sh")
                            .arg("-c")
                            .arg(format!("find \"{p}\" -type f | sort -R | head -n1"))
                            .output()
                            .unwrap()
                            .stdout,
                    )
                    .unwrap()
                    .trim()
                    .to_string()
                }
            }
            None => {
                warning("Wallpaper", "Can't find wallpaper absolute path!", send);
                exit(1);
            }
        },
        Some(_) => {
            warning("Image", "Image does not exist", send);
            exit(1);
        }
        None => {
            warning("Image", "Can't find Image", send);
            exit(1);
        }
    }
}

pub fn run(command: &str) -> bool {
    Command::new("sh")
        .arg("-c")
        .arg(command)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
}

pub fn print_colors(send: bool) {
    if send {
        if let Ok(output) = Command::new("bash")
            .arg("-c")
            .arg(r#"for i in {30..37} 90; do echo -en "\033[0;${i}m●\033[0m "; done; echo"#)
            .output()
        {
            if output.status.success() {
                print!("{}", String::from_utf8_lossy(&output.stdout));
            }
        }
    }
}

pub fn warning(title: &str, message: &str, send: bool) {
    if send {
        println!("[\x1b[33mW\x1b[0m] \x1b[31m{title}:\x1b[0m {message}.");
    }
}

pub fn info(title: &str, message: &str, send: bool) {
    if send {
        println!("[\x1b[32mI\x1b[0m] \x1b[31m{title}:\x1b[0m {message}.");
    }
}

pub fn get_home(send: bool) -> PathBuf {
    env::var("HOME").map(PathBuf::from).unwrap_or_else(|_| {
        warning("Home", "can't find the home dir", send);
        exit(1)
    })
}

pub fn get_config(send: bool) -> PathBuf {
    env::var_os("XDG_CONFIG_HOME")
        .map(PathBuf::from)
        .unwrap_or_else(|| get_home(send).join(".config"))
}
pub fn get_cache(send: bool) -> PathBuf {
    env::var_os("XDG_CACHE_HOME")
        .map(PathBuf::from)
        .unwrap_or_else(|| get_home(send).join(".cache"))
}

pub fn get_absolute_path(path_str: &str) -> Option<String> {
    let path = Path::new(path_str);
    if !path.is_absolute() {
        fs::canonicalize(path).ok()?.to_str().map(|s| s.to_string())
    } else {
        Some(path_str.to_string())
    }
}