mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
feat: update file name without changing file extension when pasting file and there is file with same name in folder
This commit is contained in:
parent
8aeef55b6f
commit
2b8a7d7b49
1 changed files with 12 additions and 3 deletions
|
|
@ -51,9 +51,9 @@ pub async fn unique_path(mut p: Url) -> Url {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while fs::symlink_metadata(&p).await.is_ok() {
|
while fs::symlink_metadata(&p).await.is_ok() {
|
||||||
i += 1;
|
i += 1;
|
||||||
let mut name = name.clone();
|
let name_str = name.as_os_str().to_str().unwrap();
|
||||||
name.push(format!("_{i}"));
|
let (base_name, extension) = split_filename_extension(name_str);
|
||||||
p.set_file_name(name);
|
p.set_file_name(format!("{base_name}_{i}{extension}"));
|
||||||
}
|
}
|
||||||
p
|
p
|
||||||
}
|
}
|
||||||
|
|
@ -112,6 +112,15 @@ pub fn optional_bool(s: &str) -> Option<bool> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn split_filename_extension(filename: &str) -> (&str, &str) {
|
||||||
|
if let Some(dot_pos) = filename.rfind('.') {
|
||||||
|
let (base_name, extension) = filename.split_at(dot_pos);
|
||||||
|
(base_name, extension)
|
||||||
|
} else {
|
||||||
|
(filename, "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::{borrow::Cow, path::Path};
|
use std::{borrow::Cow, path::Path};
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue