Skip to content

Commit

Permalink
Optimize get_truncated_path
Browse files Browse the repository at this point in the history
  • Loading branch information
mo8it authored and Vulpesx committed Jun 7, 2024
1 parent f3bebce commit 8e90a8b
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions helix-stdx/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,21 +181,20 @@ where
///
pub fn get_truncated_path(path: impl AsRef<Path>) -> PathBuf {
let cwd = current_working_dir();
let path = path
.as_ref()
.strip_prefix(cwd)
.unwrap_or_else(|_| path.as_ref());
let path = path.as_ref();
let path = path.strip_prefix(cwd).unwrap_or(path);
let file = path.file_name().unwrap_or_default();
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 {
ret.push(
d.to_string_lossy()
.chars()
.next()
.unwrap_or_default()
.to_string(),
);
let Some(first_char) = d.to_string_lossy().chars().next() else {
break;
};
first_char_buffer.push(first_char);
ret.push(&first_char_buffer);
first_char_buffer.clear();
}
ret.push(file);
ret
Expand Down

0 comments on commit 8e90a8b

Please sign in to comment.