Pi66

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

src/reload.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
use crate::utils::{get_cache, get_config, info, run, share_files, warning};
use crate::wallpaper::change_wallpaper;
use std::fs::{create_dir_all, OpenOptions};
use std::fs::{read_dir, read_to_string};
use std::io::Write;
use std::path::Path;
use std::process::exit;

fn colors(colors: Vec<String>, send: bool) {
    for i in read_dir("/dev/pts/").unwrap_or_else(|_| {
        warning("Terminal", "Unable to find open terminals", send);
        exit(1)
    }) {
        let file = i.unwrap().file_name().into_string().unwrap();
        if file != "ptmx" && file.parse::<i32>().is_ok() {
            let special_index = [(10, 7), (11, 0), (12, 15), (708, 5)];
            let term = file.parse::<i32>().unwrap();
            for (i, value) in colors.iter().enumerate() {
                let sequence = format!("\x1b]4;{i};{value}\x1b\\");
                if let Ok(mut file) = OpenOptions::new()
                    .write(true)
                    .open(format!("/dev/pts/{term}"))
                {
                    file.write_all(sequence.as_bytes()).unwrap_or_else(|_| {
                        warning("Colors", "Can't apply terminal colors", send);
                    });
                };
            }
            if let Ok(mut file) = OpenOptions::new()
                .write(true)
                .open(format!("/dev/pts/{term}"))
            {
                for (i, v) in special_index {
                    let sequence = format!("\x1b]{};{}\x1b\\", i, colors[v]);

                    let _ = file.write_all(sequence.as_bytes());
                }
            }
        }
    }
    info("Terminal", "terminal colorscheme set", send);
}

// read ~/.cache/wal/wal file and return the wallpaper path
pub fn get_wallpaper(cache: &Path, send: bool) -> String {
    read_to_string(cache.join("wal"))
        .unwrap_or_else(|_| {
            warning("Wallpaper", "Can't find the wallpaper", send);
            exit(1)
        })
        .lines()
        .next()
        .unwrap()
        .trim()
        .to_string()
}

pub fn reload(send: bool, set_wal: bool, run_scripts: bool) {
    let cache = get_cache(send).join("wal");
    let walrs_cache = share_files();
    let file_path = cache.join("colors");

    // read the colors file and load all the colors
    let lines: Vec<String> = std::fs::read_to_string(&file_path)
        .unwrap_or_else(|_| {
            warning("Colors", "can't read colors", send);
            exit(1)
        })
        .lines()
        .map(|line| line.to_string())
        .collect();

    // applie the wallpaper
    if !set_wal {
        change_wallpaper(&get_wallpaper(&cache.clone(), send.clone()), send.clone())
    }

    // change terminal colors
    colors(lines, send);

    if !run_scripts {
        // initial scripts files
        let scripts_dir = get_config(send).join("walrs").join("scripts");
        if !scripts_dir.exists() {
            match create_dir_all(&scripts_dir) {
                Ok(_) => {
                    run(&format!(
                        "cp -r {}/* {}",
                        walrs_cache.join("scripts").display(),
                        scripts_dir.display()
                    ));
                }
                Err(_) => return,
            }
        }

        // read the scripts directory and run them
        match read_dir(scripts_dir) {
            Ok(v) => {
                for scr in v {
                    let script = scr.unwrap().path();
                    if !script.is_file() {
                        continue;
                    };
                    if !run(&format!(
                        "bash {}",
                        &script.canonicalize().unwrap().to_string_lossy()
                    )) {
                        warning(
                            "Script",
                            &format!(
                                "can't run {}",
                                script.file_name().unwrap().to_string_lossy()
                            ),
                            send,
                        );
                    }
                }
                info("Scripts", "scripts runs successfully", send);
            }
            _ => return,
        }
    }
    info("Colors", "colorscheme applied successfully", send);
}