yazi/yazi-watcher/src/watched.rs
三咲雅 misaki masa 58f1013348
Some checks failed
Cachix / Publish Flake (push) Has been cancelled
Check / clippy (push) Has been cancelled
Check / rustfmt (push) Has been cancelled
Check / stylua (push) Has been cancelled
Draft / build-unix (gcc-aarch64-linux-gnu, ubuntu-latest, aarch64-unknown-linux-gnu) (push) Has been cancelled
Draft / build-unix (gcc-i686-linux-gnu, ubuntu-latest, i686-unknown-linux-gnu) (push) Has been cancelled
Draft / build-unix (gcc-riscv64-linux-gnu, ubuntu-latest, riscv64gc-unknown-linux-gnu) (push) Has been cancelled
Draft / build-unix (gcc-sparc64-linux-gnu, ubuntu-latest, sparc64-unknown-linux-gnu) (push) Has been cancelled
Draft / build-unix (macos-latest, aarch64-apple-darwin) (push) Has been cancelled
Draft / build-unix (macos-latest, x86_64-apple-darwin) (push) Has been cancelled
Draft / build-unix (ubuntu-latest, x86_64-unknown-linux-gnu) (push) Has been cancelled
Draft / build-windows (windows-latest, aarch64-pc-windows-msvc) (push) Has been cancelled
Draft / build-windows (windows-latest, x86_64-pc-windows-msvc) (push) Has been cancelled
Draft / build-musl (aarch64-unknown-linux-musl) (push) Has been cancelled
Draft / build-musl (x86_64-unknown-linux-musl) (push) Has been cancelled
Draft / build-snap (amd64, ubuntu-latest) (push) Has been cancelled
Draft / build-snap (arm64, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (macos-latest) (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
Test / test (windows-latest) (push) Has been cancelled
Draft / snap (push) Has been cancelled
Draft / draft (push) Has been cancelled
Draft / nightly (push) Has been cancelled
feat: trash bin (#4144)
2026-07-23 01:59:39 +08:00

69 lines
1.8 KiB
Rust

use std::{ops::Deref, path::{Path, PathBuf}};
use hashbrown::{HashMap, HashSet};
use indexmap::IndexSet;
use yazi_fs::FsUrl;
use yazi_shared::{path::PathDyn, url::{AsUrl, UrlBuf}};
use crate::Watchee;
#[derive(Debug, Default)]
pub struct Watched {
urls: HashSet<Watchee<'static>>,
caches: HashMap<PathBuf, UrlBuf>,
}
impl Deref for Watched {
type Target = HashSet<Watchee<'static>>;
fn deref(&self) -> &Self::Target { &self.urls }
}
impl Watched {
pub(super) fn insert(&mut self, watchee: Watchee<'static>) {
let url = watchee.as_url();
if let Some(cache) = url.cache_bucket() {
self.caches.insert(cache, url.into());
}
self.urls.insert(watchee);
}
pub(super) fn remove(&mut self, watchee: &Watchee<'static>) -> bool {
if !self.urls.remove(watchee) {
return false;
}
if let Some(cache) = watchee.as_url().cache_bucket() {
self.caches.remove(&cache);
}
true
}
pub(super) fn difference<'a>(
&'a self,
other: &'a IndexSet<Watchee<'static>>,
) -> impl Iterator<Item = &'a Watchee<'static>> {
self.urls.iter().filter(move |&watchee| !other.contains(watchee))
}
pub(super) fn contains_url(&self, url: impl AsUrl) -> bool {
let url = url.as_url();
if url.as_local().is_some() {
self.urls.contains(&Watchee::Local(url.into(), false))
|| self.urls.contains(&Watchee::Local(url.into(), true))
} else {
self.urls.contains(&Watchee::Virtual(url.into()))
}
}
pub(super) fn contains_path(&self, path: &Path) -> bool {
self.urls.iter().any(|watchee| watchee.as_url().as_local() == Some(path))
}
pub(super) fn paths(&self) -> impl Iterator<Item = &Path> {
self.urls.iter().filter_map(|watchee| watchee.as_url().as_local())
}
pub(super) fn find_by_cache(&self, cache: PathDyn) -> Option<UrlBuf> {
self.caches.get(cache.as_os().ok()?).cloned()
}
}