mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
feat: sort the order of bulk-renamed files
This commit is contained in:
parent
24a77d8a5f
commit
c152addc3b
1 changed files with 41 additions and 1 deletions
|
|
@ -53,7 +53,7 @@ impl Manager {
|
|||
return Ok(());
|
||||
}
|
||||
|
||||
let todo: Vec<_> = old.into_iter().zip(new).filter(|(o, n)| o != n).collect();
|
||||
let todo = Self::sort(old, new);
|
||||
if todo.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
|
@ -117,4 +117,44 @@ impl Manager {
|
|||
stdin().read_exact(&mut [0]).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn sort(old: Vec<PathBuf>, new: Vec<PathBuf>) -> Vec<(PathBuf, PathBuf)> {
|
||||
let mut income_map: HashMap<PathBuf, bool> =
|
||||
old.iter().map(|path| (path.clone(), false)).collect();
|
||||
let mut todos: HashMap<PathBuf, PathBuf> = old
|
||||
.into_iter()
|
||||
.zip(new)
|
||||
.map(|(old, new)| {
|
||||
if let Some(has_income) = income_map.get_mut(&new) {
|
||||
*has_income = true;
|
||||
}
|
||||
(old, new)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut sorted = vec![];
|
||||
while !todos.is_empty() {
|
||||
let mut has_no_incomes = vec![];
|
||||
income_map.iter().for_each(|(old, has_income)| {
|
||||
if !has_income {
|
||||
has_no_incomes.push(old.clone())
|
||||
}
|
||||
});
|
||||
|
||||
if has_no_incomes.is_empty() {
|
||||
todo!("Consider cycle")
|
||||
}
|
||||
|
||||
for old in has_no_incomes {
|
||||
income_map.remove(&old);
|
||||
let Some(new) = todos.remove(&old) else { unreachable!("") };
|
||||
if let Some(has_income) = income_map.get_mut(&new) {
|
||||
*has_income = false;
|
||||
}
|
||||
sorted.push((old, new));
|
||||
}
|
||||
}
|
||||
sorted.reverse();
|
||||
sorted
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue