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
|
use std::{fs, path};
use crate::fs::{JoshutoDirEntry, JoshutoMetadata};
use crate::util::sort::SortOption;
#[derive(Debug)]
pub struct JoshutoDirList {
pub index: Option<usize>,
path: path::PathBuf,
content_outdated: bool,
order_outdated: bool,
pub metadata: JoshutoMetadata,
pub contents: Vec<JoshutoDirEntry>,
}
impl JoshutoDirList {
pub fn new(path: path::PathBuf, sort_option: &SortOption) -> std::io::Result<Self> {
let filter_func = sort_option.filter_func();
let mut contents = read_dir_list(path.as_path(), filter_func)?;
let compare_func = sort_option.compare_func();
contents.sort_by(compare_func);
let index = if contents.is_empty() { None } else { Some(0) };
let metadata = JoshutoMetadata::from(&path)?;
Ok(Self {
index,
path,
content_outdated: false,
order_outdated: false,
metadata,
contents,
})
}
pub fn depreciate(&mut self) {
self.content_outdated = true;
}
pub fn need_update(&self) -> bool {
self.content_outdated
}
pub fn file_path(&self) -> &path::PathBuf {
&self.path
}
pub fn reload_contents(&mut self, sort_option: &SortOption) -> std::io::Result<()> {
let filter_func = sort_option.filter_func();
let sort_func = sort_option.compare_func();
let mut contents = read_dir_list(&self.path, filter_func)?;
contents.sort_by(sort_func);
let contents_len = contents.len();
let index: Option<usize> = if contents_len == 0 {
None
} else {
match self.index {
Some(i) if i >= contents_len => Some(contents_len - 1),
Some(i) => {
let entry = &self.contents[i];
contents
.iter()
.enumerate()
.find(|(_, e)| e.file_name() == entry.file_name())
.map(|(i, _)| i)
.or(Some(i))
}
None => Some(0),
}
};
let metadata = JoshutoMetadata::from(&self.path)?;
self.metadata = metadata;
self.contents = contents;
self.index = index;
self.content_outdated = false;
Ok(())
}
pub fn selected_entries(&self) -> impl Iterator<Item = &JoshutoDirEntry> {
self.contents.iter().filter(|entry| entry.is_selected())
}
pub fn get_selected_paths(&self) -> Vec<&path::PathBuf> {
let vec: Vec<&path::PathBuf> = self.selected_entries().map(|e| e.file_path()).collect();
if !vec.is_empty() {
vec
} else {
match self.get_curr_ref() {
Some(s) => vec![s.file_path()],
_ => vec![],
}
}
}
pub fn get_curr_ref(&self) -> Option<&JoshutoDirEntry> {
self.get_curr_ref_(self.index?)
}
pub fn get_curr_mut(&mut self) -> Option<&mut JoshutoDirEntry> {
self.get_curr_mut_(self.index?)
}
fn get_curr_mut_(&mut self, index: usize) -> Option<&mut JoshutoDirEntry> {
if index < self.contents.len() {
Some(&mut self.contents[index])
} else {
None
}
}
fn get_curr_ref_(&self, index: usize) -> Option<&JoshutoDirEntry> {
if index < self.contents.len() {
Some(&self.contents[index])
} else {
None
}
}
}
fn read_dir_list<F>(path: &path::Path, filter_func: F) -> std::io::Result<Vec<JoshutoDirEntry>>
where
F: Fn(&Result<fs::DirEntry, std::io::Error>) -> bool,
{
let results: Vec<JoshutoDirEntry> = fs::read_dir(path)?
.filter(filter_func)
.filter_map(|res| JoshutoDirEntry::from(&res.ok()?).ok())
.collect();
Ok(results)
}
|