Improve updating favorites when moving multiple dirs

Now update_favorites only needs to be called a single time after moving
a bunch of files.
This commit is contained in:
Jason Rodney Hansen 2025-03-08 17:11:34 -07:00
parent 08a3ac8620
commit 0d76f7819e

View file

@ -1567,8 +1567,8 @@ impl App {
.into() .into()
} }
// Update favorites based on a rename or a move. // Update favorites based on renaming or moving dirs.
fn update_favorites(&mut self, from: &Path, to: &Path) -> bool { fn update_favorites(&mut self, path_changes: &[(PathBuf, PathBuf)]) -> bool {
let mut favorites_changed = false; let mut favorites_changed = false;
let favorites = self let favorites = self
.config .config
@ -1577,10 +1577,12 @@ impl App {
.cloned() .cloned()
.map(|favorite| { .map(|favorite| {
if let Favorite::Path(ref path) = favorite { if let Favorite::Path(ref path) = favorite {
if path.starts_with(from) { for (from, to) in path_changes {
if let Ok(relative) = path.strip_prefix(from) { if path.starts_with(from) {
favorites_changed = true; if let Ok(relative) = path.strip_prefix(from) {
return Favorite::from_path(to.join(relative)); favorites_changed = true;
return Favorite::from_path(to.join(relative));
}
} }
} }
} }
@ -1598,9 +1600,7 @@ impl App {
} }
Err(err) => { Err(err) => {
log::warn!( log::warn!(
"failed to update favorites after moving {} to {}: {}", "failed to update favorites after moving directories: {:?}",
from.display(),
to.display(),
err, err,
); );
} }
@ -1608,9 +1608,7 @@ impl App {
} else { } else {
self.config.favorites = favorites; self.config.favorites = favorites;
log::warn!( log::warn!(
"failed to update favorites after moving {} to {}: no config handler", "failed to update favorites after moving directories: no config handler",
from.display(),
to.display(),
); );
} }
} }
@ -2743,18 +2741,17 @@ impl Application for App {
// If a favorite for a path has been renamed or moved, update it. // If a favorite for a path has been renamed or moved, update it.
if let Operation::Rename { ref from, ref to } = op { if let Operation::Rename { ref from, ref to } = op {
if self.update_favorites(from, to) { if self.update_favorites(&[(from.clone(), to.clone())]) {
commands.push(self.update_config()); commands.push(self.update_config());
} }
} else if let Operation::Move { ref paths, ref to } = op { } else if let Operation::Move { ref paths, ref to } = op {
let mut updated = false; let path_changes: Vec<_> = paths
for from in paths { .iter()
if let Some(name) = from.file_name() { .filter_map(|from| {
let to = to.join(name); from.file_name().map(|name| (from.clone(), to.join(name)))
updated |= self.update_favorites(from, &to); })
} .collect();
} if self.update_favorites(&path_changes) {
if updated {
commands.push(self.update_config()); commands.push(self.update_config());
} }
} }