Pi66

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

src/create_templates.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use crate::utils::{get_cache, get_config, share_files, warning};
use std::{
    fs::{self, create_dir_all, read_to_string, write},
    process::exit,
};

fn change(template: &str, colors: (u8, u8, u8), alpha: u8) -> String {
    let (r, g, b) = colors;

    if template.contains(".strip") {
        format!("{r:02x}{g:02x}{b:02x}")
    } else if template.contains(".xrgba") {
        format!("{r:02x}/{g:02x}/{b:02x}/{alpha:02x}")
    } else if template.contains(".rgba") {
        format!("{r},{g},{b},{alpha}")
    } else if template.contains(".rgb") {
        format!("{r},{g},{b}")
    } else if template.contains(".alpha_per") {
        format!("{:.1}", (alpha / 255) * 100)
    } else if template.contains(".alpha") {
        format!("#{r:02x}{g:02x}{b:02x}{alpha:02x}")
    } else {
        format!("#{r:02x}{g:02x}{b:02x}")
    }
}

fn fill_template(
    template_name: &str,
    template: &str,
    colors: &(Vec<(u8, u8, u8)>, u8),
    wallpaper: &str,
    send: bool,
) {
    let output_path = get_cache(send).join("wal").join(template_name);
    let alpha = colors.1;

    let mut result = template
        .replace("{wallpaper}", wallpaper)
        .replace("{alpha}", &format!("{}", (alpha / 255) * 100));

    if !colors.0.is_empty() {
        let background_patterns = [
            "{background.strip}",
            "{background.xrgba}",
            "{background.rgba}",
            "{background.rgb}",
            "{background.alpha}",
            "{background}",
        ];

        for pattern in background_patterns {
            let replacement = change(pattern, colors.0[0], alpha);
            result = result.replace(pattern, &replacement);
        }
    }

    if colors.0.len() > 7 {
        let foreground_patterns = [
            "{foreground.strip}",
            "{foreground.xrgba}",
            "{foreground.rgba}",
            "{foreground.rgb}",
            "{foreground.alpha}",
            "{foreground}",
        ];

        for pattern in foreground_patterns {
            let replacement = change(pattern, colors.0[7], alpha);
            result = result.replace(pattern, &replacement);
        }

        let cursor_patterns = [
            "{cursor.strip}",
            "{cursor.xrgba}",
            "{cursor.rgba}",
            "{cursor.rgb}",
            "{cursor.alpha}",
            "{cursor}",
        ];

        for pattern in cursor_patterns {
            let replacement = change(pattern, colors.0[7], alpha);
            result = result.replace(pattern, &replacement);
        }
    }

    for i in 0..colors.0.len().min(16) {
        let color = colors.0[i];

        let patterns = [
            format!("{{color{i}.strip}}"),
            format!("{{color{i}.xrgba}}"),
            format!("{{color{i}.rgba}}"),
            format!("{{color{i}.rgb}}"),
            format!("{{color{i}.alpha_dec}}"),
            format!("{{color{i}.alpha}}"),
            format!("{{color{i}}}"),
        ];

        for pattern in patterns {
            let replacement = change(&pattern, color, alpha);
            result = result.replace(&pattern, &replacement);
        }
    }

    if result.contains("{checksum}") {
        let checksum = colors
            .0
            .iter()
            .map(|(r, g, b)| format!("{r:02X}{g:02X}{b:02X}"))
            .collect::<Vec<String>>()
            .join("");
        result = result.replace("{checksum}", &checksum);
    }

    write(output_path, result).expect("Failed to write filled template");
}

pub fn create_template(colors: (Vec<(u8, u8, u8)>, u8), wallpaper: &str, send: bool) {
    let system_template_path = share_files().join("templates");
    let user_template_path = get_config(send).join("walrs").join("templates");
    let cache_path = get_cache(send).join("wal");
    create_dir_all(&cache_path).unwrap_or_else(|_| {
        warning("Create", "can't create the cache folder", send);
        exit(1)
    });

    // Check if user templates directory exists and has templates
    let mut has_user_templates = false;
    if let Ok(entries) = fs::read_dir(&user_template_path) {
        for entry in entries.flatten() {
            if entry.path().is_file() {
                has_user_templates = true;
                let Ok(content) = read_to_string(entry.path()) else {
                    continue;
                };
                let Some(name) = entry.file_name().into_string().ok() else {
                    continue;
                };
                fill_template(&name, &content, &colors, wallpaper, send);
            }
        }
    }

    // If no user templates, copy from system templates
    if !has_user_templates {
        create_dir_all(&user_template_path).unwrap_or_else(|_| {
            warning("Create", "can't create user template path", send);
            exit(1)
        });

        if let Ok(entries) = fs::read_dir(system_template_path) {
            for entry in entries.flatten() {
                if entry.path().is_file() {
                    let Ok(content) = read_to_string(entry.path()) else {
                        continue;
                    };
                    let Some(name) = entry.file_name().into_string().ok() else {
                        continue;
                    };

                    // Copy template to user directory
                    let user_file_path = format!("{}/{name}", user_template_path.display());
                    let _ = write(&user_file_path, &content);

                    fill_template(&name, &content, &colors, wallpaper, send);
                }
            }
        }
    }
}