This commit is contained in:
sxyazi 2023-08-28 13:35:09 +08:00
parent 1af7d728d6
commit 0feb025dcf
No known key found for this signature in database
10 changed files with 43 additions and 45 deletions

View file

@ -36,7 +36,7 @@ impl Ctx {
}
Position::Hovered(rect @ Rect { mut x, y, width, height }) => {
let Some(r) =
self.manager.hovered().and_then(|h| self.manager.current().rect_current(&h.path))
self.manager.hovered().and_then(|h| self.manager.current().rect_current(h.path()))
else {
return self.area(&Position::Top(*rect));
};

View file

@ -36,7 +36,7 @@ impl<'a> Folder<'a> {
THEME
.filetypes
.iter()
.find(|x| x.matches(&file.path, mimetype.get(&file.path).cloned(), file.meta.is_dir()))
.find(|x| x.matches(file.path(), mimetype.get(file.path()), file.meta.is_dir()))
.map(|x| x.style.get())
.unwrap_or_else(Style::new)
}
@ -60,11 +60,11 @@ impl<'a> Widget for Folder<'a> {
let icon = THEME
.icons
.iter()
.find(|x| x.name.match_path(&f.path, Some(f.meta.is_dir())))
.find(|x| x.name.match_path(f.path(), Some(f.meta.is_dir())))
.map(|x| x.display.as_ref())
.unwrap_or("");
let is_selected = self.folder.files.is_selected(&f.path);
let is_selected = self.folder.files.is_selected(f.path());
if (!self.is_selection && is_selected)
|| (self.is_selection && mode.pending(i, is_selected))
{
@ -78,7 +78,7 @@ impl<'a> Widget for Folder<'a> {
);
}
let hovered = matches!(self.folder.hovered, Some(ref h) if h.path == f.path);
let hovered = matches!(self.folder.hovered, Some(ref h) if h.path() == f.path());
let style = if self.is_preview && hovered {
THEME.preview.hovered.get()
} else if hovered {
@ -87,7 +87,7 @@ impl<'a> Widget for Folder<'a> {
self.file_style(f)
};
let mut path = format!(" {icon} {}", readable_path(&f.path, &self.folder.cwd));
let mut path = format!(" {icon} {}", readable_path(f.path(), &self.folder.cwd));
if let Some(ref link_to) = f.link_to {
if MANAGER.show_symlink {
path.push_str(&format!(" -> {}", link_to.display()));

View file

@ -17,7 +17,7 @@ impl<'a> Preview<'a> {
impl<'a> Widget for Preview<'a> {
fn render(self, area: Rect, buf: &mut Buffer) {
let manager = &self.cx.manager;
let Some(hovered) = manager.hovered().map(|h| &h.path) else {
let Some(hovered) = manager.hovered().map(|h| h.path()) else {
return;
};

View file

@ -12,12 +12,12 @@ pub struct Filetype {
}
impl Filetype {
pub fn matches(&self, path: &Path, mime: Option<String>, is_dir: bool) -> bool {
pub fn matches(&self, path: &Path, mime: Option<impl AsRef<str>>, is_dir: bool) -> bool {
if self.name.as_ref().map_or(false, |e| e.match_path(path, Some(is_dir))) {
return true;
}
if let Some(mime) = mime {
return self.mime.as_ref().map_or(false, |m| m.matches(&mime));
return self.mime.as_ref().map_or(false, |m| m.matches(mime));
}
false
}

View file

@ -5,12 +5,12 @@ use tokio::fs;
#[derive(Clone, Debug)]
pub struct File {
pub path: PathBuf,
pub meta: Metadata,
pub length: Option<u64>,
pub link_to: Option<PathBuf>,
pub is_link: bool,
pub is_hidden: bool,
pub(super) path: PathBuf,
pub meta: Metadata,
pub length: Option<u64>,
pub link_to: Option<PathBuf>,
pub is_link: bool,
pub is_hidden: bool,
}
impl File {
@ -40,10 +40,7 @@ impl File {
pub fn path(&self) -> &PathBuf { &self.path }
#[inline]
pub fn set_path(mut self, path: PathBuf) -> Self {
self.path = path;
self
}
pub fn set_path(&mut self, path: PathBuf) { self.path = path; }
#[inline]
pub fn name(&self) -> Option<Cow<str>> { self.path.file_name().map(|s| s.to_string_lossy()) }

View file

@ -118,7 +118,7 @@ impl Folder {
}
pub fn hover_force(&mut self, file: File) -> bool {
if self.hover(&file.path) {
if self.hover(file.path()) {
return true;
}

View file

@ -63,11 +63,11 @@ impl Manager {
let mime = if hovered.meta.is_dir() {
MIME_DIR.to_owned()
} else if let Some(m) = self.mimetype.get(&hovered.path).cloned() {
} else if let Some(m) = self.mimetype.get(hovered.path()).cloned() {
m
} else {
tokio::spawn(async move {
if let Ok(mimes) = external::file(&[hovered.path]).await {
if let Ok(mimes) = external::file(&[hovered.path()]).await {
emit!(Mimetype(mimes));
}
});
@ -75,9 +75,9 @@ impl Manager {
};
if sequent {
self.active_mut().preview.sequent(&hovered.path, &mime, show_image);
self.active_mut().preview.sequent(hovered.path(), &mime, show_image);
} else {
self.active_mut().preview.go(&hovered.path, &mime, show_image);
self.active_mut().preview.go(hovered.path(), &mime, show_image);
}
false
}
@ -117,7 +117,7 @@ impl Manager {
}
pub fn open(&mut self, interactive: bool) -> bool {
let mut files = self
let mut files: Vec<_> = self
.selected()
.into_iter()
.map(|f| {
@ -126,11 +126,11 @@ impl Manager {
if f.meta.is_dir() {
Some(MIME_DIR.to_owned())
} else {
self.mimetype.get(&f.path).cloned()
self.mimetype.get(f.path()).cloned()
},
)
})
.collect::<Vec<_>>();
.collect();
if files.is_empty() {
return false;
@ -339,7 +339,7 @@ impl Manager {
.or_insert_with(|| Folder::new(&path))
.update(op);
matches!(self.hovered(), Some(h) if h.path == path)
matches!(self.hovered(), Some(h) if h.path() == &path)
};
b |= self.active_mut().parent.as_mut().map_or(false, |p| p.hover(&cwd));
@ -402,7 +402,7 @@ impl Manager {
};
if hovered.meta.is_dir() {
self.watcher.trigger_dirs(&[&hovered.path]);
self.watcher.trigger_dirs(&[hovered.path()]);
}
b
}

View file

@ -131,7 +131,7 @@ impl Tab {
return false;
}
let rep = self.history_new(&hovered.path);
let rep = self.history_new(hovered.path());
let rep = mem::replace(&mut self.current, rep);
if !rep.in_search {
self.history.insert(rep.cwd.clone(), rep);
@ -140,7 +140,7 @@ impl Tab {
if let Some(rep) = self.parent.take() {
self.history.insert(rep.cwd.clone(), rep);
}
self.parent = Some(self.history_new(hovered.path.parent().unwrap()));
self.parent = Some(self.history_new(hovered.path().parent().unwrap()));
emit!(Refresh);
true
@ -151,7 +151,7 @@ impl Tab {
.current
.hovered
.as_ref()
.and_then(|h| h.path.parent())
.and_then(|h| h.path().parent())
.and_then(|p| if p == self.current.cwd { None } else { Some(p) })
.or_else(|| self.current.cwd.parent());
@ -184,7 +184,7 @@ impl Tab {
pub fn select(&mut self, state: Option<bool>) -> bool {
if let Some(ref hovered) = self.current.hovered {
return self.current.files.select(&hovered.path, state);
return self.current.files.select(hovered.path(), state);
}
false
}
@ -209,10 +209,10 @@ impl Tab {
let mut it = self.selected().into_iter().peekable();
while let Some(f) = it.next() {
s.push(match type_ {
"path" => f.path.as_os_str(),
"dirname" => f.path.parent().map_or(OsStr::new(""), |p| p.as_os_str()),
"filename" => f.path.file_name().unwrap_or(OsStr::new("")),
"name_without_ext" => f.path.file_stem().unwrap_or(OsStr::new("")),
"path" => f.path().as_os_str(),
"dirname" => f.path().parent().map_or(OsStr::new(""), |p| p.as_os_str()),
"filename" => f.path().file_name().unwrap_or(OsStr::new("")),
"name_without_ext" => f.path().file_stem().unwrap_or(OsStr::new("")),
_ => return false,
});
if it.peek().is_some() {
@ -288,7 +288,7 @@ impl Tab {
let selected: Vec<_> = self
.selected()
.into_iter()
.map(|f| (f.path.as_os_str().to_owned(), Default::default()))
.map(|f| (f.path().as_os_str().to_owned(), Default::default()))
.collect();
let mut exec = exec.to_owned();
@ -315,7 +315,7 @@ impl Tab {
return;
};
if path.as_ref().map(|p| *p != hovered.path).unwrap_or(false) {
if path.as_ref().map(|p| p != hovered.path()).unwrap_or(false) {
return;
} else if !self.preview.arrow(step, path.is_some()) {
return;
@ -333,7 +333,7 @@ impl Tab {
}
pub fn update_preview(&mut self, lock: PreviewLock) -> bool {
let Some(hovered) = self.current.hovered.as_ref().map(|h| &h.path) else {
let Some(hovered) = self.current.hovered.as_ref().map(|h| h.path()) else {
return self.preview.reset();
};

View file

@ -183,7 +183,8 @@ impl Watcher {
Ok(items) => {
let mut files = Vec::with_capacity(items.len());
for item in items {
let file = item.clone().set_path(ori.join(item.path.strip_prefix(path).unwrap()));
let mut file = item.clone();
file.set_path(ori.join(item.path().strip_prefix(path).unwrap()));
files.push(file);
}
FilesOp::Read(ori, files)

View file

@ -228,11 +228,11 @@ impl Tasks {
#[inline]
pub fn precache_mime(&self, targets: &[File], mimetype: &HashMap<PathBuf, String>) -> bool {
let targets = targets
let targets: Vec<_> = targets
.iter()
.filter(|f| f.meta.is_file() && !mimetype.contains_key(&f.path))
.map(|f| f.path.clone())
.collect::<Vec<_>>();
.filter(|f| f.meta.is_file() && !mimetype.contains_key(f.path()))
.map(|f| f.path().clone())
.collect();
if !targets.is_empty() {
self.scheduler.precache_mime(targets);