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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//! Template stuff, definitions and how it's parsed
use std::fs::read_to_string;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::collections::HashMap;

use crate::config::Config;
use crate::config::Entries;
use crate::colors::Colors;

use anyhow::Result;
use owo_colors::OwoColorize;

/// Writes `template`s into `target`s. Given the many possibilities of I/O errors, template errors,
/// user typos, etc. Most errors are reported to stderr, and ignored to `continue` with the other
/// entries.
pub fn write_template(conf: &Config, image_path: &Path, entries: &[Entries], values: &Colors, quiet: bool) -> Result<()> {
    let config = &conf.dir;

    let warn = "W".red();
    let warn = warn.bold();

    // iterate over contents and pass it as an `&String` (which is casted to &str), apply the
    // template and write the templated(?) file to entry.path
    for e in entries {
        let path = config.join(&e.template).display().to_string();

        let file_template = match read_to_string(&path) {
            Ok(o) => o,
            Err(e) => {
                eprintln!("[{warn}] Skipping {path}: {e}");
                continue;
            }
        };

        let target = &e.target;
        let file_content = file_template;

        //XXX on `shellexpand`, think about using `::full()` to support env vars. Seems a bit sketchy/sus
        let target_file = shellexpand::tilde(target);

        if ! quiet { println!("  * Templating: {}", e.template); }

        if let Some(p) = Path::new(target_file.as_ref()).parent() {
            if let Err(e) = std::fs::create_dir_all(p) {
                eprintln!("[{warn}] Failed to create parent directories from {target}: {e}");
                continue;
            }
        } else {
            eprintln!("[{warn}] Failed to find file parent from {target}");
            continue;
        }

        let val = values.to_hash(image_path);

        let rendered =
            new_string_template::template::Template::new(file_content).render_nofail(&val)
        ;

        let mut buffer = match File::create(target_file.as_ref()) {
            Ok(o) => o,
            Err(e) => {
                eprintln!("[{warn}] Failed to create file {target}: {e}");
                continue;
            }
        };

        if let Err(e) = buffer.write_all(rendered.as_bytes()) {
            eprintln!("[{warn}] Failed to write to file {target}: {e}");
            continue;
        }

        if ! quiet { println!("      Created: {} ... OK", target); }
    }
    Ok(())
}

impl Colors {
    pub fn to_hash(&self, image_path: &Path) -> HashMap<&str, String> {
        let mut map = HashMap::new();
        //XXX instead of multiple `.method()` maybe using enums and match with a single method

        //full path to the image
        map.insert("wallpaper", image_path.display().to_string());
        map.insert("alpha", 100.to_string());

        // normal output `#EEEEEE`
        map.insert("color0" , self.color0 .to_string());
        map.insert("color1" , self.color1 .to_string());
        map.insert("color2" , self.color2 .to_string());
        map.insert("color3" , self.color3 .to_string());
        map.insert("color4" , self.color4 .to_string());
        map.insert("color5" , self.color5 .to_string());
        map.insert("color6" , self.color6 .to_string());
        map.insert("color7" , self.color7 .to_string());
        map.insert("color8" , self.color8 .to_string());
        map.insert("color9" , self.color9 .to_string());
        map.insert("color10", self.color10.to_string());
        map.insert("color11", self.color11.to_string());
        map.insert("color12", self.color12.to_string());
        map.insert("color13", self.color13.to_string());
        map.insert("color14", self.color14.to_string());
        map.insert("color15", self.color15.to_string());
        map.insert("cursor", self.foreground.to_string());
        map.insert("foreground", self.foreground.to_string());
        map.insert("background", self.background.to_string());

        //.rgb output `235,235,235`
        map.insert("color0.rgb" , self.color0 .rgb());
        map.insert("color1.rgb" , self.color1 .rgb());
        map.insert("color2.rgb" , self.color2 .rgb());
        map.insert("color3.rgb" , self.color3 .rgb());
        map.insert("color4.rgb" , self.color4 .rgb());
        map.insert("color5.rgb" , self.color5 .rgb());
        map.insert("color6.rgb" , self.color6 .rgb());
        map.insert("color7.rgb" , self.color7 .rgb());
        map.insert("color8.rgb" , self.color8 .rgb());
        map.insert("color9.rgb" , self.color9 .rgb());
        map.insert("color10.rgb", self.color10.rgb());
        map.insert("color11.rgb", self.color11.rgb());
        map.insert("color12.rgb", self.color12.rgb());
        map.insert("color13.rgb", self.color13.rgb());
        map.insert("color14.rgb", self.color14.rgb());
        map.insert("color15.rgb", self.color15.rgb());
        map.insert("cursor.rgb", self.foreground.rgb());
        map.insert("foreground.rgb", self.foreground.rgb());
        map.insert("background.rgb", self.background.rgb());

        //.rgba output `235,235,235,1.0`
        map.insert("color0.rgba" , self.color0 .rgba());
        map.insert("color1.rgba" , self.color1 .rgba());
        map.insert("color2.rgba" , self.color2 .rgba());
        map.insert("color3.rgba" , self.color3 .rgba());
        map.insert("color4.rgba" , self.color4 .rgba());
        map.insert("color5.rgba" , self.color5 .rgba());
        map.insert("color6.rgba" , self.color6 .rgba());
        map.insert("color7.rgba" , self.color7 .rgba());
        map.insert("color8.rgba" , self.color8 .rgba());
        map.insert("color9.rgba" , self.color9 .rgba());
        map.insert("color10.rgba", self.color10.rgba());
        map.insert("color11.rgba", self.color11.rgba());
        map.insert("color12.rgba", self.color12.rgba());
        map.insert("color13.rgba", self.color13.rgba());
        map.insert("color14.rgba", self.color14.rgba());
        map.insert("color15.rgba", self.color15.rgba());
        map.insert("cursor.rgba", self.foreground.rgba());
        map.insert("foreground.rgba", self.foreground.rgba());
        map.insert("background.rgba", self.background.rgba());

        //.xrgba output `ee/ee/ee/ff`
        map.insert("color0.xrgba" , self.color0 .xrgba());
        map.insert("color1.xrgba" , self.color1 .xrgba());
        map.insert("color2.xrgba" , self.color2 .xrgba());
        map.insert("color3.xrgba" , self.color3 .xrgba());
        map.insert("color4.xrgba" , self.color4 .xrgba());
        map.insert("color5.xrgba" , self.color5 .xrgba());
        map.insert("color6.xrgba" , self.color6 .xrgba());
        map.insert("color7.xrgba" , self.color7 .xrgba());
        map.insert("color8.xrgba" , self.color8 .xrgba());
        map.insert("color9.xrgba" , self.color9 .xrgba());
        map.insert("color10.xrgba", self.color10.xrgba());
        map.insert("color11.xrgba", self.color11.xrgba());
        map.insert("color12.xrgba", self.color12.xrgba());
        map.insert("color13.xrgba", self.color13.xrgba());
        map.insert("color14.xrgba", self.color14.xrgba());
        map.insert("color15.xrgba", self.color15.xrgba());
        map.insert("cursor.xrgba", self.foreground.xrgba());
        map.insert("foreground.xrgba", self.foreground.xrgba());
        map.insert("background.xrgba", self.background.xrgba());

        //.strip output `EEEEEE`
        map.insert("color0.strip" , self.color0 .strip());
        map.insert("color1.strip" , self.color1 .strip());
        map.insert("color2.strip" , self.color2 .strip());
        map.insert("color3.strip" , self.color3 .strip());
        map.insert("color4.strip" , self.color4 .strip());
        map.insert("color5.strip" , self.color5 .strip());
        map.insert("color6.strip" , self.color6 .strip());
        map.insert("color7.strip" , self.color7 .strip());
        map.insert("color8.strip" , self.color8 .strip());
        map.insert("color9.strip" , self.color9 .strip());
        map.insert("color10.strip", self.color10.strip());
        map.insert("color11.strip", self.color11.strip());
        map.insert("color12.strip", self.color12.strip());
        map.insert("color13.strip", self.color13.strip());
        map.insert("color14.strip", self.color14.strip());
        map.insert("color15.strip", self.color15.strip());
        map.insert("cursor.strip", self.foreground.strip());
        map.insert("foreground.strip", self.foreground.strip());
        map.insert("background.strip", self.background.strip());

        //.red output `235`
        map.insert("color0.red" , self.color0 .red());
        map.insert("color1.red" , self.color1 .red());
        map.insert("color2.red" , self.color2 .red());
        map.insert("color3.red" , self.color3 .red());
        map.insert("color4.red" , self.color4 .red());
        map.insert("color5.red" , self.color5 .red());
        map.insert("color6.red" , self.color6 .red());
        map.insert("color7.red" , self.color7 .red());
        map.insert("color8.red" , self.color8 .red());
        map.insert("color9.red" , self.color9 .red());
        map.insert("color10.red", self.color10.red());
        map.insert("color11.red", self.color11.red());
        map.insert("color12.red", self.color12.red());
        map.insert("color13.red", self.color13.red());
        map.insert("color14.red", self.color14.red());
        map.insert("color15.red", self.color15.red());
        map.insert("cursor.red", self.foreground.red());
        map.insert("foreground.red", self.foreground.red());
        map.insert("background.red", self.background.red());

        //.green output `235`
        map.insert("color0.green" , self.color0 .green());
        map.insert("color1.green" , self.color1 .green());
        map.insert("color2.green" , self.color2 .green());
        map.insert("color3.green" , self.color3 .green());
        map.insert("color4.green" , self.color4 .green());
        map.insert("color5.green" , self.color5 .green());
        map.insert("color6.green" , self.color6 .green());
        map.insert("color7.green" , self.color7 .green());
        map.insert("color8.green" , self.color8 .green());
        map.insert("color9.green" , self.color9 .green());
        map.insert("color10.green", self.color10.green());
        map.insert("color11.green", self.color11.green());
        map.insert("color12.green", self.color12.green());
        map.insert("color13.green", self.color13.green());
        map.insert("color14.green", self.color14.green());
        map.insert("color15.green", self.color15.green());
        map.insert("cursor.green", self.foreground.green());
        map.insert("foreground.green", self.foreground.green());
        map.insert("background.green", self.background.green());

        //.blue output `235`
        map.insert("color0.blue" , self.color0 .blue());
        map.insert("color1.blue" , self.color1 .blue());
        map.insert("color2.blue" , self.color2 .blue());
        map.insert("color3.blue" , self.color3 .blue());
        map.insert("color4.blue" , self.color4 .blue());
        map.insert("color5.blue" , self.color5 .blue());
        map.insert("color6.blue" , self.color6 .blue());
        map.insert("color7.blue" , self.color7 .blue());
        map.insert("color8.blue" , self.color8 .blue());
        map.insert("color9.blue" , self.color9 .blue());
        map.insert("color10.blue", self.color10.blue());
        map.insert("color11.blue", self.color11.blue());
        map.insert("color12.blue", self.color12.blue());
        map.insert("color13.blue", self.color13.blue());
        map.insert("color14.blue", self.color14.blue());
        map.insert("color15.blue", self.color15.blue());
        map.insert("cursor.blue", self.foreground.blue());
        map.insert("foreground.blue", self.foreground.blue());
        map.insert("background.blue", self.background.blue());

        map
    }
}