This commit is contained in:
sxyazi 2023-09-05 19:12:25 +08:00
parent 4b39a966da
commit 0d44a33d12
No known key found for this signature in database
7 changed files with 29 additions and 21 deletions

View file

@ -32,7 +32,7 @@ pub fn fd(opt: FdOpt) -> Result<StreamBuf<UnboundedReceiverStream<Url>>> {
tokio::spawn(async move { tokio::spawn(async move {
while let Ok(Some(line)) = it.next_line().await { 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(); child.wait().await.ok();
}); });

View file

@ -17,7 +17,7 @@ pub fn fzf(opt: FzfOpt) -> Result<Receiver<Result<Url>>> {
if let Ok(output) = child.wait_with_output().await { if let Ok(output) = child.wait_with_output().await {
let selected = String::from_utf8_lossy(&output.stdout).trim().to_string(); let selected = String::from_utf8_lossy(&output.stdout).trim().to_string();
if !selected.is_empty() { if !selected.is_empty() {
tx.send(Ok(opt.cwd.__join(selected))).ok(); tx.send(Ok(opt.cwd.join(selected))).ok();
return; return;
} }
} }

View file

@ -30,7 +30,7 @@ pub fn rg(opt: RgOpt) -> Result<StreamBuf<UnboundedReceiverStream<Url>>> {
tokio::spawn(async move { tokio::spawn(async move {
while let Ok(Some(line)) = it.next_line().await { 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(); child.wait().await.ok();
}); });

View file

@ -166,7 +166,7 @@ impl Watcher {
let linked = watched let linked = watched
.read() .read()
.iter() .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::<Vec<_>>(); .collect::<Vec<_>>();
let result = Files::read_dir(url).await; let result = Files::read_dir(url).await;
@ -184,7 +184,7 @@ impl Watcher {
let mut files = Vec::with_capacity(items.len()); let mut files = Vec::with_capacity(items.len());
for item in items { for item in items {
let mut file = item.clone(); 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); files.push(file);
} }
FilesOp::Read(ori, files) FilesOp::Read(ori, files)

View file

@ -156,7 +156,7 @@ impl Tasks {
pub fn file_cut(&self, src: &HashSet<Url>, dest: Url, force: bool) -> bool { pub fn file_cut(&self, src: &HashSet<Url>, dest: Url, force: bool) -> bool {
for p in src { for p in src {
let to = dest.__join(p.file_name().unwrap()); let to = dest.join(p.file_name().unwrap());
if force && *p == to { if force && *p == to {
trace!("file_cut: same file, skipping {:?}", to); trace!("file_cut: same file, skipping {:?}", to);
} else { } else {
@ -168,7 +168,7 @@ impl Tasks {
pub fn file_copy(&self, src: &HashSet<Url>, dest: Url, force: bool, follow: bool) -> bool { pub fn file_copy(&self, src: &HashSet<Url>, dest: Url, force: bool, follow: bool) -> bool {
for p in src { for p in src {
let to = dest.__join(p.file_name().unwrap()); let to = dest.join(p.file_name().unwrap());
if force && *p == to { if force && *p == to {
trace!("file_copy: same file, skipping {:?}", to); trace!("file_copy: same file, skipping {:?}", to);
} else { } else {

View file

@ -202,7 +202,7 @@ impl File {
let mut dirs = VecDeque::from([task.from]); let mut dirs = VecDeque::from([task.from]);
while let Some(src) = dirs.pop_front() { while let Some(src) = dirs.pop_front() {
let dest = root.__join(src.components().skip(skip).collect::<PathBuf>()); let dest = root.join(src.components().skip(skip).collect::<PathBuf>());
match fs::create_dir(&dest).await { match fs::create_dir(&dest).await {
Err(e) if e.kind() != AlreadyExists => { Err(e) if e.kind() != AlreadyExists => {
self.log(task.id, format!("Create dir failed: {:?}, {e}", dest))?; self.log(task.id, format!("Create dir failed: {:?}, {e}", dest))?;
@ -230,7 +230,7 @@ impl File {
continue; continue;
} }
task.to = dest.__join(src.file_name().unwrap()); task.to = dest.join(src.file_name().unwrap());
task.from = src; task.from = src;
self.sch.send(TaskOp::New(task.id, meta.len()))?; self.sch.send(TaskOp::New(task.id, meta.len()))?;

View file

@ -69,10 +69,7 @@ impl Url {
} }
#[inline] #[inline]
pub fn is_search(&self) -> bool { self.scheme == UrlScheme::Search } pub fn join(&self, path: impl AsRef<Path>) -> Self { Self::new(self.path.join(path), self) }
#[inline]
pub fn set_path(&mut self, path: PathBuf) { self.path = path; }
#[inline] #[inline]
pub fn strip_prefix(&self, base: impl AsRef<Path>) -> Option<&Path> { pub fn strip_prefix(&self, base: impl AsRef<Path>) -> Option<&Path> {
@ -81,12 +78,23 @@ impl Url {
#[inline] #[inline]
pub fn into_os_string(self) -> OsString { self.path.into_os_string() } pub fn into_os_string(self) -> OsString { self.path.into_os_string() }
}
#[inline]
pub fn parent_url(&self) -> Option<Url> { impl Url {
self.path.parent().map(|p| Self::new(p.to_path_buf(), self)) // --- Scheme
} #[inline]
pub fn is_none(&self) -> bool { self.scheme == UrlScheme::None }
#[inline]
pub fn __join(&self, path: impl AsRef<Path>) -> Self { Self::new(self.path.join(path), self) } #[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<Url> { self.path.parent().map(|p| Self::new(p, self)) }
} }