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
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
extern crate dirs;
extern crate multipart;
extern crate reqwest;
extern crate tar;
#[macro_use]
extern crate error_chain;
pub mod ravenserver;
pub mod error;
use std::fs::DirEntry;
pub mod themes;
pub mod config {
use crate::themes::*;
use dirs::home_dir;
use serde_json::value::Map;
use std::{fs, fs::OpenOptions, io::Read, io::Write};
use error::*;
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() -> Result<()> {
let entries = get_themes()?;
for entry in entries {
if fs::metadata(get_home() + "/.config/raven/themes/" + &entry + "/theme").is_ok() {
convert_theme(entry)?;
}
}
Ok(())
}
pub fn init() -> Result<()> {
if fs::metadata(get_home() + "/.config/raven/config").is_err() {
fs::create_dir(get_home() + "/.config/raven")?;
fs::create_dir(get_home() + "/.config/raven/themes")?;
} 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")?;
let default = serde_json::to_string(&Config::default())?;
file.write_all(default.as_bytes())?;
println!("Correctly initialized base config and directory structure.");
Ok(())
}
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) -> Result<Config>{
OpenOptions::new()
.create(true)
.write(true)
.open(get_home() + "/.config/raven/~config.json")?
.write_all(serde_json::to_string(&conf)?.as_bytes())?;
fs::copy(
get_home() + "/.config/raven/~config.json",
get_home() + "/.config/raven/config.json",
)?;
fs::remove_file(get_home() + "/.config/raven/~config.json")?;
Ok(conf)
}
pub fn up_theme(theme: ThemeStore) -> Result<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)?
.write_all(serde_json::to_string(&theme)?.as_bytes())?;
fs::copy(&wthemepath, &themepath)?;
fs::remove_file(&wthemepath)?;
Ok(theme)
}
pub fn convert_theme<N>(theme_name: N) -> Result<ThemeStore>
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)?;
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)?;
let themes = ThemeStore {
name: theme_name.clone(),
enabled: Vec::new(),
options: options.iter().map(|x| x.to_string()).collect(),
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")?
.write_all(serde_json::to_string(&themes).unwrap().as_bytes())?;
Ok(themes)
}
pub fn load_store<N>(theme: N) -> Result<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")?
.read_to_string(&mut st)?;
Ok(serde_json::from_str(&st)?)
}
pub fn get_config() -> Result<Config> {
let mut conf = String::new();
fs::File::open(get_home() + "/.config/raven/config.json")?
.read_to_string(&mut conf)?;
Ok(serde_json::from_str(&conf)?)
}
}
pub mod daemon {
use error::*;
use std::process::Command;
use std::process::Child;
pub fn start_daemon() -> Result<Child> {
let child = Command::new("sh")
.arg("-c")
.arg("ravend")
.spawn()?;
println!("Started cycle daemon.");
Ok(child)
}
pub fn stop_daemon() -> Result<()> {
Command::new("pkill")
.arg("-SIGKILL")
.arg("ravend")
.output()?;
println!("Stopped cycle daemon.");
Ok(())
}
pub fn check_daemon() -> Result<bool> {
let out = Command::new("ps")
.arg("aux")
.output()?;
let form_out = String::from_utf8_lossy(&out.stdout);
let line_num = form_out.lines().filter(|x| x.contains("ravend")).count();
Ok(line_num > 0)
}
}
pub fn proc_path(path: DirEntry) -> String {
path.file_name().into_string().unwrap()
}