From 4b39a966dab2c612c8eef085097443fdc1746091 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Tue, 5 Sep 2023 18:30:37 +0800 Subject: [PATCH] .. --- app/src/app.rs | 6 +++--- app/src/executor.rs | 6 +++--- core/src/files/files.rs | 16 ++++++++-------- core/src/files/sorter.rs | 2 +- core/src/manager/manager.rs | 2 +- core/src/manager/tab.rs | 16 +++++++++------- core/src/manager/watcher.rs | 16 ++++++++-------- core/src/tasks/tasks.rs | 18 +++++++++--------- core/src/tasks/workers/file.rs | 6 +++--- core/src/tasks/workers/precache.rs | 2 +- shared/src/fns.rs | 6 ++++++ shared/src/url.rs | 16 ++++++++++++---- 12 files changed, 64 insertions(+), 48 deletions(-) diff --git a/app/src/app.rs b/app/src/app.rs index 47755106..ab83a0ab 100644 --- a/app/src/app.rs +++ b/app/src/app.rs @@ -4,7 +4,7 @@ use std::ffi::OsString; use anyhow::{Ok, Result}; use config::{keymap::{Control, Key, KeymapLayer}, BOOT}; use crossterm::event::KeyEvent; -use shared::{absolute_path, Term}; +use shared::{absolute_url, Term}; use tokio::sync::oneshot; use crate::{Ctx, Executor, Logs, Root, Signals}; @@ -119,9 +119,9 @@ impl App { let manager = &mut self.cx.manager; let tasks = &mut self.cx.tasks; match event { - Event::Cd(path) => { + Event::Cd(url) => { futures::executor::block_on(async { - manager.active_mut().cd(absolute_path(path).into()).await; + manager.active_mut().cd(absolute_url(url)).await; }); } Event::Refresh => { diff --git a/app/src/executor.rs b/app/src/executor.rs index 03bcdc79..28a5b432 100644 --- a/app/src/executor.rs +++ b/app/src/executor.rs @@ -73,11 +73,11 @@ impl Executor { "back" => cx.manager.active_mut().back(), "forward" => cx.manager.active_mut().forward(), "cd" => { - let path = exec.args.get(0).map(Into::into).unwrap_or_default(); + let url = exec.args.get(0).map(Into::into).unwrap_or_default(); if exec.named.contains_key("interactive") { - cx.manager.active_mut().cd_interactive(path) + cx.manager.active_mut().cd_interactive(url) } else { - emit!(Cd(path)); + emit!(Cd(url)); false } } diff --git a/core/src/files/files.rs b/core/src/files/files.rs index 2ad9b8d6..831c3ec3 100644 --- a/core/src/files/files.rs +++ b/core/src/files/files.rs @@ -1,4 +1,4 @@ -use std::{collections::{BTreeMap, BTreeSet}, mem, ops::Deref, path::Path}; +use std::{collections::{BTreeMap, BTreeSet}, mem, ops::Deref}; use anyhow::Result; use config::manager::SortBy; @@ -40,22 +40,22 @@ impl Deref for Files { } impl Files { - pub async fn read(paths: Vec) -> Vec { - let mut items = Vec::with_capacity(paths.len()); - for path in paths { - if let Ok(file) = File::from(path).await { + pub async fn read(urls: Vec) -> Vec { + let mut items = Vec::with_capacity(urls.len()); + for url in urls { + if let Ok(file) = File::from(url).await { items.push(file); } } items } - pub async fn read_dir(path: &Path) -> Result> { - let mut it = fs::read_dir(path).await?; + pub async fn read_dir(url: &Url) -> Result> { + let mut it = fs::read_dir(url).await?; let mut items = Vec::new(); while let Ok(Some(item)) = it.next_entry().await { if let Ok(meta) = item.metadata().await { - items.push(File::from_meta(item.path().into(), meta).await); + items.push(File::from_meta(Url::new(item.path(), url), meta).await); } } Ok(items) diff --git a/core/src/files/sorter.rs b/core/src/files/sorter.rs index add2a0e5..b9f7789e 100644 --- a/core/src/files/sorter.rs +++ b/core/src/files/sorter.rs @@ -29,7 +29,7 @@ impl FilesSorter { match self.by { SortBy::Alphabetical => { - items.sort_unstable_by(|a, b| self.cmp(&a.url, &b.url, self.promote(a, b))) + items.sort_unstable_by(|a, b| self.cmp(&*a.url, &*b.url, self.promote(a, b))) } SortBy::Created => items.sort_unstable_by(|a, b| { if let (Ok(aa), Ok(bb)) = (a.meta.created(), b.meta.created()) { diff --git a/core/src/manager/manager.rs b/core/src/manager/manager.rs index 5b928235..0b80df87 100644 --- a/core/src/manager/manager.rs +++ b/core/src/manager/manager.rs @@ -189,7 +189,7 @@ impl Manager { fs::File::create(path).await?; } - if let Ok(file) = File::from(hovered.into()).await { + if let Ok(file) = File::from(Url::new(hovered, &cwd)).await { emit!(Hover(file)); emit!(Refresh); } diff --git a/core/src/manager/tab.rs b/core/src/manager/tab.rs index fa9e33a3..0604c97b 100644 --- a/core/src/manager/tab.rs +++ b/core/src/manager/tab.rs @@ -23,15 +23,17 @@ pub struct Tab { impl From for Tab { fn from(url: Url) -> Self { + let parent = url.parent_url().map(Folder::from); + Self { - mode: Default::default(), - current: Folder::from(&url), - parent: url.parent_url().map(Folder::from), + mode: Default::default(), + current: Folder::from(url), + parent, history: Default::default(), preview: Default::default(), - search: None, + search: None, show_hidden: true, } } @@ -88,7 +90,7 @@ impl Tab { let mut hovered = None; if !file.is_dir() { hovered = Some(file); - target = target.parent().unwrap().into(); + target = target.parent_url().unwrap(); } if self.current.cwd == target { @@ -124,8 +126,8 @@ impl Tab { let result = emit!(Input(InputOpt::top("Change directory:").with_value(target.to_string_lossy()))); - if let Ok(target) = result.await { - emit!(Cd(target.into())); + if let Ok(s) = result.await { + emit!(Cd(Url::new(s, &target))); } }); false diff --git a/core/src/manager/watcher.rs b/core/src/manager/watcher.rs index 1032d345..b2e713d7 100644 --- a/core/src/manager/watcher.rs +++ b/core/src/manager/watcher.rs @@ -35,7 +35,7 @@ impl Watcher { let parent = path.parent_url().unwrap_or_else(|| path.clone()); match event.kind { EventKind::Create(_) => { - tx.send(parent.into()).ok(); + tx.send(parent).ok(); } EventKind::Modify(kind) => { match kind { @@ -79,12 +79,12 @@ impl Watcher { ) }; - for p in to_unwatch { - self.watcher.unwatch(&p).ok(); + for u in to_unwatch { + self.watcher.unwatch(&u).ok(); } - for p in to_watch { - if self.watcher.watch(&p, RecursiveMode::NonRecursive).is_err() { - watched.remove(&p); + for u in to_watch { + if self.watcher.watch(&u, RecursiveMode::NonRecursive).is_err() { + watched.remove(&u); } } @@ -109,7 +109,7 @@ impl Watcher { for k in to_resolve { match fs::canonicalize(&k).await { Ok(v) if v != *k => { - ext.insert(k, Some(v.into())); + ext.insert(k, Some(Url::from(v))); } _ => {} } @@ -123,7 +123,7 @@ impl Watcher { pub(super) fn trigger_dirs(&self, dirs: &[&Url]) { let watched = self.watched.clone(); - let dirs = dirs.iter().map(|&u| u.clone()).collect::>(); + let dirs: Vec<_> = dirs.iter().map(|&u| u.clone()).collect(); tokio::spawn(async move { for dir in dirs { Self::dir_changed(&dir, watched.clone()).await; diff --git a/core/src/tasks/tasks.rs b/core/src/tasks/tasks.rs index a9b334c7..6a68d43a 100644 --- a/core/src/tasks/tasks.rs +++ b/core/src/tasks/tasks.rs @@ -235,11 +235,11 @@ impl Tasks { } pub fn precache_image(&self, mimetype: &BTreeMap) -> bool { - let targets = mimetype + let targets: Vec<_> = mimetype .iter() .filter(|(_, m)| MimeKind::new(m) == MimeKind::Image) - .map(|(p, _)| p.clone()) - .collect::>(); + .map(|(u, _)| u.clone()) + .collect(); if !targets.is_empty() { self.scheduler.precache_image(targets); @@ -248,11 +248,11 @@ impl Tasks { } pub fn precache_video(&self, mimetype: &BTreeMap) -> bool { - let targets = mimetype + let targets: Vec<_> = mimetype .iter() .filter(|(_, m)| MimeKind::new(m) == MimeKind::Video) - .map(|(p, _)| p.clone()) - .collect::>(); + .map(|(u, _)| u.clone()) + .collect(); if !targets.is_empty() { self.scheduler.precache_video(targets); @@ -261,11 +261,11 @@ impl Tasks { } pub fn precache_pdf(&self, mimetype: &BTreeMap) -> bool { - let targets = mimetype + let targets: Vec<_> = mimetype .iter() .filter(|(_, m)| MimeKind::new(m) == MimeKind::PDF) - .map(|(p, _)| p.clone()) - .collect::>(); + .map(|(u, _)| u.clone()) + .collect(); if !targets.is_empty() { self.scheduler.precache_pdf(targets); diff --git a/core/src/tasks/workers/file.rs b/core/src/tasks/workers/file.rs index b893ee52..a4967e86 100644 --- a/core/src/tasks/workers/file.rs +++ b/core/src/tasks/workers/file.rs @@ -256,7 +256,7 @@ impl File { let mut dirs = VecDeque::from([task.target]); while let Some(target) = dirs.pop_front() { - let mut it = match fs::read_dir(target).await { + let mut it = match fs::read_dir(&target).await { Ok(it) => it, Err(_) => continue, }; @@ -268,11 +268,11 @@ impl File { }; if meta.is_dir() { - dirs.push_front(entry.path().into()); + dirs.push_front(Url::new(entry.path(), &target)); continue; } - task.target = entry.path().into(); + task.target = Url::new(entry.path(), &target); task.length = meta.len(); self.sch.send(TaskOp::New(task.id, meta.len()))?; self.tx.send(FileOp::Delete(task.clone())).await?; diff --git a/core/src/tasks/workers/precache.rs b/core/src/tasks/workers/precache.rs index c6d194ec..ba923923 100644 --- a/core/src/tasks/workers/precache.rs +++ b/core/src/tasks/workers/precache.rs @@ -128,7 +128,7 @@ impl Precache { handing.remove(path); } - let parent = buf[0].0.parent().unwrap().into(); + let parent = buf[0].0.parent_url().unwrap(); emit!(Files(FilesOp::Size(parent, BTreeMap::from_iter(buf)))); }); diff --git a/shared/src/fns.rs b/shared/src/fns.rs index 5a595e15..11fff35d 100644 --- a/shared/src/fns.rs +++ b/shared/src/fns.rs @@ -14,6 +14,12 @@ pub fn absolute_path(p: impl AsRef) -> PathBuf { std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf()) } +#[inline] +pub fn absolute_url(mut u: Url) -> Url { + u.set_path(absolute_path(&u)); + u +} + pub fn readable_path(p: &Path, base: &Path) -> String { if let Ok(p) = p.strip_prefix(base) { return p.display().to_string(); diff --git a/shared/src/url.rs b/shared/src/url.rs index 2ec10b64..afc3122e 100644 --- a/shared/src/url.rs +++ b/shared/src/url.rs @@ -61,9 +61,19 @@ impl AsRef for Url { } impl Url { + #[inline] + pub fn new(url: impl Into, ctx: &Url) -> Self { + let mut url: Self = url.into(); + url.scheme = ctx.scheme; + url + } + #[inline] pub fn is_search(&self) -> bool { self.scheme == UrlScheme::Search } + #[inline] + pub fn set_path(&mut self, path: PathBuf) { self.path = path; } + #[inline] pub fn strip_prefix(&self, base: impl AsRef) -> Option<&Path> { self.path.strip_prefix(base).ok() @@ -74,11 +84,9 @@ impl Url { #[inline] pub fn parent_url(&self) -> Option { - self.path.parent().map(|p| Self { path: p.to_path_buf(), ..*self }) + self.path.parent().map(|p| Self::new(p.to_path_buf(), self)) } #[inline] - pub fn __join(&self, path: impl AsRef) -> Url { - Url { path: self.path.join(path), ..*self } - } + pub fn __join(&self, path: impl AsRef) -> Self { Self::new(self.path.join(path), self) } }