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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
extern crate dirs;
extern crate multipart;
extern crate reqwest;
extern crate tar;
pub mod ravenserver;
use std::fs::DirEntry;
pub mod themes;
pub mod config {
use crate::themes::*;
use dirs::home_dir;
use std::{fs, fs::OpenOptions, io::Read, io::Write};
use serde_json::value::Map;
pub fn get_home() -> String {
return String::from(home_dir().unwrap().to_str().unwrap());
}
pub fn default_host() -> String {
String::from("https://demenses.net")
}
pub fn default_screen() -> String {
String::new()
}
pub fn default_desc() -> String {
String::from("A raven theme.")
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
pub monitors: i32,
pub polybar: Vec<String>,
pub menu_command: String,
pub last: String,
pub editing: String,
#[serde(default = "default_host")]
pub host: String,
}
impl Config {
pub fn default() -> Config {
Config {
monitors: 1,
polybar: vec!["main".to_string(), "other".to_string()],
menu_command: "rofi -theme sidebar -mesg 'raven:' -p '> ' -dmenu".to_string(),
last: "".to_string(),
editing: "".to_string(),
host: default_host(),
}
}
}
pub fn check_themes() {
let entries = get_themes();
for entry in entries {
if fs::metadata(get_home() + "/.config/raven/themes/" + &entry + "/theme").is_ok() {
convert_theme(entry);
}
}
}
pub fn init() {
if fs::metadata(get_home() + "/.config/raven/config").is_err() {
fs::create_dir(get_home() + "/.config/raven").unwrap();
fs::create_dir(get_home() + "/.config/raven/themes").unwrap();
} else {
println!(
"The config file format has changed. Please check ~/.config/raven/config.json to reconfigure raven."
);
}
let mut file = OpenOptions::new()
.create(true)
.write(true)
.open(get_home() + "/.config/raven/config.json")
.unwrap();
let default = serde_json::to_string(&Config::default()).unwrap();
file.write_all(default.as_bytes()).unwrap();
println!("Correctly initialized base config. Please run again to use raven.");
}
pub fn check_init() -> bool {
fs::metadata(get_home() + "/.config/raven").is_err()
|| fs::metadata(get_home() + "/.config/raven/config.json").is_err()
|| fs::metadata(get_home() + "/.config/raven/themes").is_err()
}
pub fn up_config(conf: Config) {
OpenOptions::new()
.create(true)
.write(true)
.open(get_home() + "/.config/raven/~config.json")
.expect("Couldn't open last theme file")
.write_all(serde_json::to_string(&conf).unwrap().as_bytes())
.expect("Couldn't write to last theme file");
fs::copy(
get_home() + "/.config/raven/~config.json",
get_home() + "/.config/raven/config.json",
)
.unwrap();
fs::remove_file(get_home() + "/.config/raven/~config.json").unwrap();
}
pub fn up_theme(theme: ThemeStore) {
let wthemepath = get_home() + "/.config/raven/themes/" + &theme.name + "/~theme.json";
let themepath = get_home() + "/.config/raven/themes/" + &theme.name + "/theme.json";
OpenOptions::new()
.create(true)
.write(true)
.open(&wthemepath)
.expect("Couldn't open theme file")
.write_all(serde_json::to_string(&theme).unwrap().as_bytes())
.expect("Couldn't write to theme file");
fs::copy(&wthemepath, &themepath).unwrap();
fs::remove_file(&wthemepath).unwrap();
}
pub fn convert_theme<N>(theme_name: N)
where
N: Into<String>,
{
let theme_name = theme_name.into();
let mut theme = String::new();
let otp = get_home() + "/.config/raven/themes/" + &theme_name + "/theme";
fs::File::open(&otp)
.expect("Couldn't read theme")
.read_to_string(&mut theme)
.unwrap();
let options = theme
.split('|')
.map(|x| String::from(String::from(x).trim()))
.filter(|x| x.len() > 0)
.filter(|x| x != "|")
.collect::<Vec<String>>();
fs::remove_file(otp).unwrap();
let themes = ThemeStore {
name: theme_name.clone(),
enabled: Vec::new(),
options: options,
screenshot: default_screen(),
description: default_desc(),
kv: Map::new()
};
OpenOptions::new()
.create(true)
.write(true)
.open(get_home() + "/.config/raven/themes/" + &theme_name + "/theme.json")
.expect("Can't open theme.json")
.write_all(serde_json::to_string(&themes).unwrap().as_bytes())
.unwrap();
}
pub fn load_store<N>(theme: N) -> ThemeStore
where
N: Into<String>,
{
let theme = theme.into();
let mut st = String::new();
fs::File::open(get_home() + "/.config/raven/themes/" + &theme + "/theme.json")
.unwrap()
.read_to_string(&mut st)
.unwrap();
serde_json::from_str(&st).unwrap()
}
pub fn load_theme<N>(theme_name: N) -> Result<Theme, &'static str>
where
N: Into<String>,
{
let theme_name = theme_name.into();
let conf = get_config();
let ent_res = fs::read_dir(get_home() + "/.config/raven/themes/" + &theme_name);
if ent_res.is_ok() {
println!("Found theme {}", theme_name);
if fs::metadata(get_home() + "/.config/raven/themes/" + &theme_name + "/theme.json")
.is_ok()
{
let theme_info = load_store(theme_name.as_str());
let opts: Vec<String> = theme_info.options;
let new_theme = Theme {
name: theme_name,
options: opts,
monitor: conf.monitors,
enabled: theme_info.enabled,
order: conf.polybar,
kv: theme_info.kv,
screenshot: theme_info.screenshot,
description: theme_info.description
};
Ok(new_theme)
} else {
Err("Can't find Theme data")
}
} else {
println!("Theme does not exist.");
Err("Theme does not exist")
}
}
pub fn load_themes() -> Vec<Theme> {
get_themes().iter().map(|x| load_theme(x.as_str())).filter(|x| x.is_ok()).map(|x| x.unwrap()).collect::<Vec<Theme>>()
}
pub fn get_config() -> Config {
let mut conf = String::new();
fs::File::open(get_home() + "/.config/raven/config.json")
.expect("Couldn't read config")
.read_to_string(&mut conf)
.unwrap();
serde_json::from_str(&conf).expect("Couldn't read config file")
}
}
pub mod daemon {
use std::process::Command;
pub fn start_daemon() {
Command::new("sh")
.arg("-c")
.arg("ravend")
.spawn()
.expect("Couldn't start daemon.");
println!("Started cycle daemon.");
}
pub fn stop_daemon() {
Command::new("pkill")
.arg("-SIGKILL")
.arg("ravend")
.output()
.expect("Couldn't stop daemon.");
println!("Stopped cycle daemon.");
}
pub fn check_daemon() -> bool {
let out = Command::new("ps")
.arg("aux")
.output()
.expect("Couldn't find daemon");
let form_out = String::from_utf8_lossy(&out.stdout);
let line_num = form_out.lines().filter(|x| x.contains("ravend")).count();
line_num > 0
}
}
pub fn proc_path(path: DirEntry) -> String {
path.file_name().into_string().unwrap()
}