Skip to main content

leenfetch_core/modules/
utils.rs

1use std::{collections::HashMap, fs, path::Path};
2
3use super::{ascii::get_builtin_ascii_art, colors::get_builtin_distro_colors};
4
5pub const DEFAULT_ANSI_ALL_COLORS: [&str; 16] = [
6    "\x1b[1;30m", // Black
7    "\x1b[1;31m", // Red
8    "\x1b[1;32m", // Green
9    "\x1b[1;33m", // Yellow
10    "\x1b[1;34m", // Blue
11    "\x1b[1;35m", // Magenta
12    "\x1b[1;36m", // Cyan
13    "\x1b[1;37m", // White
14    "\x1b[1;90m", // Bright Black
15    "\x1b[1;91m", // Bright Red
16    "\x1b[1;92m", // Bright Green
17    "\x1b[1;93m", // Bright Yellow
18    "\x1b[1;94m", // Bright Blue
19    "\x1b[1;95m", // Bright Magenta
20    "\x1b[1;96m", // Bright Cyan
21    "\x1b[1;97m", // Bright White
22];
23
24/// Generates a visual bar representation of a percentage using Unicode blocks.
25///
26/// # Arguments
27///
28/// * `percent` - An unsigned 8-bit integer representing the percentage (0-100) to be visualized.
29///
30/// # Returns
31///
32/// * A `String` containing a visual bar representation, with filled blocks (`█`) indicating the
33///   percentage, and empty blocks (`░`) for the remainder. The total length of the bar is 14
34///   characters, enclosed in square brackets.
35pub fn get_bar(percent: u8) -> String {
36    let total_blocks = 14;
37    let filled_blocks = (percent as usize * total_blocks) / 100;
38    let empty_blocks = total_blocks - filled_blocks;
39
40    // Pre-computed block strings for common values (avoid allocations)
41    const BLOCKS: &[&str] = &[
42        "",
43        "█",
44        "██",
45        "███",
46        "████",
47        "█████",
48        "██████",
49        "███████",
50        "████████",
51        "█████████",
52        "██████████",
53        "███████████",
54        "████████████",
55        "█████████████",
56        "██████████████",
57    ];
58    const EMPTY_BLOCKS: &[&str] = &[
59        "",
60        "░",
61        "░░",
62        "░░░",
63        "░░░░",
64        "░░░░░",
65        "░░░░░░",
66        "░░░░░░░",
67        "░░░░░░░░",
68        "░░░░░░░░░",
69        "░░░░░░░░░░",
70        "░░░░░░░░░░░",
71        "░░░░░░░░░░░░",
72        "░░░░░░░░░░░░░",
73        "░░░░░░░░░░░░░░",
74    ];
75
76    let filled = BLOCKS[filled_blocks.min(14)];
77    let empty = EMPTY_BLOCKS[empty_blocks.min(14)];
78
79    format!("[{}{}]", filled, empty)
80}
81
82/// Generates a vector of 2 strings, each containing a row of 8 blocks
83/// colored with different ANSI foreground colors. The first string has
84/// normal colors, the second has bold colors.
85///
86/// The input string `color_blocks` should contain 8 identical block characters
87/// (e.g. █, ░, ▓, ▒, etc.). The output strings will have these blocks
88/// colored with different ANSI colors.
89pub fn get_terminal_color(color_blocks: &str) -> String {
90    let color_codes: [u8; 8] = [30, 31, 32, 33, 34, 35, 36, 37]; // ANSI foreground colors
91
92    let mut normal = Vec::with_capacity(8);
93    // let mut bold = Vec::with_capacity(8);
94
95    for &code in &color_codes {
96        normal.push(format!("\x1b[{}m{}\x1b[0m", code, color_blocks)); // normal
97                                                                       // bold.push(format!("\x1b[1;{}m{}\x1b[0m", code, color_blocks)); // bold
98    }
99
100    // vec![normal.join(""), bold.join("")]
101    normal.join("")
102}
103
104// ---------------------------------
105//        ASCII ART Functions
106// ---------------------------------
107
108/// Reads a file at the given custom path and returns its content as a string.
109/// If there is an error while reading the file, an empty string is returned.
110///
111/// # Arguments
112///
113/// * `custom_path`: The path to the custom ASCII art file.
114///
115/// # Returns
116///
117/// A string containing the content of the file, or an empty string if there was an error.
118pub fn get_custom_ascii(custom_path: &str) -> String {
119    if let Ok(content) = fs::read_to_string(Path::new(custom_path)) {
120        return content;
121    }
122
123    "".to_string()
124}
125
126/// Given a distro name, returns a string of its corresponding ASCII art.
127/// If the distro isn't found, an empty string is returned.
128/// If the distro is "off", an empty string is returned.
129pub fn get_ascii_and_colors(ascii_distro: &str) -> String {
130    if ascii_distro == "off" {
131        return "".to_string();
132    }
133
134    let ascii_art = get_builtin_ascii_art(ascii_distro);
135
136    ascii_art.to_string()
137}
138
139// ---------------------------------
140//        Color Functions
141// ---------------------------------
142
143/// Replaces placeholders in a string with ANSI escape codes to colorize
144/// the output.
145///
146/// Placeholders are in the form of `${{key}}`, where `key` is the key in
147/// the provided `colors` HashMap. The value associated with the `key` is
148/// the ANSI escape code for the color.
149pub fn colorize_text(input: String, colors: &HashMap<&str, &str>) -> String {
150    let mut result = String::new();
151
152    for line in input.lines() {
153        let mut colored = line.to_owned();
154        for (key, code) in colors {
155            let placeholder = format!("${{{}}}", key);
156            colored = colored.replace(&placeholder, code);
157        }
158        result.push_str(&colored);
159        result.push('\n');
160    }
161
162    result
163}
164
165/// Creates a `HashMap` of ANSI color codes from the given entries.
166///
167/// Each entry is a tuple containing a key and a corresponding ANSI
168/// color code. The function populates the `HashMap` with these entries
169/// and also adds bold variants for each color code that starts with
170/// `\x1b[0;`. The bold variant key is prefixed with "bold." (e.g.,
171/// `bold.c1` for `c1`).
172///
173/// Additionally, a "reset" key is included in the map, which maps to
174/// the ANSI reset code `\x1b[0m`.
175///
176/// # Arguments
177///
178/// * `entries` - A slice of tuples where each tuple contains a string
179///   key and an ANSI color code.
180///
181/// # Returns
182///
183/// * A `HashMap` with the original entries, their bold variants, and a
184///   reset entry.
185pub fn color_palette(
186    entries: &[(&'static str, &'static str)],
187) -> HashMap<&'static str, &'static str> {
188    let mut map = HashMap::new();
189    for (k, v) in entries {
190        map.insert(*k, *v);
191
192        // Add bold variant: bold.c1 → \x1b[1;31m if c1 is \x1b[0;31m
193        if let Some(code) = v.strip_prefix("\x1b[0;") {
194            let bold_code = format!("\x1b[1;{}", code);
195            let bold_key = format!("bold.{}", k);
196            map.insert(
197                Box::leak(bold_key.into_boxed_str()),
198                Box::leak(bold_code.into_boxed_str()),
199            );
200        }
201    }
202
203    map.insert("reset", "\x1b[0m");
204    map
205}
206
207/// Given a slice of color indices or a distro name, generates a HashMap
208/// of `cX` keys (where `X` is the index, starting from 1) mapped to the
209/// corresponding ANSI foreground color codes.
210///
211/// The color order is determined by the input slice. The first color is
212/// assigned to `c1`, the second to `c2`, and so on. If the input slice is
213/// shorter than 16 elements, the remaining colors are filled from the
214/// default color palette in the order they appear.
215///
216/// If the input slice is empty, the function returns an empty HashMap.
217///
218/// The HashMap also includes bold variants for each color, which can be
219/// accessed using the `bold.*` keys. For example, `bold.c1` would be the
220/// bold variant of `c1`.
221///
222/// The `reset` key is also included, which resets the text to the default
223/// color.
224pub fn get_colors_in_order(color_order: &[u8]) -> HashMap<&'static str, &'static str> {
225    // Start with c0 = bold black
226    let mut entries: Vec<(&'static str, &'static str)> = vec![("c0", "\x1b[1;30m")];
227
228    let mut used = vec![false; 16]; // support 0–15
229
230    // Fill c1 to cX using given color_order
231    for (i, &idx) in color_order.iter().enumerate() {
232        if idx < 16 {
233            let key: &'static str = Box::leak(format!("c{}", i + 1).into_boxed_str());
234            entries.push((key, DEFAULT_ANSI_ALL_COLORS[idx as usize]));
235            used[idx as usize] = true;
236        }
237    }
238
239    // Fill remaining cX from unused colors
240    let mut next_index = color_order.len() + 1;
241    for (i, &color) in DEFAULT_ANSI_ALL_COLORS.iter().enumerate() {
242        if !used[i] {
243            let key: &'static str = Box::leak(format!("c{}", next_index).into_boxed_str());
244            entries.push((key, color));
245            next_index += 1;
246        }
247    }
248
249    // Generate HashMap with bold.* variants and reset
250    let mut map = color_palette(&entries);
251    map.insert("reset", "\x1b[0m");
252    map
253}
254
255/// Given a string of comma-separated color indices or a distro name, returns a
256/// HashMap of color codes c0 to cX as found in the given distro's color
257/// definition. The color codes are from the ANSI color palette.
258pub fn get_custom_colors_order(colors_str_order: &str) -> HashMap<&'static str, &'static str> {
259    let custom_color_str_list: Vec<&str> = colors_str_order.split(',').map(str::trim).collect();
260
261    // Try to parse all color indices
262    let all_parsed: Option<Vec<u8>> = custom_color_str_list
263        .iter()
264        .map(|s| s.parse::<u8>().ok())
265        .collect();
266
267    let color_list: Vec<u8> = if let Some(list) = all_parsed {
268        list
269    } else {
270        // Fallback: interpret the string as a distro name
271        get_builtin_distro_colors(colors_str_order).to_vec()
272    };
273
274    get_colors_in_order(&color_list)
275}
276
277/// Given a distro name, returns a HashMap of color codes c0 to cX as found
278/// in the given distro's color definition. The color codes are from the
279/// ANSI color palette. If the distro isn't found, an empty HashMap is returned.
280pub fn get_distro_colors(distro: &str) -> HashMap<&'static str, &'static str> {
281    let dist_color = get_builtin_distro_colors(distro);
282
283    get_colors_in_order(dist_color)
284}
285
286#[cfg(test)]
287mod tests {
288    use super::*;
289
290    #[test]
291    fn bar_respects_percentage_bounds() {
292        assert_eq!(get_bar(0), "[░░░░░░░░░░░░░░]");
293        assert_eq!(get_bar(100), "[██████████████]");
294        assert!(get_bar(50).contains('█'));
295    }
296
297    #[test]
298    fn terminal_color_emits_expected_blocks() {
299        let visual = get_terminal_color("■");
300        assert_eq!(visual.matches('■').count(), 8);
301        assert!(visual.contains("\x1b[31m"), "missing ANSI escape: {visual}");
302    }
303
304    #[test]
305    fn color_palette_includes_bold_and_reset() {
306        let map = color_palette(&[("c1", "\x1b[0;31m")]);
307        assert_eq!(map.get("c1"), Some(&"\x1b[0;31m"));
308        let bold_key = map
309            .keys()
310            .find(|k| k.starts_with("bold.c1"))
311            .expect("bold variant missing");
312        assert!(map.get(bold_key).unwrap().starts_with("\x1b[1;"));
313        assert_eq!(map.get("reset"), Some(&"\x1b[0m"));
314    }
315
316    #[test]
317    fn colors_in_order_fills_defaults() {
318        let map = get_colors_in_order(&[1, 2, 3]);
319        assert_eq!(map.get("c1"), Some(&DEFAULT_ANSI_ALL_COLORS[1]));
320        assert_eq!(map.get("c2"), Some(&DEFAULT_ANSI_ALL_COLORS[2]));
321        assert_eq!(map.get("c3"), Some(&DEFAULT_ANSI_ALL_COLORS[3]));
322        assert!(map.contains_key("c4"));
323        assert_eq!(map.get("reset"), Some(&"\x1b[0m"));
324    }
325}