forked from reujab/silver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir.rs
72 lines (66 loc) · 1.95 KB
/
dir.rs
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
use crate::{icons, Segment, CONFIG};
use std::{env, path::Path};
pub fn segment(segment: &mut Segment, _: &[&str]) {
let mut wd = match env::current_dir() {
Ok(wd) => wd,
Err(_) => return,
};
// processes aliases
let mut aliases = CONFIG
.dir
.aliases
.iter()
.flat_map(|(i, j)| {
if let Ok(p) = Path::new(
&shellexpand::full_with_context_no_errors(j, dirs::home_dir, |s| {
env::var(s).map(Some).unwrap_or_default()
})
.into_owned(),
)
.canonicalize()
{
Some((p, Path::new(i).to_owned()))
} else {
None
}
})
.collect::<Vec<_>>();
// default home alias
if let Some(home) = dirs::home_dir() {
aliases.push((home, Path::new(&icons::get("home")).to_owned()))
}
// sorts from deepest alias to shallowest
aliases.sort_by(|a, b| {
a.0.iter()
.count()
.partial_cmp(&b.0.iter().count())
.unwrap()
.reverse()
});
for (dir, alias) in aliases {
wd = match wd.strip_prefix(dir) {
Ok(stripped) => alias.join(stripped.to_path_buf()),
Err(_) => wd.clone(),
}
}
// processes length
if let Some(len) = CONFIG.dir.length {
let iter_len = wd.iter().count();
let mut i = 0;
wd = wd
.iter()
.map(|component| {
i += 1;
if i != iter_len && component.len() > len {
component.to_str().unwrap().chars().take(len).collect()
} else {
component.to_str().unwrap().to_owned()
}
})
.collect();
}
segment.value = wd.to_str().unwrap().to_owned();
if segment.value != "/" && segment.value.ends_with('/') {
segment.value.pop();
}
}