This commit is contained in:
sxyazi 2023-08-28 13:15:34 +08:00
parent 99acd09d26
commit 1af7d728d6
No known key found for this signature in database
8 changed files with 57 additions and 47 deletions

View file

@ -106,7 +106,7 @@ impl Executor {
} }
} }
"remove" => { "remove" => {
let targets = cx.manager.selected().into_iter().map(|p| p.path()).collect(); let targets = cx.manager.selected().into_iter().map(|f| f.path().clone()).collect();
cx.tasks.file_remove(targets, exec.named.contains_key("permanently")) cx.tasks.file_remove(targets, exec.named.contains_key("permanently"))
} }
"create" => cx.manager.create(), "create" => cx.manager.create(),

View file

@ -37,7 +37,7 @@ impl File {
impl File { impl File {
#[inline] #[inline]
pub fn path(&self) -> PathBuf { self.path.clone() } pub fn path(&self) -> &PathBuf { &self.path }
#[inline] #[inline]
pub fn set_path(mut self, path: PathBuf) -> Self { pub fn set_path(mut self, path: PathBuf) -> Self {

View file

@ -86,7 +86,7 @@ impl Files {
match state { match state {
Some(true) => { Some(true) => {
self.selected = self.iter().map(|f| f.path()).collect(); self.selected = self.iter().map(|f| f.path().clone()).collect();
} }
Some(false) => { Some(false) => {
self.selected.clear(); self.selected.clear();
@ -96,7 +96,7 @@ impl Files {
if self.selected.contains(&item.path) { if self.selected.contains(&item.path) {
self.selected.remove(&item.path); self.selected.remove(&item.path);
} else { } else {
self.selected.insert(item.path()); self.selected.insert(item.path().clone());
} }
} }
} }
@ -106,7 +106,7 @@ impl Files {
pub fn select_index(&mut self, indices: &BTreeSet<usize>, state: Option<bool>) -> bool { pub fn select_index(&mut self, indices: &BTreeSet<usize>, state: Option<bool>) -> bool {
let mut applied = false; let mut applied = false;
let paths: Vec<_> = self.pick(indices).iter().map(|f| f.path()).collect(); let paths: Vec<_> = self.pick(indices).iter().map(|f| f.path().clone()).collect();
for path in paths { for path in paths {
applied |= self.select(&path, state); applied |= self.select(&path, state);

View file

@ -40,7 +40,7 @@ impl Folder {
self.cursor = self.cursor.min(len.saturating_sub(1)); self.cursor = self.cursor.min(len.saturating_sub(1));
self.set_page(true); self.set_page(true);
if let Some(h) = self.hovered.as_ref().map(|h| h.path()) { if let Some(h) = self.hovered.as_ref().map(|h| h.path().clone()) {
self.hover(&h); self.hover(&h);
} }
self.hovered = self.files.duplicate(self.cursor); self.hovered = self.files.duplicate(self.cursor);

View file

@ -39,15 +39,15 @@ impl Manager {
let mut to_watch = BTreeSet::new(); let mut to_watch = BTreeSet::new();
for tab in self.tabs.iter() { for tab in self.tabs.iter() {
to_watch.insert(tab.current.cwd.clone()); to_watch.insert(&tab.current.cwd);
if let Some(ref p) = tab.parent {
to_watch.insert(p.cwd.clone());
}
if let Some(ref h) = tab.current.hovered { if let Some(ref h) = tab.current.hovered {
if h.meta.is_dir() { if h.meta.is_dir() {
to_watch.insert(h.path()); to_watch.insert(h.path());
} }
} }
if let Some(ref p) = tab.parent {
to_watch.insert(&p.cwd);
}
} }
self.watcher.watch(to_watch); self.watcher.watch(to_watch);
} }
@ -83,10 +83,8 @@ impl Manager {
} }
pub fn yank(&mut self, cut: bool) -> bool { pub fn yank(&mut self, cut: bool) -> bool {
let selected = self.selected().into_iter().map(|f| f.path()).collect::<Vec<_>>();
self.yanked.0 = cut; self.yanked.0 = cut;
self.yanked.1.clear(); self.yanked.1 = self.selected().into_iter().map(|f| f.path().clone()).collect();
self.yanked.1.extend(selected);
false false
} }
@ -124,7 +122,7 @@ impl Manager {
.into_iter() .into_iter()
.map(|f| { .map(|f| {
( (
f.path().into_os_string(), f.path().as_os_str().to_owned(),
if f.meta.is_dir() { if f.meta.is_dir() {
Some(MIME_DIR.to_owned()) Some(MIME_DIR.to_owned())
} else { } else {
@ -203,7 +201,7 @@ impl Manager {
return self.bulk_rename(); return self.bulk_rename();
} }
let Some(hovered) = self.hovered().map(|h| h.path()) else { let Some(hovered) = self.hovered().map(|h| h.path().clone()) else {
return false; return false;
}; };
@ -221,10 +219,10 @@ impl Manager {
} }
pub fn bulk_rename(&self) -> bool { pub fn bulk_rename(&self) -> bool {
let mut old: Vec<_> = self.selected().iter().map(|&f| f.path()).collect(); let old: Vec<_> = self.selected().into_iter().map(|f| f.path()).collect();
let root = max_common_root(&old); let root = max_common_root(&old);
old.iter_mut().for_each(|p| *p = p.strip_prefix(&root).unwrap().to_owned()); let old: Vec<_> = old.into_iter().map(|p| p.strip_prefix(&root).unwrap().to_owned()).collect();
let tmp = BOOT.tmpfile("bulk"); let tmp = BOOT.tmpfile("bulk");
tokio::spawn(async move { tokio::spawn(async move {
@ -327,7 +325,7 @@ impl Manager {
pub fn update_read(&mut self, op: FilesOp) -> bool { pub fn update_read(&mut self, op: FilesOp) -> bool {
let path = op.path(); let path = op.path();
let cwd = self.cwd().to_owned(); let cwd = self.cwd().to_owned();
let hovered = self.hovered().map(|h| h.path()); let hovered = self.hovered().map(|h| h.path().clone());
let mut b = if cwd == path && !self.current().in_search { let mut b = if cwd == path && !self.current().in_search {
self.current_mut().update(op) self.current_mut().update(op)
@ -347,7 +345,7 @@ impl Manager {
b |= self.active_mut().parent.as_mut().map_or(false, |p| p.hover(&cwd)); b |= self.active_mut().parent.as_mut().map_or(false, |p| p.hover(&cwd));
b |= hovered.as_ref().map_or(false, |h| self.current_mut().hover(h)); b |= hovered.as_ref().map_or(false, |h| self.current_mut().hover(h));
if hovered != self.hovered().map(|h| h.path()) { if hovered.as_ref() != self.hovered().map(|h| h.path()) {
emit!(Hover); emit!(Hover);
} }
b b

View file

@ -69,44 +69,55 @@ impl Watcher {
instance instance
} }
pub(super) fn watch(&mut self, mut to_watch: BTreeSet<PathBuf>) { pub(super) fn watch(&mut self, mut watched: BTreeSet<&PathBuf>) {
let keys = self.watched.read().keys().cloned().collect::<BTreeSet<_>>(); let (to_unwatch, to_watch): (BTreeSet<_>, BTreeSet<_>) = {
for p in keys.difference(&to_watch) { let guard = self.watched.read();
self.watcher.unwatch(p).ok(); let keys = guard.keys().collect::<BTreeSet<_>>();
(
keys.difference(&watched).map(|&x| x.clone()).collect(),
watched.difference(&keys).map(|&x| x.clone()).collect(),
)
};
for p in to_unwatch {
self.watcher.unwatch(&p).ok();
} }
for p in to_watch.clone().difference(&keys) { for p in to_watch {
if self.watcher.watch(p, RecursiveMode::NonRecursive).is_err() { if self.watcher.watch(&p, RecursiveMode::NonRecursive).is_err() {
to_watch.remove(p); watched.remove(&p);
} }
} }
let mut todo = Vec::new(); let mut to_resolve = Vec::new();
let mut watched = self.watched.write(); let mut guard = self.watched.write();
*watched = IndexMap::from_iter(to_watch.into_iter().map(|k| { *guard = watched
if let Some(v) = watched.remove(&k) { .into_iter()
(k, v) .map(|k| {
} else { if let Some((k, v)) = guard.remove_entry(k) {
todo.push(k.clone()); (k, v)
(k, None) } else {
} to_resolve.push(k.clone());
})); (k.clone(), None)
watched.sort_unstable_by(|_, a, _, b| b.cmp(a)); }
})
.collect();
guard.sort_unstable_by(|_, a, _, b| b.cmp(a));
let watched = self.watched.clone(); let lock = self.watched.clone();
tokio::spawn(async move { tokio::spawn(async move {
let mut ext = IndexMap::new(); let mut ext = IndexMap::new();
for k in todo { for k in to_resolve {
match fs::canonicalize(&k).await { match fs::canonicalize(&k).await {
Ok(v) if v != k => { Ok(v) if v != *k => {
ext.insert(k, Some(v)); ext.insert(k, Some(v));
} }
_ => {} _ => {}
} }
} }
let mut watched = watched.write(); let mut guard = lock.write();
watched.extend(ext); guard.extend(ext);
watched.sort_unstable_by(|_, a, _, b| b.cmp(a)); guard.sort_unstable_by(|_, a, _, b| b.cmp(a));
}); });
} }

View file

@ -318,13 +318,13 @@ impl Scheduler {
}); });
} }
pub(super) fn precache_size(&self, targets: Vec<PathBuf>) { pub(super) fn precache_size(&self, targets: Vec<&PathBuf>) {
let throttle = Arc::new(Throttle::new(targets.len(), Duration::from_millis(300))); let throttle = Arc::new(Throttle::new(targets.len(), Duration::from_millis(300)));
let mut handing = self.precache.size_handing.lock(); let mut handing = self.precache.size_handing.lock();
let mut running = self.running.write(); let mut running = self.running.write();
for target in targets { for target in targets {
if !handing.contains(&target) { if !handing.contains(target) {
handing.insert(target.clone()); handing.insert(target.clone());
} else { } else {
continue; continue;
@ -333,6 +333,7 @@ impl Scheduler {
let id = running.add(format!("Calculate the size of {:?}", target)); let id = running.add(format!("Calculate the size of {:?}", target));
let _ = self.todo.send_blocking({ let _ = self.todo.send_blocking({
let precache = self.precache.clone(); let precache = self.precache.clone();
let target = target.clone();
let throttle = throttle.clone(); let throttle = throttle.clone();
async move { async move {
precache.size(PrecacheOpSize { id, target, throttle }).await.ok(); precache.size(PrecacheOpSize { id, target, throttle }).await.ok();

View file

@ -148,12 +148,12 @@ pub fn file_mode(mode: u32) -> String {
// Find the max common root of a list of files // Find the max common root of a list of files
// e.g. /a/b/c, /a/b/d -> /a/b // e.g. /a/b/c, /a/b/d -> /a/b
// /aa/bb/cc, /aa/dd/ee -> /aa // /aa/bb/cc, /aa/dd/ee -> /aa
pub fn max_common_root(files: &[PathBuf]) -> PathBuf { pub fn max_common_root(files: &[impl AsRef<Path>]) -> PathBuf {
if files.is_empty() { if files.is_empty() {
return PathBuf::new(); return PathBuf::new();
} }
let mut it = files.iter().map(|p| p.parent().unwrap_or(Path::new("")).components()); let mut it = files.iter().map(|p| p.as_ref().parent().unwrap_or(Path::new("")).components());
let mut root = it.next().unwrap().collect::<PathBuf>(); let mut root = it.next().unwrap().collect::<PathBuf>();
for components in it { for components in it {
let mut new_root = PathBuf::new(); let mut new_root = PathBuf::new();