Skip to content

Commit

Permalink
Handle no renaming pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoheiu committed Jan 11, 2025
1 parent 36c9ac4 commit 0ad66fc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
32 changes: 18 additions & 14 deletions src/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ fn log(op: &OpKind) {
info!("DELETE: {:?}", item_to_pathvec(&op.original));
}
OpKind::Rename(op) => {
info!(
"RENAME: {:?}",
op.iter()
.map(|v| format!("{:?} -> {:?}", v.0, v.1))
.collect::<Vec<String>>()
);
if !op.is_empty() {
info!(
"RENAME: {:?}",
op.iter()
.map(|v| format!("{:?} -> {:?}", v.0, v.1))
.collect::<Vec<String>>()
);
}
}
}
}
Expand All @@ -83,14 +85,16 @@ pub fn relog(op: &OpKind, undo: bool) {
info!("{} {:?}", result, item_to_pathvec(&op.original));
}
OpKind::Rename(op) => {
result.push_str("RENAME");
info!(
"{} {:?}",
result,
op.iter()
.map(|v| format!("{:?} -> {:?}", v.0, v.1))
.collect::<Vec<String>>()
);
if !op.is_empty() {
result.push_str("RENAME");
info!(
"{} {:?}",
result,
op.iter()
.map(|v| format!("{:?} -> {:?}", v.0, v.1))
.collect::<Vec<String>>()
);
}
}
}
}
Expand Down
28 changes: 17 additions & 11 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1371,21 +1371,27 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
.map(ItemBuffer::new)
.collect();
execute!(screen, EnterAlternateScreen)?;
let mut err: Option<FxError> = None;
if let Err(e) = state.rename_multiple_items(&items) {
err = Some(e);
}
let result = state.rename_multiple_items(&items);
execute!(screen, EnterAlternateScreen)?;
hide_cursor();
state.reset_selection();
state.reload(state.layout.y)?;
if let Some(e) = err {
print_warning(e, state.layout.y);
} else {
print_info(
format!("Renamed {} items.", items.len()),
state.layout.y,
);
match result {
Err(e) => {
print_warning(e, state.layout.y);
}
Ok(result_len) => {
let message = {
match result_len {
0 => "No item renamed.".to_owned(),
1 => "1 item renamed.".to_owned(),
count => {
format!("{} items renamed.", count)
}
}
};
print_info(message, state.layout.y);
}
}
continue;
}
Expand Down
11 changes: 7 additions & 4 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ impl State {
}

/// Rename selected items at once.
pub fn rename_multiple_items(&mut self, items: &[ItemBuffer]) -> Result<(), FxError> {
pub fn rename_multiple_items(&mut self, items: &[ItemBuffer]) -> Result<usize, FxError> {
let names: Vec<&str> = items.iter().map(|item| item.file_name.as_str()).collect();
let mut file = tempfile::NamedTempFile::new()?;
writeln!(file, "{}", names.join("\n"))?;
Expand Down Expand Up @@ -1328,13 +1328,16 @@ impl State {
for (i, new_name) in new_names.iter().enumerate() {
let mut to = self.current_dir.clone();
to.push(new_name);
std::fs::rename(&items[i].file_path, &to)?;
result.push((items[i].file_path.clone(), to))
if &items[i].file_name != new_name {
std::fs::rename(&items[i].file_path, &to)?;
result.push((items[i].file_path.clone(), to))
}
}
let len = result.len();
self.operations.branch();
self.operations.push(OpKind::Rename(result));

Ok(())
Ok(len)
}
}
}
Expand Down

0 comments on commit 0ad66fc

Please sign in to comment.