| 1 |
use std::{io::Result, path::PathBuf, sync::Arc}; |
| 2 |
|
| 3 |
use super::{File, FileExplorer, Filter}; |
| 4 |
use crate::Theme; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#[derive(Clone, Default, educe::Educe)] |
| 10 |
#[educe(Debug, PartialEq, Eq, Hash)] |
| 11 |
pub struct FileExplorerBuilder { |
| 12 |
cwd: Option<PathBuf>, |
| 13 |
theme: Option<Theme>, |
| 14 |
show_hidden: bool, |
| 15 |
#[educe(Debug(ignore), PartialEq(ignore), Hash(ignore))] |
| 16 |
filter: Option<Arc<Filter>>, |
| 17 |
custom_selected: bool, |
| 18 |
} |
| 19 |
|
| 20 |
impl FileExplorerBuilder { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
─ .git |
| 29 |
uments |
| 30 |
rt.png |
| 31 |
me.pdf |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
pub fn working_dir<P: Into<PathBuf>>(mut self, working_dir: P) -> Self { |
| 44 |
self.cwd = Some(working_dir.into()); |
| 45 |
self |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
─ .git |
| 58 |
uments |
| 59 |
rt.png |
| 60 |
me.pdf |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
pub fn working_file<P: Into<PathBuf>>(mut self, working_file: P) -> Self { |
| 74 |
self.custom_selected = true; |
| 75 |
self.working_dir(working_file) |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
pub fn show_hidden(mut self, show: bool) -> Self { |
| 80 |
self.show_hidden = show; |
| 81 |
self |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
pub fn filter_map(mut self, f: impl Fn(File) -> Option<File> + Send + Sync + 'static) -> Self { |
| 101 |
self.filter = Some(Arc::new(f)); |
| 102 |
self |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
pub fn theme(mut self, theme: Theme) -> Self { |
| 118 |
self.theme = Some(theme); |
| 119 |
self |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
#[allow(clippy::unwrap_or_default)] |
| 132 |
pub fn build(self) -> Result<FileExplorer> { |
| 133 |
let show_hidden = self.show_hidden; |
| 134 |
let theme = self.theme.unwrap_or_else(Theme::new); |
| 135 |
let filter = self.filter; |
| 136 |
|
| 137 |
let mut file_explorer = FileExplorer { |
| 138 |
cwd: PathBuf::new(), |
| 139 |
files: Vec::new(), |
| 140 |
show_hidden, |
| 141 |
selected: 0, |
| 142 |
theme, |
| 143 |
filter, |
| 144 |
}; |
| 145 |
|
| 146 |
if self.custom_selected { |
| 147 |
file_explorer.set_working_file(self.cwd.unwrap())?; |
| 148 |
} else { |
| 149 |
file_explorer.set_cwd(self.cwd.clone().unwrap_or(std::env::current_dir()?))?; |
| 150 |
} |
| 151 |
|
| 152 |
Ok(file_explorer) |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
pub fn build_with_theme(theme: Theme) -> Result<FileExplorer> { |
| 158 |
Self::default().theme(theme).build() |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
pub fn build_with_working_dir<P: Into<PathBuf>>(working_dir: P) -> Result<FileExplorer> { |
| 164 |
Self::default().working_dir(working_dir).build() |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
pub fn build_with_working_file<P: Into<PathBuf>>(working_dir: P) -> Result<FileExplorer> { |
| 170 |
Self::default().working_file(working_dir).build() |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
#[cfg(test)] |
| 175 |
mod tests { |
| 176 |
use super::*; |
| 177 |
|
| 178 |
use std::fs::{self, File}; |
| 179 |
use tempfile::TempDir; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
ot |
| 185 |
─ .git |
| 186 |
uments |
| 187 |
rt.png |
| 188 |
me.pdf |
| 189 |
|
| 190 |
fn build_tmp_file_system() -> Result<TempDir> { |
| 191 |
let root = TempDir::new()?; |
| 192 |
|
| 193 |
let git_path = root.path().join(".git"); |
| 194 |
let documents_path = root.path().join("Documents"); |
| 195 |
let passport_path = root.path().join("Documents/passport.png"); |
| 196 |
let resume_path = root.path().join("Documents/resume.pdf"); |
| 197 |
|
| 198 |
fs::create_dir(git_path)?; |
| 199 |
fs::create_dir(documents_path)?; |
| 200 |
File::create(passport_path)?; |
| 201 |
File::create(resume_path)?; |
| 202 |
|
| 203 |
Ok(root) |
| 204 |
} |
| 205 |
|
| 206 |
#[test] |
| 207 |
fn test_thread_safe() { |
| 208 |
fn is_sync<T: Sync>() {} |
| 209 |
|
| 210 |
fn is_send<T: Send>() {} |
| 211 |
|
| 212 |
is_send::<FileExplorerBuilder>(); |
| 213 |
is_sync::<FileExplorerBuilder>(); |
| 214 |
} |
| 215 |
|
| 216 |
#[test] |
| 217 |
fn test_working_file_correcty_set_selected_file() -> Result<()> { |
| 218 |
let root = build_tmp_file_system()?; |
| 219 |
let documents_path = root.path().join("Documents"); |
| 220 |
let passport_path = documents_path.join("passport.png"); |
| 221 |
|
| 222 |
let file_explorer = FileExplorerBuilder::default() |
| 223 |
.working_file(&passport_path) |
| 224 |
.build() |
| 225 |
.unwrap(); |
| 226 |
|
| 227 |
assert_eq!(*file_explorer.cwd(), documents_path); |
| 228 |
assert_eq!(file_explorer.current().path, passport_path); |
| 229 |
|
| 230 |
Ok(()) |
| 231 |
} |
| 232 |
|
| 233 |
#[test] |
| 234 |
fn test_working_file_correcty_set_selected_dir() -> Result<()> { |
| 235 |
let root = build_tmp_file_system()?; |
| 236 |
let documents_path = root.path().join("Documents"); |
| 237 |
|
| 238 |
let file_explorer = FileExplorerBuilder::default() |
| 239 |
.show_hidden(true) |
| 240 |
.working_file(&documents_path) |
| 241 |
.build() |
| 242 |
.unwrap(); |
| 243 |
|
| 244 |
assert_eq!(*file_explorer.cwd(), root.path()); |
| 245 |
assert_eq!(*file_explorer.current().path, documents_path); |
| 246 |
|
| 247 |
Ok(()) |
| 248 |
} |
| 249 |
} |