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 | diff --git a/src/main.rs b/src/main.rs
index e1ee390..ab409b8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,10 +12,10 @@ use create_templates::create_template;
use get_colors::get_colors;
use reload::reload;
use std::fs::{copy, create_dir_all};
-use std::path::Path;
use std::process::exit;
use theme::{print_themes, set_theme, theme_exists};
use utils::*;
+use wallpaper::change_wallpaper;
#[derive(FromArgs)]
#[argh(description = "walrs - Generate colorscheme from image")]
@@ -130,26 +130,25 @@ fn main() {
// show or set theme from user
if let Some(v) = arg.theme {
+ let config = get_config(send);
if v == "themes" {
print_themes(send);
+ } else if theme_exists(&config) {
+ set_theme(v, send);
} else {
- let config = get_config(send);
- if theme_exists(&config) {
- set_theme(v, send);
- } else {
- let colorschemes_dir = get_config(send).join("walrs").join("colorschemes");
- create_dir_all(&colorschemes_dir).unwrap();
- let etc = Path::new("/etc/");
- if !theme_exists(etc) {
- warning("theme", "Can't find configuration directory", send);
- exit(1)
- }
- run(&format!(
- "cp -r /etc/walrs/colorschemes/* {}",
- colorschemes_dir.display()
- ));
- set_theme(v, send);
- };
+ let colorschemes_dir = config.join("walrs").join("colorschemes");
+ create_dir_all(&colorschemes_dir).unwrap();
+ let walrs_cache = get_cache(send);
+ if !theme_exists(&walrs_cache) {
+ warning("theme", "Can't find configuration directory", send);
+ exit(1)
+ }
+ run(&format!(
+ "cp -r {}/* {}",
+ walrs_cache.join("walrs").join("colorschemes").display(),
+ colorschemes_dir.display()
+ ));
+ set_theme(v, send);
}
exit(0);
}
@@ -176,7 +175,8 @@ fn main() {
create_template(palette, &image_path, send);
info("Template", "create templates", send);
- reload(send, true);
+ change_wallpaper(&image_path, send);
+ reload(send, false);
print_colors(send);
};
}
diff --git a/src/reload.rs b/src/reload.rs
index c45ae88..823e175 100644
--- a/src/reload.rs
+++ b/src/reload.rs
@@ -43,7 +43,6 @@ fn colors(colors: Vec<String>, send: bool) {
// 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);
@@ -58,6 +57,7 @@ pub fn get_wallpaper(cache: &Path, send: bool) -> String {
pub fn reload(send: bool, set_wal: bool) {
let cache = get_cache(send).join("wal");
+ let walrs_cache = get_cache(send).join("walrs");
let file_path = cache.join("colors");
// read the colors file and load all the colors
@@ -84,8 +84,9 @@ pub fn reload(send: bool, set_wal: bool) {
match create_dir_all(&scripts_dir) {
Ok(_) => {
run(&format!(
- "cp /etc/walrs/scripts/* {}",
- scripts_dir.to_string_lossy()
+ "cp -r {}/* {}",
+ walrs_cache.join("scripts").display(),
+ scripts_dir.display()
));
}
Err(_) => return,
diff --git a/src/theme.rs b/src/theme.rs
index eac2838..1b7ebcf 100644
--- a/src/theme.rs
+++ b/src/theme.rs
@@ -1,14 +1,14 @@
use crate::{
create_templates::create_template,
reload::reload,
- utils::{get_config, run, warning},
+ utils::{get_cache, get_config, run, warning},
};
use std::fs::{create_dir_all, read_dir, read_to_string};
use std::path::Path;
use std::process::exit;
pub fn theme_exists(dir: &Path) -> bool {
- dir.join("walrs").join("colorscheme").exists() || dir.join("wal").join("colorscheme").exists()
+ dir.join("walrs").join("colorschemes").exists() || dir.join("wal").join("colorschemes").exists()
}
pub fn print_themes(send: bool) {
@@ -60,11 +60,12 @@ pub fn set_theme(theme_name: String, send: bool) {
.collect();
if !theme.is_empty() {
- let dis = get_config(send).join("wal").join("colorschemes");
+ let dis = get_config(send).join("walrs").join("colorschemes");
create_dir_all(&dis).unwrap();
run(&format!(
- "cp -r /etc/walrs/colorschemes/* {}/walrs/colorschemes",
- base.display()
+ "cp -r {}/* {}",
+ get_cache(send).join("walrs").join("colorschemes").display(),
+ base.join("walrs").join("colorschemes").display()
));
}
theme.sort();
diff --git a/src/utils.rs b/src/utils.rs
index 9aa7c0b..d86088c 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -75,21 +75,26 @@ pub fn info(title: &str, message: &str, send: bool) {
}
pub fn get_home(send: bool) -> PathBuf {
- Path::new(&env::var("HOME").unwrap_or_else(|_| {
+ env::var("HOME").map(PathBuf::from).unwrap_or_else(|_| {
warning("Home", "can't find the home dir", send);
exit(1)
- }))
- .to_path_buf()
+ })
}
pub fn get_config(send: bool) -> PathBuf {
- get_home(send).join(".config")
+ env::var_os("XDG_CONFIG_HOME")
+ .map(PathBuf::from)
+ .unwrap_or_else(|| get_home(send).join(".config"))
}
pub fn get_cache(send: bool) -> PathBuf {
- get_home(send).join(".cache")
+ env::var_os("XDG_CACHE_HOME")
+ .map(PathBuf::from)
+ .unwrap_or_else(|| get_home(send).join(".cache"))
}
-pub fn get_absolute_path(path_str: &str) -> Option<String> {
- let path = Path::new(path_str);
- fs::canonicalize(path).ok()?.to_str().map(|s| s.to_string())
+fn get_absolute_path(path_str: &str) -> Option<String> {
+ fs::canonicalize(Path::new(path_str))
+ .ok()?
+ .to_str()
+ .map(|s| s.to_string())
}
|