Optimize get_truncated_path

pull/9932/head
mo8it 9 months ago committed by Blaž Hrastnik
parent e91ec8e880
commit 0f5430ab9e

@ -181,21 +181,20 @@ where
/// ///
pub fn get_truncated_path(path: impl AsRef<Path>) -> PathBuf { pub fn get_truncated_path(path: impl AsRef<Path>) -> PathBuf {
let cwd = current_working_dir(); let cwd = current_working_dir();
let path = path let path = path.as_ref();
.as_ref() let path = path.strip_prefix(cwd).unwrap_or(path);
.strip_prefix(cwd)
.unwrap_or_else(|_| path.as_ref());
let file = path.file_name().unwrap_or_default(); let file = path.file_name().unwrap_or_default();
let base = path.parent().unwrap_or_else(|| Path::new("")); let base = path.parent().unwrap_or_else(|| Path::new(""));
let mut ret = PathBuf::new(); let mut ret = PathBuf::with_capacity(file.len());
// A char can't be directly pushed to a PathBuf
let mut first_char_buffer = String::new();
for d in base { for d in base {
ret.push( let Some(first_char) = d.to_string_lossy().chars().next() else {
d.to_string_lossy() break;
.chars() };
.next() first_char_buffer.push(first_char);
.unwrap_or_default() ret.push(&first_char_buffer);
.to_string(), first_char_buffer.clear();
);
} }
ret.push(file); ret.push(file);
ret ret

Loading…
Cancel
Save