Pi66

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

src/theme.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
use crate::{
    create_templates::create_template,
    reload::reload,
    utils::{get_config, run, share_files, warning},
};
use std::fs::{create_dir_all, read_dir, read_to_string};
use std::path::Path;
use std::process::exit;

pub fn theme_exists(dir: &Path) -> bool {
    dir.join("walrs").join("colorschemes").exists() || dir.join("wal").join("colorschemes").exists()
}

pub fn print_themes(send: bool) {
    let (dark, light) = (collect_themes("dark", send), collect_themes("light", send));

    println!("[\x1b[33mDark\x1b[0m]");
    for theme in dark {
        println!("    -{theme}")
    }

    println!("[\x1b[33mLight\x1b[0m]");
    for theme in light {
        println!("    -{theme}")
    }
}

pub fn collect_themes(subdir: &str, send: bool) -> Vec<String> {
    let base = get_config(send);
    let mut themes = vec![];
    for folder in ["wal", "walrs"] {
        let path = base.join(folder).join("colorschemes").join(subdir);
        if let Ok(entries) = read_dir(path) {
            themes.extend(
                entries
                    .filter_map(|e| e.ok())
                    .filter_map(|e| e.file_name().into_string().ok()),
            );
        }
    }

    themes
}

fn hex_to_rgb(file: Vec<String>) -> Vec<(u8, u8, u8)> {
    file.iter()
        .map(|h| {
            u32::from_str_radix(&h[1..], 16)
                .map(|v| ((v >> 16) as u8, (v >> 8 & 0xFF) as u8, (v & 0xFF) as u8))
        })
        .collect::<Result<Vec<_>, _>>()
        .unwrap()
}

pub fn set_theme(theme_name: String, send: bool, scripts: bool) {
    let base = get_config(send);
    let mut theme: Vec<String> = ["dark", "light"]
        .iter()
        .flat_map(|variant| collect_themes(variant, send))
        .collect();

    if !theme.is_empty() {
        let dis = get_config(send).join("walrs").join("colorschemes");
        create_dir_all(&dis).unwrap();
        run(&format!(
            "cp -r {}/* {}",
            share_files().join("colorschemes").display(),
            base.join("walrs").join("colorschemes").display()
        ));
    }
    theme.sort();
    theme.dedup();
    if theme.contains(&theme_name) {
        let file: Vec<String> = [
            base.join("wal/colorschemes/dark"),
            base.join("wal/colorschemes/light"),
            base.join("walrs/colorschemes/dark"),
            base.join("walrs/colorschemes/light"),
        ]
        .into_iter()
        .find_map(|p| read_to_string(p.join(&theme_name)).ok())
        .unwrap()
        .lines()
        .map(|l| l.to_string())
        .collect();

        let rgb_colors = hex_to_rgb(file);

        create_template((rgb_colors, 100), "None", send);
        reload(send, true, scripts);
    } else {
        warning("Theme", "Can't find theme", send);
        exit(1)
    }
}