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
use std::env::var;
use std::path::PathBuf;
use std::fs::File;
use std::io::Read;
use regex::Regex;
pub mod parser;
pub fn theme_search_paths() -> Vec<PathBuf> {
let mut res: Vec<PathBuf> = Vec::new();
res.push([var("HOME").unwrap(), String::from(".icons")].iter().collect());
for i in var("XDG_DATA_DIRS").unwrap_or("/usr/local/share/:/usr/share/".to_string()).split(':') {
res.push([i, "icons"].iter().collect());
}
res.push(PathBuf::from("/usr/share/pixmaps"));
res
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct CursorTheme {
name: String,
dirs: Vec<PathBuf>,
inherits: String,
search_paths: Vec<PathBuf>,
}
impl CursorTheme {
pub fn load(name: &str, search_paths: &Vec<PathBuf>) -> Self {
let mut dirs = Vec::new();
let mut inherits = String::from("default");
for mut path in search_paths.clone() {
path.push(name);
if path.is_dir() {
dirs.push(path.clone());
}
}
for mut path in dirs.clone() {
path.push("index.theme");
if let Some(i) = theme_inherits(&path) {
inherits = i;
}
}
CursorTheme {
name: String::from(name),
dirs,
inherits,
search_paths: search_paths.clone(),
}
}
pub fn load_icon(&self, icon_name: &str) -> Option<PathBuf> {
for mut icon_path in self.dirs.clone() {
icon_path.push("cursors");
icon_path.push(icon_name);
if icon_path.is_file() {
return Some(icon_path);
}
}
if self.name == self.inherits {
return None;
}
CursorTheme::load(&self.inherits, &self.search_paths).load_icon(icon_name)
}
}
pub fn theme_inherits(file_path: &PathBuf) -> Option<String> {
let mut content = String::new();
let mut file = File::open(file_path).ok()?;
file.read_to_string(&mut content).ok()?;
let re = Regex::new(r"Inherits\s*=\s*([a-zA-Z0-9-_]+)").unwrap();
let matches = re.captures_iter(&content).nth(0)?;
Some(String::from(&matches[1]))
}