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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
use std::collections::HashMap;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use crate::config::option::{DisplayOption, TabDisplayOption};
use crate::context::UiContext;
use crate::fs::{JoshutoDirEntry, JoshutoDirList, JoshutoMetadata};
pub trait DirectoryHistory {
fn populate_to_root(
&mut self,
path: &Path,
ui_context: &UiContext,
options: &DisplayOption,
tab_options: &TabDisplayOption,
) -> io::Result<()>;
fn create_or_soft_update(
&mut self,
path: &Path,
options: &DisplayOption,
tab_options: &TabDisplayOption,
) -> io::Result<()>;
fn create_or_reload(
&mut self,
path: &Path,
options: &DisplayOption,
tab_options: &TabDisplayOption,
) -> io::Result<()>;
fn reload(
&mut self,
path: &Path,
options: &DisplayOption,
tab_options: &TabDisplayOption,
) -> io::Result<()>;
fn depreciate_all_entries(&mut self);
fn depreciate_entry(&mut self, path: &Path);
}
pub type JoshutoHistory = HashMap<PathBuf, JoshutoDirList>;
impl DirectoryHistory for JoshutoHistory {
fn populate_to_root(
&mut self,
path: &Path,
ui_context: &UiContext,
options: &DisplayOption,
tab_options: &TabDisplayOption,
) -> io::Result<()> {
let mut dirlists = Vec::new();
let mut prev: Option<&Path> = None;
for curr in path.ancestors() {
if self.contains_key(curr) {
let mut new_dirlist =
create_dirlist_with_history(self, curr, options, tab_options)?;
if let Some(ancestor) = prev.as_ref() {
if let Some(i) = get_index_of_value(&new_dirlist.contents, ancestor) {
new_dirlist.set_index(Some(i), ui_context, options);
}
}
dirlists.push(new_dirlist);
} else {
let mut new_dirlist =
JoshutoDirList::from_path(curr.to_path_buf().clone(), options, tab_options)?;
if let Some(ancestor) = prev.as_ref() {
if let Some(i) = get_index_of_value(&new_dirlist.contents, ancestor) {
new_dirlist.set_index(Some(i), ui_context, options);
}
}
dirlists.push(new_dirlist);
}
prev = Some(curr);
}
for dirlist in dirlists {
self.insert(dirlist.file_path().to_path_buf(), dirlist);
}
Ok(())
}
fn create_or_soft_update(
&mut self,
path: &Path,
options: &DisplayOption,
tab_options: &TabDisplayOption,
) -> io::Result<()> {
let (contains_key, need_update) = if let Some(dirlist) = self.get(path) {
(true, dirlist.need_update())
} else {
(false, true)
};
if need_update {
let dirlist = if contains_key {
create_dirlist_with_history(self, path, options, tab_options)?
} else {
JoshutoDirList::from_path(path.to_path_buf(), options, tab_options)?
};
self.insert(path.to_path_buf(), dirlist);
}
Ok(())
}
fn create_or_reload(
&mut self,
path: &Path,
options: &DisplayOption,
tab_options: &TabDisplayOption,
) -> io::Result<()> {
let dirlist = if self.contains_key(path) {
create_dirlist_with_history(self, path, options, tab_options)?
} else {
JoshutoDirList::from_path(path.to_path_buf(), options, tab_options)?
};
self.insert(path.to_path_buf(), dirlist);
Ok(())
}
fn reload(
&mut self,
path: &Path,
options: &DisplayOption,
tab_options: &TabDisplayOption,
) -> io::Result<()> {
let dirlist = create_dirlist_with_history(self, path, options, tab_options)?;
self.insert(path.to_path_buf(), dirlist);
Ok(())
}
fn depreciate_all_entries(&mut self) {
self.iter_mut().for_each(|(_, v)| v.depreciate());
}
fn depreciate_entry(&mut self, path: &Path) {
if let Some(v) = self.get_mut(path) {
v.depreciate();
}
}
}
fn get_index_of_value(arr: &[JoshutoDirEntry], val: &Path) -> Option<usize> {
arr.iter().enumerate().find_map(|(i, dir)| {
if dir.file_path() == val {
Some(i)
} else {
None
}
})
}
pub fn create_dirlist_with_history(
history: &JoshutoHistory,
path: &Path,
options: &DisplayOption,
tab_options: &TabDisplayOption,
) -> io::Result<JoshutoDirList> {
let filter_func = options.filter_func();
let mut contents = read_directory(path, filter_func, options)?;
// re-use directory size information on reload
for entry in contents.iter_mut() {
if entry.metadata.is_dir() {
if let Some(lst) = history.get(entry.file_path()) {
entry.metadata.update_directory_size(lst.len());
}
}
}
// preserve selection status of entries on reload
if let Some(former_dir_list) = history.get(path) {
let former_entries_by_file_name = HashMap::<&str, &JoshutoDirEntry>::from_iter(
former_dir_list.contents.iter().map(|e| (e.file_name(), e)),
);
for entry in contents.iter_mut() {
if let Some(former_entry) = former_entries_by_file_name.get(entry.file_name()) {
entry.set_permanent_selected(former_entry.is_permanent_selected());
entry.set_visual_mode_selected(former_entry.is_visual_mode_selected());
}
}
}
let sort_options = tab_options.sort_options_ref();
contents.sort_by(|f1, f2| sort_options.compare(f1, f2));
let contents_len = contents.len();
let index = if contents_len == 0 {
None
} else {
match history.get(path) {
Some(dirlist) => match dirlist.get_index() {
Some(i) if i >= contents_len => Some(contents_len - 1),
Some(i) => {
let entry = &dirlist.contents[i];
contents
.iter()
.enumerate()
.find(|(_, e)| e.file_name() == entry.file_name())
.map(|(i, _)| i)
.or(Some(i))
}
None => Some(0),
},
None => Some(0),
}
};
let viewport_index: usize = if contents_len == 0 {
0
} else {
match history.get(path) {
Some(dirlist) => match dirlist.first_index_for_viewport() {
i if i >= contents_len => contents_len - 1,
i => i,
},
None => 0,
}
};
let visual_mode_anchor_index = match history.get(path) {
None => None,
Some(dirlist) => {
dirlist
.get_visual_mode_anchor_index()
.map(|old_visual_mode_anchor_index| {
if old_visual_mode_anchor_index < contents_len {
old_visual_mode_anchor_index
} else {
contents_len - 1
}
})
}
};
let metadata = JoshutoMetadata::from(path)?;
let dirlist = JoshutoD
|