From 0d44a33d12c185f2a2331bec6041233cb23bf558 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Tue, 5 Sep 2023 19:12:25 +0800 Subject: [PATCH] .. --- core/src/external/fd.rs | 2 +- core/src/external/fzf.rs | 2 +- core/src/external/rg.rs | 2 +- core/src/manager/watcher.rs | 4 ++-- core/src/tasks/tasks.rs | 4 ++-- core/src/tasks/workers/file.rs | 4 ++-- shared/src/url.rs | 32 ++++++++++++++++++++------------ 7 files changed, 29 insertions(+), 21 deletions(-) diff --git a/core/src/external/fd.rs b/core/src/external/fd.rs index 48dd1498..ba302f69 100644 --- a/core/src/external/fd.rs +++ b/core/src/external/fd.rs @@ -32,7 +32,7 @@ pub fn fd(opt: FdOpt) -> Result>> { tokio::spawn(async move { while let Ok(Some(line)) = it.next_line().await { - tx.send(opt.cwd.__join(line)).ok(); + tx.send(opt.cwd.join(line)).ok(); } child.wait().await.ok(); }); diff --git a/core/src/external/fzf.rs b/core/src/external/fzf.rs index a5d7ee91..2c9dba77 100644 --- a/core/src/external/fzf.rs +++ b/core/src/external/fzf.rs @@ -17,7 +17,7 @@ pub fn fzf(opt: FzfOpt) -> Result>> { if let Ok(output) = child.wait_with_output().await { let selected = String::from_utf8_lossy(&output.stdout).trim().to_string(); if !selected.is_empty() { - tx.send(Ok(opt.cwd.__join(selected))).ok(); + tx.send(Ok(opt.cwd.join(selected))).ok(); return; } } diff --git a/core/src/external/rg.rs b/core/src/external/rg.rs index b1fc568d..9e3a5fb1 100644 --- a/core/src/external/rg.rs +++ b/core/src/external/rg.rs @@ -30,7 +30,7 @@ pub fn rg(opt: RgOpt) -> Result>> { tokio::spawn(async move { while let Ok(Some(line)) = it.next_line().await { - tx.send(opt.cwd.__join(line)).ok(); + tx.send(opt.cwd.join(line)).ok(); } child.wait().await.ok(); }); diff --git a/core/src/manager/watcher.rs b/core/src/manager/watcher.rs index b2e713d7..2c19c820 100644 --- a/core/src/manager/watcher.rs +++ b/core/src/manager/watcher.rs @@ -166,7 +166,7 @@ impl Watcher { let linked = watched .read() .iter() - .map_while(|(k, v)| v.as_ref().and_then(|v| url.strip_prefix(v)).map(|v| k.__join(v))) + .map_while(|(k, v)| v.as_ref().and_then(|v| url.strip_prefix(v)).map(|v| k.join(v))) .collect::>(); let result = Files::read_dir(url).await; @@ -184,7 +184,7 @@ impl Watcher { let mut files = Vec::with_capacity(items.len()); for item in items { let mut file = item.clone(); - file.set_url(ori.__join(item.url().strip_prefix(url).unwrap())); + file.set_url(ori.join(item.url().strip_prefix(url).unwrap())); files.push(file); } FilesOp::Read(ori, files) diff --git a/core/src/tasks/tasks.rs b/core/src/tasks/tasks.rs index 6a68d43a..6ac76edf 100644 --- a/core/src/tasks/tasks.rs +++ b/core/src/tasks/tasks.rs @@ -156,7 +156,7 @@ impl Tasks { pub fn file_cut(&self, src: &HashSet, dest: Url, force: bool) -> bool { for p in src { - let to = dest.__join(p.file_name().unwrap()); + let to = dest.join(p.file_name().unwrap()); if force && *p == to { trace!("file_cut: same file, skipping {:?}", to); } else { @@ -168,7 +168,7 @@ impl Tasks { pub fn file_copy(&self, src: &HashSet, dest: Url, force: bool, follow: bool) -> bool { for p in src { - let to = dest.__join(p.file_name().unwrap()); + let to = dest.join(p.file_name().unwrap()); if force && *p == to { trace!("file_copy: same file, skipping {:?}", to); } else { diff --git a/core/src/tasks/workers/file.rs b/core/src/tasks/workers/file.rs index a4967e86..4f52eacb 100644 --- a/core/src/tasks/workers/file.rs +++ b/core/src/tasks/workers/file.rs @@ -202,7 +202,7 @@ impl File { let mut dirs = VecDeque::from([task.from]); while let Some(src) = dirs.pop_front() { - let dest = root.__join(src.components().skip(skip).collect::()); + let dest = root.join(src.components().skip(skip).collect::()); match fs::create_dir(&dest).await { Err(e) if e.kind() != AlreadyExists => { self.log(task.id, format!("Create dir failed: {:?}, {e}", dest))?; @@ -230,7 +230,7 @@ impl File { continue; } - task.to = dest.__join(src.file_name().unwrap()); + task.to = dest.join(src.file_name().unwrap()); task.from = src; self.sch.send(TaskOp::New(task.id, meta.len()))?; diff --git a/shared/src/url.rs b/shared/src/url.rs index afc3122e..4cff2a51 100644 --- a/shared/src/url.rs +++ b/shared/src/url.rs @@ -69,10 +69,7 @@ impl Url { } #[inline] - pub fn is_search(&self) -> bool { self.scheme == UrlScheme::Search } - - #[inline] - pub fn set_path(&mut self, path: PathBuf) { self.path = path; } + pub fn join(&self, path: impl AsRef) -> Self { Self::new(self.path.join(path), self) } #[inline] pub fn strip_prefix(&self, base: impl AsRef) -> Option<&Path> { @@ -81,12 +78,23 @@ impl Url { #[inline] pub fn into_os_string(self) -> OsString { self.path.into_os_string() } - - #[inline] - pub fn parent_url(&self) -> Option { - self.path.parent().map(|p| Self::new(p.to_path_buf(), self)) - } - - #[inline] - pub fn __join(&self, path: impl AsRef) -> Self { Self::new(self.path.join(path), self) } +} + +impl Url { + // --- Scheme + #[inline] + pub fn is_none(&self) -> bool { self.scheme == UrlScheme::None } + + #[inline] + pub fn is_search(&self) -> bool { self.scheme == UrlScheme::Search } + + #[inline] + pub fn is_archive(&self) -> bool { self.scheme == UrlScheme::Archive } + + // --- Path + #[inline] + pub fn set_path(&mut self, path: PathBuf) { self.path = path; } + + #[inline] + pub fn parent_url(&self) -> Option { self.path.parent().map(|p| Self::new(p, self)) } }