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
use std::borrow::Borrow;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use crate::constant_strings_paths::{CONFIG_FOLDER, HARDCODED_SHORTCUTS};
use crate::git::git_root;
use crate::impl_selectable_content;
#[derive(Debug, Clone)]
pub struct Shortcut {
pub content: Vec<PathBuf>,
pub index: usize,
non_mount_size: 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);
shortcuts = Self::with_config_folder(shortcuts);
shortcuts = Self::with_git_root(shortcuts);
let non_mount_size = shortcuts.len();
Self {
content: shortcuts,
index: 0,
non_mount_size,
}
}
fn hardcoded_shortcuts() -> Vec<PathBuf> {
HARDCODED_SHORTCUTS
.iter()
.map(|s| PathBuf::from_str(s).unwrap())
.collect()
}
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
}
fn with_config_folder(mut shortcuts: Vec<PathBuf>) -> Vec<PathBuf> {
if let Ok(config_folder) = PathBuf::from_str(shellexpand::tilde(CONFIG_FOLDER).borrow()) {
shortcuts.push(config_folder);
}
shortcuts
}
fn git_root_or_cwd() -> PathBuf {
if let Ok(git_root) = git_root() {
PathBuf::from(git_root)
} else {
std::env::current_dir().unwrap()
}
}
fn with_git_root(mut shortcuts: Vec<PathBuf>) -> Vec<PathBuf> {
shortcuts.push(Self::git_root_or_cwd());
shortcuts
}
pub fn update_git_root(&mut self) {
self.content[self.non_mount_size - 1] = Self::git_root_or_cwd();
}
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(self.non_mount_size);
self.extend_with_mount_points(mount_points)
}
}
impl_selectable_content!(PathBuf, Shortcut);