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
|
use std::path;
use crate::config::option::{DisplayOption, TabDisplayOption};
use crate::context::UiContext;
use crate::fs::JoshutoDirList;
use crate::history::{DirectoryHistory, JoshutoHistory};
#[derive(Clone, Copy, Debug)]
pub enum TabHomePage {
Inherit,
Home,
Root,
}
pub struct JoshutoTab {
history: JoshutoHistory,
_cwd: path::PathBuf,
// history is just a HashMap, so we have this property to store last workdir
_previous_dir: Option<path::PathBuf>,
options: TabDisplayOption,
}
impl JoshutoTab {
pub fn new(
cwd: path::PathBuf,
ui_context: &UiContext,
options: &DisplayOption,
) -> std::io::Result<Self> {
let mut history = JoshutoHistory::new();
let tab_options = options.default_tab_display_option.clone();
history.populate_to_root(cwd.as_path(), ui_context, options, &tab_options)?;
let new_tab = Self {
history,
_cwd: cwd,
_previous_dir: None,
options: tab_options,
};
Ok(new_tab)
}
pub fn option_ref(&self) -> &TabDisplayOption {
&self.options
}
pub fn option_mut(&mut self) -> &mut TabDisplayOption {
&mut self.options
}
pub fn cwd(&self) -> &path::Path {
self._cwd.as_path()
}
pub fn set_cwd(&mut self, cwd: &path::Path) {
self._previous_dir = Some(self._cwd.to_path_buf());
self._cwd = cwd.to_path_buf();
}
pub fn previous_dir(&self) -> Option<&path::Path> {
// This converts PathBuf to Path
match &self._previous_dir {
Some(path) => Some(path),
None => None,
}
}
pub fn history_ref(&self) -> &JoshutoHistory {
&self.history
}
pub fn history_mut(&mut self) -> &mut JoshutoHistory {
&mut self.history
}
pub fn curr_list_ref(&self) -> Option<&JoshutoDirList> {
self.history.get(self.cwd())
}
pub fn parent_list_ref(&self) -> Option<&JoshutoDirList> {
let parent = self.cwd().parent()?;
self.history.get(parent)
}
pub fn child_list_ref(&self) -> Option<&JoshutoDirList> {
let curr_list = self.curr_list_ref()?;
let index = curr_list.get_index()?;
let path = curr_list.contents[index].file_path();
self.history.get(path)
}
pub fn curr_list_mut(&mut self) -> Option<&mut JoshutoDirList> {
self.history.get_mut(self._cwd.as_path())
}
pub fn parent_list_mut(&mut self) -> Option<&mut JoshutoDirList> {
let parent = self._cwd.parent()?;
self.history.get_mut(parent)
}
#[allow(dead_code)]
pub fn child_list_mut(&mut self) -> Option<&mut JoshutoDirList> {
let child_path = {
let curr_list = self.curr_list_ref()?;
let index = curr_list.get_index()?;
curr_list.contents[index].file_path().to_path_buf()
};
self.history.get_mut(child_path.as_path())
}
}
|