walrs
pywal but if its Fast-Minimal-Simple
git clone https://git.pi66.xyz/walrs
About | Files | Commits | Refs | License
remove dirs-next and use env instead
Author: pixel2175
Date: 2025-05-31 20:14
commit: 99c7c441
Stat
| M | src/create_templates.rs | 26 | +17 | -9 |
| M | src/reload.rs | 13 | +3 | -10 |
| M | src/theme.rs | 23 | +9 | -14 |
| M | src/utils.rs | 22 | +14 | -8 |
Diff
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(),
...
Show all
Show less
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(),
...
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> {