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
use std::borrow::Borrow;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use crate::constant_strings_paths::HARDCODED_SHORTCUTS;
use crate::impl_selectable_content;
#[derive(Debug, Clone)]
pub struct Shortcut {
pub content: Vec<PathBuf>,
pub index: usize,
}
impl Default for Shortcut {
fn default() -> Self {
Self::new()
}
}
impl Shortcut {
pub fn new() -> Self {
let mut shortcuts = Self::hardcoded_shortcuts();
shortcuts = Self::with_home_path(shortcuts);
Self {
content: shortcuts,
index: 0,
}
}
fn hardcoded_shortcuts() -> Vec<PathBuf> {
HARDCODED_SHORTCUTS
.iter()
.map(|s| PathBuf::from_str(s).unwrap())
.collect()
}
pub fn with_home_path(mut shortcuts: Vec<PathBuf>) -> Vec<PathBuf> {
if let Ok(home_path) = PathBuf::from_str(shellexpand::tilde("~").borrow()) {
shortcuts.push(home_path);
}
shortcuts
}
pub fn extend_with_mount_points(&mut self, mount_points: &[&Path]) {
self.content
.extend(mount_points.iter().map(|p| p.to_path_buf()));
}
pub fn refresh(&mut self, mount_points: &[&Path]) {
self.content.truncate(HARDCODED_SHORTCUTS.len() + 1);
self.extend_with_mount_points(mount_points)
}
}
impl_selectable_content!(PathBuf, Shortcut);