Skip to content

Commit

Permalink
feat: sort the order of bulk-renamed files
Browse files Browse the repository at this point in the history
  • Loading branch information
yw1ee committed Oct 17, 2024
1 parent a6c9c93 commit 6e36b5c
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion yazi-core/src/manager/commands/bulk_rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
}
Expand Down Expand Up @@ -117,4 +117,47 @@ 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();

println!("{:?}", income_map);
println!("{:?}", todos);

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
}
}

0 comments on commit 6e36b5c

Please sign in to comment.