Pi66

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

← back to log

remove dirs-next and use env instead

author: pixel2175
date: 2025-05-31 20:14
hash: 99c7c441209583c4bb232f93487815f57b0abf3e

Diffstat:

M

src/create_templates.rs
26 ++++++++++++++++++++----------

M

src/reload.rs
13 +++------------

M

src/theme.rs
23 +++++++++++----------------

M

src/utils.rs
22 ++++++++++++++++---------
  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
diff --git a/src/create_templates.rs b/src/create_templates.rs
index 879d019..43d46a5 100644
--- a/src/create_templates.rs
+++ b/src/create_templates.rs
@@ -1,4 +1,4 @@
-use crate::utils::{get_cache_folder, get_config_folder};
+use crate::utils::{get_cache, get_config, warning};
 use std::fs::{self, create_dir_all, read_to_string, write};

 fn change(template: &str, colors: (u8, u8, u8), alpha: u8) -> String {
@@ -26,10 +26,11 @@ fn fill_template(
     template: &str,
     colors: &(Vec<(u8, u8, u8)>, u8),
     wallpaper: &str,
+    send: bool,
 ) {
     let output_path = format!(
         "{}/wal/{}",
-        get_cache_folder().expect("Can't find cache folder"),
+        get_cache(send).to_string_lossy().to_string(),
         template_name
     );
     let alpha = colors.1;
@@ -116,11 +117,16 @@ fn fill_template(
     write(output_path, result).expect("Failed to write filled template");
 }

-pub fn create_template(colors: (Vec<(u8, u8, u8)>, u8), wallpaper: &str) {
+pub fn create_template(colors: (Vec<(u8, u8, u8)>, u8), wallpaper: &str, send: bool) {
     let system_template_path = "/etc/walrs/templates/";
-    let user_template_path = format!("{}/walrs/templates/", get_config_folder().unwrap());
-    let cache_path = format!("{}/wal/", get_cache_folder().unwrap());
-    let _ = create_dir_all(&cache_path);
+    let user_template_path = format!(
+        "{}/walrs/templates/",
+        get_config(send).to_string_lossy().to_string()
+    );
+    let cache_path = format!("{}/wal/", get_cache(send).to_string_lossy().to_string());
+    create_dir_all(&cache_path).unwrap_or_else(|_| {
+        warning("Create", "can't create the cache folder", send);
+    });

     // Check if user templates directory exists and has templates
     let mut has_user_templates = false;
@@ -134,14 +140,16 @@ pub fn create_template(colors: (Vec<(u8, u8, u8)>, u8), wallpaper: &str) {
                 let Some(name) = entry.file_name().into_string().ok() else {
                     continue;
                 };
-                fill_template(&name, &content, &colors, wallpaper);
+                fill_template(&name, &content, &colors, wallpaper, send);
             }
         }
     }

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

         if let Ok(entries) = fs::read_dir(system_template_path) {
             for entry in entries.flatten() {
@@ -157,7 +165,7 @@ pub fn create_template(colors: (Vec<(u8, u8, u8)>, u8), wallpaper: &str) {
                     let user_file_path = format!("{}{}", user_template_path, name);
                     let _ = write(&user_file_path, &content);

-                    fill_template(&name, &content, &colors, wallpaper);
+                    fill_template(&name, &content, &colors, wallpaper, send);
                 }
             }
         }
diff --git a/src/reload.rs b/src/reload.rs
index cdf3aad..f4d43b9 100644
--- a/src/reload.rs
+++ b/src/reload.rs
@@ -1,12 +1,11 @@
 use std::fs::OpenOptions;
 use std::fs::{read_dir, read_to_string};
 use std::io::Write;
-use std::process::exit;
 use std::process::{Command, Stdio};

 use crate::wallpaper;

-use crate::utils::{get_cache_folder, info, warning};
+use crate::utils::{get_cache, info};

 fn run(command: &str) -> bool {
     Command::new("sh")
@@ -105,7 +104,7 @@ fn colors(colors: Vec<String>, send: bool) {
 }

 pub fn reload(send: bool, set_wal: bool) {
-    let cache = get_cache_folder().expect("Can't get cache path");
+    let cache = get_cache(send).to_string_lossy().to_string();
     let file_path = format!("{}/wal/colors", cache);

     let lines: Vec<String> = std::fs::read_to_string(&file_path)
@@ -115,13 +114,7 @@ pub fn reload(send: bool, set_wal: bool) {
         .collect();

     // Spawn threads
-    let cache = match get_cache_folder() {
-        Some(v) => v,
-        None => {
-            warning("Path", "can't found cache folder", send);
-            exit(1)
-        }
-    };
+    let cache = get_cache(send).to_string_lossy().to_string();

     if set_wal {
         let wallpaper = read_to_string(format!("{}/wal/wal", cache))
diff --git a/src/theme.rs b/src/theme.rs
index 6260b38..38a9490 100644
--- a/src/theme.rs
+++ b/src/theme.rs
@@ -1,17 +1,12 @@
 use crate::{
     create_templates::create_template,
     reload::reload,
-    utils::{get_config_folder, run, warning},
-};
-use dirs_next::config_dir;
-use std::{
-    fs::{create_dir_all, read_dir, read_to_string},
-    path::Path,
+    utils::{get_config, run, warning},
 };
+use std::fs::{create_dir_all, read_dir, read_to_string};

-pub fn collect_themes(subdir: &str) -> Vec<String> {
-    let binding = get_config_folder().unwrap();
-    let base = Path::new(&binding);
+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);
@@ -28,20 +23,20 @@ pub fn collect_themes(subdir: &str) -> Vec<String> {
 }

 pub fn set_theme(theme_name: String, send: bool) {
-    let (dark, light) = (collect_themes("dark"), collect_themes("light"));
+    let (dark, light) = (collect_themes("dark", send), collect_themes("light", send));
     let mut theme: Vec<String> = dark.into_iter().chain(light).collect();
     if theme.is_empty() {
-        let dis = config_dir().unwrap().join("wal").join("colorschemes");
+        let dis = get_config(send).join("wal").join("colorschemes");
         create_dir_all(&dis).unwrap();
         run(&format!(
             "cp -r /etc/walrs/colorschemes/* {}/walrs/colorschemes",
-            get_config_folder().unwrap()
+            get_config(send).to_string_lossy().to_string()
         ));
     }
     theme.sort();
     theme.dedup();
     if theme.contains(&theme_name) {
-        let base = config_dir().unwrap();
+        let base = get_config(send);

         let file: Vec<String> = [
             base.join("wal/colorschemes/dark").join(&theme_name),
@@ -65,7 +60,7 @@ pub fn set_theme(theme_name: String, send: bool) {
             .collect::<Result<Vec<_>, _>>()
             .unwrap();

-        create_template((rgb_colors, 100), "None");
+        create_template((rgb_colors, 100), "None", send);
         reload(send, false);
     } else {
         warning("Theme", "Can't find theme", send);
diff --git a/src/utils.rs b/src/utils.rs
index e568477..14d6ab9 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -1,6 +1,6 @@
-use dirs_next::{cache_dir, config_dir};
-use std::path::Path;
-use std::process::Stdio;
+use std::env;
+use std::path::{Path, PathBuf};
+use std::process::{exit, Stdio};
 use std::{fs, process::Command};

 pub fn run(command: &str) -> bool {
@@ -31,6 +31,7 @@ pub fn print_colors(send: bool) {
 pub fn warning(title: &str, message: &str, send: bool) {
     if send {
         println!("[\x1b[33mW\x1b[0m] \x1b[31m{title}:\x1b[0m {message}.");
+        exit(1)
     }
 }

@@ -40,12 +41,17 @@ pub fn info(title: &str, message: &str, send: bool) {
     }
 }

-pub fn get_config_folder() -> Option<String> {
-    config_dir()?.to_str().map(|s| s.to_string())
+pub fn get_home(send: bool) -> PathBuf {
+    env::home_dir().unwrap_or_else(|| {
+        warning("Home", "can't find the home dir", send);
+        exit(1)
+    })
 }
-
-pub fn get_cache_folder() -> Option<String> {
-    cache_dir()?.to_str().map(|s| s.to_string())
+pub fn get_config(send: bool) -> PathBuf {
+    get_home(send).join(".config")
+}
+pub fn get_cache(send: bool) -> PathBuf {
+    get_home(send).join(".cache")
 }

 pub fn get_absolute_path(path_str: &str) -> Option<String> {